A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.9 KiB

13 years ago
  1. <?php
  2. class QPaginator extends QPaginatorBase {
  3. // APPEARANCE
  4. protected $intIndexCount = 10;
  5. protected $strLabelForPrevious;
  6. protected $strLabelForNext;
  7. protected $strCssClass = 'paginator';
  8. //////////
  9. // Methods
  10. //////////
  11. public function __construct($objParentObject, $strControlId = null) {
  12. parent::__construct($objParentObject, $strControlId);
  13. $this->strLabelForPrevious = QApplication::Translate('Previous');
  14. $this->strLabelForNext = QApplication::Translate('Next');
  15. }
  16. public function GetControlHtml() {
  17. $this->objPaginatedControl->DataBind();
  18. $strStyle = $this->GetStyleAttributes();
  19. if ($strStyle)
  20. $strStyle = sprintf(' style="%s"', $strStyle);
  21. $strToReturn = sprintf('<span id="%s" %s%s>', $this->strControlId, $strStyle, $this->GetAttributes(true, false));
  22. if ($this->intPageNumber <= 1)
  23. $strToReturn .= sprintf('<span class="arrow">%s</span>', $this->strLabelForPrevious);
  24. else {
  25. $this->strActionParameter = $this->intPageNumber - 1;
  26. $strToReturn .= sprintf('<span class="arrow"><a href="" %s>%s</a></span>',
  27. $this->GetActionAttributes(), $this->strLabelForPrevious);
  28. }
  29. $strToReturn .= '<span class="break">|</span>';
  30. if ($this->PageCount <= $this->intIndexCount) {
  31. // We have less pages than total indexcount -- so let's go ahead
  32. // and just display all page indexes
  33. for ($intIndex = 1; $intIndex <= $this->PageCount; $intIndex++) {
  34. if ($this->intPageNumber == $intIndex) {
  35. $strToReturn .= sprintf('<span class="selected">%s</span>', $intIndex);
  36. } else {
  37. $this->strActionParameter = $intIndex;
  38. $strToReturn .= sprintf('<span class="page"><a href="" %s>%s</a></span>',
  39. $this->GetActionAttributes(), $intIndex);
  40. }
  41. }
  42. } else {
  43. // Figure Out Constants
  44. /**
  45. * "Bunch" is defined as the collection of numbers that lies in between the pair of Ellipsis ("...")
  46. *
  47. * LAYOUT
  48. *
  49. * For IndexCount of 10
  50. * 2 213 2 (two items to the left of the bunch, and then 2 indexes, selected index, 3 indexes, and then two items to the right of the bunch)
  51. * e.g. 1 ... 5 6 *7* 8 9 10 ... 100
  52. *
  53. * For IndexCount of 11
  54. * 2 313 2
  55. *
  56. * For IndexCount of 12
  57. * 2 314 2
  58. *
  59. * For IndexCount of 13
  60. * 2 414 2
  61. *
  62. * For IndexCount of 14
  63. * 2 415 2
  64. *
  65. *
  66. *
  67. * START/END PAGE NUMBERS FOR THE BUNCH
  68. *
  69. * For IndexCount of 10
  70. * 1 2 3 4 5 6 7 8 .. 100
  71. * 1 .. 4 5 *6* 7 8 9 .. 100
  72. * 1 .. 92 93 *94* 95 96 97 .. 100
  73. * 1 .. 93 94 95 96 97 98 99 100
  74. *
  75. * For IndexCount of 11
  76. * 1 2 3 4 5 6 7 8 9 .. 100
  77. * 1 .. 4 5 6 *7* 8 9 10 .. 100
  78. * 1 .. 91 92 93 *94* 95 96 97 .. 100
  79. * 1 .. 92 93 94 95 96 97 98 99 100
  80. *
  81. * For IndexCount of 12
  82. * 1 2 3 4 5 6 7 8 9 10 .. 100
  83. * 1 .. 4 5 6 *7* 8 9 10 11 .. 100
  84. * 1 .. 90 91 92 *93* 94 95 96 97 .. 100
  85. * 1 .. 91 92 93 94 95 96 97 98 99 100
  86. *
  87. * For IndexCount of 13
  88. * 1 2 3 4 5 6 7 8 9 11 .. 100
  89. * 1 .. 4 5 6 7 *8* 9 10 11 12 .. 100
  90. * 1 .. 89 90 91 92 *93* 94 95 96 97 .. 100
  91. * 1 .. 90 91 92 93 94 95 96 97 98 99 100
  92. */
  93. $intMinimumEndOfBunch = $this->intIndexCount - 2;
  94. $intMaximumStartOfBunch = $this->PageCount - $this->intIndexCount + 3;
  95. $intLeftOfBunchCount = floor(($this->intIndexCount - 5) / 2);
  96. $intRightOfBunchCount = round(($this->intIndexCount - 5.0) / 2.0);
  97. $intLeftBunchTrigger = 4 + $intLeftOfBunchCount;
  98. $intRightBunchTrigger = $intMaximumStartOfBunch + round(($this->intIndexCount - 8.0) / 2.0);
  99. if ($this->intPageNumber < $intLeftBunchTrigger) {
  100. $intPageStart = 1;
  101. $strStartEllipsis = "";
  102. } else {
  103. $intPageStart = min($intMaximumStartOfBunch, $this->intPageNumber - $intLeftOfBunchCount);
  104. $this->strActionParameter = 1;
  105. $strStartEllipsis = sprintf('<span class="page"><a href="" %s>%s</a></span>',
  106. $this->GetActionAttributes(), 1);
  107. $strStartEllipsis .= '<span class="ellipsis">...</span>';
  108. }
  109. if ($this->intPageNumber > $intRightBunchTrigger) {
  110. $intPageEnd = $this->PageCount;
  111. $strEndEllipsis = "";
  112. } else {
  113. $intPageEnd = max($intMinimumEndOfBunch, $this->intPageNumber + $intRightOfBunchCount);
  114. $strEndEllipsis = '<span class="ellipsis">...</span>';
  115. $this->strActionParameter = $this->PageCount;
  116. $strEndEllipsis .= sprintf('<span class="page"><a href="" %s>%s</a></span>',
  117. $this->GetActionAttributes(), $this->PageCount);
  118. }
  119. $strToReturn .= $strStartEllipsis;
  120. for ($intIndex = $intPageStart; $intIndex <= $intPageEnd; $intIndex++) {
  121. if ($this->intPageNumber == $intIndex) {
  122. $strToReturn .= sprintf('<span class="selected">%s</span>', $intIndex);
  123. } else {
  124. $this->strActionParameter = $intIndex;
  125. $strToReturn .= sprintf('<span class="page"><a href="" %s>%s</a></span>',
  126. $this->GetActionAttributes(), $intIndex);
  127. }
  128. }
  129. $strToReturn .= $strEndEllipsis;
  130. }
  131. $strToReturn .= '<span class="break">|</span>';
  132. if ($this->intPageNumber >= $this->PageCount)
  133. $strToReturn .= sprintf('<span class="arrow">%s</span>', $this->strLabelForNext);
  134. else {
  135. $this->strActionParameter = $this->intPageNumber + 1;
  136. $strToReturn .= sprintf('<span class="arrow"><a href="" %s>%s</a></span>',
  137. $this->GetActionAttributes(), $this->strLabelForNext);
  138. }
  139. $strToReturn .= '</span>';
  140. return $strToReturn;
  141. }
  142. /////////////////////////
  143. // Public Properties: GET
  144. /////////////////////////
  145. public function __get($strName) {
  146. switch ($strName) {
  147. case 'IndexCount':
  148. return $this->intIndexCount;
  149. case 'LabelForNext':
  150. return $this->strLabelForNext;
  151. case 'LabelForPrevious':
  152. return $this->strLabelForPrevious;
  153. default:
  154. try {
  155. return parent::__get($strName);
  156. } catch (QCallerException $objExc) {
  157. $objExc->IncrementOffset();
  158. throw $objExc;
  159. }
  160. }
  161. }
  162. /////////////////////////
  163. // Public Properties: SET
  164. /////////////////////////
  165. public function __set($strName, $mixValue) {
  166. switch ($strName) {
  167. case 'IndexCount':
  168. $this->intIndexCount = QType::Cast($mixValue, QType::Integer);
  169. if ($this->intIndexCount < 7)
  170. throw new QCallerException('Paginator must have an IndexCount >= 7');
  171. return $this->intIndexCount;
  172. case 'LabelForNext':
  173. try {
  174. return ($this->strLabelForNext = QType::Cast($mixValue, QType::String));
  175. } catch (QCallerException $objExc) {
  176. $objExc->IncrementOffset();
  177. throw $objExc;
  178. }
  179. case 'LabelForPrevious':
  180. try {
  181. return ($this->strLabelForPrevious = QType::Cast($mixValue, QType::String));
  182. } catch (QCallerException $objExc) {
  183. $objExc->IncrementOffset();
  184. throw $objExc;
  185. }
  186. default:
  187. try {
  188. return (parent::__set($strName, $mixValue));
  189. } catch (QCallerException $objExc) {
  190. $objExc->IncrementOffset();
  191. throw $objExc;
  192. }
  193. break;
  194. }
  195. }
  196. }
  197. ?>