HtmlPagination::getUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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\service\PhalconUtils;
8
use Ajax\common\html\BaseHtml;
9
use Ajax\JsUtils;
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 Dispatcher $dispatcher the request dispatcher
118
	 * @return \Ajax\bootstrap\html\HtmlPagination
119
	 */
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
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 View Code Duplication
		if (is_int($size)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
			return $this->addToPropertyUnique("class", CssRef::sizes("pagination")[$size], CssRef::sizes("pagination"));
148
		}
149
		if(!PhalconUtils::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
}