Advertisement
Guest User

Untitled

a guest
Jul 14th, 2013
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php
  2.  
  3. class Paginator
  4. {
  5.  
  6.     /** @var int */
  7.     private $limit = 20;
  8.    
  9.     /** @var int */
  10.     private $page;
  11.    
  12.     /** @var int */
  13.     private $numItems;
  14.    
  15.     /** @var int */
  16.     private $numLinks;
  17.    
  18.    
  19.     /**
  20.      * @param int
  21.      * @param int
  22.      * @return void
  23.      */
  24.     public function __construct($numItems, $page = 1)
  25.     {
  26.         $this->numItems = (int) $numItems;
  27.         $this->numLinks = $this->getNumLinks();
  28.         $this->page = $page > 0 ? (int) $page : 1;
  29.         if ($page > $this->numLinks) {
  30.             $this->page = 1;
  31.         }
  32.     }
  33.    
  34.    
  35.     /**
  36.      * @param int
  37.      * @return void
  38.      */
  39.     public function setLimit($limit)
  40.     {
  41.         $this->limit = $limit;
  42.     }
  43.    
  44.    
  45.     /**
  46.      * @return int
  47.      */
  48.     private function getNumLinks()
  49.     {
  50.         return (int) floor($this->numItems / $this->limit);
  51.     }
  52.    
  53.    
  54.     /**
  55.      * @return int
  56.      */
  57.     public function getOffset()
  58.     {
  59.         return $this->page * $this->limit;
  60.     }
  61.    
  62.    
  63.     /**
  64.      * @param int
  65.      * @param string|NULL
  66.      * @param string|NULL
  67.      * @param array
  68.      * @param bool
  69.      * @return string
  70.      */
  71.     private function getLinkHtml($page, $char = NULL, $attributes = array(), $isActive = FALSE)
  72.     {
  73.         if ($char === NULL) {
  74.             $char = $page;
  75.         }
  76.         if ($page !== NULL) {
  77.             $page = "?page=" . $page;
  78.         } else {
  79.             $page = "#";
  80.         }
  81.        
  82.         $attrs = $this->getAttributesAsString($attributes);
  83.         $active = "";
  84.         if ($isActive) {
  85.             $active = ' class="active"';
  86.         }
  87.        
  88.         return '<a href="' . $page . '"' . $attrs . $active . '>' . $char . '</a>';
  89.     }
  90.    
  91.    
  92.     /**
  93.      * @param string
  94.      * @param array
  95.      * @return string
  96.      */
  97.     private function getInactiveLinkHtml($char, $attributes = array())
  98.     {
  99.         $attrs = $this->getAttributesAsString($attributes);
  100.        
  101.         return '<span class="inactive"' . $attrs . '>' . $char . '</span>';
  102.     }
  103.    
  104.    
  105.     /**
  106.      * @param array
  107.      * @return string
  108.      */
  109.     private function getAttributesAsString(array $attributes)
  110.     {
  111.         $attrs = "";
  112.         foreach ($attributes as $name => $value) {
  113.             $attrs .= ' ' . $name . '="' . $value . '"';
  114.         }
  115.        
  116.         return $attrs;
  117.     }
  118.  
  119.    
  120.     /**
  121.      * @return string
  122.      */
  123.     public function __toString()
  124.     {
  125.         $list = "";
  126.        
  127.         // previous and the first page
  128.         if ($this->page > 1) {
  129.             $previousPage = $this->page - 1;
  130.             $list .= $this->getLinkHtml(1, "&laquo;&laquo;", array("title" => "První stránka"));
  131.             $list .= $this->getLinkHtml($previousPage, "&laquo;", array("title" => "Předchozí stránka"));
  132.         } else {
  133.             $list .= $this->getInactiveLinkHtml("&laquo;&laquo");
  134.             $list .= $this->getInactiveLinkHtml("&laquo");
  135.         }
  136.        
  137.        
  138.         // each pages
  139.         if ($this->numLinks === 0) {
  140.             $list .= $this->getLinkHtml(1, NULL, array(), TRUE);
  141.         } else {
  142.             for ($i = -2; $i <= 2; $i++) {
  143.                 if ($i <= $this->numLinks) {
  144.                     $page = $this->page + $i;
  145.                     if ($page > 0 && $page <= $this->numLinks) {
  146.                         $active = $page === $this->page;
  147.                         $list .= $this->getLinkHtml($page, NULL, array(), $active);
  148.                     }
  149.                 }
  150.             }
  151.         }  
  152.        
  153.        
  154.         // next and the last page
  155.         if ($this->page < $this->numLinks) {
  156.             $nextPage = $this->page + 1;
  157.             $list .= $this->getLinkHtml($nextPage, "&raquo;", array("title" => "Další stránka"));
  158.             $list .= $this->getLinkHtml($this->numLinks, "&raquo;&raquo;", array("title" => "Poslední stránka"));
  159.         } else {
  160.             $list .= $this->getInactiveLinkHtml("&raquo;");
  161.             $list .= $this->getInactiveLinkHtml("&raquo;&raquo");
  162.         }
  163.        
  164.         return <<<EOT
  165. <div class="paginator">
  166.     {$list}
  167. </div>
  168. EOT;
  169.     }
  170. }
  171.  
  172.  
  173. // použití
  174. $numItems = 100;
  175. $page = isset($_GET["page"]) ? (int) $_GET["page"] : 1;
  176.  
  177. echo new Paginator($numItems, $page);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement