PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/html/pagination.php

https://github.com/adothompson/ucsc-identity-joomla
PHP | 550 lines | 325 code | 67 blank | 158 comment | 60 complexity | 25632cbbe98c9bc8ef8a1e041120ee77 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: pagination.php 10707 2008-08-21 09:52:47Z eddieajau $
  4. * @package Joomla.Framework
  5. * @subpackage HTML
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * Pagination Class. Provides a common interface for content pagination for the
  18. * Joomla! Framework
  19. *
  20. * @package Joomla.Framework
  21. * @subpackage HTML
  22. * @since 1.5
  23. */
  24. class JPagination extends JObject
  25. {
  26. /**
  27. * The record number to start dislpaying from
  28. *
  29. * @access public
  30. * @var int
  31. */
  32. var $limitstart = null;
  33. /**
  34. * Number of rows to display per page
  35. *
  36. * @access public
  37. * @var int
  38. */
  39. var $limit = null;
  40. /**
  41. * Total number of rows
  42. *
  43. * @access public
  44. * @var int
  45. */
  46. var $total = null;
  47. /**
  48. * View all flag
  49. *
  50. * @access protected
  51. * @var boolean
  52. */
  53. var $_viewall = false;
  54. /**
  55. * Constructor
  56. *
  57. * @param int The total number of items
  58. * @param int The offset of the item to start at
  59. * @param int The number of items to display per page
  60. */
  61. function __construct($total, $limitstart, $limit)
  62. {
  63. // Value/Type checking
  64. $this->total = (int) $total;
  65. $this->limitstart = (int) max($limitstart, 0);
  66. $this->limit = (int) max($limit, 0);
  67. if ($this->limit > $this->total) {
  68. $this->limitstart = 0;
  69. }
  70. if (!$this->limit)
  71. {
  72. $this->limit = $total;
  73. $this->limitstart = 0;
  74. }
  75. if ($this->limitstart > $this->total) {
  76. $this->limitstart -= $this->limitstart % $this->limit;
  77. }
  78. // Set the total pages and current page values
  79. if($this->limit > 0)
  80. {
  81. $this->set( 'pages.total', ceil($this->total / $this->limit));
  82. $this->set( 'pages.current', ceil(($this->limitstart + 1) / $this->limit));
  83. }
  84. // Set the pagination iteration loop values
  85. $displayedPages = 10;
  86. $this->set( 'pages.start', (floor(($this->get('pages.current') -1) / $displayedPages)) * $displayedPages +1);
  87. if ($this->get('pages.start') + $displayedPages -1 < $this->get('pages.total')) {
  88. $this->set( 'pages.stop', $this->get('pages.start') + $displayedPages -1);
  89. } else {
  90. $this->set( 'pages.stop', $this->get('pages.total'));
  91. }
  92. // If we are viewing all records set the view all flag to true
  93. if ($this->limit == $total) {
  94. $this->_viewall = true;
  95. }
  96. }
  97. /**
  98. * Return the rationalised offset for a row with a given index.
  99. *
  100. * @access public
  101. * @param int $index The row index
  102. * @return int Rationalised offset for a row with a given index
  103. * @since 1.5
  104. */
  105. function getRowOffset($index)
  106. {
  107. return $index +1 + $this->limitstart;
  108. }
  109. /**
  110. * Return the pagination data object, only creating it if it doesn't already exist
  111. *
  112. * @access public
  113. * @return object Pagination data object
  114. * @since 1.5
  115. */
  116. function getData()
  117. {
  118. static $data;
  119. if (!is_object($data)) {
  120. $data = $this->_buildDataObject();
  121. }
  122. return $data;
  123. }
  124. /**
  125. * Create and return the pagination pages counter string, ie. Page 2 of 4
  126. *
  127. * @access public
  128. * @return string Pagination pages counter string
  129. * @since 1.5
  130. */
  131. function getPagesCounter()
  132. {
  133. // Initialize variables
  134. $html = null;
  135. if ($this->get('pages.total') > 1) {
  136. $html .= JText::_('Page')." ".$this->get('pages.current')." ".JText::_('of')." ".$this->get('pages.total');
  137. }
  138. return $html;
  139. }
  140. /**
  141. * Create and return the pagination result set counter string, ie. Results 1-10 of 42
  142. *
  143. * @access public
  144. * @return string Pagination result set counter string
  145. * @since 1.5
  146. */
  147. function getResultsCounter()
  148. {
  149. // Initialize variables
  150. $html = null;
  151. $fromResult = $this->limitstart + 1;
  152. // If the limit is reached before the end of the list
  153. if ($this->limitstart + $this->limit < $this->total) {
  154. $toResult = $this->limitstart + $this->limit;
  155. } else {
  156. $toResult = $this->total;
  157. }
  158. // If there are results found
  159. if ($this->total > 0) {
  160. $msg = JText::sprintf('Results of', $fromResult, $toResult, $this->total);
  161. $html .= "\n".$msg;
  162. } else {
  163. $html .= "\n".JText::_('No records found');
  164. }
  165. return $html;
  166. }
  167. /**
  168. * Create and return the pagination page list string, ie. Previous, Next, 1 2 3 ... x
  169. *
  170. * @access public
  171. * @return string Pagination page list string
  172. * @since 1.0
  173. */
  174. function getPagesLinks()
  175. {
  176. global $mainframe;
  177. $lang =& JFactory::getLanguage();
  178. // Build the page navigation list
  179. $data = $this->_buildDataObject();
  180. $list = array();
  181. $itemOverride = false;
  182. $listOverride = false;
  183. $chromePath = JPATH_THEMES.DS.$mainframe->getTemplate().DS.'html'.DS.'pagination.php';
  184. if (file_exists($chromePath))
  185. {
  186. require_once ($chromePath);
  187. if (function_exists('pagination_item_active') && function_exists('pagination_item_inactive')) {
  188. $itemOverride = true;
  189. }
  190. if (function_exists('pagination_list_render')) {
  191. $listOverride = true;
  192. }
  193. }
  194. // Build the select list
  195. if ($data->all->base !== null) {
  196. $list['all']['active'] = true;
  197. $list['all']['data'] = ($itemOverride) ? pagination_item_active($data->all) : $this->_item_active($data->all);
  198. } else {
  199. $list['all']['active'] = false;
  200. $list['all']['data'] = ($itemOverride) ? pagination_item_inactive($data->all) : $this->_item_inactive($data->all);
  201. }
  202. if ($data->start->base !== null) {
  203. $list['start']['active'] = true;
  204. $list['start']['data'] = ($itemOverride) ? pagination_item_active($data->start) : $this->_item_active($data->start);
  205. } else {
  206. $list['start']['active'] = false;
  207. $list['start']['data'] = ($itemOverride) ? pagination_item_inactive($data->start) : $this->_item_inactive($data->start);
  208. }
  209. if ($data->previous->base !== null) {
  210. $list['previous']['active'] = true;
  211. $list['previous']['data'] = ($itemOverride) ? pagination_item_active($data->previous) : $this->_item_active($data->previous);
  212. } else {
  213. $list['previous']['active'] = false;
  214. $list['previous']['data'] = ($itemOverride) ? pagination_item_inactive($data->previous) : $this->_item_inactive($data->previous);
  215. }
  216. $list['pages'] = array(); //make sure it exists
  217. foreach ($data->pages as $i => $page)
  218. {
  219. if ($page->base !== null) {
  220. $list['pages'][$i]['active'] = true;
  221. $list['pages'][$i]['data'] = ($itemOverride) ? pagination_item_active($page) : $this->_item_active($page);
  222. } else {
  223. $list['pages'][$i]['active'] = false;
  224. $list['pages'][$i]['data'] = ($itemOverride) ? pagination_item_inactive($page) : $this->_item_inactive($page);
  225. }
  226. }
  227. if ($data->next->base !== null) {
  228. $list['next']['active'] = true;
  229. $list['next']['data'] = ($itemOverride) ? pagination_item_active($data->next) : $this->_item_active($data->next);
  230. } else {
  231. $list['next']['active'] = false;
  232. $list['next']['data'] = ($itemOverride) ? pagination_item_inactive($data->next) : $this->_item_inactive($data->next);
  233. }
  234. if ($data->end->base !== null) {
  235. $list['end']['active'] = true;
  236. $list['end']['data'] = ($itemOverride) ? pagination_item_active($data->end) : $this->_item_active($data->end);
  237. } else {
  238. $list['end']['active'] = false;
  239. $list['end']['data'] = ($itemOverride) ? pagination_item_inactive($data->end) : $this->_item_inactive($data->end);
  240. }
  241. if($this->total > $this->limit){
  242. return ($listOverride) ? pagination_list_render($list) : $this->_list_render($list);
  243. }
  244. else{
  245. return '';
  246. }
  247. }
  248. /**
  249. * Return the pagination footer
  250. *
  251. * @access public
  252. * @return string Pagination footer
  253. * @since 1.0
  254. */
  255. function getListFooter()
  256. {
  257. global $mainframe;
  258. $list = array();
  259. $list['limit'] = $this->limit;
  260. $list['limitstart'] = $this->limitstart;
  261. $list['total'] = $this->total;
  262. $list['limitfield'] = $this->getLimitBox();
  263. $list['pagescounter'] = $this->getPagesCounter();
  264. $list['pageslinks'] = $this->getPagesLinks();
  265. $chromePath = JPATH_THEMES.DS.$mainframe->getTemplate().DS.'html'.DS.'pagination.php';
  266. if (file_exists( $chromePath ))
  267. {
  268. require_once( $chromePath );
  269. if (function_exists( 'pagination_list_footer' )) {
  270. return pagination_list_footer( $list );
  271. }
  272. }
  273. return $this->_list_footer($list);
  274. }
  275. /**
  276. * Creates a dropdown box for selecting how many records to show per page
  277. *
  278. * @access public
  279. * @return string The html for the limit # input box
  280. * @since 1.0
  281. */
  282. function getLimitBox()
  283. {
  284. global $mainframe;
  285. // Initialize variables
  286. $limits = array ();
  287. // Make the option list
  288. for ($i = 5; $i <= 30; $i += 5) {
  289. $limits[] = JHTML::_('select.option', "$i");
  290. }
  291. $limits[] = JHTML::_('select.option', '50');
  292. $limits[] = JHTML::_('select.option', '100');
  293. $limits[] = JHTML::_('select.option', '0', JText::_('all'));
  294. $selected = $this->_viewall ? 0 : $this->limit;
  295. // Build the select list
  296. if ($mainframe->isAdmin()) {
  297. $html = JHTML::_('select.genericlist', $limits, 'limit', 'class="inputbox" size="1" onchange="submitform();"', 'value', 'text', $selected);
  298. } else {
  299. $html = JHTML::_('select.genericlist', $limits, 'limit', 'class="inputbox" size="1" onchange="this.form.submit()"', 'value', 'text', $selected);
  300. }
  301. return $html;
  302. }
  303. /**
  304. * Return the icon to move an item UP
  305. *
  306. * @access public
  307. * @param int $i The row index
  308. * @param boolean $condition True to show the icon
  309. * @param string $task The task to fire
  310. * @param string $alt The image alternate text string
  311. * @return string Either the icon to move an item up or a space
  312. * @since 1.0
  313. */
  314. function orderUpIcon($i, $condition = true, $task = 'orderup', $alt = 'Move Up', $enabled = true)
  315. {
  316. $alt = JText::_($alt);
  317. $html = '&nbsp;';
  318. if (($i > 0 || ($i + $this->limitstart > 0)) && $condition)
  319. {
  320. if($enabled) {
  321. $html = '<a href="#reorder" onclick="return listItemTask(\'cb'.$i.'\',\''.$task.'\')" title="'.$alt.'">';
  322. $html .= ' <img src="images/uparrow.png" width="16" height="16" border="0" alt="'.$alt.'" />';
  323. $html .= '</a>';
  324. } else {
  325. $html = '<img src="images/uparrow0.png" width="16" height="16" border="0" alt="'.$alt.'" />';
  326. }
  327. }
  328. return $html;
  329. }
  330. /**
  331. * Return the icon to move an item DOWN
  332. *
  333. * @access public
  334. * @param int $i The row index
  335. * @param int $n The number of items in the list
  336. * @param boolean $condition True to show the icon
  337. * @param string $task The task to fire
  338. * @param string $alt The image alternate text string
  339. * @return string Either the icon to move an item down or a space
  340. * @since 1.0
  341. */
  342. function orderDownIcon($i, $n, $condition = true, $task = 'orderdown', $alt = 'Move Down', $enabled = true)
  343. {
  344. $alt = JText::_($alt);
  345. $html = '&nbsp;';
  346. if (($i < $n -1 || $i + $this->limitstart < $this->total - 1) && $condition)
  347. {
  348. if($enabled) {
  349. $html = '<a href="#reorder" onclick="return listItemTask(\'cb'.$i.'\',\''.$task.'\')" title="'.$alt.'">';
  350. $html .= ' <img src="images/downarrow.png" width="16" height="16" border="0" alt="'.$alt.'" />';
  351. $html .= '</a>';
  352. } else {
  353. $html = '<img src="images/downarrow0.png" width="16" height="16" border="0" alt="'.$alt.'" />';
  354. }
  355. }
  356. return $html;
  357. }
  358. function _list_footer($list)
  359. {
  360. // Initialize variables
  361. $html = "<div class=\"list-footer\">\n";
  362. $html .= "\n<div class=\"limit\">".JText::_('Display Num').$list['limitfield']."</div>";
  363. $html .= $list['pageslinks'];
  364. $html .= "\n<div class=\"counter\">".$list['pagescounter']."</div>";
  365. $html .= "\n<input type=\"hidden\" name=\"limitstart\" value=\"".$list['limitstart']."\" />";
  366. $html .= "\n</div>";
  367. return $html;
  368. }
  369. function _list_render($list)
  370. {
  371. // Initialize variables
  372. $html = null;
  373. // Reverse output rendering for right-to-left display
  374. $html .= '&lt;&lt; ';
  375. $html .= $list['start']['data'];
  376. $html .= ' &lt; ';
  377. $html .= $list['previous']['data'];
  378. foreach( $list['pages'] as $page ) {
  379. $html .= ' '.$page['data'];
  380. }
  381. $html .= ' '. $list['next']['data'];
  382. $html .= ' &gt;';
  383. $html .= ' '. $list['end']['data'];
  384. $html .= ' &gt;&gt;';
  385. return $html;
  386. }
  387. function _item_active(&$item)
  388. {
  389. global $mainframe;
  390. if ($mainframe->isAdmin())
  391. {
  392. if($item->base>0)
  393. return "<a title=\"".$item->text."\" onclick=\"javascript: document.adminForm.limitstart.value=".$item->base."; submitform();return false;\">".$item->text."</a>";
  394. else
  395. return "<a title=\"".$item->text."\" onclick=\"javascript: document.adminForm.limitstart.value=0; submitform();return false;\">".$item->text."</a>";
  396. } else {
  397. return "<a title=\"".$item->text."\" href=\"".$item->link."\" class=\"pagenav\">".$item->text."</a>";
  398. }
  399. }
  400. function _item_inactive(&$item)
  401. {
  402. global $mainframe;
  403. if ($mainframe->isAdmin()) {
  404. return "<span>".$item->text."</span>";
  405. } else {
  406. return "<span class=\"pagenav\">".$item->text."</span>";
  407. }
  408. }
  409. /**
  410. * Create and return the pagination data object
  411. *
  412. * @access public
  413. * @return object Pagination data object
  414. * @since 1.5
  415. */
  416. function _buildDataObject()
  417. {
  418. // Initialize variables
  419. $data = new stdClass();
  420. $data->all = new JPaginationObject(JText::_('View All'));
  421. if (!$this->_viewall) {
  422. $data->all->base = '0';
  423. $data->all->link = JRoute::_("&limitstart=");
  424. }
  425. // Set the start and previous data objects
  426. $data->start = new JPaginationObject(JText::_('Start'));
  427. $data->previous = new JPaginationObject(JText::_('Prev'));
  428. if ($this->get('pages.current') > 1)
  429. {
  430. $page = ($this->get('pages.current') -2) * $this->limit;
  431. $page = $page == 0 ? '' : $page; //set the empty for removal from route
  432. $data->start->base = '0';
  433. $data->start->link = JRoute::_("&limitstart=");
  434. $data->previous->base = $page;
  435. $data->previous->link = JRoute::_("&limitstart=".$page);
  436. }
  437. // Set the next and end data objects
  438. $data->next = new JPaginationObject(JText::_('Next'));
  439. $data->end = new JPaginationObject(JText::_('End'));
  440. if ($this->get('pages.current') < $this->get('pages.total'))
  441. {
  442. $next = $this->get('pages.current') * $this->limit;
  443. $end = ($this->get('pages.total') -1) * $this->limit;
  444. $data->next->base = $next;
  445. $data->next->link = JRoute::_("&limitstart=".$next);
  446. $data->end->base = $end;
  447. $data->end->link = JRoute::_("&limitstart=".$end);
  448. }
  449. $data->pages = array();
  450. $stop = $this->get('pages.stop');
  451. for ($i = $this->get('pages.start'); $i <= $stop; $i ++)
  452. {
  453. $offset = ($i -1) * $this->limit;
  454. $offset = $offset == 0 ? '' : $offset; //set the empty for removal from route
  455. $data->pages[$i] = new JPaginationObject($i);
  456. if ($i != $this->get('pages.current') || $this->_viewall)
  457. {
  458. $data->pages[$i]->base = $offset;
  459. $data->pages[$i]->link = JRoute::_("&limitstart=".$offset);
  460. }
  461. }
  462. return $data;
  463. }
  464. }
  465. /**
  466. * Pagination object representing a particular item in the pagination lists
  467. *
  468. * @package Joomla.Framework
  469. * @subpackage HTML
  470. * @since 1.5
  471. */
  472. class JPaginationObject extends JObject
  473. {
  474. var $text;
  475. var $base;
  476. var $link;
  477. function __construct($text, $base=null, $link=null)
  478. {
  479. $this->text = $text;
  480. $this->base = $base;
  481. $this->link = $link;
  482. }
  483. }