pager.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 = empty($variables['quantity']) ? 0 : $variables['quantity'];
  300. global $pager_page_array, $pager_total;
  301. // Nothing to do if there is no pager.
  302. if (!isset($pager_page_array[$element]) || !isset($pager_total[$element])) {
  303. return;
  304. }
  305. // Nothing to do if there is only one page.
  306. if ($pager_total[$element] <= 1) {
  307. return;
  308. }
  309. // Calculate various markers within this pager piece:
  310. // Middle is used to "center" pages around the current page.
  311. $pager_middle = ceil($quantity / 2);
  312. // current is the page we are currently paged to
  313. $pager_current = $pager_page_array[$element] + 1;
  314. // first is the first page listed by this pager piece (re quantity)
  315. $pager_first = $pager_current - $pager_middle + 1;
  316. // last is the last page listed by this pager piece (re quantity)
  317. $pager_last = $pager_current + $quantity - $pager_middle;
  318. // max is the maximum page number
  319. $pager_max = $pager_total[$element];
  320. // End of marker calculations.
  321. // Prepare for generation loop.
  322. $i = $pager_first;
  323. if ($pager_last > $pager_max) {
  324. // Adjust "center" if at end of query.
  325. $i = $i + ($pager_max - $pager_last);
  326. $pager_last = $pager_max;
  327. }
  328. if ($i <= 0) {
  329. // Adjust "center" if at start of query.
  330. $pager_last = $pager_last + (1 - $i);
  331. $i = 1;
  332. }
  333. // End of generation loop preparation.
  334. $li_first = theme('pager_first', array('text' => (isset($tags[0]) ? $tags[0] : t('« first')), 'element' => $element, 'parameters' => $parameters));
  335. $li_previous = theme('pager_previous', array('text' => (isset($tags[1]) ? $tags[1] : t('‹ previous')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  336. $li_next = theme('pager_next', array('text' => (isset($tags[3]) ? $tags[3] : t('next ›')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  337. $li_last = theme('pager_last', array('text' => (isset($tags[4]) ? $tags[4] : t('last »')), 'element' => $element, 'parameters' => $parameters));
  338. if ($pager_total[$element] > 1) {
  339. if ($li_first) {
  340. $items[] = array(
  341. 'class' => array('pager-first'),
  342. 'data' => $li_first,
  343. );
  344. }
  345. if ($li_previous) {
  346. $items[] = array(
  347. 'class' => array('pager-previous'),
  348. 'data' => $li_previous,
  349. );
  350. }
  351. // When there is more than one page, create the pager list.
  352. if ($i != $pager_max) {
  353. if ($i > 1) {
  354. $items[] = array(
  355. 'class' => array('pager-ellipsis'),
  356. 'data' => '…',
  357. );
  358. }
  359. // Now generate the actual pager piece.
  360. for (; $i <= $pager_last && $i <= $pager_max; $i++) {
  361. if ($i < $pager_current) {
  362. $items[] = array(
  363. 'class' => array('pager-item'),
  364. 'data' => theme('pager_previous', array('text' => $i, 'element' => $element, 'interval' => ($pager_current - $i), 'parameters' => $parameters)),
  365. );
  366. }
  367. if ($i == $pager_current) {
  368. $items[] = array(
  369. 'class' => array('pager-current'),
  370. 'data' => $i,
  371. );
  372. }
  373. if ($i > $pager_current) {
  374. $items[] = array(
  375. 'class' => array('pager-item'),
  376. 'data' => theme('pager_next', array('text' => $i, 'element' => $element, 'interval' => ($i - $pager_current), 'parameters' => $parameters)),
  377. );
  378. }
  379. }
  380. if ($i < $pager_max) {
  381. $items[] = array(
  382. 'class' => array('pager-ellipsis'),
  383. 'data' => '…',
  384. );
  385. }
  386. }
  387. // End generation.
  388. if ($li_next) {
  389. $items[] = array(
  390. 'class' => array('pager-next'),
  391. 'data' => $li_next,
  392. );
  393. }
  394. if ($li_last) {
  395. $items[] = array(
  396. 'class' => array('pager-last'),
  397. 'data' => $li_last,
  398. );
  399. }
  400. return '<h2 class="element-invisible">' . t('Pages') . '</h2>' . theme('item_list', array(
  401. 'items' => $items,
  402. 'attributes' => array('class' => array('pager')),
  403. ));
  404. }
  405. }
  406. /**
  407. * @defgroup pagerpieces Pager pieces
  408. * @{
  409. * Theme functions for customizing pager elements.
  410. *
  411. * Note that you should NOT modify this file to customize your pager.
  412. */
  413. /**
  414. * Returns HTML for the "first page" link in a query pager.
  415. *
  416. * @param $variables
  417. * An associative array containing:
  418. * - text: The name (or image) of the link.
  419. * - element: An optional integer to distinguish between multiple pagers on
  420. * one page.
  421. * - parameters: An associative array of query string parameters to append to
  422. * the pager links.
  423. *
  424. * @ingroup themeable
  425. */
  426. function theme_pager_first($variables) {
  427. $text = $variables['text'];
  428. $element = $variables['element'];
  429. $parameters = $variables['parameters'];
  430. global $pager_page_array;
  431. $output = '';
  432. // Nothing to do if there is no pager.
  433. if (!isset($pager_page_array[$element])) {
  434. return;
  435. }
  436. // If we are anywhere but the first page
  437. if ($pager_page_array[$element] > 0) {
  438. $output = theme('pager_link', array('text' => $text, 'page_new' => pager_load_array(0, $element, $pager_page_array), 'element' => $element, 'parameters' => $parameters));
  439. }
  440. return $output;
  441. }
  442. /**
  443. * Returns HTML for the "previous page" link in a query pager.
  444. *
  445. * @param $variables
  446. * An associative array containing:
  447. * - text: The name (or image) of the link.
  448. * - element: An optional integer to distinguish between multiple pagers on
  449. * one page.
  450. * - interval: The number of pages to move backward when the link is clicked.
  451. * - parameters: An associative array of query string parameters to append to
  452. * the pager links.
  453. *
  454. * @ingroup themeable
  455. */
  456. function theme_pager_previous($variables) {
  457. $text = $variables['text'];
  458. $element = $variables['element'];
  459. $interval = $variables['interval'];
  460. $parameters = $variables['parameters'];
  461. global $pager_page_array;
  462. $output = '';
  463. // Nothing to do if there is no pager.
  464. if (!isset($pager_page_array[$element])) {
  465. return;
  466. }
  467. // If we are anywhere but the first page
  468. if ($pager_page_array[$element] > 0) {
  469. $page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array);
  470. // If the previous page is the first page, mark the link as such.
  471. if ($page_new[$element] == 0) {
  472. $output = theme('pager_first', array('text' => $text, 'element' => $element, 'parameters' => $parameters));
  473. }
  474. // The previous page is not the first page.
  475. else {
  476. $output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters));
  477. }
  478. }
  479. return $output;
  480. }
  481. /**
  482. * Returns HTML for the "next page" link in a query pager.
  483. *
  484. * @param $variables
  485. * An associative array containing:
  486. * - text: The name (or image) of the link.
  487. * - element: An optional integer to distinguish between multiple pagers on
  488. * one page.
  489. * - interval: The number of pages to move forward when the link is clicked.
  490. * - parameters: An associative array of query string parameters to append to
  491. * the pager links.
  492. *
  493. * @ingroup themeable
  494. */
  495. function theme_pager_next($variables) {
  496. $text = $variables['text'];
  497. $element = $variables['element'];
  498. $interval = $variables['interval'];
  499. $parameters = $variables['parameters'];
  500. global $pager_page_array, $pager_total;
  501. $output = '';
  502. // Nothing to do if there is no pager.
  503. if (!isset($pager_page_array[$element]) || !isset($pager_total[$element])) {
  504. return;
  505. }
  506. // If we are anywhere but the last page
  507. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  508. $page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
  509. // If the next page is the last page, mark the link as such.
  510. if ($page_new[$element] == ($pager_total[$element] - 1)) {
  511. $output = theme('pager_last', array('text' => $text, 'element' => $element, 'parameters' => $parameters));
  512. }
  513. // The next page is not the last page.
  514. else {
  515. $output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters));
  516. }
  517. }
  518. return $output;
  519. }
  520. /**
  521. * Returns HTML for the "last page" link in query pager.
  522. *
  523. * @param $variables
  524. * An associative array containing:
  525. * - text: The name (or image) of the link.
  526. * - element: An optional integer to distinguish between multiple pagers on
  527. * one page.
  528. * - parameters: An associative array of query string parameters to append to
  529. * the pager links.
  530. *
  531. * @ingroup themeable
  532. */
  533. function theme_pager_last($variables) {
  534. $text = $variables['text'];
  535. $element = $variables['element'];
  536. $parameters = $variables['parameters'];
  537. global $pager_page_array, $pager_total;
  538. $output = '';
  539. // Nothing to do if there is no pager.
  540. if (!isset($pager_page_array[$element]) || !isset($pager_total[$element])) {
  541. return;
  542. }
  543. // If we are anywhere but the last page
  544. if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
  545. $output = theme('pager_link', array('text' => $text, 'page_new' => pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), 'element' => $element, 'parameters' => $parameters));
  546. }
  547. return $output;
  548. }
  549. /**
  550. * Returns HTML for a link to a specific query result page.
  551. *
  552. * @param $variables
  553. * An associative array containing:
  554. * - text: The link text. Also used to figure out the title attribute of the
  555. * link, if it is not provided in $variables['attributes']['title']; in
  556. * this case, $variables['text'] must be one of the standard pager link
  557. * text strings that would be generated by the pager theme functions, such
  558. * as a number or t('« first').
  559. * - page_new: The first result to display on the linked page.
  560. * - element: An optional integer to distinguish between multiple pagers on
  561. * one page.
  562. * - parameters: An associative array of query string parameters to append to
  563. * the pager link.
  564. * - attributes: An associative array of HTML attributes to apply to the
  565. * pager link.
  566. *
  567. * @see theme_pager()
  568. *
  569. * @ingroup themeable
  570. */
  571. function theme_pager_link($variables) {
  572. $text = $variables['text'];
  573. $page_new = $variables['page_new'];
  574. $element = $variables['element'];
  575. $parameters = $variables['parameters'];
  576. $attributes = $variables['attributes'];
  577. $page = isset($_GET['page']) ? $_GET['page'] : '';
  578. if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
  579. $parameters['page'] = $new_page;
  580. }
  581. $query = array();
  582. if (count($parameters)) {
  583. $query = drupal_get_query_parameters($parameters, array());
  584. }
  585. if ($query_pager = pager_get_query_parameters()) {
  586. $query = array_merge($query, $query_pager);
  587. }
  588. // Set each pager link title
  589. if (!isset($attributes['title'])) {
  590. static $titles = NULL;
  591. if (!isset($titles)) {
  592. $titles = array(
  593. t('« first') => t('Go to first page'),
  594. t('‹ previous') => t('Go to previous page'),
  595. t('next ›') => t('Go to next page'),
  596. t('last »') => t('Go to last page'),
  597. );
  598. }
  599. if (isset($titles[$text])) {
  600. $attributes['title'] = $titles[$text];
  601. }
  602. elseif (is_numeric($text)) {
  603. $attributes['title'] = t('Go to page @number', array('@number' => $text));
  604. }
  605. }
  606. // @todo l() cannot be used here, since it adds an 'active' class based on the
  607. // path only (which is always the current path for pager links). Apparently,
  608. // none of the pager links is active at any time - but it should still be
  609. // possible to use l() here.
  610. // @see http://drupal.org/node/1410574
  611. $attributes['href'] = url($_GET['q'], array('query' => $query));
  612. return '<a' . drupal_attributes($attributes) . '>' . check_plain($text) . '</a>';
  613. }
  614. /**
  615. * @} End of "Pager pieces".
  616. */
  617. /**
  618. * Helper function
  619. *
  620. * Copies $old_array to $new_array and sets $new_array[$element] = $value
  621. * Fills in $new_array[0 .. $element - 1] = 0
  622. */
  623. function pager_load_array($value, $element, $old_array) {
  624. $new_array = $old_array;
  625. // Look for empty elements.
  626. for ($i = 0; $i < $element; $i++) {
  627. if (empty($new_array[$i])) {
  628. // Load found empty element with 0.
  629. $new_array[$i] = 0;
  630. }
  631. }
  632. // Update the changed element.
  633. $new_array[$element] = (int) $value;
  634. return $new_array;
  635. }