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-6.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. * 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. $pager_total_items[$element] = db_result(db_query($count_query, $args));
  66. $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  67. $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  68. return db_query_range($query, $args, $pager_page_array[$element] * $limit, $limit);
  69. }
  70. /**
  71. * Compose a query string to append to pager requests.
  72. *
  73. * @return
  74. * A query string that consists of all components of the current page request
  75. * except for those pertaining to paging.
  76. */
  77. function pager_get_querystring() {
  78. static $string = NULL;
  79. if (!isset($string)) {
  80. $string = drupal_query_string_encode($_REQUEST, array_merge(array('q', 'page', 'pass'), array_keys($_COOKIE)));
  81. }
  82. return $string;
  83. }
  84. /**
  85. * Returns HTML for a query pager.
  86. *
  87. * Menu callbacks that display paged query results should call theme('pager') to
  88. * retrieve a pager control so that users can view other results.
  89. * Format a list of nearby pages with additional query results.
  90. *
  91. * @param $tags
  92. * An array of labels for the controls in the pager.
  93. * @param $limit
  94. * The number of query results to display per page.
  95. * @param $element
  96. * An optional integer to distinguish between multiple pagers on one page.
  97. * @param $parameters
  98. * An associative array of query string parameters to append to the pager links.
  99. * @param $quantity
  100. * The number of pages in the list.
  101. * @return
  102. * An HTML string that generates the query pager.
  103. *
  104. * @ingroup themeable
  105. */
  106. function theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9) {
  107. global $pager_page_array, $pager_total;
  108. // Calculate various markers within this pager piece:
  109. // Middle is used to "center" pages around the current page.
  110. $pager_middle = ceil($quantity / 2);
  111. // current is the page we are currently paged to
  112. $pager_current = $pager_page_array[$element] + 1;
  113. // first is the first page listed by this pager piece (re quantity)
  114. $pager_first = $pager_current - $pager_middle + 1;
  115. // last is the last page listed by this pager piece (re quantity)
  116. $pager_last = $pager_current + $quantity - $pager_middle;
  117. // max is the maximum page number
  118. $pager_max = $pager_total[$element];
  119. // End of marker calculations.
  120. // Prepare for generation loop.
  121. $i = $pager_first;
  122. if ($pager_last > $pager_max) {
  123. // Adjust "center" if at end of query.
  124. $i = $i + ($pager_max - $pager_last);
  125. $pager_last = $pager_max;
  126. }
  127. if ($i <= 0) {
  128. // Adjust "center" if at start of query.
  129. $pager_last = $pager_last + (1 - $i);
  130. $i = 1;
  131. }
  132. // End of generation loop preparation.
  133. $li_first = theme('pager_first', (isset($tags[0]) ? $tags[0] : t('« first')), $limit, $element, $parameters);
  134. $li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('‹ previous')), $limit, $element, 1, $parameters);
  135. $li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('next ›')), $limit, $element, 1, $parameters);
  136. $li_last = theme('pager_last', (isset($tags[4]) ? $tags[4] : t('last »')), $limit, $element, $parameters);
  137. if ($pager_total[$element] > 1) {
  138. if ($li_first) {
  139. $items[] = array(
  140. 'class' => 'pager-first',
  141. 'data' => $li_first,
  142. );
  143. }
  144. if ($li_previous) {
  145. $items[] = array(
  146. 'class' => 'pager-previous',
  147. 'data' => $li_previous,
  148. );
  149. }
  150. // When there is more than one page, create the pager list.
  151. if ($i != $pager_max) {
  152. if ($i > 1) {
  153. $items[] = array(
  154. 'class' => 'pager-ellipsis',
  155. 'data' => '…',
  156. );
  157. }
  158. // Now generate the actual pager piece.
  159. for (; $i <= $pager_last && $i <= $pager_max; $i++) {
  160. if ($i < $pager_current) {
  161. $items[] = array(
  162. 'class' => 'pager-item',
  163. 'data' => theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters),
  164. );
  165. }
  166. if ($i == $pager_current) {
  167. $items[] = array(
  168. 'class' => 'pager-current',
  169. 'data' => $i,
  170. );
  171. }
  172. if ($i > $pager_current) {
  173. $items[] = array(
  174. 'class' => 'pager-item',
  175. 'data' => theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters),
  176. );
  177. }
  178. }
  179. if ($i < $pager_max) {
  180. $items[] = array(
  181. 'class' => 'pager-ellipsis',
  182. 'data' => '…',
  183. );
  184. }
  185. }
  186. // End generation.
  187. if ($li_next) {
  188. $items[] = array(
  189. 'class' => 'pager-next',
  190. 'data' => $li_next,
  191. );
  192. }
  193. if ($li_last) {
  194. $items[] = array(
  195. 'class' => 'pager-last',
  196. 'data' => $li_last,
  197. );
  198. }
  199. return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
  200. }
  201. }
  202. /**
  203. * @defgroup pagerpieces Pager pieces
  204. * @{
  205. * Theme functions for customizing pager elements.
  206. *
  207. * Note that you should NOT modify this file to customize your pager.
  208. */
  209. /**
  210. * Returns HTML for a "first page" link.
  211. *
  212. * @param $text
  213. * The name (or image) of the link.
  214. * @param $limit
  215. * The number of query results to display per page.
  216. * @param $element
  217. * An optional integer to distinguish between multiple pagers on one page.
  218. * @param $parameters
  219. * An associative array of query string parameters to append to the pager links.
  220. * @return
  221. * An HTML string that generates this piece of the query pager.
  222. *
  223. * @ingroup themeable
  224. */
  225. function theme_pager_first($text, $limit, $element = 0, $parameters = array()) {
  226. global $pager_page_array;
  227. $output = '';
  228. // If we are anywhere but the first page
  229. if ($pager_page_array[$element] > 0) {
  230. $output = theme('pager_link', $text, pager_load_array(0, $element, $pager_page_array), $element, $parameters);
  231. }
  232. return $output;
  233. }
  234. /**
  235. * Returns HTML for a "previous page" link.
  236. *
  237. * @param $text
  238. * The name (or image) of the link.
  239. * @param $limit
  240. * The number of query results to display per page.
  241. * @param $element
  242. * An optional integer to distinguish between multiple pagers on one page.
  243. * @param $interval
  244. * The number of pages to move backward when the link is clicked.
  245. * @param $parameters
  246. * An associative array of query string parameters to append to the pager links.
  247. * @return
  248. * An HTML string that generates this piece of the query pager.
  249. *
  250. * @ingroup themeable
  251. */
  252. function theme_pager_previous($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
  253. global $pager_page_array;
  254. $output = '';
  255. // If we are anywhere but the first page
  256. if ($pager_page_array[$element] > 0) {
  257. $page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array);
  258. // If the previous page is the first page, mark the link as such.
  259. if ($page_new[$element] == 0) {
  260. $output = theme('pager_first', $text, $limit, $element, $parameters);
  261. }
  262. // The previous page is not the first page.
  263. else {
  264. $output = theme('pager_link', $text, $page_new, $element, $parameters);
  265. }
  266. }
  267. return $output;
  268. }
  269. /**
  270. * Returns HTML for a "next page" link.
  271. *
  272. * @param $text
  273. * The name (or image) of the link.
  274. * @param $limit
  275. * The number of query results to display per page.
  276. * @param $element
  277. * An optional integer to distinguish between multiple pagers on one page.
  278. * @param $interval
  279. * The number of pages to move forward when the link is clicked.
  280. * @param $parameters
  281. * An associative array of query string parameters to append to the pager links.
  282. * @return
  283. * An HTML string that generates this piece of the query pager.
  284. *
  285. * @ingroup themeable
  286. */
  287. function theme_pager_next($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
  288. global $pager_page_array, $pager_total;
  289. $output = '';
  290. // If we are anywhere but the last page
  291. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  292. $page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
  293. // If the next page is the last page, mark the link as such.
  294. if ($page_new[$element] == ($pager_total[$element] - 1)) {
  295. $output = theme('pager_last', $text, $limit, $element, $parameters);
  296. }
  297. // The next page is not the last page.
  298. else {
  299. $output = theme('pager_link', $text, $page_new, $element, $parameters);
  300. }
  301. }
  302. return $output;
  303. }
  304. /**
  305. * Returns HTML for a "last page" link.
  306. *
  307. * @param $text
  308. * The name (or image) of the link.
  309. * @param $limit
  310. * The number of query results to display per page.
  311. * @param $element
  312. * An optional integer to distinguish between multiple pagers on one page.
  313. * @param $parameters
  314. * An associative array of query string parameters to append to the pager links.
  315. * @return
  316. * An HTML string that generates this piece of the query pager.
  317. *
  318. * @ingroup themeable
  319. */
  320. function theme_pager_last($text, $limit, $element = 0, $parameters = array()) {
  321. global $pager_page_array, $pager_total;
  322. $output = '';
  323. // If we are anywhere but the last page
  324. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  325. $output = theme('pager_link', $text, pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), $element, $parameters);
  326. }
  327. return $output;
  328. }
  329. /**
  330. * Returns HTML for a link to a specific query result page.
  331. *
  332. * @param $text
  333. * The link text. Also used to figure out the title attribute of the link,
  334. * if it is not provided in $attributes['title']; in this case, $text must
  335. * be one of the standard pager link text strings that would be generated by
  336. * the pager theme functions, such as a number or t('« first').
  337. * @param $page_new
  338. * The first result to display on the linked page.
  339. * @param $element
  340. * An optional integer to distinguish between multiple pagers on one page.
  341. * @param $parameters
  342. * An associative array of query string parameters to append to the pager link.
  343. * @param $attributes
  344. * An associative array of HTML attributes to apply to the pager link.
  345. * @return
  346. * An HTML string that generates the link.
  347. *
  348. * @ingroup themeable
  349. */
  350. function theme_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array()) {
  351. $page = isset($_GET['page']) ? $_GET['page'] : '';
  352. if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
  353. $parameters['page'] = $new_page;
  354. }
  355. $query = array();
  356. if (count($parameters)) {
  357. $query[] = drupal_query_string_encode($parameters, array());
  358. }
  359. $querystring = pager_get_querystring();
  360. if ($querystring != '') {
  361. $query[] = $querystring;
  362. }
  363. // Set each pager link title
  364. if (!isset($attributes['title'])) {
  365. static $titles = NULL;
  366. if (!isset($titles)) {
  367. $titles = array(
  368. t('« first') => t('Go to first page'),
  369. t('‹ previous') => t('Go to previous page'),
  370. t('next ›') => t('Go to next page'),
  371. t('last »') => t('Go to last page'),
  372. );
  373. }
  374. if (isset($titles[$text])) {
  375. $attributes['title'] = $titles[$text];
  376. }
  377. else if (is_numeric($text)) {
  378. $attributes['title'] = t('Go to page @number', array('@number' => $text));
  379. }
  380. }
  381. return l($text, $_GET['q'], array('attributes' => $attributes, 'query' => count($query) ? implode('&', $query) : NULL));
  382. }
  383. /**
  384. * @} End of "Pager pieces".
  385. */
  386. /**
  387. * Helper function
  388. *
  389. * Copies $old_array to $new_array and sets $new_array[$element] = $value
  390. * Fills in $new_array[0 .. $element - 1] = 0
  391. */
  392. function pager_load_array($value, $element, $old_array) {
  393. $new_array = $old_array;
  394. // Look for empty elements.
  395. for ($i = 0; $i < $element; $i++) {
  396. if (!$new_array[$i]) {
  397. // Load found empty element with 0.
  398. $new_array[$i] = 0;
  399. }
  400. }
  401. // Update the changed element.
  402. $new_array[$element] = (int)$value;
  403. return $new_array;
  404. }