PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Gbili/Navigation/SPageSelector.php

https://bitbucket.org/gbili/gfw
PHP | 313 lines | 132 code | 37 blank | 144 comment | 14 complexity | aa7b760777c7db441077b7f20d8c4ab5 MD5 | raw file
  1. <?php
  2. namespace Gbili\Navigation;
  3. /**
  4. *
  5. * @author gui
  6. *
  7. */
  8. class SPageSelector
  9. {
  10. /**
  11. * Current Page Number
  12. *
  13. * @var unknown_type
  14. */
  15. private $currPageNum = null;
  16. /**
  17. * total Items In Book
  18. *
  19. * @var unknown_type
  20. */
  21. private $totalIIB = null;
  22. /**
  23. * Number of Items Per Page
  24. *
  25. * @var unknown_type
  26. */
  27. private $nIPP = 6;
  28. /**
  29. * Number of Buttons In Nav
  30. *
  31. * @var unknown_type
  32. */
  33. private $nBIN = 5;
  34. /**
  35. * Class used for css rendering
  36. *
  37. * @var unknown_type
  38. */
  39. private $cssSUPClass = array('this_page', 'other_page');
  40. /**
  41. *
  42. * @var unknown_type
  43. */
  44. private static $defaultSprintfFormat = '/page/%s/ipp/%s';
  45. /**
  46. *
  47. * @var unknown_type
  48. */
  49. private $sprintfFormat = null;
  50. /**
  51. *
  52. * @var unknown_type
  53. */
  54. private $prepUri = '';
  55. /**
  56. *
  57. * @return unknown_type
  58. */
  59. public function __construct()
  60. {
  61. }
  62. /**
  63. *
  64. * @param unknown_type $n
  65. * @return unknown_type
  66. */
  67. public function setCurrentPageNumber($n)
  68. {
  69. $this->currPageNum = $n;
  70. }
  71. /**
  72. *
  73. * @return unknown_type
  74. */
  75. public function getCurrentPageNumber()
  76. {
  77. if (null === $this->currPageNum) {
  78. throw new Exception('current page number must be set through setCurrentPageNumber($n)');
  79. }
  80. return $this->currPageNum;
  81. }
  82. /**
  83. *
  84. * @param unknown_type $n
  85. * @return unknown_type
  86. */
  87. public function setNumberItemsInBook($n)
  88. {
  89. if (0 > $n) {
  90. throw new Exception('The number of items in book cannot be less than 0');
  91. }
  92. $this->totalIIB = $n;
  93. }
  94. /**
  95. *
  96. * @return unknown_type
  97. */
  98. public function getNumberItemsInBook()
  99. {
  100. if (null === $this->totalIIB) {
  101. throw new Exception('the total number of items in book must be set with : setNumberItemsInBook($n)');
  102. }
  103. return $this->totalIIB;
  104. }
  105. /**
  106. *
  107. * @param unknown_type $n
  108. * @return unknown_type
  109. */
  110. public function setNumberOfItemsPerPage($n)
  111. {
  112. if (0 >= $n) {
  113. throw new Exception('The number of items per page cannot be less than or equal to 0');
  114. }
  115. $this->nIPP = $n;
  116. }
  117. /**
  118. *
  119. * @return unknown_type
  120. */
  121. public function getNumberOfItemsPerPage()
  122. {
  123. return $this->nIPP;
  124. }
  125. /**
  126. *
  127. * @param unknown_type $n
  128. * @return unknown_type
  129. */
  130. public function setNumberOfButtonsInNav($n)
  131. {
  132. $this->nBIN = $n;
  133. }
  134. /**
  135. *
  136. * @return unknown_type
  137. */
  138. public function getNumberOfButtonsInNav()
  139. {
  140. return $this->nBIN;
  141. }
  142. /**
  143. *
  144. * @param unknown_type $str
  145. * @return unknown_type
  146. */
  147. public function setCssSUPageClasses($selectedClassStr, $unselectedClassStr)
  148. {
  149. $this->cssSUPClasses = array($selectedClassStr, $unselectedClassStr);
  150. }
  151. /**
  152. *
  153. * @return unknown_type
  154. */
  155. public function getCssSUPageClasses()
  156. {
  157. return $this->cssSUPClasses;
  158. }
  159. /**
  160. *
  161. * @param $format
  162. * @return unknown_type
  163. */
  164. public function setSprintfFormat($format)
  165. {
  166. $this->sprintfFormat = $format;
  167. }
  168. /**
  169. *
  170. * @return unknown_type
  171. */
  172. public function getSprintfFormat()
  173. {
  174. if (null === $this->sprintfFormat) {
  175. $this->sprintfFormat = self::$defaultSprintfFormat;
  176. }
  177. return $this->sprintfFormat;
  178. }
  179. /**
  180. *
  181. * @param $format
  182. * @return unknown_type
  183. */
  184. public static function setDefaultSprintfFormat($format)
  185. {
  186. self::$defaultSprintfFormat = $format;
  187. }
  188. /**
  189. *
  190. * @return unknown_type
  191. */
  192. public static function getDefaultSprintfFormat()
  193. {
  194. return self::$defaultSprintfFormat;
  195. }
  196. /**
  197. *
  198. * @param unknown_type $str
  199. * @return unknown_type
  200. */
  201. public function setPrependedUri($str)
  202. {
  203. $this->prepUri = $str;
  204. }
  205. /**
  206. *
  207. * @param unknown_type $str
  208. * @return unknown_type
  209. */
  210. public function getPrependedUri($str)
  211. {
  212. return $this->prepUri;
  213. }
  214. /**
  215. *
  216. * @return unknown_type
  217. */
  218. public function render()
  219. {
  220. //make sure $this->sprintfFormat has something
  221. $this->getSprintfFormat();
  222. $html = '';
  223. $totalAmountOfPages = ceil($this->getNumberItemsInBook() / $this->getNumberOfItemsPerPage());
  224. /*
  225. * Only render navigator if there is more than one page
  226. */
  227. if ($totalAmountOfPages <= 1) {
  228. return $html;
  229. }
  230. /*
  231. * Determine whether to show the "<<.. <" (go to first, go to previous) portion of the navigation
  232. * When ? -- Only if we are not on the first page
  233. */
  234. if ($this->getCurrentPageNumber() > 1) {//if the user is viewing a page bigger than the first show <<.. link
  235. $html .= $this->renderHtmlButton($this->cssSUPClass[1], 1, '<<...');
  236. $html .= $this->renderHtmlButton($this->cssSUPClass[1], ($this->currPageNum - 1), '<');
  237. }
  238. /*
  239. * Render the buttons for each page: this portion of the navigation:
  240. * | 1 | | 2 | | 3 | | 4 | | 5 |
  241. * When ? -- In any situation
  242. */
  243. $pagesLeft = $totalAmountOfPages - $this->currPageNum;
  244. if ($this->currPageNum <= ($this->nBIN - 2) || $totalAmountOfPages <= $this->nBIN) {//until <-cond is true
  245. $forStart = 1;
  246. $forCount = min($this->nBIN, $totalAmountOfPages);//don't show more buttons than pages available
  247. } else if ($pagesLeft >= $this->nBIN) {//if there are more pages left than the number of buttons in nav + previous condition (we are not on the first pages)
  248. $forStart = $this->currPageNum - 2;//render the nav with the currentPage button positioned two buttons from the left
  249. $forCount = $forStart + $this->nBIN - 1;//show the buttons for the next x pages
  250. } else {//if we are on the last pages
  251. $forStart = ($totalAmountOfPages + 1) - $this->nBIN;//but start counting so that there are allways the number of buttons per page in nav
  252. $forCount = $totalAmountOfPages;//show the rest of pages
  253. }
  254. for ($i=$forStart; $i<= $forCount; $i++) {
  255. $html .= $this->renderHtmlButton(((integer) $i === (integer) $this->currPageNum)? $this->cssSUPClass[0]: $this->cssSUPClass[1], $i, $i);
  256. }
  257. /*
  258. * Determine whether to render this protion "> ..>>" (go to next, go to end)
  259. * When ? -- Only if we are not in the last page
  260. */
  261. if ($this->currPageNum < $totalAmountOfPages) {
  262. $html .= $this->renderHtmlButton($this->cssSUPClass[1], ($this->currPageNum + 1), '>');
  263. $html .= $this->renderHtmlButton($this->cssSUPClass[1], $totalAmountOfPages, '...>>');
  264. }
  265. return $html;
  266. }
  267. /**
  268. *
  269. * @return unknown_type
  270. */
  271. private function renderHtmlButton($class, $pageNum, $value)
  272. {
  273. $url = sprintf($this->sprintfFormat, $pageNum, $this->nIPP);
  274. return "<div class=\"$class\"><a href=\"{$this->prepUri}{$url}\">$value</a></div>";
  275. }
  276. }