/application/lib/Auxilary/Paging.php
PHP | 123 lines | 57 code | 36 blank | 30 comment | 17 complexity | e92954a97f0e16b6863fd787ec35e22b MD5 | raw file
- <?php
- /**
- * A common class for the paging routines.
- *
- */
- class lib_Auxilary_Paging {
- /**
- * Basic paging routine. Lists all pages from 1 to the end.
- *
- * @param string $classPrefix Class prefix that will be used for output
- * @param string $total Count of records || Database query (normally is `SELECT COUNT(*)`)
- * @param int $perPage Number of records per page
- * @param int $currentPage Page we are currently on
- * @param string $hrefTemplate Custom template, %page% is replaced by actual page number
- * @param string $title Common title, omitted if empty
- * @return string
- */
- public function load ($classPrefix, $total, $perPage = 10, $currentPage = 1, $hrefTemplate = "./page-%page%/", $title = "????????: ") {
- if (!is_int($total)) {
- $db = Zend_Registry::get ('db');
- $total = $db->fetchFirst ($query);
- }
- if (ceil ($total / $perPage) <= 1) return '';
- $Range = array();
- for ($i = 1; $i <= ceil ($total / $perPage); $i++)
- $Range[$i] = $i;
- return $this->_display($classPrefix, $total, $currentPage, $Range, $hrefTemplate, $title);
- }
-
- /**
- * Advanced paging routine. Lists some pages around current page and then some from the end of paging list.
- *
- * @param string $classPrefix Class prefix that will be used for output
- * @param int $total Count of records
- * @param int $firstPages Number of pages displayed around currentPage
- * @param int $lastPages Number of pages displayed at the far edge of the list
- * @param int $perPage Number of records per page
- * @param int $currentPage Page we are currently on
- * @param string $hrefTemplate Custom template, %page% is replaced by actual page number
- * @param string $title Common title, omitted if empty
- * @return string
- */
- public static function loadFirstAndFinal ($classPrefix, $total, $firstPages = 10, $lastPages = 2, $perPage = 10, $currentPage = 1, $hrefTemplate = "./page-%page%/", $title = "????????: ") {
- $Range = array();
- $total = ceil ($total / $perPage);
- if ($total <= 1) return '';
- do {
- if ($total <= $firstPages + $lastPages) { $Range = self::_range(1, $total); break; }
- if ($currentPage <= (ceil ($total / 2))) {
- // Beginning of the array
-
- $start = $currentPage - ceil(($firstPages / 2));
- if ($start < 1) $start = 1;
- $Range = $Range + self::_range($start, $start + $firstPages - 1);
- end($Range); $latest = key($Range); $Range[$latest] = '..';
- reset($Range); $first = key($Range); if ($Range[$first] != 1) $Range[$first] = '..';
- $Range = $Range + self::_range($total - $lastPages + 1, $total);
- break;
- }
- // End of the array
- $start = $currentPage + ceil(($firstPages / 2));
- if ($start > $total) $start = $total;
- $Range = $Range + self::_range($total, $total - $firstPages + 1);
- reset($Range); $latest = key($Range); $Range[$latest] = '..';
- end($Range); $first = key($Range); if ($Range[$first] != $total) $Range[$first] = '..';
- $Range = self::_range(1, $lastPages) + $Range;
- } while(0);
- return self::_display($classPrefix, $currentPage, $Range, $hrefTemplate, $title);
- }
- private static function _range ($start, $finish) {
- $Range = array();
- if ($finish <= $start) list ($start, $finish) = array ($finish, $start);
- for ($i = $start; $i <= $finish; $i++) $Range[$i] = $i;
- return $Range;
- }
- private static function _display ($classPrefix, $currentPage, $Range, $hrefTemplate = "./page-%page%/", $title = "????????: ") {
- $result = '<div class="'.$classPrefix.'"><ul>';
- if ($title)
- $result .= "<li class=\"{$classPrefix}_title\">$title</li>";
- foreach ($Range as $number => $string) {
- if ($number == $currentPage) {
- $result .= '<li class="'.$classPrefix.'_currentpage">'.$string.'</li>'; continue;
- }
- $result .= '<li><a href="'.str_replace('%page%', $number, $hrefTemplate).'">'.$string.'</a></li>';
- }
- $result .= '</ul></div>';
- return $result;
- }
- }