propel-util
[ class tree: propel-util ] [ index: propel-util ] [ all elements ]

Source for file PropelPager.php

Documentation is available at PropelPager.php

  1. <?php
  2. /*
  3.  *  $Id: PropelPager.php 563 2007-02-01 09:45:55Z heltem $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information please see
  19.  * <http://propel.phpdb.org>.
  20.  */
  21.  
  22. /**
  23.  *  PropelPager
  24.  *
  25.  *  Example Usage:
  26.  *
  27.  *  require_once 'propel/util/PropelPager.php';
  28.  *  require_once 'PEACH/Propel/Poem/poemPeer.php';
  29.  *
  30.  *  $c = new Criteria();
  31.  *  $c->addDescendingOrderByColumn(poemPeer::SID);
  32.  *
  33.  *  // with join
  34.  *  $pager = new PropelPager($c, 'poemPeer', 'doSelectJoinPoemUsers', 1, 50);
  35.  *
  36.  *  // without Join
  37.  *
  38.  *  $pager = new PropelPager($c, 'poemPeer', 'doSelect', 1, 50);
  39.  *
  40.  * Some template:
  41.  *
  42.  * <p>
  43.  * Total Pages: <?=$pager->getTotalPages()?>  Total Records: <?=$pager->getTotalRecordCount()?>
  44.  * </p>
  45.  * <table>
  46.  * <tr>
  47.  * <td>
  48.  * <?if ($link = $pager->getFirstPage):?>
  49.  * <a href="somescript?page=<?=$link?>"><?=$link?></a>|
  50.  * <?endif?>
  51.  * </td>
  52.  * <td>
  53.  * <?if ($link = $pager->getPrev()):?>
  54.  * <a href="somescript?page=<?=$link?>">Previous</a>|
  55.  * <?endif?>
  56.  * </td>
  57.  * <td>
  58.  * <?foreach ($pager->getPrevLinks() as $link):?>
  59.  * <a href="somescript?page=<?=$link?>"><?=$link?></a>|
  60.  * <?endforeach?>
  61.  * </td>
  62.  * <td><?=$pager->getPage()?></td>
  63.  * <td>
  64.  * <?foreach ($pager->getNextLinks() as $link):?>
  65.  * | <a href="somescript?page=<?=$link?>"><?=$link?></a>
  66.  * <?endforeach?>
  67.  * </td>
  68.  * <td>
  69.  * <?if ($link = $pager->getNext()):?>
  70.  * <a href="somescript?page=<?=$link?>">Last</a>|
  71.  * <?endif?>
  72.  * </td>
  73.  * <td>
  74.  * <?if ($link = $pager->getLastPage()):?>
  75.  * <a href="somescript?page=<?=$link?>"><?=$link?></a>|
  76.  * <?endif?>
  77.  * </td>
  78.  * </tr>
  79.  * </table>
  80.  * <table id="latestPoems">
  81.  * <tr>
  82.  * <th>Title</th>
  83.  * <th>Auteur</th>
  84.  * <th>Date</th>
  85.  * <th>comments</th>
  86.  * </tr>
  87.  * <?foreach ($pager->getResult() as $poem):?>
  88.  * <tr>
  89.  * <td><?=$poem->getTitle()?></td>
  90.  * <td><?=$poem->getPoemUsers()->getUname()?></td>
  91.  * <td><?=$poem->getTime()?></td>
  92.  * <td><?=$poem->getComments()?></td>
  93.  * </tr>
  94.  * <?endforeach?>
  95.  * </table>
  96.  *
  97.  *
  98.  * @author     Rob Halff <info@rhalff.com>
  99.  * @version    $Revision: 563 $
  100.  * @copyright  Copyright (c) 2004 Rob Halff: LGPL - See LICENCE
  101.  * @package    propel.util
  102.  */
  103. class PropelPager {
  104.  
  105.     private $recordCount;
  106.     private $pages;
  107.     private $peerClass;
  108.     private $peerSelectMethod;
  109.     private $peerCountMethod;
  110.     private $criteria;
  111.     private $countCriteria;
  112.     private $page;
  113.     private $rs null;
  114.  
  115.     /** @var        int Start row (offset) */
  116.     protected $start = 0;
  117.  
  118.     /** @var        int Max rows to return (0 means all) */
  119.     protected $max = 0;
  120.  
  121.     /**
  122.      * Create a new Propel Pager.
  123.      * @param      Criteria $c 
  124.      * @param      string $peerClass The name of the static Peer class.
  125.      * @param      string $peerSelectMethod The name of the static method for selecting content from the Peer class.
  126.      * @param      int $page The current page (1-based).
  127.      * @param      int $rowsPerPage The number of rows that should be displayed per page.
  128.      */
  129.     public function __construct($c null$peerClass null$peerSelectMethod null$page 1$rowsPerPage 25)
  130.     {
  131.         if (!isset($c)) {
  132.             $c new Criteria();
  133.         }
  134.         $this->setCriteria($c);
  135.         $this->setPeerClass($peerClass);
  136.         $this->setPeerSelectMethod($peerSelectMethod);
  137.         $this->guessPeerCountMethod();
  138.         $this->setPage($page);
  139.         $this->setRowsPerPage($rowsPerPage);
  140.     }
  141.  
  142.     /**
  143.      * Set the criteria for this pager.
  144.      * @param      Criteria $c 
  145.      * @return     void 
  146.      */
  147.     public function setCriteria(Criteria $c)
  148.     {
  149.         $this->criteria $c;
  150.     }
  151.  
  152.     /**
  153.      * Return the Criteria object for this pager.
  154.      * @return     Criteria 
  155.      */
  156.     public function getCriteria()
  157.     {
  158.         return $this->criteria;
  159.     }
  160.  
  161.     /**
  162.      * Set the Peer Classname
  163.      *
  164.      * @param      string $class 
  165.      * @return     void 
  166.      */
  167.     public function setPeerClass($class)
  168.     {
  169.         $this->peerClass $class;
  170.     }
  171.  
  172.     /**
  173.      * Return the Peer Classname.
  174.      * @return     string 
  175.      */
  176.     public function getPeerClass()
  177.     {
  178.         return $this->peerClass;
  179.     }
  180.  
  181.     /**
  182.      * Set the Peer select method.
  183.      * This exists for legacy support, please use setPeerSelectMethod().
  184.      * @param      string $method The name of the static method to call on the Peer class.
  185.      * @return     void 
  186.      * @see        setPeerSelectMethod()
  187.      * @deprecated
  188.      */
  189.     public function setPeerMethod($method)
  190.     {
  191.         $this->setPeerSelectMethod($method);
  192.     }
  193.  
  194.     /**
  195.      * Return the Peer select method.
  196.      * This exists for legacy support, please use getPeerSelectMethod().
  197.      * @return     string 
  198.      * @see        getPeerSelectMethod()
  199.      * @deprecated
  200.      */
  201.     public function getPeerMethod()
  202.     {
  203.         return $this->getPeerSelectMethod();
  204.     }
  205.  
  206.     /**
  207.      * Set the Peer select method.
  208.      *
  209.      * @param      string $method The name of the static method to call on the Peer class.
  210.      * @return     void 
  211.      */
  212.     public function setPeerSelectMethod($method)
  213.     {
  214.         $this->peerSelectMethod $method;
  215.     }
  216.  
  217.     /**
  218.      * Return the Peer select method.
  219.      * @return     string 
  220.      */
  221.     public function getPeerSelectMethod()
  222.     {
  223.         return $this->peerSelectMethod;
  224.     }
  225.  
  226.     /**
  227.      * Sets the Count method.
  228.      * This is set based on the Peer method, for example if Peer method is doSelectJoin*() then the
  229.      * count method will be doCountJoin*().
  230.      * @param      string $method The name of the static method to call on the Peer class.
  231.      */
  232.     public function setPeerCountMethod($method)
  233.     {
  234.         $this->peerCountMethod $method;
  235.     }
  236.  
  237.     /**
  238.      * Return the Peer count method.
  239.      */
  240.     public function getPeerCountMethod()
  241.     {
  242.         return $this->peerCountMethod;
  243.     }
  244.  
  245.     /**
  246.      * Guesses the Peer count method based on the select method.
  247.      */
  248.     private function guessPeerCountMethod()
  249.     {
  250.         $selectMethod $this->getPeerSelectMethod();
  251.         if ($selectMethod == 'doSelect'{
  252.             $countMethod 'doCount';
  253.         elseif ( ($pos stripos($selectMethod'doSelectJoin')) === 0{
  254.             $countMethod 'doCount' substr($selectMethodstrlen('doSelect'));
  255.         else {
  256.             // we will fall back to doCount() if we don't understand the join
  257.             // method; however, it probably won't be accurate.  Maybe triggering an error would
  258.             // be appropriate ...
  259.             $countMethod 'doCount';
  260.         }
  261.         $this->setPeerCountMethod($countMethod);
  262.     }
  263.  
  264.     /**
  265.      * Get the paged resultset
  266.      *
  267.      * @return     mixed $rs
  268.      */
  269.     public function getResult()
  270.     {
  271.         if (!isset($this->rs)) {
  272.             $this->doRs();
  273.         }
  274.  
  275.         return $this->rs;
  276.     }
  277.  
  278.     /**
  279.      * Get the paged resultset
  280.      *
  281.      * Main method which creates a paged result set based on the criteria
  282.      * and the requested peer select method.
  283.      *
  284.      */
  285.     private function doRs()
  286.     {
  287.         $this->criteria->setOffset($this->start);
  288.         $this->criteria->setLimit($this->max);
  289.         $this->rs call_user_func(array($this->getPeerClass()$this->getPeerSelectMethod())$this->criteria);
  290.     }
  291.  
  292.     /**
  293.      * Get the first page
  294.      *
  295.      * For now I can only think of returning 1 always.
  296.      * It should probably return 0 if there are no pages
  297.      *
  298.      * @return     int 1
  299.      */
  300.     public function getFirstPage()
  301.     {
  302.         return '1';
  303.     }
  304.  
  305.     /**
  306.      * Convenience method to indicate whether current page is the first page.
  307.      *
  308.      * @return     boolean 
  309.      */
  310.     public function atFirstPage()
  311.     {
  312.         return $this->getPage(== $this->getFirstPage();
  313.     }
  314.  
  315.     /**
  316.      * Get last page
  317.      *
  318.      * @return     int $lastPage
  319.      */
  320.     public function getLastPage()
  321.     {
  322.         $totalPages $this->getTotalPages();
  323.         if ($totalPages == 0{
  324.             return 1;
  325.         else {
  326.             return $totalPages;
  327.         }
  328.     }
  329.  
  330.     /**
  331.      * Convenience method to indicate whether current page is the last page.
  332.      *
  333.      * @return     boolean 
  334.      */
  335.     public function atLastPage()
  336.     {
  337.         return $this->getPage(== $this->getLastPage();
  338.     }
  339.  
  340.     /**
  341.      * get total pages
  342.      *
  343.      * @return     int $this->pages
  344.      */
  345.     public function getTotalPages({
  346.         if (!isset($this->pages)) {
  347.             $recordCount $this->getTotalRecordCount();
  348.             if ($this->max > 0{
  349.                     $this->pages ceil($recordCount/$this->max);
  350.             else {
  351.                     $this->pages 0;
  352.             }
  353.         }
  354.         return $this->pages;
  355.     }
  356.  
  357.     /**
  358.      * get an array of previous id's
  359.      *
  360.      * @param      int $range 
  361.      * @return     array $links
  362.      */
  363.     public function getPrevLinks($range 5)
  364.     {
  365.         $total $this->getTotalPages();
  366.         $start $this->getPage(1;
  367.         $end $this->getPage($range;
  368.         $first =  $this->getFirstPage();
  369.         $links array();
  370.         for ($i=$start$i>$end$i--{
  371.             if ($i $first{
  372.                     break;
  373.             }
  374.             $links[$i;
  375.         }
  376.  
  377.         return array_reverse($links);
  378.     }
  379.  
  380.     /**
  381.      * get an array of next id's
  382.      *
  383.      * @param      int $range 
  384.      * @return     array $links
  385.      */
  386.     public function getNextLinks($range 5)
  387.     {
  388.         $total $this->getTotalPages();
  389.         $start $this->getPage(1;
  390.         $end $this->getPage($range;
  391.         $last =  $this->getLastPage();
  392.         $links array();
  393.         for ($i=$start$i<$end$i++{
  394.             if ($i $last{
  395.                     break;
  396.             }
  397.             $links[$i;
  398.         }
  399.  
  400.         return $links;
  401.     }
  402.  
  403.     /**
  404.      * Returns whether last page is complete
  405.      *
  406.      * @return     bool Last page complete or not
  407.      */
  408.     public function isLastPageComplete()
  409.     {
  410.         return !($this->getTotalRecordCount($this->max);
  411.     }
  412.  
  413.     /**
  414.      * get previous id
  415.      *
  416.      * @return     mixed $prev
  417.      */
  418.     public function getPrev({
  419.         if ($this->getPage(!= $this->getFirstPage()) {
  420.                 $prev $this->getPage(1;
  421.         else {
  422.                 $prev false;
  423.         }
  424.         return $prev;
  425.     }
  426.  
  427.     /**
  428.      * get next id
  429.      *
  430.      * @return     mixed $next
  431.      */
  432.     public function getNext({
  433.         if ($this->getPage(!= $this->getLastPage()) {
  434.                 $next $this->getPage(1;
  435.         else {
  436.                 $next false;
  437.         }
  438.         return $next;
  439.     }
  440.  
  441.     /**
  442.      * Set the current page number (First page is 1).
  443.      * @param      int $page 
  444.      * @return     void 
  445.      */
  446.     public function setPage($page)
  447.     {
  448.         $this->page $page;
  449.         // (re-)calculate start rec
  450.         $this->calculateStart();
  451.     }
  452.  
  453.     /**
  454.      * Get current page.
  455.      * @return     int 
  456.      */
  457.     public function getPage()
  458.     {
  459.         return $this->page;
  460.     }
  461.  
  462.     /**
  463.      * Set the number of rows per page.
  464.      * @param      int $r 
  465.      */
  466.     public function setRowsPerPage($r)
  467.     {
  468.         $this->max = $r;
  469.         // (re-)calculate start rec
  470.         $this->calculateStart();
  471.     }
  472.  
  473.     /**
  474.      * Get number of rows per page.
  475.      * @return     int 
  476.      */
  477.     public function getRowsPerPage()
  478.     {
  479.         return $this->max;
  480.     }
  481.  
  482.     /**
  483.      * Calculate startrow / max rows based on current page and rows-per-page.
  484.      * @return     void 
  485.      */
  486.     private function calculateStart()
  487.     {
  488.         $this->start = ( ($this->page 1$this->max );
  489.     }
  490.  
  491.     /**
  492.      * Gets the total number of (un-LIMITed) records.
  493.      *
  494.      * This method will perform a query that executes un-LIMITed query.
  495.      *
  496.      * @return     int Total number of records - disregarding page, maxrows, etc.
  497.      */
  498.     public function getTotalRecordCount()
  499.     {
  500.  
  501.                 if (!isset($this->rs)) {
  502.                     $this->doRs();
  503.                 }
  504.  
  505.                 if (empty($this->recordCount)) {
  506.                         $this->countCriteria clone $this->criteria;
  507.                         $this->countCriteria->setLimit(0);
  508.                         $this->countCriteria->setOffset(0);
  509.  
  510.                         $this->recordCount call_user_func(
  511.                                         array(
  512.                                                 $this->getPeerClass(),
  513.                                                 $this->getPeerCountMethod()
  514.                                              ),
  515.                                         $this->countCriteria
  516.                                         );
  517.  
  518.                 }
  519.  
  520.                 return $this->recordCount;
  521.  
  522.     }
  523.  
  524.     /**
  525.      * Sets the start row or offset.
  526.      * @param      int $v 
  527.      */
  528.     public function setStart($v)
  529.     {
  530.         $this->start = $v;
  531.     }
  532.  
  533.     /**
  534.      * Sets max rows (limit).
  535.      * @param      int $v 
  536.      * @return     void 
  537.      */
  538.     public function setMax($v)
  539.     {
  540.         $this->max = $v;
  541.     }
  542.  
  543. }

Documentation generated on Thu, 22 Nov 2007 03:33:53 +0000 by phpDocumentor 1.4.0