pager.inc

  1. 7.x drupal-7.x/includes/pager.inc
  2. 6.x drupal-6.x/includes/pager.inc

Functions to aid in presenting database results as a set of pages.

File

drupal-7.x/includes/pager.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Functions to aid in presenting database results as a set of pages.
  5. */
  6. /**
  7. * Query extender for pager queries.
  8. *
  9. * This is the "default" pager mechanism. It creates a paged query with a fixed
  10. * number of entries per page.
  11. */
  12. class PagerDefault extends SelectQueryExtender {
  13. /**
  14. * The highest element we've autogenerated so far.
  15. *
  16. * @var int
  17. */
  18. static $maxElement = 0;
  19. /**
  20. * The number of elements per page to allow.
  21. *
  22. * @var int
  23. */
  24. protected $limit = 10;
  25. /**
  26. * The unique ID of this pager on this page.
  27. *
  28. * @var int
  29. */
  30. protected $element = NULL;
  31. /**
  32. * The count query that will be used for this pager.
  33. *
  34. * @var SelectQueryInterface
  35. */
  36. protected $customCountQuery = FALSE;
  37. public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) {
  38. parent::__construct($query, $connection);
  39. // Add pager tag. Do this here to ensure that it is always added before
  40. // preExecute() is called.
  41. $this->addTag('pager');
  42. }
  43. /**
  44. * Override the execute method.
  45. *
  46. * Before we run the query, we need to add pager-based range() instructions
  47. * to it.
  48. */
  49. public function execute() {
  50. // Add convenience tag to mark that this is an extended query. We have to
  51. // do this in the constructor to ensure that it is set before preExecute()
  52. // gets called.
  53. if (!$this->preExecute($this)) {
  54. return NULL;
  55. }
  56. // A NULL limit is the "kill switch" for pager queries.
  57. if (empty($this->limit)) {
  58. return;
  59. }
  60. $this->ensureElement();
  61. $total_items = $this->getCountQuery()->execute()->fetchField();
  62. $current_page = pager_default_initialize($total_items, $this->limit, $this->element);
  63. $this->range($current_page * $this->limit, $this->limit);
  64. // Now that we've added our pager-based range instructions, run the query normally.
  65. return $this->query->execute();
  66. }
  67. /**
  68. * Ensure that there is an element associated with this query.
  69. * If an element was not specified previously, then the value of the
  70. * $maxElement counter is taken, after which the counter is incremented.
  71. *
  72. * After running this method, access $this->element to get the element for this
  73. * query.
  74. */
  75. protected function ensureElement() {
  76. if (!isset($this->element)) {
  77. $this->element = self::$maxElement++;
  78. }
  79. }
  80. /**
  81. * Specify the count query object to use for this pager.
  82. *
  83. * You will rarely need to specify a count query directly. If not specified,
  84. * one is generated off of the pager query itself.
  85. *
  86. * @param SelectQueryInterface $query
  87. * The count query object. It must return a single row with a single column,
  88. * which is the total number of records.
  89. */
  90. public function setCountQuery(SelectQueryInterface $query) {
  91. $this->customCountQuery = $query;
  92. }
  93. /**
  94. * Retrieve the count query for this pager.
  95. *
  96. * The count query may be specified manually or, by default, taken from the
  97. * query we are extending.
  98. *
  99. * @return SelectQueryInterface
  100. * A count query object.
  101. */
  102. public function getCountQuery() {
  103. if ($this->customCountQuery) {
  104. return $this->customCountQuery;
  105. }
  106. else {
  107. return $this->query->countQuery();
  108. }
  109. }
  110. /**
  111. * Specify the maximum number of elements per page for this query.
  112. *
  113. * The default if not specified is 10 items per page.
  114. *
  115. * @param $limit
  116. * An integer specifying the number of elements per page. If passed a false
  117. * value (FALSE, 0, NULL), the pager is disabled.
  118. */
  119. public function limit($limit = 10) {
  120. $this->limit = $limit;
  121. return $this;
  122. }
  123. /**
  124. * Specify the element ID for this pager query.
  125. *
  126. * The element is used to differentiate different pager queries on the same
  127. * page so that they may be operated independently. If you do not specify an
  128. * element, every pager query on the page will get a unique element. If for
  129. * whatever reason you want to explicitly define an element for a given query,
  130. * you may do so here.
  131. *
  132. * Setting the element here also increments the static $maxElement counter,
  133. * which is used for determining the $element when there's none specified.
  134. *
  135. * Note that no collision detection is done when setting an element ID
  136. * explicitly, so it is possible for two pagers to end up using the same ID
  137. * if both are set explicitly.
  138. *
  139. * @param $element
  140. */
  141. public function element($element) {
  142. $this->element = $element;
  143. if ($element >= self::$maxElement) {
  144. self::$maxElement = $element + 1;
  145. }
  146. return $this;
  147. }
  148. }
  149. /**
  150. * Returns the current page being requested for display within a pager.
  151. *
  152. * @param $element
  153. * An optional integer to distinguish between multiple pagers on one page.
  154. *
  155. * @return
  156. * The number of the current requested page, within the pager represented by
  157. * $element. This is determined from the URL query parameter $_GET['page'], or
  158. * 0 by default. Note that this number may differ from the actual page being
  159. * displayed. For example, if a search for "example text" brings up three
  160. * pages of results, but a users visits search/node/example+text?page=10, this
  161. * function will return 10, even though the default pager implementation
  162. * adjusts for this and still displays the third page of search results at
  163. * that URL.
  164. *
  165. * @see pager_default_initialize()
  166. */
  167. function pager_find_page($element = 0) {
  168. $page = isset($_GET['page']) ? $_GET['page'] : '';
  169. $page_array = explode(',', $page);
  170. if (!isset($page_array[$element])) {
  171. $page_array[$element] = 0;
  172. }
  173. return (int) $page_array[$element];
  174. }
  175. /**
  176. * Initializes a pager for theme('pager').
  177. *
  178. * This function sets up the necessary global variables so that future calls
  179. * to theme('pager') will render a pager that correctly corresponds to the
  180. * items being displayed.
  181. *
  182. * If the items being displayed result from a database query performed using
  183. * Drupal's database API, and if you have control over the construction of the
  184. * database query, you do not need to call this function directly; instead, you
  185. * can simply extend the query object with the 'PagerDefault' extender before
  186. * executing it. For example:
  187. * @code
  188. * $query = db_select('some_table')->extend('PagerDefault');
  189. * @endcode
  190. *
  191. * However, if you are using a different method for generating the items to be
  192. * paged through, then you should call this function in preparation.
  193. *
  194. * The following example shows how this function can be used in a page callback
  195. * that invokes an external datastore with an SQL-like syntax:
  196. * @code
  197. * // First find the total number of items and initialize the pager.
  198. * $where = "status = 1";
  199. * $total = mymodule_select("SELECT COUNT(*) FROM data " . $where)->result();
  200. * $num_per_page = variable_get('mymodule_num_per_page', 10);
  201. * $page = pager_default_initialize($total, $num_per_page);
  202. *
  203. * // Next, retrieve and display the items for the current page.
  204. * $offset = $num_per_page * $page;
  205. * $result = mymodule_select("SELECT * FROM data " . $where . " LIMIT %d, %d", $offset, $num_per_page)->fetchAll();
  206. * $output = theme('mymodule_results', array('result' => $result));
  207. *
  208. * // Finally, display the pager controls, and return.
  209. * $output .= theme('pager');
  210. * return $output;
  211. * @endcode
  212. *
  213. * A second example involves a page callback that invokes an external search
  214. * service where the total number of matching results is provided as part of
  215. * the returned set (so that we do not need a separate query in order to obtain
  216. * this information). Here, we call pager_find_page() to calculate the desired
  217. * offset before the search is invoked:
  218. * @code
  219. * // Perform the query, using the requested offset from pager_find_page().
  220. * // This comes from a URL parameter, so here we are assuming that the URL
  221. * // parameter corresponds to an actual page of results that will exist
  222. * // within the set.
  223. * $page = pager_find_page();
  224. * $num_per_page = variable_get('mymodule_num_per_page', 10);
  225. * $offset = $num_per_page * $page;
  226. * $result = mymodule_remote_search($keywords, $offset, $num_per_page);
  227. *
  228. * // Now that we have the total number of results, initialize the pager.
  229. * pager_default_initialize($result->total, $num_per_page);
  230. *
  231. * // Display the search results.
  232. * $output = theme('search_results', array('results' => $result->data, 'type' => 'remote'));
  233. *
  234. * // Finally, display the pager controls, and return.
  235. * $output .= theme('pager');
  236. * return $output;
  237. * @endcode
  238. *
  239. * @param $total
  240. * The total number of items to be paged.
  241. * @param $limit
  242. * The number of items the calling code will display per page.
  243. * @param $element
  244. * An optional integer to distinguish between multiple pagers on one page.
  245. *
  246. * @return
  247. * The number of the current page, within the pager represented by $element.
  248. * This is determined from the URL query parameter $_GET['page'], or 0 by
  249. * default. However, if a page that does not correspond to the actual range
  250. * of the result set was requested, this function will return the closest
  251. * page actually within the result set.
  252. */
  253. function pager_default_initialize($total, $limit, $element = 0) {
  254. global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
  255. $page = pager_find_page($element);
  256. // We calculate the total of pages as ceil(items / limit).
  257. $pager_total_items[$element] = $total;
  258. $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  259. $pager_page_array[$element] = max(0, min($page, ((int) $pager_total[$element]) - 1));
  260. $pager_limits[$element] = $limit;
  261. return $pager_page_array[$element];
  262. }
  263. /**
  264. * Compose a URL query parameter array for pager links.
  265. *
  266. * @return
  267. * A URL query parameter array that consists of all components of the current
  268. * page request except for those pertaining to paging.
  269. */
  270. function pager_get_query_parameters() {
  271. $query = &drupal_static(__FUNCTION__);
  272. if (!isset($query)) {
  273. $query = drupal_get_query_parameters($_GET, array('q', 'page'));
  274. }
  275. return $query;
  276. }
  277. /**
  278. * Returns HTML for a query pager.
  279. *
  280. * Menu callbacks that display paged query results should call theme('pager') to
  281. * retrieve a pager control so that users can view other results. Format a list
  282. * of nearby pages with additional query results.
  283. *
  284. * @param $variables
  285. * An associative array containing:
  286. * - tags: An array of labels for the controls in the pager.
  287. * - element: An optional integer to distinguish between multiple pagers on
  288. * one page.
  289. * - parameters: An associative array of query string parameters to append to
  290. * the pager links.
  291. * - quantity: The number of pages in the list.
  292. *
  293. * @ingroup themeable
  294. */
  295. function theme_pager($variables) {
  296. $tags = $variables['tags'];
  297. $element = $variables['element'];
  298. $parameters = $variables['parameters'];
  299. $quantity = $variables['quantity'];
  300. global $pager_page_array, $pager_total;
  301. // Calculate various markers within this pager piece:
  302. // Middle is used to "center" pages around the current page.
  303. $pager_middle = ceil($quantity / 2);
  304. // current is the page we are currently paged to
  305. $pager_current = $pager_page_array[$element] + 1;
  306. // first is the first page listed by this pager piece (re quantity)
  307. $pager_first = $pager_current - $pager_middle + 1;
  308. // last is the last page listed by this pager piece (re quantity)
  309. $pager_last = $pager_current + $quantity - $pager_middle;
  310. // max is the maximum page number
  311. $pager_max = $pager_total[$element];
  312. // End of marker calculations.
  313. // Prepare for generation loop.
  314. $i = $pager_first;
  315. if ($pager_last > $pager_max) {
  316. // Adjust "center" if at end of query.
  317. $i = $i + ($pager_max - $pager_last);
  318. $pager_last = $pager_max;
  319. }
  320. if ($i <= 0) {
  321. // Adjust "center" if at start of query.
  322. $pager_last = $pager_last + (1 - $i);
  323. $i = 1;
  324. }
  325. // End of generation loop preparation.
  326. $li_first = theme('pager_first', array('text' => (isset($tags[0]) ? $tags[0] : t('« first')), 'element' => $element, 'parameters' => $parameters));
  327. $li_previous = theme('pager_previous', array('text' => (isset($tags[1]) ? $tags[1] : t('‹ previous')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  328. $li_next = theme('pager_next', array('text' => (isset($tags[3]) ? $tags[3] : t('next ›')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  329. $li_last = theme('pager_last', array('text' => (isset($tags[4]) ? $tags[4] : t('last »')), 'element' => $element, 'parameters' => $parameters));
  330. if ($pager_total[$element] > 1) {
  331. if ($li_first) {
  332. $items[] = array(
  333. 'class' => array('pager-first'),
  334. 'data' => $li_first,
  335. );
  336. }
  337. if ($li_previous) {
  338. $items[] = array(
  339. 'class' => array('pager-previous'),
  340. 'data' => $li_previous,
  341. );
  342. }
  343. // When there is more than one page, create the pager list.
  344. if ($i != $pager_max) {
  345. if ($i > 1) {
  346. $items[] = array(
  347. 'class' => array('pager-ellipsis'),
  348. 'data' => '…',
  349. );
  350. }
  351. // Now generate the actual pager piece.
  352. for (; $i <= $pager_last && $i <= $pager_max; $i++) {
  353. if ($i < $pager_current) {
  354. $items[] = array(
  355. 'class' => array('pager-item'),
  356. 'data' => theme('pager_previous', array('text' => $i, 'element' => $element, 'interval' => ($pager_current - $i), 'parameters' => $parameters)),
  357. );
  358. }
  359. if ($i == $pager_current) {
  360. $items[] = array(
  361. 'class' => array('pager-current'),
  362. 'data' => $i,
  363. );
  364. }
  365. if ($i > $pager_current) {
  366. $items[] = array(
  367. 'class' => array('pager-item'),
  368. 'data' => theme('pager_next', array('text' => $i, 'element' => $element, 'interval' => ($i - $pager_current), 'parameters' => $parameters)),
  369. );
  370. }
  371. }
  372. if ($i < $pager_max) {
  373. $items[] = array(
  374. 'class' => array('pager-ellipsis'),
  375. 'data' => '…',
  376. );
  377. }
  378. }
  379. // End generation.
  380. if ($li_next) {
  381. $items[] = array(
  382. 'class' => array('pager-next'),
  383. 'data' => $li_next,
  384. );
  385. }
  386. if ($li_last) {
  387. $items[] = array(
  388. 'class' => array('pager-last'),
  389. 'data' => $li_last,
  390. );
  391. }
  392. return '<h2 class="element-invisible">' . t('Pages') . '</h2>' . theme('item_list', array(
  393. 'items' => $items,
  394. 'attributes' => array('class' => array('pager')),
  395. ));
  396. }
  397. }
  398. /**
  399. * @defgroup pagerpieces Pager pieces
  400. * @{
  401. * Theme functions for customizing pager elements.
  402. *
  403. * Note that you should NOT modify this file to customize your pager.
  404. */
  405. /**
  406. * Returns HTML for the "first page" link in a query pager.
  407. *
  408. * @param $variables
  409. * An associative array containing:
  410. * - text: The name (or image) of the link.
  411. * - element: An optional integer to distinguish between multiple pagers on
  412. * one page.
  413. * - parameters: An associative array of query string parameters to append to
  414. * the pager links.
  415. *
  416. * @ingroup themeable
  417. */
  418. function theme_pager_first($variables) {
  419. $text = $variables['text'];
  420. $element = $variables['element'];
  421. $parameters = $variables['parameters'];
  422. global $pager_page_array;
  423. $output = '';
  424. // If we are anywhere but the first page
  425. if ($pager_page_array[$element] > 0) {
  426. $output = theme('pager_link', array('text' => $text, 'page_new' => pager_load_array(0, $element, $pager_page_array), 'element' => $element, 'parameters' => $parameters));
  427. }
  428. return $output;
  429. }
  430. /**
  431. * Returns HTML for the "previous page" link in a query pager.
  432. *
  433. * @param $variables
  434. * An associative array containing:
  435. * - text: The name (or image) of the link.
  436. * - element: An optional integer to distinguish between multiple pagers on
  437. * one page.
  438. * - interval: The number of pages to move backward when the link is clicked.
  439. * - parameters: An associative array of query string parameters to append to
  440. * the pager links.
  441. *
  442. * @ingroup themeable
  443. */
  444. function theme_pager_previous($variables) {
  445. $text = $variables['text'];
  446. $element = $variables['element'];
  447. $interval = $variables['interval'];
  448. $parameters = $variables['parameters'];
  449. global $pager_page_array;
  450. $output = '';
  451. // If we are anywhere but the first page
  452. if ($pager_page_array[$element] > 0) {
  453. $page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array);
  454. // If the previous page is the first page, mark the link as such.
  455. if ($page_new[$element] == 0) {
  456. $output = theme('pager_first', array('text' => $text, 'element' => $element, 'parameters' => $parameters));
  457. }
  458. // The previous page is not the first page.
  459. else {
  460. $output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters));
  461. }
  462. }
  463. return $output;
  464. }
  465. /**
  466. * Returns HTML for the "next page" link in a query pager.
  467. *
  468. * @param $variables
  469. * An associative array containing:
  470. * - text: The name (or image) of the link.
  471. * - element: An optional integer to distinguish between multiple pagers on
  472. * one page.
  473. * - interval: The number of pages to move forward when the link is clicked.
  474. * - parameters: An associative array of query string parameters to append to
  475. * the pager links.
  476. *
  477. * @ingroup themeable
  478. */
  479. function theme_pager_next($variables) {
  480. $text = $variables['text'];
  481. $element = $variables['element'];
  482. $interval = $variables['interval'];
  483. $parameters = $variables['parameters'];
  484. global $pager_page_array, $pager_total;
  485. $output = '';
  486. // If we are anywhere but the last page
  487. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  488. $page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
  489. // If the next page is the last page, mark the link as such.
  490. if ($page_new[$element] == ($pager_total[$element] - 1)) {
  491. $output = theme('pager_last', array('text' => $text, 'element' => $element, 'parameters' => $parameters));
  492. }
  493. // The next page is not the last page.
  494. else {
  495. $output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters));
  496. }
  497. }
  498. return $output;
  499. }
  500. /**
  501. * Returns HTML for the "last page" link in query pager.
  502. *
  503. * @param $variables
  504. * An associative array containing:
  505. * - text: The name (or image) of the link.
  506. * - element: An optional integer to distinguish between multiple pagers on
  507. * one page.
  508. * - parameters: An associative array of query string parameters to append to
  509. * the pager links.
  510. *
  511. * @ingroup themeable
  512. */
  513. function theme_pager_last($variables) {
  514. $text = $variables['text'];
  515. $element = $variables['element'];
  516. $parameters = $variables['parameters'];
  517. global $pager_page_array, $pager_total;
  518. $output = '';
  519. // If we are anywhere but the last page
  520. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  521. $output = theme('pager_link', array('text' => $text, 'page_new' => pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), 'element' => $element, 'parameters' => $parameters));
  522. }
  523. return $output;
  524. }
  525. /**
  526. * Returns HTML for a link to a specific query result page.
  527. *
  528. * @param $variables
  529. * An associative array containing:
  530. * - text: The link text. Also used to figure out the title attribute of the
  531. * link, if it is not provided in $variables['attributes']['title']; in
  532. * this case, $variables['text'] must be one of the standard pager link
  533. * text strings that would be generated by the pager theme functions, such
  534. * as a number or t('« first').
  535. * - page_new: The first result to display on the linked page.
  536. * - element: An optional integer to distinguish between multiple pagers on
  537. * one page.
  538. * - parameters: An associative array of query string parameters to append to
  539. * the pager link.
  540. * - attributes: An associative array of HTML attributes to apply to the
  541. * pager link.
  542. *
  543. * @see theme_pager()
  544. *
  545. * @ingroup themeable
  546. */
  547. function theme_pager_link($variables) {
  548. $text = $variables['text'];
  549. $page_new = $variables['page_new'];
  550. $element = $variables['element'];
  551. $parameters = $variables['parameters'];
  552. $attributes = $variables['attributes'];
  553. $page = isset($_GET['page']) ? $_GET['page'] : '';
  554. if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
  555. $parameters['page'] = $new_page;
  556. }
  557. $query = array();
  558. if (count($parameters)) {
  559. $query = drupal_get_query_parameters($parameters, array());
  560. }
  561. if ($query_pager = pager_get_query_parameters()) {
  562. $query = array_merge($query, $query_pager);
  563. }
  564. // Set each pager link title
  565. if (!isset($attributes['title'])) {
  566. static $titles = NULL;
  567. if (!isset($titles)) {
  568. $titles = array(
  569. t('« first') => t('Go to first page'),
  570. t('‹ previous') => t('Go to previous page'),
  571. t('next ›') => t('Go to next page'),
  572. t('last »') => t('Go to last page'),
  573. );
  574. }
  575. if (isset($titles[$text])) {
  576. $attributes['title'] = $titles[$text];
  577. }
  578. elseif (is_numeric($text)) {
  579. $attributes['title'] = t('Go to page @number', array('@number' => $text));
  580. }
  581. }
  582. // @todo l() cannot be used here, since it adds an 'active' class based on the
  583. // path only (which is always the current path for pager links). Apparently,
  584. // none of the pager links is active at any time - but it should still be
  585. // possible to use l() here.
  586. // @see http://drupal.org/node/1410574
  587. $attributes['href'] = url($_GET['q'], array('query' => $query));
  588. return '<a' . drupal_attributes($attributes) . '>' . check_plain($text) . '</a>';
  589. }
  590. /**
  591. * @} End of "Pager pieces".
  592. */
  593. /**
  594. * Helper function
  595. *
  596. * Copies $old_array to $new_array and sets $new_array[$element] = $value
  597. * Fills in $new_array[0 .. $element - 1] = 0
  598. */
  599. function pager_load_array($value, $element, $old_array) {
  600. $new_array = $old_array;
  601. // Look for empty elements.
  602. for ($i = 0; $i < $element; $i++) {
  603. if (empty($new_array[$i])) {
  604. // Load found empty element with 0.
  605. $new_array[$i] = 0;
  606. }
  607. }
  608. // Update the changed element.
  609. $new_array[$element] = (int) $value;
  610. return $new_array;
  611. }