Issues (59)

Ajax/bootstrap/html/HtmlPagination.php (1 issue)

Severity
1
<?php
2
namespace Ajax\bootstrap\html;
3
4
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
5
use Ajax\bootstrap\html\base\HtmlNavElement;
6
use Ajax\bootstrap\html\base\CssRef;
7
use Ajax\common\html\BaseHtml;
8
use Ajax\JsUtils;
9
use Ajax\service\JString;
10
11
/**
12
 * Twitter Bootstrap Pagination component
13
 * @see http://getbootstrap.com/components/#pagination
14
 * @author jc
15
 * @version 1.001
16
 */
17
class HtmlPagination extends HtmlNavElement {
18
19
	/**
20
	 * @var int
21
	 */
22
	protected $from;
23
24
	/**
25
	 * @var int
26
	 */
27
	protected $to;
28
29
30
	/**
31
	 * @var int
32
	 */
33
	protected $countVisible;
34
35
	/**
36
	 * @var int
37
	 */
38
	protected $active;
39
40
	/**
41
	 * @var string
42
	 */
43
	protected $urlMask;
44
45
	/**
46
	 * @param string $identifier
47
	 */
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
	}
114
	/**
115
	 * set the active page corresponding to request dispatcher : controllerName, actionName, parameters and $urlMask
116
	 * @param JsUtils $js
117
	 * @param object $dispatcher the request dispatcher
118
	 * @return \Ajax\bootstrap\html\HtmlPagination
119
	 */
120
	public function fromDispatcher(JsUtils $js,$dispatcher,$startIndex=0){
0 ignored issues
show
The parameter $startIndex is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

120
	public function fromDispatcher(JsUtils $js,$dispatcher,/** @scrutinizer ignore-unused */ $startIndex=0){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
139
	/**
140
	 * define the buttons size
141
	 * available values : "lg","","sm","xs"
142
	 * @param string|int $size
143
	 * @return HtmlPagination default : ""
144
	 */
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
}
197