You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

701 lines
16 KiB

7 years ago
  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 - 2017, 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 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://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. $this->initialize($params);
  302. log_message('info', 'Pagination Class Initialized');
  303. }
  304. // --------------------------------------------------------------------
  305. /**
  306. * Initialize Preferences
  307. *
  308. * @param array $params Initialization parameters
  309. * @return CI_Pagination
  310. */
  311. public function initialize(array $params = array())
  312. {
  313. isset($params['attributes']) OR $params['attributes'] = array();
  314. if (is_array($params['attributes']))
  315. {
  316. $this->_parse_attributes($params['attributes']);
  317. unset($params['attributes']);
  318. }
  319. // Deprecated legacy support for the anchor_class option
  320. // Should be removed in CI 3.1+
  321. if (isset($params['anchor_class']))
  322. {
  323. empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class'];
  324. unset($params['anchor_class']);
  325. }
  326. foreach ($params as $key => $val)
  327. {
  328. if (property_exists($this, $key))
  329. {
  330. $this->$key = $val;
  331. }
  332. }
  333. if ($this->CI->config->item('enable_query_strings') === TRUE)
  334. {
  335. $this->page_query_string = TRUE;
  336. }
  337. if ($this->use_global_url_suffix === TRUE)
  338. {
  339. $this->suffix = $this->CI->config->item('url_suffix');
  340. }
  341. return $this;
  342. }
  343. // --------------------------------------------------------------------
  344. /**
  345. * Generate the pagination links
  346. *
  347. * @return string
  348. */
  349. public function create_links()
  350. {
  351. // If our item count or per-page total is zero there is no need to continue.
  352. // Note: DO NOT change the operator to === here!
  353. if ($this->total_rows == 0 OR $this->per_page == 0)
  354. {
  355. return '';
  356. }
  357. // Calculate the total number of pages
  358. $num_pages = (int) ceil($this->total_rows / $this->per_page);
  359. // Is there only one page? Hm... nothing more to do here then.
  360. if ($num_pages === 1)
  361. {
  362. return '';
  363. }
  364. // Check the user defined number of links.
  365. $this->num_links = (int) $this->num_links;
  366. if ($this->num_links < 0)
  367. {
  368. show_error('Your number of links must be a non-negative number.');
  369. }
  370. // Keep any existing query string items.
  371. // Note: Has nothing to do with any other query string option.
  372. if ($this->reuse_query_string === TRUE)
  373. {
  374. $get = $this->CI->input->get();
  375. // Unset the control, method, old-school routing options
  376. unset($get['c'], $get['m'], $get[$this->query_string_segment]);
  377. }
  378. else
  379. {
  380. $get = array();
  381. }
  382. // Put together our base and first URLs.
  383. // Note: DO NOT append to the properties as that would break successive calls
  384. $base_url = trim($this->base_url);
  385. $first_url = $this->first_url;
  386. $query_string = '';
  387. $query_string_sep = (strpos($base_url, '?') === FALSE) ? '?' : '&amp;';
  388. // Are we using query strings?
  389. if ($this->page_query_string === TRUE)
  390. {
  391. // If a custom first_url hasn't been specified, we'll create one from
  392. // the base_url, but without the page item.
  393. if ($first_url === '')
  394. {
  395. $first_url = $base_url;
  396. // If we saved any GET items earlier, make sure they're appended.
  397. if ( ! empty($get))
  398. {
  399. $first_url .= $query_string_sep.http_build_query($get);
  400. }
  401. }
  402. // Add the page segment to the end of the query string, where the
  403. // page number will be appended.
  404. $base_url .= $query_string_sep.http_build_query(array_merge($get, array($this->query_string_segment => '')));
  405. }
  406. else
  407. {
  408. // Standard segment mode.
  409. // Generate our saved query string to append later after the page number.
  410. if ( ! empty($get))
  411. {
  412. $query_string = $query_string_sep.http_build_query($get);
  413. $this->suffix .= $query_string;
  414. }
  415. // Does the base_url have the query string in it?
  416. // If we're supposed to save it, remove it so we can append it later.
  417. if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($base_url, '?')) !== FALSE)
  418. {
  419. $base_url = substr($base_url, 0, $base_query_pos);
  420. }
  421. if ($first_url === '')
  422. {
  423. $first_url = $base_url.$query_string;
  424. }
  425. $base_url = rtrim($base_url, '/').'/';
  426. }
  427. // Determine the current page number.
  428. $base_page = ($this->use_page_numbers) ? 1 : 0;
  429. // Are we using query strings?
  430. if ($this->page_query_string === TRUE)
  431. {
  432. $this->cur_page = $this->CI->input->get($this->query_string_segment);
  433. }
  434. elseif (empty($this->cur_page))
  435. {
  436. // Default to the last segment number if one hasn't been defined.
  437. if ($this->uri_segment === 0)
  438. {
  439. $this->uri_segment = count($this->CI->uri->segment_array());
  440. }
  441. $this->cur_page = $this->CI->uri->segment($this->uri_segment);
  442. // Remove any specified prefix/suffix from the segment.
  443. if ($this->prefix !== '' OR $this->suffix !== '')
  444. {
  445. $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
  446. }
  447. }
  448. else
  449. {
  450. $this->cur_page = (string) $this->cur_page;
  451. }
  452. // If something isn't quite right, back to the default base page.
  453. if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0))
  454. {
  455. $this->cur_page = $base_page;
  456. }
  457. else
  458. {
  459. // Make sure we're using integers for comparisons later.
  460. $this->cur_page = (int) $this->cur_page;
  461. }
  462. // Is the page number beyond the result range?
  463. // If so, we show the last page.
  464. if ($this->use_page_numbers)
  465. {
  466. if ($this->cur_page > $num_pages)
  467. {
  468. $this->cur_page = $num_pages;
  469. }
  470. }
  471. elseif ($this->cur_page > $this->total_rows)
  472. {
  473. $this->cur_page = ($num_pages - 1) * $this->per_page;
  474. }
  475. $uri_page_number = $this->cur_page;
  476. // If we're using offset instead of page numbers, convert it
  477. // to a page number, so we can generate the surrounding number links.
  478. if ( ! $this->use_page_numbers)
  479. {
  480. $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1);
  481. }
  482. // Calculate the start and end numbers. These determine
  483. // which number to start and end the digit links with.
  484. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  485. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  486. // And here we go...
  487. $output = '';
  488. // Render the "First" link.
  489. if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1 + ! $this->num_links))
  490. {
  491. // Take the general parameters, and squeeze this pagination-page attr in for JS frameworks.
  492. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1);
  493. $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
  494. .$this->first_link.'</a>'.$this->first_tag_close;
  495. }
  496. // Render the "Previous" link.
  497. if ($this->prev_link !== FALSE && $this->cur_page !== 1)
  498. {
  499. $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
  500. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, ($this->cur_page - 1));
  501. if ($i === $base_page)
  502. {
  503. // First page
  504. $output .= $this->prev_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('prev').'>'
  505. .$this->prev_link.'</a>'.$this->prev_tag_close;
  506. }
  507. else
  508. {
  509. $append = $this->prefix.$i.$this->suffix;
  510. $output .= $this->prev_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.$this->_attr_rel('prev').'>'
  511. .$this->prev_link.'</a>'.$this->prev_tag_close;
  512. }
  513. }
  514. // Render the pages
  515. if ($this->display_pages !== FALSE)
  516. {
  517. // Write the digit links
  518. for ($loop = $start - 1; $loop <= $end; $loop++)
  519. {
  520. $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
  521. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop);
  522. if ($i >= $base_page)
  523. {
  524. if ($this->cur_page === $loop)
  525. {
  526. // Current page
  527. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
  528. }
  529. elseif ($i === $base_page)
  530. {
  531. // First page
  532. $output .= $this->num_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
  533. .$loop.'</a>'.$this->num_tag_close;
  534. }
  535. else
  536. {
  537. $append = $this->prefix.$i.$this->suffix;
  538. $output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.'>'
  539. .$loop.'</a>'.$this->num_tag_close;
  540. }
  541. }
  542. }
  543. }
  544. // Render the "next" link
  545. if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
  546. {
  547. $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
  548. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1);
  549. $output .= $this->next_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
  550. .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
  551. }
  552. // Render the "Last" link
  553. if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links + ! $this->num_links) < $num_pages)
  554. {
  555. $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
  556. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages);
  557. $output .= $this->last_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>'
  558. .$this->last_link.'</a>'.$this->last_tag_close;
  559. }
  560. // Kill double slashes. Note: Sometimes we can end up with a double slash
  561. // in the penultimate link so we'll kill all double slashes.
  562. $output = preg_replace('#([^:"])//+#', '\\1/', $output);
  563. // Add the wrapper HTML if exists
  564. return $this->full_tag_open.$output.$this->full_tag_close;
  565. }
  566. // --------------------------------------------------------------------
  567. /**
  568. * Parse attributes
  569. *
  570. * @param array $attributes
  571. * @return void
  572. */
  573. protected function _parse_attributes($attributes)
  574. {
  575. isset($attributes['rel']) OR $attributes['rel'] = TRUE;
  576. $this->_link_types = ($attributes['rel'])
  577. ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next')
  578. : array();
  579. unset($attributes['rel']);
  580. $this->_attributes = '';
  581. foreach ($attributes as $key => $value)
  582. {
  583. $this->_attributes .= ' '.$key.'="'.$value.'"';
  584. }
  585. }
  586. // --------------------------------------------------------------------
  587. /**
  588. * Add "rel" attribute
  589. *
  590. * @link http://www.w3.org/TR/html5/links.html#linkTypes
  591. * @param string $type
  592. * @return string
  593. */
  594. protected function _attr_rel($type)
  595. {
  596. if (isset($this->_link_types[$type]))
  597. {
  598. unset($this->_link_types[$type]);
  599. return ' rel="'.$type.'"';
  600. }
  601. return '';
  602. }
  603. }