pager.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. * Gets the element ID for this pager query.
  150. *
  151. * The element is used to differentiate different pager queries on the same
  152. * page so that they may be operated independently.
  153. *
  154. * @return
  155. * Element ID that is used to differentiate between different pager
  156. * queries.
  157. */
  158. public function getElement() {
  159. $this->ensureElement();
  160. return $this->element;
  161. }
  162. }
  163. /**
  164. * Returns the current page being requested for display within a pager.
  165. *
  166. * @param $element
  167. * An optional integer to distinguish between multiple pagers on one page.
  168. *
  169. * @return
  170. * The number of the current requested page, within the pager represented by
  171. * $element. This is determined from the URL query parameter $_GET['page'], or
  172. * 0 by default. Note that this number may differ from the actual page being
  173. * displayed. For example, if a search for "example text" brings up three
  174. * pages of results, but a users visits search/node/example+text?page=10, this
  175. * function will return 10, even though the default pager implementation
  176. * adjusts for this and still displays the third page of search results at
  177. * that URL.
  178. *
  179. * @see pager_default_initialize()
  180. */
  181. function pager_find_page($element = 0) {
  182. $page = isset($_GET['page']) ? $_GET['page'] : '';
  183. $page_array = explode(',', $page);
  184. if (!isset($page_array[$element])) {
  185. $page_array[$element] = 0;
  186. }
  187. return (int) $page_array[$element];
  188. }
  189. /**
  190. * Initializes a pager for theme('pager').
  191. *
  192. * This function sets up the necessary global variables so that future calls
  193. * to theme('pager') will render a pager that correctly corresponds to the
  194. * items being displayed.
  195. *
  196. * If the items being displayed result from a database query performed using
  197. * Drupal's database API, and if you have control over the construction of the
  198. * database query, you do not need to call this function directly; instead, you
  199. * can simply extend the query object with the 'PagerDefault' extender before
  200. * executing it. For example:
  201. * @code
  202. * $query = db_select('some_table')->extend('PagerDefault');
  203. * @endcode
  204. *
  205. * However, if you are using a different method for generating the items to be
  206. * paged through, then you should call this function in preparation.
  207. *
  208. * The following example shows how this function can be used in a page callback
  209. * that invokes an external datastore with an SQL-like syntax:
  210. * @code
  211. * // First find the total number of items and initialize the pager.
  212. * $where = "status = 1";
  213. * $total = mymodule_select("SELECT COUNT(*) FROM data " . $where)->result();
  214. * $num_per_page = variable_get('mymodule_num_per_page', 10);
  215. * $page = pager_default_initialize($total, $num_per_page);
  216. *
  217. * // Next, retrieve and display the items for the current page.
  218. * $offset = $num_per_page * $page;
  219. * $result = mymodule_select("SELECT * FROM data " . $where . " LIMIT %d, %d", $offset, $num_per_page)->fetchAll();
  220. * $output = theme('mymodule_results', array('result' => $result));
  221. *
  222. * // Finally, display the pager controls, and return.
  223. * $output .= theme('pager');
  224. * return $output;
  225. * @endcode
  226. *
  227. * A second example involves a page callback that invokes an external search
  228. * service where the total number of matching results is provided as part of
  229. * the returned set (so that we do not need a separate query in order to obtain
  230. * this information). Here, we call pager_find_page() to calculate the desired
  231. * offset before the search is invoked:
  232. * @code
  233. * // Perform the query, using the requested offset from pager_find_page().
  234. * // This comes from a URL parameter, so here we are assuming that the URL
  235. * // parameter corresponds to an actual page of results that will exist
  236. * // within the set.
  237. * $page = pager_find_page();
  238. * $num_per_page = variable_get('mymodule_num_per_page', 10);
  239. * $offset = $num_per_page * $page;
  240. * $result = mymodule_remote_search($keywords, $offset, $num_per_page);
  241. *
  242. * // Now that we have the total number of results, initialize the pager.
  243. * pager_default_initialize($result->total, $num_per_page);
  244. *
  245. * // Display the search results.
  246. * $output = theme('search_results', array('results' => $result->data, 'type' => 'remote'));
  247. *
  248. * // Finally, display the pager controls, and return.
  249. * $output .= theme('pager');
  250. * return $output;
  251. * @endcode
  252. *
  253. * @param $total
  254. * The total number of items to be paged.
  255. * @param $limit
  256. * The number of items the calling code will display per page.
  257. * @param $element
  258. * An optional integer to distinguish between multiple pagers on one page.
  259. *
  260. * @return
  261. * The number of the current page, within the pager represented by $element.
  262. * This is determined from the URL query parameter $_GET['page'], or 0 by
  263. * default. However, if a page that does not correspond to the actual range
  264. * of the result set was requested, this function will return the closest
  265. * page actually within the result set.
  266. */
  267. function pager_default_initialize($total, $limit, $element = 0) {
  268. global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
  269. $page = pager_find_page($element);
  270. // We calculate the total of pages as ceil(items / limit).
  271. $pager_total_items[$element] = $total;
  272. $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  273. $pager_page_array[$element] = max(0, min($page, ((int) $pager_total[$element]) - 1));
  274. $pager_limits[$element] = $limit;
  275. return $pager_page_array[$element];
  276. }
  277. /**
  278. * Compose a URL query parameter array for pager links.
  279. *
  280. * @return
  281. * A URL query parameter array that consists of all components of the current
  282. * page request except for those pertaining to paging.
  283. */
  284. function pager_get_query_parameters() {
  285. $query = &drupal_static(__FUNCTION__);
  286. if (!isset($query)) {
  287. $query = drupal_get_query_parameters($_GET, array('q', 'page'));
  288. }
  289. return $query;
  290. }
  291. /**
  292. * Returns HTML for a query pager.
  293. *
  294. * Menu callbacks that display paged query results should call theme('pager') to
  295. * retrieve a pager control so that users can view other results. Format a list
  296. * of nearby pages with additional query results.
  297. *
  298. * @param $variables
  299. * An associative array containing:
  300. * - tags: An array of labels for the controls in the pager.
  301. * - element: An optional integer to distinguish between multiple pagers on
  302. * one page.
  303. * - parameters: An associative array of query string parameters to append to
  304. * the pager links.
  305. * - quantity: The number of pages in the list.
  306. *
  307. * @ingroup themeable
  308. */
  309. function theme_pager($variables) {
  310. $tags = $variables['tags'];
  311. $element = $variables['element'];
  312. $parameters = $variables['parameters'];
  313. $quantity = empty($variables['quantity']) ? 0 : $variables['quantity'];
  314. global $pager_page_array, $pager_total;
  315. // Nothing to do if there is no pager.
  316. if (!isset($pager_page_array[$element]) || !isset($pager_total[$element])) {
  317. return;
  318. }
  319. // Nothing to do if there is only one page.
  320. if ($pager_total[$element] <= 1) {
  321. return;
  322. }
  323. // Calculate various markers within this pager piece:
  324. // Middle is used to "center" pages around the current page.
  325. $pager_middle = ceil($quantity / 2);
  326. // current is the page we are currently paged to
  327. $pager_current = $pager_page_array[$element] + 1;
  328. // first is the first page listed by this pager piece (re quantity)
  329. $pager_first = $pager_current - $pager_middle + 1;
  330. // last is the last page listed by this pager piece (re quantity)
  331. $pager_last = $pager_current + $quantity - $pager_middle;
  332. // max is the maximum page number
  333. $pager_max = $pager_total[$element];
  334. // End of marker calculations.
  335. // Prepare for generation loop.
  336. $i = $pager_first;
  337. if ($pager_last > $pager_max) {
  338. // Adjust "center" if at end of query.
  339. $i = $i + ($pager_max - $pager_last);
  340. $pager_last = $pager_max;
  341. }
  342. if ($i <= 0) {
  343. // Adjust "center" if at start of query.
  344. $pager_last = $pager_last + (1 - $i);
  345. $i = 1;
  346. }
  347. // End of generation loop preparation.
  348. $li_first = theme('pager_first', array('text' => (isset($tags[0]) ? $tags[0] : t('« first')), 'element' => $element, 'parameters' => $parameters));
  349. $li_previous = theme('pager_previous', array('text' => (isset($tags[1]) ? $tags[1] : t('‹ previous')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  350. $li_next = theme('pager_next', array('text' => (isset($tags[3]) ? $tags[3] : t('next ›')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  351. $li_last = theme('pager_last', array('text' => (isset($tags[4]) ? $tags[4] : t('last »')), 'element' => $element, 'parameters' => $parameters));
  352. if ($pager_total[$element] > 1) {
  353. if ($li_first) {
  354. $items[] = array(
  355. 'class' => array('pager-first'),
  356. 'data' => $li_first,
  357. );
  358. }
  359. if ($li_previous) {
  360. $items[] = array(
  361. 'class' => array('pager-previous'),
  362. 'data' => $li_previous,
  363. );
  364. }
  365. // When there is more than one page, create the pager list.
  366. if ($i != $pager_max) {
  367. if ($i > 1) {
  368. $items[] = array(
  369. 'class' => array('pager-ellipsis'),
  370. 'data' => '…',
  371. );
  372. }
  373. // Now generate the actual pager piece.
  374. for (; $i <= $pager_last && $i <= $pager_max; $i++) {
  375. if ($i < $pager_current) {
  376. $items[] = array(
  377. 'class' => array('pager-item'),
  378. 'data' => theme('pager_previous', array('text' => $i, 'element' => $element, 'interval' => ($pager_current - $i), 'parameters' => $parameters)),
  379. );
  380. }
  381. if ($i == $pager_current) {
  382. $items[] = array(
  383. 'class' => array('pager-current'),
  384. 'data' => $i,
  385. );
  386. }
  387. if ($i > $pager_current) {
  388. $items[] = array(
  389. 'class' => array('pager-item'),
  390. 'data' => theme('pager_next', array('text' => $i, 'element' => $element, 'interval' => ($i - $pager_current), 'parameters' => $parameters)),
  391. );
  392. }
  393. }
  394. if ($i < $pager_max) {
  395. $items[] = array(
  396. 'class' => array('pager-ellipsis'),
  397. 'data' => '…',
  398. );
  399. }
  400. }
  401. // End generation.
  402. if ($li_next) {
  403. $items[] = array(
  404. 'class' => array('pager-next'),
  405. 'data' => $li_next,
  406. );
  407. }
  408. if ($li_last) {
  409. $items[] = array(
  410. 'class' => array('pager-last'),
  411. 'data' => $li_last,
  412. );
  413. }
  414. return '<h2 class="element-invisible">' . t('Pages') . '</h2>' . theme('item_list', array(
  415. 'items' => $items,
  416. 'attributes' => array('class' => array('pager')),
  417. ));
  418. }
  419. }
  420. /**
  421. * @defgroup pagerpieces Pager pieces
  422. * @{
  423. * Theme functions for customizing pager elements.
  424. *
  425. * Note that you should NOT modify this file to customize your pager.
  426. */
  427. /**
  428. * Returns HTML for the "first page" link in a query pager.
  429. *
  430. * @param $variables
  431. * An associative array containing:
  432. * - text: The name (or image) of the link.
  433. * - element: An optional integer to distinguish between multiple pagers on
  434. * one page.
  435. * - parameters: An associative array of query string parameters to append to
  436. * the pager links.
  437. *
  438. * @ingroup themeable
  439. */
  440. function theme_pager_first($variables) {
  441. $text = $variables['text'];
  442. $element = $variables['element'];
  443. $parameters = $variables['parameters'];
  444. global $pager_page_array;
  445. $output = '';
  446. // Nothing to do if there is no pager.
  447. if (!isset($pager_page_array[$element])) {
  448. return;
  449. }
  450. // If we are anywhere but the first page
  451. if ($pager_page_array[$element] > 0) {
  452. $output = theme('pager_link', array('text' => $text, 'page_new' => pager_load_array(0, $element, $pager_page_array), 'element' => $element, 'parameters' => $parameters));
  453. }
  454. return $output;
  455. }
  456. /**
  457. * Returns HTML for the "previous page" link in a query pager.
  458. *
  459. * @param $variables
  460. * An associative array containing:
  461. * - text: The name (or image) of the link.
  462. * - element: An optional integer to distinguish between multiple pagers on
  463. * one page.
  464. * - interval: The number of pages to move backward when the link is clicked.
  465. * - parameters: An associative array of query string parameters to append to
  466. * the pager links.
  467. *
  468. * @ingroup themeable
  469. */
  470. function theme_pager_previous($variables) {
  471. $text = $variables['text'];
  472. $element = $variables['element'];
  473. $interval = $variables['interval'];
  474. $parameters = $variables['parameters'];
  475. global $pager_page_array;
  476. $output = '';
  477. // Nothing to do if there is no pager.
  478. if (!isset($pager_page_array[$element])) {
  479. return;
  480. }
  481. // If we are anywhere but the first page
  482. if ($pager_page_array[$element] > 0) {
  483. $page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array);
  484. // If the previous page is the first page, mark the link as such.
  485. if ($page_new[$element] == 0) {
  486. $output = theme('pager_first', array('text' => $text, 'element' => $element, 'parameters' => $parameters));
  487. }
  488. // The previous page is not the first page.
  489. else {
  490. $output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters));
  491. }
  492. }
  493. return $output;
  494. }
  495. /**
  496. * Returns HTML for the "next page" link in a query pager.
  497. *
  498. * @param $variables
  499. * An associative array containing:
  500. * - text: The name (or image) of the link.
  501. * - element: An optional integer to distinguish between multiple pagers on
  502. * one page.
  503. * - interval: The number of pages to move forward when the link is clicked.
  504. * - parameters: An associative array of query string parameters to append to
  505. * the pager links.
  506. *
  507. * @ingroup themeable
  508. */
  509. function theme_pager_next($variables) {
  510. $text = $variables['text'];
  511. $element = $variables['element'];
  512. $interval = $variables['interval'];
  513. $parameters = $variables['parameters'];
  514. global $pager_page_array, $pager_total;
  515. $output = '';
  516. // Nothing to do if there is no pager.
  517. if (!isset($pager_page_array[$element]) || !isset($pager_total[$element])) {
  518. return;
  519. }
  520. // If we are anywhere but the last page
  521. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  522. $page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
  523. // If the next page is the last page, mark the link as such.
  524. if ($page_new[$element] == ($pager_total[$element] - 1)) {
  525. $output = theme('pager_last', array('text' => $text, 'element' => $element, 'parameters' => $parameters));
  526. }
  527. // The next page is not the last page.
  528. else {
  529. $output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters));
  530. }
  531. }
  532. return $output;
  533. }
  534. /**
  535. * Returns HTML for the "last page" link in query pager.
  536. *
  537. * @param $variables
  538. * An associative array containing:
  539. * - text: The name (or image) of the link.
  540. * - element: An optional integer to distinguish between multiple pagers on
  541. * one page.
  542. * - parameters: An associative array of query string parameters to append to
  543. * the pager links.
  544. *
  545. * @ingroup themeable
  546. */
  547. function theme_pager_last($variables) {
  548. $text = $variables['text'];
  549. $element = $variables['element'];
  550. $parameters = $variables['parameters'];
  551. global $pager_page_array, $pager_total;
  552. $output = '';
  553. // Nothing to do if there is no pager.
  554. if (!isset($pager_page_array[$element]) || !isset($pager_total[$element])) {
  555. return;
  556. }
  557. // If we are anywhere but the last page
  558. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  559. $output = theme('pager_link', array('text' => $text, 'page_new' => pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), 'element' => $element, 'parameters' => $parameters));
  560. }
  561. return $output;
  562. }
  563. /**
  564. * Returns HTML for a link to a specific query result page.
  565. *
  566. * @param $variables
  567. * An associative array containing:
  568. * - text: The link text. Also used to figure out the title attribute of the
  569. * link, if it is not provided in $variables['attributes']['title']; in
  570. * this case, $variables['text'] must be one of the standard pager link
  571. * text strings that would be generated by the pager theme functions, such
  572. * as a number or t('« first').
  573. * - page_new: The first result to display on the linked page.
  574. * - element: An optional integer to distinguish between multiple pagers on
  575. * one page.
  576. * - parameters: An associative array of query string parameters to append to
  577. * the pager link.
  578. * - attributes: An associative array of HTML attributes to apply to the
  579. * pager link.
  580. *
  581. * @see theme_pager()
  582. *
  583. * @ingroup themeable
  584. */
  585. function theme_pager_link($variables) {
  586. $text = $variables['text'];
  587. $page_new = $variables['page_new'];
  588. $element = $variables['element'];
  589. $parameters = $variables['parameters'];
  590. $attributes = $variables['attributes'];
  591. $page = isset($_GET['page']) ? $_GET['page'] : '';
  592. if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
  593. $parameters['page'] = $new_page;
  594. }
  595. $query = array();
  596. if (count($parameters)) {
  597. $query = drupal_get_query_parameters($parameters, array());
  598. }
  599. if ($query_pager = pager_get_query_parameters()) {
  600. $query = array_merge($query, $query_pager);
  601. }
  602. // Set each pager link title
  603. if (!isset($attributes['title'])) {
  604. static $titles = NULL;
  605. if (!isset($titles)) {
  606. $titles = array(
  607. t('« first') => t('Go to first page'),
  608. t('‹ previous') => t('Go to previous page'),
  609. t('next ›') => t('Go to next page'),
  610. t('last »') => t('Go to last page'),
  611. );
  612. }
  613. if (isset($titles[$text])) {
  614. $attributes['title'] = $titles[$text];
  615. }
  616. elseif (is_numeric($text)) {
  617. $attributes['title'] = t('Go to page @number', array('@number' => $text));
  618. }
  619. }
  620. // @todo l() cannot be used here, since it adds an 'active' class based on the
  621. // path only (which is always the current path for pager links). Apparently,
  622. // none of the pager links is active at any time - but it should still be
  623. // possible to use l() here.
  624. // @see http://drupal.org/node/1410574
  625. $attributes['href'] = url($_GET['q'], array('query' => $query));
  626. return '<a' . drupal_attributes($attributes) . '>' . check_plain($text) . '</a>';
  627. }
  628. /**
  629. * @} End of "Pager pieces".
  630. */
  631. /**
  632. * Helper function
  633. *
  634. * Copies $old_array to $new_array and sets $new_array[$element] = $value
  635. * Fills in $new_array[0 .. $element - 1] = 0
  636. */
  637. function pager_load_array($value, $element, $old_array) {
  638. $new_array = $old_array;
  639. // Look for empty elements.
  640. for ($i = 0; $i < $element; $i++) {
  641. if (empty($new_array[$i])) {
  642. // Load found empty element with 0.
  643. $new_array[$i] = 0;
  644. }
  645. }
  646. // Update the changed element.
  647. $new_array[$element] = (int) $value;
  648. return $new_array;
  649. }