[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/src/components/pager/ -> WactPagerComponent.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  /**

  11   * class WactPagerComponent.

  12   *

  13   * @package wact

  14   * @version $Id: WactPagerComponent.class.php 5945 2007-06-06 08:31:43Z pachanga $

  15   */
  16  class WactPagerComponent extends WactRuntimeComponent
  17  {
  18    protected $total_items = 0;
  19    protected $total_page_count = 0;
  20    protected $page_counter = 0;
  21    protected $displayed_page = 0;
  22    protected $items_per_page = 20;
  23    protected $pager_prefix = 'page';
  24    protected $base_url = null;
  25    protected $paged_dataset = null;
  26  
  27    protected $display_sections = true;
  28    protected $pages_per_section = 10;
  29  
  30    protected $display_elipses = false;
  31    protected $pages_in_middle = 5;
  32    protected $pages_in_sides = 3;
  33  
  34    function prepare()
  35    {
  36      if ($this->paged_dataset)
  37        $this->setTotalItems($this->paged_dataset->count());
  38  
  39      $this->total_page_count = ceil($this->total_items / $this->items_per_page);
  40  
  41      if ($this->total_page_count < 1)
  42        $this->total_page_count = 1;
  43  
  44      $this->_initBaseUrl();
  45  
  46      $this->page_counter = 1;
  47    }
  48  
  49    function resetPagesIterator()
  50    {
  51      $this->page_counter = 1;
  52    }
  53  
  54    function useSections($flag = true)
  55    {
  56      $this->display_sections = $flag;
  57    }
  58  
  59    function isSectionsMode()
  60    {
  61      return $this->display_sections;
  62    }
  63  
  64    function useElipses($flag = true)
  65    {
  66      $this->useSections(!$flag);
  67      $this->display_elipses = $flag;
  68    }
  69  
  70    function isElipsesMode()
  71    {
  72      return $this->display_elipses;
  73    }
  74  
  75    function setPagesPerSection($pages)
  76    {
  77      $this->pages_per_section = $pages;
  78    }
  79  
  80    function setPagesInMiddle($pages)
  81    {
  82      $this->pages_in_middle = $pages;
  83    }
  84  
  85    function setPagesInSides($pages)
  86    {
  87      $this->pages_in_sides = $pages;
  88    }
  89  
  90    function setPagerPrefix($prefix)
  91    {
  92      $this->pager_prefix = $prefix;
  93    }
  94  
  95    function setTotalItems($items)
  96    {
  97      $this->total_items = $items;
  98    }
  99  
 100    function getPagesPerSection()
 101    {
 102      return $this->pages_per_section;
 103    }
 104  
 105    function getTotalItems()
 106    {
 107      return $this->total_items;
 108    }
 109  
 110    function hasMoreThanOnePage()
 111    {
 112      return $this->total_items > $this->items_per_page;
 113    }
 114  
 115    function setItemsPerPage($items)
 116    {
 117      $this->items_per_page = $items;
 118    }
 119  
 120    //implementing WACT pager interface

 121    function getStartingItem()
 122    {
 123      $number = $this->getDisplayedPageBeginItem();
 124      return ($number == 0) ? 0 : $number - 1;
 125    }
 126  
 127    function setPagedDataSet($dataset)
 128    {
 129      $this->paged_dataset = $dataset;
 130  
 131      $this->prepare();
 132    }
 133  
 134    function getDisplayedPageBeginItem()
 135    {
 136      if($this->total_items < 1)
 137        return 0;
 138  
 139      return $this->items_per_page * ($this->displayed_page - 1) + 1;
 140    }
 141  
 142    function getDisplayedPageEndItem()
 143    {
 144      $res = $this->items_per_page * $this->displayed_page;
 145  
 146      if($res > $this->total_items)
 147        return $this->total_items;
 148      else
 149        return $res;
 150    }
 151  
 152    function getItemsPerPage()
 153    {
 154      return $this->items_per_page;
 155    }
 156  
 157    function getTotalPages()
 158    {
 159      return $this->total_page_count;
 160    }
 161  
 162    function isFirst()
 163    {
 164      return ($this->displayed_page == 1);
 165    }
 166  
 167    function hasPrev()
 168    {
 169      return ($this->displayed_page > 1);
 170    }
 171  
 172    function hasNext()
 173    {
 174      return ($this->displayed_page < $this->total_page_count);
 175    }
 176  
 177    function isLast()
 178    {
 179      return ($this->displayed_page == $this->total_page_count);
 180    }
 181  
 182    protected function _initBaseUrl()
 183    {
 184      $this->base_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
 185      $pos = strpos($this->base_url, '?');
 186      if (is_integer($pos))
 187          $this->base_url = substr($this->base_url, 0, $pos);
 188  
 189      $this->displayed_page = @$_GET[$this->getPagerId()];
 190      if (empty($this->displayed_page)) {
 191          $this->displayed_page = 1;
 192      }
 193  
 194      if (empty($this->displayed_page))
 195        $this->displayed_page = 1;
 196  
 197      if($this->displayed_page > $this->total_page_count)
 198        $this->displayed_page = $this->total_page_count;
 199    }
 200  
 201    function nextPage()
 202    {
 203      $this->page_counter++;
 204  
 205      return $this->isValid();
 206    }
 207  
 208    function isValid()
 209    {
 210      return ($this->page_counter <= $this->total_page_count);
 211    }
 212  
 213    function nextSection()
 214    {
 215      $this->page_counter += $this->pages_per_section;
 216  
 217      return $this->isValid();
 218    }
 219  
 220    function getPage()
 221    {
 222      return $this->page_counter;
 223    }
 224  
 225    function isDisplayedPage()
 226    {
 227      return $this->page_counter == $this->displayed_page;
 228    }
 229  
 230    function shouldDisplayPage()
 231    {
 232      if($this->display_sections)
 233        return $this->isDisplayedSection();
 234  
 235      $half_windows_size = ($this->pages_in_middle - 1) / 2;
 236      return (
 237          $this->page_counter <= $this->pages_in_sides ||
 238          $this->page_counter > $this->total_page_count - $this->pages_in_sides ||
 239          ($this->page_counter >= $this->displayed_page - $half_windows_size &&
 240          $this->page_counter <= $this->displayed_page + $half_windows_size) ||
 241          ($this->page_counter == $this->pages_in_sides + 1 &&
 242          $this->page_counter == $this->displayed_page - $half_windows_size - 1) ||
 243          ($this->page_counter == $this->total_page_count - $this->pages_in_sides &&
 244          $this->page_counter == $this->displayed_page + $half_windows_size + 1));
 245    }
 246  
 247    function isDisplayedSection()
 248    {
 249      if($this->getSection() == $this->getDisplayedSection())
 250        return true;
 251      else
 252        return false;
 253    }
 254  
 255    function getSection()
 256    {
 257      return ceil($this->page_counter / $this->pages_per_section);
 258    }
 259  
 260    function getDisplayedSection()
 261    {
 262      return ceil($this->displayed_page / $this->pages_per_section);
 263    }
 264  
 265    function getSectionUri()
 266    {
 267      $section = $this->getSection();
 268  
 269      if ($section > $this->getDisplayedSection())
 270        return $this->getPageUri(($section - 1) * $this->pages_per_section + 1);
 271      else
 272        return $this->getPageUri($section  * $this->pages_per_section);
 273    }
 274  
 275    function getSectionBeginPage()
 276    {
 277      $result = ($this->getSection() - 1) * $this->pages_per_section + 1;
 278  
 279      if($result < 0)
 280        return 0;
 281      else
 282        return $result;
 283    }
 284  
 285    function getSectionEndPage()
 286    {
 287      $result = $this->getSection() * $this->pages_per_section;
 288  
 289      if ($result >= $this->total_page_count)
 290        $result = $this->total_page_count;
 291  
 292      return $result;
 293    }
 294  
 295    function getDisplayedPageUri()
 296    {
 297      return $this->getPageUri($this->displayed_page);
 298    }
 299  
 300    function getDisplayedPage()
 301    {
 302      return $this->displayed_page;
 303    }
 304  
 305    function getPagerId()
 306    {
 307      return $this->pager_prefix . '_' . $this->getId();
 308    }
 309  
 310    function getPageUri($page = null)
 311    {
 312      if ($page == null)
 313        $page = $this->page_counter;
 314  
 315      $params = $_GET;
 316  
 317      if ($page <= 1)
 318        unset($params[$this->getPagerId()]);
 319      else
 320        $params[$this->getPagerId()] = $page;
 321  
 322      $flat_params = array();
 323      $this->toFlatArray($params, $flat_params);
 324  
 325      $query_items = array();
 326      foreach ($flat_params as $key => $value)
 327        $query_items[] = $key . '=' . urlencode($value);
 328  
 329      $query = implode('&', $query_items);
 330  
 331      if (empty($query))
 332        return $this->base_url;
 333      else
 334        return $this->base_url . '?' . $query;
 335    }
 336  
 337    function toFlatArray($array, &$result, $prefix='')
 338    {
 339      foreach($array as $key => $value)
 340      {
 341        $string_key = ($prefix) ? '[' . $key . ']' : $key;
 342  
 343        if(is_array($value))
 344          $this->toFlatArray($value, $result, $prefix . $string_key);
 345        else
 346          $result[$prefix . $string_key] = $value;
 347      }
 348    }
 349  
 350    function getFirstPageUri()
 351    {
 352      return $this->getPageUri(1);
 353    }
 354  
 355    function getLastPageUri()
 356    {
 357      return $this->getPageUri($this->total_page_count);
 358    }
 359  }
 360  
 361  ?>


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7