PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/application/lib/Auxilary/Paging.php

https://bitbucket.org/mustangostang/zfast
PHP | 123 lines | 57 code | 36 blank | 30 comment | 17 complexity | e92954a97f0e16b6863fd787ec35e22b MD5 | raw file
  1. <?php
  2. /**
  3. * A common class for the paging routines.
  4. *
  5. */
  6. class lib_Auxilary_Paging {
  7. /**
  8. * Basic paging routine. Lists all pages from 1 to the end.
  9. *
  10. * @param string $classPrefix Class prefix that will be used for output
  11. * @param string $total Count of records || Database query (normally is `SELECT COUNT(*)`)
  12. * @param int $perPage Number of records per page
  13. * @param int $currentPage Page we are currently on
  14. * @param string $hrefTemplate Custom template, %page% is replaced by actual page number
  15. * @param string $title Common title, omitted if empty
  16. * @return string
  17. */
  18. public function load ($classPrefix, $total, $perPage = 10, $currentPage = 1, $hrefTemplate = "./page-%page%/", $title = "????????: ") {
  19. if (!is_int($total)) {
  20. $db = Zend_Registry::get ('db');
  21. $total = $db->fetchFirst ($query);
  22. }
  23. if (ceil ($total / $perPage) <= 1) return '';
  24. $Range = array();
  25. for ($i = 1; $i <= ceil ($total / $perPage); $i++)
  26. $Range[$i] = $i;
  27. return $this->_display($classPrefix, $total, $currentPage, $Range, $hrefTemplate, $title);
  28. }
  29. /**
  30. * Advanced paging routine. Lists some pages around current page and then some from the end of paging list.
  31. *
  32. * @param string $classPrefix Class prefix that will be used for output
  33. * @param int $total Count of records
  34. * @param int $firstPages Number of pages displayed around currentPage
  35. * @param int $lastPages Number of pages displayed at the far edge of the list
  36. * @param int $perPage Number of records per page
  37. * @param int $currentPage Page we are currently on
  38. * @param string $hrefTemplate Custom template, %page% is replaced by actual page number
  39. * @param string $title Common title, omitted if empty
  40. * @return string
  41. */
  42. public static function loadFirstAndFinal ($classPrefix, $total, $firstPages = 10, $lastPages = 2, $perPage = 10, $currentPage = 1, $hrefTemplate = "./page-%page%/", $title = "????????: ") {
  43. $Range = array();
  44. $total = ceil ($total / $perPage);
  45. if ($total <= 1) return '';
  46. do {
  47. if ($total <= $firstPages + $lastPages) { $Range = self::_range(1, $total); break; }
  48. if ($currentPage <= (ceil ($total / 2))) {
  49. // Beginning of the array
  50. $start = $currentPage - ceil(($firstPages / 2));
  51. if ($start < 1) $start = 1;
  52. $Range = $Range + self::_range($start, $start + $firstPages - 1);
  53. end($Range); $latest = key($Range); $Range[$latest] = '..';
  54. reset($Range); $first = key($Range); if ($Range[$first] != 1) $Range[$first] = '..';
  55. $Range = $Range + self::_range($total - $lastPages + 1, $total);
  56. break;
  57. }
  58. // End of the array
  59. $start = $currentPage + ceil(($firstPages / 2));
  60. if ($start > $total) $start = $total;
  61. $Range = $Range + self::_range($total, $total - $firstPages + 1);
  62. reset($Range); $latest = key($Range); $Range[$latest] = '..';
  63. end($Range); $first = key($Range); if ($Range[$first] != $total) $Range[$first] = '..';
  64. $Range = self::_range(1, $lastPages) + $Range;
  65. } while(0);
  66. return self::_display($classPrefix, $currentPage, $Range, $hrefTemplate, $title);
  67. }
  68. private static function _range ($start, $finish) {
  69. $Range = array();
  70. if ($finish <= $start) list ($start, $finish) = array ($finish, $start);
  71. for ($i = $start; $i <= $finish; $i++) $Range[$i] = $i;
  72. return $Range;
  73. }
  74. private static function _display ($classPrefix, $currentPage, $Range, $hrefTemplate = "./page-%page%/", $title = "????????: ") {
  75. $result = '<div class="'.$classPrefix.'"><ul>';
  76. if ($title)
  77. $result .= "<li class=\"{$classPrefix}_title\">$title</li>";
  78. foreach ($Range as $number => $string) {
  79. if ($number == $currentPage) {
  80. $result .= '<li class="'.$classPrefix.'_currentpage">'.$string.'</li>'; continue;
  81. }
  82. $result .= '<li><a href="'.str_replace('%page%', $number, $hrefTemplate).'">'.$string.'</a></li>';
  83. }
  84. $result .= '</ul></div>';
  85. return $result;
  86. }
  87. }