Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 3.74 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  * Override default pager to get a pager with last and first page represented by numbers.
  4.  * E.g: « 1 2 3 4 5 6 7 8 9 ... 44 »
  5.  */
  6. function THEME_pager($variables) {
  7.   $tags = $variables['tags'];
  8.   $element = $variables['element'];
  9.   $parameters = $variables['parameters'];
  10.   $quantity = $variables['quantity'];
  11.  
  12.   global $pager_page_array, $pager_total;
  13.  
  14.   // Calculate various markers within this pager piece:
  15.   // Middle is used to "center" pages around the current page.
  16.   $pager_middle = ceil($quantity / 2);
  17.   // current is the page we are currently paged to
  18.   $pager_current = $pager_page_array[$element] + 1;
  19.   // first is the first page listed by this pager piece (re quantity)
  20.   $pager_first = $pager_current - $pager_middle + 1;
  21.   // last is the last page listed by this pager piece (re quantity)
  22.   $pager_last = $pager_current + $quantity - $pager_middle;
  23.   // max is the maximum page number
  24.   $pager_max = $pager_total[$element];
  25.   // End of marker calculations.
  26.  
  27.   // Prepare for generation loop.
  28.   $i = $pager_first;
  29.   if ($pager_last > $pager_max) {
  30.     // Adjust "center" if at end of query.
  31.     $i = $i + ($pager_max - $pager_last);
  32.     $pager_last = $pager_max;
  33.   }
  34.   if ($i <= 0) {
  35.     // Adjust "center" if at start of query.
  36.     $pager_last = $pager_last + (1 - $i);
  37.     $i = 1;
  38.   }
  39.  
  40.   $li_first = theme('pager_first', array('text' =>  (isset($tags[0]) ? $tags[0] : t('1')), 'element' => $element, 'parameters' => $parameters));
  41.   $li_previous = theme('pager_previous', array('text' => (isset($tags[1]) ? $tags[1] : t('‹ previous')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  42.   $li_next = theme('pager_next', array('text' => (isset($tags[3]) ? $tags[3] : t('next ›')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  43.   $li_last = theme('pager_last', array('text' => (isset($tags[4]) ? $tags[4] : t('@last', array('@last' => $pager_max))), 'element' => $element, 'parameters' => $parameters));
  44.   if ($pager_total[$element] > 1) {
  45.     if ($li_previous) {
  46.       $items[] = array(
  47.         'class' => array('pager-previous'),
  48.         'data' => $li_previous,
  49.       );
  50.     }
  51.     if ($i > 1) {
  52.       if ($li_first) {
  53.         $items[] = array(
  54.           'class' => array('pager-first'),
  55.           'data' => $li_first,
  56.         );
  57.       }
  58.       $items[] = array(
  59.         'class' => array('pager-ellipsis'),
  60.         'data' => '…',
  61.       );
  62.     }
  63.     // Now generate the actual pager piece.
  64.     for (; $i <= $pager_last && $i <= $pager_max; $i++) {
  65.       if ($i < $pager_current) {
  66.         $items[] = array(
  67.           'class' => array('pager-item'),
  68.           'data' => theme('pager_previous', array('text' => $i, 'element' => $element, 'interval' => ($pager_current - $i), 'parameters' => $parameters)),
  69.         );
  70.       }
  71.       if ($i == $pager_current) {
  72.         $items[] = array(
  73.           'class' => array('pager-current'),
  74.           'data' => $i,
  75.         );
  76.       }
  77.       if ($i > $pager_current) {
  78.         $items[] = array(
  79.           'class' => array('pager-item'),
  80.           'data' => theme('pager_next', array('text' => $i, 'element' => $element, 'interval' => ($i - $pager_current), 'parameters' => $parameters)),
  81.         );
  82.       }
  83.     }
  84.     if ($i < $pager_max) {
  85.       $items[] = array(
  86.         'class' => array('pager-ellipsis'),
  87.         'data' => '…',
  88.       );
  89.       if ($li_last) {
  90.         $items[] = array(
  91.           'class' => array('pager-last'),
  92.           'data' => $li_last,
  93.         );
  94.       }
  95.     }
  96.     if ($li_next) {
  97.       $items[] = array(
  98.         'class' => array('pager-next'),
  99.         'data' => $li_next,
  100.       );
  101.     }
  102.  
  103.     return '<h2 class="element-invisible">' . t('Pages') . '</h2>' . theme('item_list', array(
  104.       'items' => $items,
  105.       'attributes' => array('class' => array('pager clearfix')),
  106.     ));
  107.   }
  108. }