pager.inc

  1. drupal
    1. 4.6 includes/pager.inc
    2. 4.7 includes/pager.inc
    3. 5 includes/pager.inc
    4. 6 includes/pager.inc
    5. 7 includes/pager.inc
    6. 8 core/includes/pager.inc

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

Functions & methods

NameDescription
pager_get_querystringCompose a query string to append to pager requests.
pager_load_arrayHelper function
pager_queryPerform a paged database query.
theme_pagerFormat a query pager.
theme_pager_firstFormat a "first page" link.
theme_pager_lastFormat a "last page" link.
theme_pager_linkFormat a link to a specific query result page.
theme_pager_listFormat a list of nearby pages with additional query results.
theme_pager_nextFormat a "next page" link.
theme_pager_previousFormat a "previous page" link.

File

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. * Perform a paged database query.
  8. *
  9. * Use this function when doing select queries you wish to be able to page. The
  10. * pager uses LIMIT-based queries to fetch only the records required to render a
  11. * certain page. However, it has to learn the total number of records returned
  12. * by the query to compute the number of pages (the number of records / records
  13. * per page). This is done by inserting "COUNT(*)" in the original query. For
  14. * example, the query "SELECT nid, type FROM node WHERE status = '1' ORDER BY
  15. * sticky DESC, created DESC" would be rewritten to read "SELECT COUNT(*) FROM
  16. * node WHERE status = '1' ORDER BY sticky DESC, created DESC". Rewriting the
  17. * query is accomplished using a regular expression.
  18. *
  19. * Unfortunately, the rewrite rule does not always work as intended for queries
  20. * that already have a "COUNT(*)" or a "GROUP BY" clause, and possibly for
  21. * other complex queries. In those cases, you can optionally pass a query that
  22. * will be used to count the records.
  23. *
  24. * For example, if you want to page the query "SELECT COUNT(*), TYPE FROM node
  25. * GROUP BY TYPE", pager_query() would invoke the incorrect query "SELECT
  26. * COUNT(*) FROM node GROUP BY TYPE". So instead, you should pass "SELECT
  27. * COUNT(DISTINCT(TYPE)) FROM node" as the optional $count_query parameter.
  28. *
  29. * @param $query
  30. * The SQL query that needs paging.
  31. * @param $limit
  32. * The number of query results to display per page.
  33. * @param $element
  34. * An optional integer to distinguish between multiple pagers on one page.
  35. * @param $count_query
  36. * An SQL query used to count matching records.
  37. * @param ...
  38. * A variable number of arguments which are substituted into the query (and
  39. * the count query) using printf() syntax. Instead of a variable number of
  40. * query arguments, you may also pass a single array containing the query
  41. * arguments.
  42. * @return
  43. * A database query result resource, or FALSE if the query was not executed
  44. * correctly.
  45. *
  46. * @ingroup database
  47. */
  48. function pager_query($query, $limit = 10, $element = 0, $count_query = NULL) {
  49. global $pager_page_array, $pager_total, $pager_total_items;
  50. $page = isset($_GET['page']) ? $_GET['page'] : '';
  51. // Substitute in query arguments.
  52. $args = func_get_args();
  53. $args = array_slice($args, 4);
  54. // Alternative syntax for '...'
  55. if (isset($args[0]) && is_array($args[0])) {
  56. $args = $args[0];
  57. }
  58. // Construct a count query if none was given.
  59. if (!isset($count_query)) {
  60. $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM ', ''), $query);
  61. }
  62. // Convert comma-separated $page to an array, used by other functions.
  63. $pager_page_array = explode(',', $page);
  64. // We calculate the total of pages as ceil(items / limit).
  65. if (count($args)) {
  66. $pager_total_items[$element] = db_result(db_query($count_query, $args));
  67. $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  68. $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  69. return db_query_range($query, $args, $pager_page_array[$element] * $limit, $limit);
  70. }
  71. else {
  72. $pager_total_items[$element] = db_result(db_query($count_query));
  73. $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  74. $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  75. return db_query_range($query, $pager_page_array[$element] * $limit, $limit);
  76. }
  77. }
  78. /**
  79. * Compose a query string to append to pager requests.
  80. *
  81. * @return
  82. * A query string that consists of all components of the current page request
  83. * except for those pertaining to paging.
  84. */
  85. function pager_get_querystring() {
  86. static $string = NULL;
  87. if (!isset($string)) {
  88. $string = drupal_query_string_encode($_REQUEST, array_merge(array('q', 'page'), array_keys($_COOKIE)));
  89. }
  90. return $string;
  91. }
  92. /**
  93. * Format a query pager.
  94. *
  95. * Menu callbacks that display paged query results should call theme('pager') to
  96. * retrieve a pager control so that users can view other results.
  97. *
  98. * @param $tags
  99. * An array of labels for the controls in the pager.
  100. * @param $limit
  101. * The number of query results to display per page.
  102. * @param $element
  103. * An optional integer to distinguish between multiple pagers on one page.
  104. * @param $parameters
  105. * An associative array of query string parameters to append to the pager links.
  106. * @return
  107. * An HTML string that generates the query pager.
  108. *
  109. * @ingroup themeable
  110. */
  111. function theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array()) {
  112. global $pager_total;
  113. $output = '';
  114. if ($pager_total[$element] > 1) {
  115. $output .= '<div id="pager">';
  116. $output .= theme('pager_first', ($tags[0] ? $tags[0] : t('« first')), $limit, $element, $parameters);
  117. $output .= theme('pager_previous', ($tags[1] ? $tags[1] : t('‹ previous')), $limit, $element, 1, $parameters);
  118. $output .= theme('pager_list', $limit, $element, ($tags[2] ? $tags[2] : 9 ), '', $parameters);
  119. $output .= theme('pager_next', ($tags[3] ? $tags[3] : t('next ›')), $limit, $element, 1, $parameters);
  120. $output .= theme('pager_last', ($tags[4] ? $tags[4] : t('last »')), $limit, $element, $parameters);
  121. $output .= '</div>';
  122. return $output;
  123. }
  124. }
  125. /**
  126. * @name Pager pieces
  127. * @{
  128. * Use these pieces to construct your own custom pagers in your theme. Note that
  129. * you should NOT modify this file to customize your pager.
  130. */
  131. /**
  132. * Format a "first page" link.
  133. *
  134. * @param $text
  135. * The name (or image) of the link.
  136. * @param $limit
  137. * The number of query results to display per page.
  138. * @param $element
  139. * An optional integer to distinguish between multiple pagers on one page.
  140. * @param $parameters
  141. * An associative array of query string parameters to append to the pager links.
  142. * @return
  143. * An HTML string that generates this piece of the query pager.
  144. *
  145. * @ingroup themeable
  146. */
  147. function theme_pager_first($text, $limit, $element = 0, $parameters = array()) {
  148. global $pager_page_array;
  149. $output = '';
  150. // If we are anywhere but the first page
  151. if ($pager_page_array[$element] > 0) {
  152. $output = theme('pager_link', $text, pager_load_array(0, $element, $pager_page_array), $element, $parameters, array('class' => 'pager-first'));
  153. }
  154. return $output;
  155. }
  156. /**
  157. * Format a "previous page" link.
  158. *
  159. * @param $text
  160. * The name (or image) of the link.
  161. * @param $limit
  162. * The number of query results to display per page.
  163. * @param $element
  164. * An optional integer to distinguish between multiple pagers on one page.
  165. * @param $interval
  166. * The number of pages to move backward when the link is clicked.
  167. * @param $parameters
  168. * An associative array of query string parameters to append to the pager links.
  169. * @return
  170. * An HTML string that generates this piece of the query pager.
  171. *
  172. * @ingroup themeable
  173. */
  174. function theme_pager_previous($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
  175. global $pager_page_array;
  176. $output = '';
  177. // If we are anywhere but the first page
  178. if ($pager_page_array[$element] > 0) {
  179. $page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array);
  180. // If the previous page is the first page, mark the link as such.
  181. if ($page_new[$element] == 0) {
  182. $output = theme('pager_first', $text, $limit, $element, $parameters);
  183. }
  184. // The previous page is not the first page.
  185. else {
  186. $output = theme('pager_link', $text, $page_new, $element, $parameters, array('class' => 'pager-previous'));
  187. }
  188. }
  189. return $output;
  190. }
  191. /**
  192. * Format a "next page" link.
  193. *
  194. * @param $text
  195. * The name (or image) of the link.
  196. * @param $limit
  197. * The number of query results to display per page.
  198. * @param $element
  199. * An optional integer to distinguish between multiple pagers on one page.
  200. * @param $interval
  201. * The number of pages to move forward when the link is clicked.
  202. * @param $parameters
  203. * An associative array of query string parameters to append to the pager links.
  204. * @return
  205. * An HTML string that generates this piece of the query pager.
  206. *
  207. * @ingroup themeable
  208. */
  209. function theme_pager_next($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
  210. global $pager_page_array, $pager_total;
  211. $output = '';
  212. // If we are anywhere but the last page
  213. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  214. $page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
  215. // If the next page is the last page, mark the link as such.
  216. if ($page_new[$element] == ($pager_total[$element] - 1)) {
  217. $output = theme('pager_last', $text, $limit, $element, $parameters);
  218. }
  219. // The next page is not the last page.
  220. else {
  221. $output = theme('pager_link', $text, $page_new, $element, $parameters, array('class' => 'pager-next'));
  222. }
  223. }
  224. return $output;
  225. }
  226. /**
  227. * Format a "last page" link.
  228. *
  229. * @param $text
  230. * The name (or image) of the link.
  231. * @param $limit
  232. * The number of query results to display per page.
  233. * @param $element
  234. * An optional integer to distinguish between multiple pagers on one page.
  235. * @param $parameters
  236. * An associative array of query string parameters to append to the pager links.
  237. * @return
  238. * An HTML string that generates this piece of the query pager.
  239. *
  240. * @ingroup themeable
  241. */
  242. function theme_pager_last($text, $limit, $element = 0, $parameters = array()) {
  243. global $pager_page_array, $pager_total;
  244. $output = '';
  245. // If we are anywhere but the last page
  246. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  247. $output = theme('pager_link', $text, pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), $element, $parameters, array('class' => 'pager-last'));
  248. }
  249. return $output;
  250. }
  251. /**
  252. * Format a list of nearby pages with additional query results.
  253. *
  254. * @param $limit
  255. * The number of query results to display per page.
  256. * @param $element
  257. * An optional integer to distinguish between multiple pagers on one page.
  258. * @param $quantity
  259. * The number of pages in the list.
  260. * @param $text
  261. * A string of text to display before the page list.
  262. * @param $parameters
  263. * An associative array of query string parameters to append to the pager links.
  264. * @return
  265. * An HTML string that generates this piece of the query pager.
  266. *
  267. * @ingroup themeable
  268. */
  269. function theme_pager_list($limit, $element = 0, $quantity = 5, $text = '', $parameters = array()) {
  270. global $pager_page_array, $pager_total;
  271. $output = '<span class="pager-list">';
  272. // Calculate various markers within this pager piece:
  273. // Middle is used to "center" pages around the current page.
  274. $pager_middle = ceil($quantity / 2);
  275. // current is the page we are currently paged to
  276. $pager_current = $pager_page_array[$element] + 1;
  277. // first is the first page listed by this pager piece (re quantity)
  278. $pager_first = $pager_current - $pager_middle + 1;
  279. // last is the last page listed by this pager piece (re quantity)
  280. $pager_last = $pager_current + $quantity - $pager_middle;
  281. // max is the maximum page number
  282. $pager_max = $pager_total[$element];
  283. // End of marker calculations.
  284. // Prepare for generation loop.
  285. $i = $pager_first;
  286. if ($pager_last > $pager_max) {
  287. // Adjust "center" if at end of query.
  288. $i = $i + ($pager_max - $pager_last);
  289. $pager_last = $pager_max;
  290. }
  291. if ($i <= 0) {
  292. // Adjust "center" if at start of query.
  293. $pager_last = $pager_last + (1 - $i);
  294. $i = 1;
  295. }
  296. // End of generation loop preparation.
  297. // When there is more than one page, create the pager list.
  298. if ($i != $pager_max) {
  299. $output .= $text;
  300. if ($i > 1) {
  301. $output .= '<span class="pager-ellipsis">…</span>';
  302. }
  303. // Now generate the actual pager piece.
  304. for (; $i <= $pager_last && $i <= $pager_max; $i++) {
  305. if ($i < $pager_current) {
  306. $output .= theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters);
  307. }
  308. if ($i == $pager_current) {
  309. $output .= '<strong class="pager-current">'. $i .'</strong>';
  310. }
  311. if ($i > $pager_current) {
  312. $output .= theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters);
  313. }
  314. }
  315. if ($i < $pager_max) {
  316. $output .= '<span class="pager-ellipsis">…</span>';
  317. }
  318. }
  319. $output .= '</span>';
  320. return $output;
  321. }
  322. /**
  323. * Format a link to a specific query result page.
  324. *
  325. * @param $page_new
  326. * The first result to display on the linked page.
  327. * @param $element
  328. * An optional integer to distinguish between multiple pagers on one page.
  329. * @param $parameters
  330. * An associative array of query string parameters to append to the pager link.
  331. * @param $attributes
  332. * An associative array of HTML attributes to apply to a pager anchor tag.
  333. * @return
  334. * An HTML string that generates the link.
  335. */
  336. function theme_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array()) {
  337. $page = isset($_GET['page']) ? $_GET['page'] : '';
  338. if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
  339. $parameters['page'] = $new_page;
  340. }
  341. $query = array();
  342. if (count($parameters)) {
  343. $query[] = drupal_query_string_encode($parameters, array());
  344. }
  345. $querystring = pager_get_querystring();
  346. if ($querystring != '') {
  347. $query[] = $querystring;
  348. }
  349. // Set each pager link title
  350. if (!isset($attributes['title'])) {
  351. static $titles = null;
  352. if (!isset($titles)) {
  353. $titles = array(
  354. t('« first') => t('Go to first page'),
  355. t('‹ previous') => t('Go to previous page'),
  356. t('next ›') => t('Go to next page'),
  357. t('last »') => t('Go to last page'),
  358. );
  359. }
  360. if (isset($titles[$text])) {
  361. $attributes['title'] = $titles[$text];
  362. }
  363. else if (is_numeric($text)) {
  364. $attributes['title'] = t('Go to page %number', array('%number' => $text));
  365. }
  366. }
  367. return l($text, $_GET['q'], $attributes, count($query) ? implode('&', $query) : NULL);
  368. }
  369. /**
  370. * @} End of "Pager pieces".
  371. */
  372. /**
  373. * Helper function
  374. *
  375. * Copies $old_array to $new_array and sets $new_array[$element] = $value
  376. * Fills in $new_array[0 .. $element - 1] = 0
  377. */
  378. function pager_load_array($value, $element, $old_array) {
  379. $new_array = $old_array;
  380. // Look for empty elements.
  381. for ($i = 0; $i < $element; $i++) {
  382. if (!$new_array[$i]) {
  383. // Load found empty element with 0.
  384. $new_array[$i] = 0;
  385. }
  386. }
  387. // Update the changed element.
  388. $new_array[$element] = (int)$value;
  389. return $new_array;
  390. }
Login or register to post comments