Pagination.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Pagination Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Pagination
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/libraries/pagination.html
  47. */
  48. class CI_Pagination {
  49. /**
  50. * Base URL
  51. *
  52. * The page that we're linking to
  53. *
  54. * @var string
  55. */
  56. protected $base_url = '';
  57. /**
  58. * Prefix
  59. *
  60. * @var string
  61. */
  62. protected $prefix = '';
  63. /**
  64. * Suffix
  65. *
  66. * @var string
  67. */
  68. protected $suffix = '';
  69. /**
  70. * Total number of items
  71. *
  72. * @var int
  73. */
  74. protected $total_rows = 0;
  75. /**
  76. * Number of links to show
  77. *
  78. * Relates to "digit" type links shown before/after
  79. * the currently viewed page.
  80. *
  81. * @var int
  82. */
  83. protected $num_links = 2;
  84. /**
  85. * Items per page
  86. *
  87. * @var int
  88. */
  89. public $per_page = 10;
  90. /**
  91. * Current page
  92. *
  93. * @var int
  94. */
  95. public $cur_page = 0;
  96. /**
  97. * Use page numbers flag
  98. *
  99. * Whether to use actual page numbers instead of an offset
  100. *
  101. * @var bool
  102. */
  103. protected $use_page_numbers = FALSE;
  104. /**
  105. * First link
  106. *
  107. * @var string
  108. */
  109. protected $first_link = '&lsaquo; First';
  110. /**
  111. * Next link
  112. *
  113. * @var string
  114. */
  115. protected $next_link = '&gt;';
  116. /**
  117. * Previous link
  118. *
  119. * @var string
  120. */
  121. protected $prev_link = '&lt;';
  122. /**
  123. * Last link
  124. *
  125. * @var string
  126. */
  127. protected $last_link = 'Last &rsaquo;';
  128. /**
  129. * URI Segment
  130. *
  131. * @var int
  132. */
  133. protected $uri_segment = 0;
  134. /**
  135. * Full tag open
  136. *
  137. * @var string
  138. */
  139. protected $full_tag_open = '';
  140. /**
  141. * Full tag close
  142. *
  143. * @var string
  144. */
  145. protected $full_tag_close = '';
  146. /**
  147. * First tag open
  148. *
  149. * @var string
  150. */
  151. protected $first_tag_open = '';
  152. /**
  153. * First tag close
  154. *
  155. * @var string
  156. */
  157. protected $first_tag_close = '';
  158. /**
  159. * Last tag open
  160. *
  161. * @var string
  162. */
  163. protected $last_tag_open = '';
  164. /**
  165. * Last tag close
  166. *
  167. * @var string
  168. */
  169. protected $last_tag_close = '';
  170. /**
  171. * First URL
  172. *
  173. * An alternative URL for the first page
  174. *
  175. * @var string
  176. */
  177. protected $first_url = '';
  178. /**
  179. * Current tag open
  180. *
  181. * @var string
  182. */
  183. protected $cur_tag_open = '<strong>';
  184. /**
  185. * Current tag close
  186. *
  187. * @var string
  188. */
  189. protected $cur_tag_close = '</strong>';
  190. /**
  191. * Next tag open
  192. *
  193. * @var string
  194. */
  195. protected $next_tag_open = '';
  196. /**
  197. * Next tag close
  198. *
  199. * @var string
  200. */
  201. protected $next_tag_close = '';
  202. /**
  203. * Previous tag open
  204. *
  205. * @var string
  206. */
  207. protected $prev_tag_open = '';
  208. /**
  209. * Previous tag close
  210. *
  211. * @var string
  212. */
  213. protected $prev_tag_close = '';
  214. /**
  215. * Number tag open
  216. *
  217. * @var string
  218. */
  219. protected $num_tag_open = '';
  220. /**
  221. * Number tag close
  222. *
  223. * @var string
  224. */
  225. protected $num_tag_close = '';
  226. /**
  227. * Page query string flag
  228. *
  229. * @var bool
  230. */
  231. protected $page_query_string = FALSE;
  232. /**
  233. * Query string segment
  234. *
  235. * @var string
  236. */
  237. protected $query_string_segment = 'per_page';
  238. /**
  239. * Display pages flag
  240. *
  241. * @var bool
  242. */
  243. protected $display_pages = TRUE;
  244. /**
  245. * Attributes
  246. *
  247. * @var string
  248. */
  249. protected $_attributes = '';
  250. /**
  251. * Link types
  252. *
  253. * "rel" attribute
  254. *
  255. * @see CI_Pagination::_attr_rel()
  256. * @var array
  257. */
  258. protected $_link_types = array();
  259. /**
  260. * Reuse query string flag
  261. *
  262. * @var bool
  263. */
  264. protected $reuse_query_string = FALSE;
  265. /**
  266. * Use global URL suffix flag
  267. *
  268. * @var bool
  269. */
  270. protected $use_global_url_suffix = FALSE;
  271. /**
  272. * Data page attribute
  273. *
  274. * @var string
  275. */
  276. protected $data_page_attr = 'data-ci-pagination-page';
  277. /**
  278. * CI Singleton
  279. *
  280. * @var object
  281. */
  282. protected $CI;
  283. // --------------------------------------------------------------------
  284. /**
  285. * Constructor
  286. *
  287. * @param array $params Initialization parameters
  288. * @return void
  289. */
  290. public function __construct($params = array())
  291. {
  292. $this->CI =& get_instance();
  293. $this->CI->load->language('pagination');
  294. foreach (array('first_link', 'next_link', 'prev_link', 'last_link') as $key)
  295. {
  296. if (($val = $this->CI->lang->line('pagination_'.$key)) !== FALSE)
  297. {
  298. $this->$key = $val;
  299. }
  300. }
  301. // _parse_attributes(), called by initialize(), needs to run at least once
  302. // in order to enable "rel" attributes, and this triggers it.
  303. isset($params['attributes']) OR $params['attributes'] = array();
  304. $this->initialize($params);
  305. log_message('info', 'Pagination Class Initialized');
  306. }
  307. // --------------------------------------------------------------------
  308. /**
  309. * Initialize Preferences
  310. *
  311. * @param array $params Initialization parameters
  312. * @return CI_Pagination
  313. */
  314. public function initialize(array $params = array())
  315. {
  316. if (isset($params['attributes']) && is_array($params['attributes']))
  317. {
  318. $this->_parse_attributes($params['attributes']);
  319. unset($params['attributes']);
  320. }
  321. // Deprecated legacy support for the anchor_class option
  322. // Should be removed in CI 3.1+
  323. if (isset($params['anchor_class']))
  324. {
  325. empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class'];
  326. unset($params['anchor_class']);
  327. }
  328. foreach ($params as $key => $val)
  329. {
  330. if (property_exists($this, $key))
  331. {
  332. $this->$key = $val;
  333. }
  334. }
  335. if ($this->CI->config->item('enable_query_strings') === TRUE)
  336. {
  337. $this->page_query_string = TRUE;
  338. }
  339. if ($this->use_global_url_suffix === TRUE)
  340. {
  341. $this->suffix = $this->CI->config->item('url_suffix');
  342. }
  343. return $this;
  344. }
  345. // --------------------------------------------------------------------
  346. /**
  347. * Generate the pagination links
  348. *
  349. * @return string
  350. */
  351. public function create_links()
  352. {
  353. // If our item count or per-page total is zero there is no need to continue.
  354. // Note: DO NOT change the operator to === here!
  355. if ($this->total_rows == 0 OR $this->per_page == 0)
  356. {
  357. return '';
  358. }
  359. // Calculate the total number of pages
  360. $num_pages = (int) ceil($this->total_rows / $this->per_page);
  361. // Is there only one page? Hm... nothing more to do here then.
  362. if ($num_pages === 1)
  363. {
  364. return '';
  365. }
  366. // Check the user defined number of links.
  367. $this->num_links = (int) $this->num_links;
  368. if ($this->num_links < 0)
  369. {
  370. show_error('Your number of links must be a non-negative number.');
  371. }
  372. // Keep any existing query string items.
  373. // Note: Has nothing to do with any other query string option.
  374. if ($this->reuse_query_string === TRUE)
  375. {
  376. $get = $this->CI->input->get();
  377. // Unset the control, method, old-school routing options
  378. unset($get['c'], $get['m'], $get[$this->query_string_segment]);
  379. }
  380. else
  381. {
  382. $get = array();
  383. }
  384. // Put together our base and first URLs.
  385. // Note: DO NOT append to the properties as that would break successive calls
  386. $base_url = trim($this->base_url);
  387. $first_url = $this->first_url;
  388. $query_string = '';
  389. $query_string_sep = (strpos($base_url, '?') === FALSE) ? '?' : '&amp;';
  390. // Are we using query strings?
  391. if ($this->page_query_string === TRUE)
  392. {
  393. // If a custom first_url hasn't been specified, we'll create one from
  394. // the base_url, but without the page item.
  395. if ($first_url === '')
  396. {
  397. $first_url = $base_url;
  398. // If we saved any GET items earlier, make sure they're appended.
  399. if ( ! empty($get))
  400. {
  401. $first_url .= $query_string_sep.http_build_query($get);
  402. }
  403. }
  404. // Add the page segment to the end of the query string, where the
  405. // page number will be appended.
  406. $base_url .= $query_string_sep.http_build_query(array_merge($get, array($this->query_string_segment => '')));
  407. }
  408. else
  409. {
  410. // Standard segment mode.
  411. // Generate our saved query string to append later after the page number.
  412. if ( ! empty($get))
  413. {
  414. $query_string = $query_string_sep.http_build_query($get);
  415. $this->suffix .= $query_string;
  416. }
  417. // Does the base_url have the query string in it?
  418. // If we're supposed to save it, remove it so we can append it later.
  419. if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($base_url, '?')) !== FALSE)
  420. {
  421. $base_url = substr($base_url, 0, $base_query_pos);
  422. }
  423. if ($first_url === '')
  424. {
  425. $first_url = $base_url.$query_string;
  426. }
  427. $base_url = rtrim($base_url, '/').'/';
  428. }
  429. // Determine the current page number.
  430. $base_page = ($this->use_page_numbers) ? 1 : 0;
  431. // Are we using query strings?
  432. if ($this->page_query_string === TRUE)
  433. {
  434. $this->cur_page = $this->CI->input->get($this->query_string_segment);
  435. }
  436. elseif (empty($this->cur_page))
  437. {
  438. // Default to the last segment number if one hasn't been defined.
  439. if ($this->uri_segment === 0)
  440. {
  441. $this->uri_segment = count($this->CI->uri->segment_array());
  442. }
  443. $this->cur_page = $this->CI->uri->segment($this->uri_segment);
  444. // Remove any specified prefix/suffix from the segment.
  445. if ($this->prefix !== '' OR $this->suffix !== '')
  446. {
  447. $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
  448. }
  449. }
  450. else
  451. {
  452. $this->cur_page = (string) $this->cur_page;
  453. }
  454. // If something isn't quite right, back to the default base page.
  455. if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0))
  456. {
  457. $this->cur_page = $base_page;
  458. }
  459. else
  460. {
  461. // Make sure we're using integers for comparisons later.
  462. $this->cur_page = (int) $this->cur_page;
  463. }
  464. // Is the page number beyond the result range?
  465. // If so, we show the last page.
  466. if ($this->use_page_numbers)
  467. {
  468. if ($this->cur_page > $num_pages)
  469. {
  470. $this->cur_page = $num_pages;
  471. }
  472. }
  473. elseif ($this->cur_page > $this->total_rows)
  474. {
  475. $this->cur_page = ($num_pages - 1) * $this->per_page;
  476. }
  477. $uri_page_number = $this->cur_page;
  478. // If we're using offset instead of page numbers, convert it
  479. // to a page number, so we can generate the surrounding number links.
  480. if ( ! $this->use_page_numbers)
  481. {
  482. $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1);
  483. }
  484. // Calculate the start and end numbers. These determine
  485. // which number to start and end the digit links with.
  486. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  487. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  488. // And here we go...
  489. $output = '';
  490. // Render the "First" link.
  491. if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1 + ! $this->num_links))
  492. {
  493. // Take the general parameters, and squeeze this pagination-page attr in for JS frameworks.
  494. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1);
  495. $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
  496. .$this->first_link.'</a>'.$this->first_tag_close;
  497. }
  498. // Render the "Previous" link.
  499. if ($this->prev_link !== FALSE && $this->cur_page !== 1)
  500. {
  501. $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
  502. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, ($this->cur_page - 1));
  503. if ($i === $base_page)
  504. {
  505. // First page
  506. $output .= $this->prev_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('prev').'>'
  507. .$this->prev_link.'</a>'.$this->prev_tag_close;
  508. }
  509. else
  510. {
  511. $append = $this->prefix.$i.$this->suffix;
  512. $output .= $this->prev_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.$this->_attr_rel('prev').'>'
  513. .$this->prev_link.'</a>'.$this->prev_tag_close;
  514. }
  515. }
  516. // Render the pages
  517. if ($this->display_pages !== FALSE)
  518. {
  519. // Write the digit links
  520. for ($loop = $start - 1; $loop <= $end; $loop++)
  521. {
  522. $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
  523. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop);
  524. if ($i >= $base_page)
  525. {
  526. if ($this->cur_page === $loop)
  527. {
  528. // Current page
  529. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
  530. }
  531. elseif ($i === $base_page)
  532. {
  533. // First page
  534. $output .= $this->num_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
  535. .$loop.'</a>'.$this->num_tag_close;
  536. }
  537. else
  538. {
  539. $append = $this->prefix.$i.$this->suffix;
  540. $output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.'>'
  541. .$loop.'</a>'.$this->num_tag_close;
  542. }
  543. }
  544. }
  545. }
  546. // Render the "next" link
  547. if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
  548. {
  549. $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
  550. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1);
  551. $output .= $this->next_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
  552. .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
  553. }
  554. // Render the "Last" link
  555. if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links + ! $this->num_links) < $num_pages)
  556. {
  557. $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
  558. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages);
  559. $output .= $this->last_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>'
  560. .$this->last_link.'</a>'.$this->last_tag_close;
  561. }
  562. // Kill double slashes. Note: Sometimes we can end up with a double slash
  563. // in the penultimate link so we'll kill all double slashes.
  564. $output = preg_replace('#([^:"])//+#', '\\1/', $output);
  565. // Add the wrapper HTML if exists
  566. return $this->full_tag_open.$output.$this->full_tag_close;
  567. }
  568. // --------------------------------------------------------------------
  569. /**
  570. * Parse attributes
  571. *
  572. * @param array $attributes
  573. * @return void
  574. */
  575. protected function _parse_attributes($attributes)
  576. {
  577. isset($attributes['rel']) OR $attributes['rel'] = TRUE;
  578. $this->_link_types = ($attributes['rel'])
  579. ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next')
  580. : array();
  581. unset($attributes['rel']);
  582. $this->_attributes = '';
  583. foreach ($attributes as $key => $value)
  584. {
  585. $this->_attributes .= ' '.$key.'="'.$value.'"';
  586. }
  587. }
  588. // --------------------------------------------------------------------
  589. /**
  590. * Add "rel" attribute
  591. *
  592. * @link http://www.w3.org/TR/html5/links.html#linkTypes
  593. * @param string $type
  594. * @return string
  595. */
  596. protected function _attr_rel($type)
  597. {
  598. if (isset($this->_link_types[$type]))
  599. {
  600. unset($this->_link_types[$type]);
  601. return ' rel="'.$type.'"';
  602. }
  603. return '';
  604. }
  605. }