phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlPagination.php
Go to the documentation of this file.
1 <?php
2 namespace Ajax\bootstrap\html;
3 
8 use Ajax\JsUtils;
10 
18 
22  protected $from;
23 
27  protected $to;
28 
29 
33  protected $countVisible;
34 
38  protected $active;
39 
43  protected $urlMask;
44 
48  public function __construct($identifier,$from=1,$to=1,$active=NULL,$countVisible=NULL){
49  parent::__construct($identifier,"ul");
50  $this->setProperty("class", "pagination");
51  $this->active=$active;
52  $this->from=$from;
53  $this->to=$to;
54  $this->urlMask="%page%";
55  if(!isset($countVisible))
56  $this->countVisible=$to-$from+1;
57  else
58  $this->countVisible=$countVisible;
59  $this->createContent();
60  }
61 
62  private function createElement($num,$content,$disabled=false,$current=false){
63  $count=sizeof($this->content)+1;
64  $elem=new HtmlBsDoubleElement("li-".$this->identifier."-".$count,"li");
65  if($disabled){
66  $elem->setProperty("class", "disabled");
67  }
68  if($current){
69  $content.="<span class='sr-only'>(current)</span>";
70  $elem->setProperty("class", "active");
71  }
72  if(!$disabled){
73  $url=$this->getUrl($num);
74  $href=new HtmlLink("a-".$this->identifier."-".$count,$url,$content);
75  $href->setProperty($this->attr, $url);
76  $elem->setContent($href);
77  }else{
78  $elem->setContent($content);
79  }
80  $this->content[]=$elem;
81  return $this;
82  }
83 
84  protected function createContent(){
85  $this->content=array();
86  $this->createElement($this->active-1,"<span aria-hidden='true'>&laquo;</span>",$this->active===1);
87  $start=$this->getStart();
88  $end=min($start+$this->countVisible-1,$this->to);
89  for($index=$start;$index<=$end;$index++){
90  $this->createElement($index,$index,false,$index===$this->active);
91  }
92  $this->createElement($this->active+1,"<span aria-hidden='true'>&raquo;</span>",$this->active===$this->to);
93  }
94 
95  protected function half(){
96  return (int)($this->countVisible/2);
97  }
98 
99  protected function getStart(){
100  $result=1;
101  if($this->countVisible!==$this->to-$this->from+1){
102  $result=max($this->active-$this->half(),$result);
103  }
104  return $result;
105  }
106 
107  public function _addEvent($event, $jsCode) {
108  foreach ($this->content as $li){
109  $content=$li->getContent();
110  if($content instanceof BaseHtml)
111  $content->_addEvent($event,$jsCode);
112  }
113  }
120  public function fromDispatcher(JsUtils $js,$dispatcher,$startIndex=0){
121  $items=$js->fromDispatcher($dispatcher);
122  $url=implode("/", $items);
123  if($this->urlMask==="%page%"){
124  $this->urlMask=preg_replace("/[0-9]/", "%page%", $url);
125  }
126  for($index=$this->from;$index<=$this->to;$index++){
127  if($this->getUrl($index)==$url){
128  $this->setActive($index);
129  break;
130  }
131  }
132  return $this;
133  }
134 
135  public function getUrl($index){
136  return str_ireplace("%page%", $index, $this->urlMask);
137  }
138 
145  public function setSize($size) {
146  if (is_int($size)) {
147  return $this->addToPropertyUnique("class", CssRef::sizes("pagination")[$size], CssRef::sizes("pagination"));
148  }
149  if(!JString::startsWith($size, "pagination-") && $size!=="")
150  $size="pagination-".$size;
151  return $this->addToPropertyCtrl("class", $size, CssRef::sizes("pagination"));
152  }
153 
154  public function getFrom() {
155  return $this->from;
156  }
157  public function setFrom($from) {
158  $this->from = $from;
159  $this->createContent();
160  return $this;
161  }
162  public function getTo() {
163  return $this->to;
164  }
165  public function setTo($to) {
166  $this->to = $to;
167  $this->createContent();
168  return $this;
169  }
170  public function getActive() {
171  return $this->active;
172  }
173  public function setActive($active) {
174  $this->active = $active;
175  $this->createContent();
176  return $this;
177  }
178  public function getUrlMask() {
179  return $this->urlMask;
180  }
181  public function setUrlMask($urlMask) {
182  $this->urlMask = $urlMask;
183  $this->createContent();
184  return $this;
185  }
186  public function getCountVisible() {
187  return $this->countVisible;
188  }
189  public function setCountVisible($countVisible) {
190  $this->countVisible = $countVisible;
191  $this->createContent();
192  return $this;
193  }
194 
195 
196 }
BaseHtml for HTML components.
Definition: BaseHtml.php:17
setSize($size)
define the buttons size available values : "lg","","sm","xs"
Twitter Bootstrap Pagination component.
__construct($identifier, $from=1, $to=1, $active=NULL, $countVisible=NULL)
fromDispatcher($dispatcher)
Collects url parts from the request dispatcher : controllerName, actionName, parameters Used internal...
createElement($num, $content, $disabled=false, $current=false)
fromDispatcher(JsUtils $js, $dispatcher, $startIndex=0)
set the active page corresponding to request dispatcher : controllerName, actionName, parameters and $urlMask
JQuery PHP library.
Definition: JsUtils.php:23
addToPropertyUnique($name, $value, $typeCtrl)
static sizes($prefix="btn")
Definition: CssRef.php:38
Bs class for navigation elements : Breadcrumbs and Pagination.
attr($element='this', $attributeName='id', $value="", $immediatly=false)
Get or set the value of an attribute for the first element in the set of matched elements or set one ...
addToPropertyCtrl($name, $value, $typeCtrl)