energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file Pager.class.php

Documentation is available at Pager.class.php

  1. <?php
  2.  
  3. /**
  4.  * Класс Pager
  5.  *
  6.  * @package energine
  7.  * @subpackage core
  8.  * @author 1m.dm
  9.  * @copyright Energine 2006
  10.  * @version $Id$
  11.  */
  12.  
  13. //require_once('core/framework/Object.class.php');
  14. //require_once('core/modules/share/components/Toolbar.class.php');
  15.  
  16. /**
  17.  * Список страниц для навигации при постраничном выводе.
  18.  *
  19.  * @package energine
  20.  * @subpackage core
  21.  * @author 1m.dm
  22.  * @final
  23.  */
  24. final class Pager extends Object {
  25.  
  26.     /**
  27.      * Количество отображаемых номеров страниц с каждой стороны от текущей
  28.      *
  29.      * Пример для VISIBLE_PAGES_COUNT = 3
  30.      * 1 2 ... 5  6  7 _8_ 9 10 11 ... 455 456
  31.      */
  32.     const VISIBLE_PAGES_COUNT = 2;
  33.  
  34.     /**
  35.      * @access private
  36.      * @var int количество записей на странице
  37.      */
  38.     private $recordsPerPage;
  39.  
  40.     /**
  41.      * @access private
  42.      * @var int количество страниц
  43.      */
  44.     private $numPages = 0;
  45.  
  46.     /**
  47.      * @access private
  48.      * @var int общее количество записей
  49.      */
  50.     private $recordsCount;
  51.  
  52.     /**
  53.      * @access private
  54.      * @var int номер текущей страницы
  55.      */
  56.     private $currentPage;
  57.  
  58.     /**
  59.      * @access private
  60.      * @var array дополнительные свойства списка страниц
  61.      */
  62.     private $properties = array();
  63.  
  64.     /**
  65.      * Конструктор класса.
  66.      *
  67.      * @access public
  68.      * @param int $recordsPerPage 
  69.      * @param int $currentPage 
  70.      * @return void 
  71.      */
  72.     public function __construct($recordsPerPage 0$currentPage 1{
  73.         parent::__construct();
  74.  
  75.         $this->setRecordsPerPage($recordsPerPage);
  76.         $this->setCurrentPage($currentPage);
  77.     }
  78.  
  79.     /**
  80.      * Устанавливает количество записей на странице.
  81.      *
  82.      * @access public
  83.      * @param int $recordsPerPage 
  84.      * @return void 
  85.      */
  86.     public function setRecordsPerPage($recordsPerPage{
  87.         $recordsPerPage intval($recordsPerPage);
  88.         if ($recordsPerPage 1{
  89.             throw new SystemException('ERR_DEV_BAD_RECORDS_PER_PAGE'SystemException::ERR_DEVELOPER);
  90.         }
  91.         $this->recordsPerPage = $recordsPerPage;
  92.     }
  93.  
  94.     /**
  95.      * Возвращает количество записей на странице.
  96.      *
  97.      * @access public
  98.      * @return int 
  99.      */
  100.     public function getRecordsPerPage({
  101.         return $this->recordsPerPage;
  102.     }
  103.  
  104.     /**
  105.      * Возвращает количество страниц.
  106.      *
  107.      * @access public
  108.      * @return int 
  109.      */
  110.     public function getNumPages({
  111.         return $this->numPages;
  112.     }
  113.  
  114.     /**
  115.      * Устанавливает номер текущей страницы.
  116.      *
  117.      * @access public
  118.      * @param int $currentPage 
  119.      */
  120.     public function setCurrentPage($currentPage{
  121.         $currentPage intval($currentPage);
  122.         if ($currentPage 1{
  123.             throw new SystemException('ERR_DEV_BAD_PAGE_NUMBER'SystemException::ERR_DEVELOPER);
  124.         }
  125.         $this->currentPage = $currentPage;
  126.     }
  127.  
  128.     /**
  129.      * Устанавливает общее количество записей.
  130.      *
  131.      * @access public
  132.      * @param int $count 
  133.      * @return void 
  134.      */
  135.     public function setRecordsCount($count{
  136.         $recordsCount intval($count);
  137.         if ($recordsCount 0{
  138.             throw new SystemException('ERR_DEV_BAD_RECORDS_COUNT'SystemException::ERR_DEVELOPER);
  139.         }
  140.         $this->recordsCount = $recordsCount;
  141.         // пересчитываем количество страниц
  142.         $this->numPages = ceil($this->recordsCount / $this->recordsPerPage);
  143.     }
  144.  
  145.     /**
  146.      * Возвращает общее количество записей.
  147.      *
  148.      * @access public
  149.      * @return int 
  150.      */
  151.     public function getRecordsCount({
  152.         return $this->recordsCount;
  153.     }
  154.  
  155.     /**
  156.      * Возвращает номер текущей страницы.
  157.      *
  158.      * @access public
  159.      * @return int 
  160.      */
  161.     public function getCurrentPage({
  162.         return $this->currentPage;
  163.     }
  164.  
  165.     /**
  166.      * Устанавливает свойство списка страниц.
  167.      *
  168.      * @access public
  169.      * @param string $name 
  170.      * @param mixed $value 
  171.      * @return void 
  172.      */
  173.     public function setProperty($name$value{
  174.         $this->properties[$name$value;
  175.     }
  176.  
  177.     /**
  178.      * Возвращает лимит выборки для SELECT-запроса.
  179.      *
  180.      * @access public
  181.      * @return array 
  182.      * @see QAL::select()
  183.      */
  184.     public function getLimit({
  185.         return array(($this->getCurrentPage(1$this->getRecordsPerPage()$this->getRecordsPerPage());
  186.     }
  187.  
  188.     /**
  189.      * Строит документ списка страниц и возвращает ссылку на корневой узел документа.
  190.      *
  191.      * @access public
  192.      * @return DOMNode 
  193.      */
  194.     public function build({
  195.  
  196.         $pager new Toolbar('pager');
  197.         if (!empty($_GET)) {
  198.             $this->setProperty('get_string'http_build_query($_GET));
  199.         }
  200.         if (!empty($this->properties)) {
  201.             foreach ($this->properties as $propName => $propValue{
  202.                 $pager->setProperty($propName$propValue);
  203.             }
  204.         }
  205.  
  206.         $pager->setProperty('from'DBWorker::_translate('TXT_FROM'));
  207.         $pager->setProperty('to'DBWorker::_translate('TXT_TO'));
  208.  
  209.  
  210.         $startPage (($page $this->currentPage - self::VISIBLE_PAGES_COUNT1)?1:$page;
  211.         $endPage  (($page $this->currentPage + self::VISIBLE_PAGES_COUNT$this->numPages)?$this->numPages:$page;
  212.  
  213.         if ($startPage 1{
  214.             $control new Link("page1"11);
  215.             $pager->attachControl($control);
  216.         }
  217.         if ($startPage 2{
  218.             $control new Link("page2"22);
  219.             $pager->attachControl($control);
  220.             if ($startPage != 1{
  221.                 $pager->attachControl(new Separator('sep_start'));
  222.             }
  223.         }
  224.  
  225.         for ($i $startPage$i <= $endPage$i++{
  226.             $isCurrent ($i == $this->currentPage);
  227.  
  228.             $control new Link("page$i"$i$i);
  229.             if ($isCurrent{
  230.                 $control->disable();
  231.             }
  232.             $pager->attachControl($control);
  233.         }
  234.  
  235.         if ($endPage $this->numPages - 2{
  236.             if ($endPage != $this->numPages - 1{
  237.                 $pager->attachControl(new Separator('sep_end'));
  238.             }
  239.             $control new Link("page".($this->numPages-1)$this->numPages-1$this->numPages-1);
  240.             $pager->attachControl($control);
  241.         }
  242.         if ($endPage $this->numPages{
  243.             $control new Link("page$this->numPages"$this->numPages$this->numPages);
  244.             $pager->attachControl($control);
  245.         }
  246.  
  247.         return $pager->build();
  248.     }
  249. }
В создании документации нам помог: phpDocumentor