[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/addon/ -> sfPager.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * 
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * @package    symfony
  13   * @subpackage addon
  14   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  15   * @version    SVN: $Id: sfPager.class.php 3099 2006-12-20 08:16:15Z fabien $
  16   */
  17  
  18  /**
  19   *
  20   * sfPager class.
  21   *
  22   * @package    symfony
  23   * @subpackage addon
  24   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  25   * @version    SVN: $Id: sfPager.class.php 3099 2006-12-20 08:16:15Z fabien $
  26   */
  27  abstract class sfPager
  28  {
  29    protected
  30      $page            = 1,
  31      $maxPerPage      = 0,
  32      $lastPage        = 1,
  33      $nbResults       = 0,
  34      $class           = '',
  35      $tableName       = '',
  36      $objects         = null,
  37      $cursor          = 1,
  38      $parameters      = array(),
  39      $currentMaxLink  = 1,
  40      $parameterHolder = null,
  41      $maxRecordLimit = false;
  42  
  43    public function __construct($class, $maxPerPage = 10)
  44    {
  45      $this->setClass($class);
  46      $this->setMaxPerPage($maxPerPage);
  47      $this->setPage(1);
  48      $this->parameterHolder = new sfParameterHolder();
  49    }
  50  
  51    // function to be called after parameters have been set
  52    abstract public function init();
  53  
  54    // main method: returns an array of result on the given page
  55    abstract public function getResults();
  56  
  57    // used internally by getCurrent()
  58    abstract protected function retrieveObject($offset);
  59  
  60    public function getCurrentMaxLink()
  61    {
  62      return $this->currentMaxLink;
  63    }
  64  
  65    public function getMaxRecordLimit()
  66    {
  67      return $this->maxRecordLimit;
  68    }
  69  
  70    public function setMaxRecordLimit($limit)
  71    {
  72      $this->maxRecordLimit = $limit;
  73    }
  74  
  75    public function getLinks($nb_links = 5)
  76    {
  77      $links = array();
  78      $tmp   = $this->page - floor($nb_links / 2);
  79      $check = $this->lastPage - $nb_links + 1;
  80      $limit = ($check > 0) ? $check : 1;
  81      $begin = ($tmp > 0) ? (($tmp > $limit) ? $limit : $tmp) : 1;
  82  
  83      $i = $begin;
  84      while (($i < $begin + $nb_links) && ($i <= $this->lastPage))
  85      {
  86        $links[] = $i++;
  87      }
  88  
  89      $this->currentMaxLink = $links[count($links) - 1];
  90  
  91      return $links;
  92    }
  93  
  94    public function haveToPaginate()
  95    {
  96      return (($this->getPage() != 0) && ($this->getNbResults() > $this->getMaxPerPage()));
  97    }
  98  
  99    public function getCursor()
 100    {
 101      return $this->cursor;
 102    }
 103  
 104    public function setCursor($pos)
 105    {
 106      if ($pos < 1)
 107      {
 108        $this->cursor = 1;
 109      }
 110      else if ($pos > $this->nbResults)
 111      {
 112        $this->cursor = $this->nbResults;
 113      }
 114      else
 115      {
 116        $this->cursor = $pos;
 117      }
 118    }
 119  
 120    public function getObjectByCursor($pos)
 121    {
 122      $this->setCursor($pos);
 123  
 124      return $this->getCurrent();
 125    }
 126  
 127    public function getCurrent()
 128    {
 129      return $this->retrieveObject($this->cursor);
 130    }
 131  
 132    public function getNext()
 133    {
 134      if (($this->cursor + 1) > $this->nbResults)
 135      {
 136        return null;
 137      }
 138      else
 139      {
 140        return $this->retrieveObject($this->cursor + 1);
 141      }
 142    }
 143  
 144    public function getPrevious()
 145    {
 146      if (($this->cursor - 1) < 1)
 147      {
 148        return null;
 149      }
 150      else
 151      {
 152        return $this->retrieveObject($this->cursor - 1);
 153      }
 154    }
 155  
 156    public function getFirstIndice()
 157    {
 158      if ($this->page == 0)
 159      {
 160        return 1;
 161      }
 162      else
 163      {
 164        return ($this->page - 1) * $this->maxPerPage + 1;
 165      }
 166    }
 167  
 168    public function getLastIndice()
 169    {
 170      if ($this->page == 0)
 171      {
 172        return $this->nbResults;
 173      }
 174      else
 175      {
 176        if (($this->page * $this->maxPerPage) >= $this->nbResults)
 177        {
 178          return $this->nbResults;
 179        }
 180        else
 181        {
 182          return ($this->page * $this->maxPerPage);
 183        }
 184      }
 185    }
 186  
 187    public function getCriteria()
 188    {
 189      return $this->criteria;
 190    }
 191  
 192    public function setCriteria($c)
 193    {
 194      $this->criteria = $c;
 195    }
 196  
 197    public function getClass()
 198    {
 199      return $this->class;
 200    }
 201  
 202    public function setClass($class)
 203    {
 204      $this->class = $class;
 205    }
 206  
 207    public function getNbResults()
 208    {
 209      return $this->nbResults;
 210    }
 211  
 212    protected function setNbResults($nb)
 213    {
 214      $this->nbResults = $nb;
 215    }
 216  
 217    public function getFirstPage()
 218    {
 219      return 1;
 220    }
 221  
 222    public function getLastPage()
 223    {
 224      return $this->lastPage;
 225    }
 226  
 227    protected function setLastPage($page)
 228    {
 229      $this->lastPage = $page;
 230      if ($this->getPage() > $page)
 231      {
 232        $this->setPage($page);
 233      }
 234    }
 235  
 236    public function getPage()
 237    {
 238      return $this->page;
 239    }
 240  
 241    public function getNextPage()
 242    {
 243      return min($this->getPage() + 1, $this->getLastPage());
 244    }
 245  
 246    public function getPreviousPage()
 247    {
 248      return max($this->getPage() - 1, $this->getFirstPage());
 249    }
 250  
 251    public function setPage($page)
 252    {
 253      $page = intval($page);
 254  
 255      $this->page = ($page <= 0) ? 1 : $page;
 256    }
 257  
 258    public function getMaxPerPage()
 259    {
 260      return $this->maxPerPage;
 261    }
 262  
 263    public function setMaxPerPage($max)
 264    {
 265      if ($max > 0)
 266      {
 267        $this->maxPerPage = $max;
 268        if ($this->page == 0)
 269        {
 270          $this->page = 1;
 271        }
 272      }
 273      else if ($max == 0)
 274      {
 275        $this->maxPerPage = 0;
 276        $this->page = 0;
 277      }
 278      else
 279      {
 280        $this->maxPerPage = 1;
 281        if ($this->page == 0)
 282        {
 283          $this->page = 1;
 284        }
 285      }
 286    }
 287  
 288    public function getParameterHolder()
 289    {
 290      return $this->parameterHolder;
 291    }
 292  
 293    public function getParameter($name, $default = null, $ns = null)
 294    {
 295      return $this->parameterHolder->get($name, $default, $ns);
 296    }
 297  
 298    public function hasParameter($name, $ns = null)
 299    {
 300      return $this->parameterHolder->has($name, $ns);
 301    }
 302  
 303    public function setParameter($name, $value, $ns = null)
 304    {
 305      return $this->parameterHolder->set($name, $value, $ns);
 306    }
 307  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7