Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pagenavigation.php
1<?php
8namespace Bitrix\Main\UI;
9
11
27{
28 protected $id;
29 protected $pageSizes = array();
30 protected $pageSize = 20;
31 protected $recordCount;
32 protected $currentPage;
33 protected $allowAll = false;
34 protected $allRecords = false;
35
39 public function __construct($id)
40 {
41 $this->id = $id;
42 }
43
47 public function initFromUri()
48 {
49 $navParams = array();
50
51 $request = \Bitrix\Main\Context::getCurrent()->getRequest();
52
53 if(($value = $request->getQuery($this->id)) !== null)
54 {
55 //parameters are in the QUERY_STRING
56 $params = explode("-", $value);
57 for($i = 0, $n = count($params); $i < $n; $i += 2)
58 {
59 $navParams[$params[$i]] = $params[$i+1];
60 }
61 }
62 else
63 {
64 //probably parametrs are in the SEF URI
65 $matches = array();
66 if(preg_match("'/".preg_quote($this->id, "'")."/page-([\\d]+|all)+(/size-([\\d]+))?'", $request->getRequestUri(), $matches))
67 {
68 $navParams["page"] = $matches[1];
69 if(isset($matches[3]))
70 {
71 $navParams["size"] = $matches[3];
72 }
73 }
74 }
75
76 if(isset($navParams["size"]))
77 {
78 //set page size from user request
79 if(in_array($navParams["size"], $this->pageSizes))
80 {
81 $this->setPageSize((int)$navParams["size"]);
82 }
83 }
84
85 if(isset($navParams["page"]))
86 {
87 if($navParams["page"] == "all" && $this->allowAll == true)
88 {
89 //show all records in one page
90 $this->allRecords = true;
91 }
92 else
93 {
94 //set current page within boundaries
95 $currentPage = (int)$navParams["page"];
96 if($currentPage >= 1)
97 {
98 if($this->recordCount !== null)
99 {
100 $maxPage = $this->getPageCount();
101 if($currentPage > $maxPage)
102 {
103 $currentPage = $maxPage;
104 }
105 }
107 }
108 }
109 }
110 }
111
116 public function getPageCount()
117 {
118 if($this->allRecords)
119 {
120 return 1;
121 }
122 $maxPages = (int)floor($this->recordCount/$this->pageSize);
123 if(($this->recordCount % $this->pageSize) > 0)
124 {
125 $maxPages++;
126 }
127 return $maxPages;
128 }
129
134 public function setPageSize($n)
135 {
136 $this->pageSize = (int)$n;
137 return $this;
138 }
139
144 public function setCurrentPage($n)
145 {
146 $this->currentPage = (int)$n;
147 return $this;
148 }
149
154 public function getCurrentPage()
155 {
156 if($this->currentPage !== null)
157 {
158 return $this->currentPage;
159 }
160 return 1;
161 }
162
167 public function allowAllRecords($mode)
168 {
169 $this->allowAll = (bool)$mode;
170 return $this;
171 }
172
177 public function setRecordCount($n)
178 {
179 $this->recordCount = (int)$n;
180 return $this;
181 }
182
187 public function getRecordCount()
188 {
189 return $this->recordCount;
190 }
191
197 public function setPageSizes(array $sizes)
198 {
199 $this->pageSizes = $sizes;
200 return $this;
201 }
202
207 public function getPageSizes()
208 {
209 return $this->pageSizes;
210 }
211
216 public function getPageSize()
217 {
218 return $this->pageSize;
219 }
220
225 public function getId()
226 {
227 return $this->id;
228 }
229
234 public function getOffset()
235 {
236 if($this->allRecords)
237 {
238 return 0;
239 }
240 return ($this->getCurrentPage() - 1) * $this->pageSize;
241 }
242
247 public function getLimit()
248 {
249 if($this->allRecords)
250 {
251 return $this->getRecordCount();
252 }
253 return $this->pageSize;
254 }
255
260 public function allRecordsShown()
261 {
262 return $this->allRecords;
263 }
264
269 public function allRecordsAllowed()
270 {
271 return $this->allowAll;
272 }
273
282 public function addParams(Web\Uri $uri, $sef, $page, $size = null)
283 {
284 if($sef == true)
285 {
286 $this->clearParams($uri, $sef);
287
288 $path = $uri->getPath();
289 $pos = mb_strrpos($path, "/");
290 $path = mb_substr($path, 0, $pos + 1).$this->id."/page-".$page."/".($size !== null? "size-".$size."/" : '').mb_substr($path, $pos + 1);
291 $uri->setPath($path);
292 }
293 else
294 {
295 $uri->addParams(array($this->id => "page-".$page.($size !== null? "-size-".$size : '')));
296 }
297 return $uri;
298 }
299
306 public function clearParams(Web\Uri $uri, $sef)
307 {
308 if($sef == true)
309 {
310 $path = $uri->getPath();
311 $path = preg_replace("'/".preg_quote($this->id, "'")."/page-([\\d]+|all)+(/size-([\\d]+))?'", "", $path);
312 $uri->setPath($path);
313 }
314 else
315 {
316 $uri->deleteParams(array($this->id));
317 }
318 return $uri;
319 }
320}
addParams(Web\Uri $uri, $sef, $page, $size=null)
clearParams(Web\Uri $uri, $sef)