PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Pagination/Pagination.class.php

https://gitlab.com/webbroteam/satisfaction-mvc
PHP | 447 lines | 174 code | 31 blank | 242 comment | 11 complexity | 1147b6e8c27e63ecf66a102b821f0d5a MD5 | raw file
  1. <?php
  2. /**
  3. * Pagination
  4. * @link https://github.com/onassar/PHP-Pagination/blob/master/Pagination.class.php
  5. * Supplies an API for setting pagination details, and renders the resulting
  6. * pagination markup (html) through the included render.inc.php file.
  7. *
  8. * @note The SEO methods (canonical/rel) were written following Google's
  9. * suggested patterns. Namely, the canoical url excludes any
  10. * peripheral parameters that don't relate to the pagination
  11. * series. Whereas the prev/next rel link tags include any params
  12. * found in the request.
  13. * @author Oliver Nassar <onassar@gmail.com>
  14. * @todo add setter parameter type and range checks w/ exceptions
  15. * @example
  16. * <code>
  17. * // source inclusion
  18. * require_once APP . '/vendors/PHP-Pagination/Pagination.class.php';
  19. *
  20. * // determine page (based on <_GET>)
  21. * $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
  22. *
  23. * // instantiate with page and records as constructor parameters
  24. * $pagination = (new Pagination($page, 200));
  25. * $markup = $pagination->parse();
  26. * </code>
  27. * @example
  28. * <code>
  29. * // source inclusion
  30. * require_once APP . '/vendors/PHP-Pagination/Pagination.class.php';
  31. *
  32. * // determine page (based on <_GET>)
  33. * $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
  34. *
  35. * // instantiate; set current page; set number of records
  36. * $pagination = (new Pagination());
  37. * $pagination->setCurrent($page);
  38. * $pagination->setTotal(200);
  39. *
  40. * // grab rendered/parsed pagination markup
  41. * $markup = $pagination->parse();
  42. * </code>
  43. */
  44. class Pagination
  45. {
  46. /**
  47. * _variables
  48. *
  49. * Sets default variables for the rendering of the pagination markup.
  50. *
  51. * @var array
  52. * @access protected
  53. */
  54. protected $_variables = array(
  55. 'classes' => array('clearfix', 'pagination'),
  56. 'crumbs' => 8,
  57. 'rpp' => 6,
  58. 'key' => 'page',
  59. 'target' => '',
  60. 'next' => 'Вперед &raquo;',
  61. 'previous' => '&laquo; Назад',
  62. 'alwaysShowPagination' => false,
  63. 'clean' => false
  64. );
  65. /**
  66. * __construct
  67. *
  68. * @access public
  69. * @param integer $current (default: null)
  70. * @param integer $total (default: null)
  71. * @return void
  72. */
  73. public function __construct($current = null, $total = null)
  74. {
  75. // current instantiation setting
  76. if (!is_null($current)) {
  77. $this->setCurrent($current);
  78. }
  79. // total instantiation setting
  80. if (!is_null($total)) {
  81. $this->setTotal($total);
  82. }
  83. // Pass along get (for link generation)
  84. //$this->_variables['get'] = $_GET; // or $this->setUrl('');
  85. }
  86. /**
  87. * _check
  88. *
  89. * Checks the current (page) and total (records) parameters to ensure
  90. * they've been set. Throws an exception otherwise.
  91. *
  92. * @access protected
  93. * @return void
  94. */
  95. protected function _check()
  96. {
  97. if (!isset($this->_variables['current'])) {
  98. throw new Exception('Pagination::current must be set.');
  99. } elseif (!isset($this->_variables['total'])) {
  100. throw new Exception('Pagination::total must be set.');
  101. }
  102. }
  103. /**
  104. * addClasses
  105. *
  106. * Sets the classes to be added to the pagination div node.
  107. * Useful with Twitter Bootstrap (eg. pagination-centered, etc.)
  108. *
  109. * @see <http://twitter.github.com/bootstrap/components.html#pagination>
  110. * @access public
  111. * @param mixed $classes
  112. * @return void
  113. */
  114. public function addClasses($classes)
  115. {
  116. $this->_variables['classes'] = array_merge(
  117. $this->_variables['classes'],
  118. (array) $classes
  119. );
  120. }
  121. /**
  122. * alwaysShowPagination
  123. *
  124. * Tells the rendering engine to show the pagination links even if there
  125. * aren't any pages to paginate through.
  126. *
  127. * @access public
  128. * @return void
  129. */
  130. public function alwaysShowPagination()
  131. {
  132. $this->_variables['alwaysShowPagination'] = true;
  133. }
  134. /**
  135. * getCanonicalUrl
  136. *
  137. * @access public
  138. * @return string
  139. */
  140. public function getCanonicalUrl()
  141. {
  142. $target = $this->_variables['target'];
  143. if (empty($target)) {
  144. $target = $_SERVER['PHP_SELF'];
  145. }
  146. $page = (int) $this->_variables['current'];
  147. if ($page !== 1) {
  148. return 'http://' . ($_SERVER['HTTP_HOST']) . ($target) . $this->getPageParam();
  149. }
  150. return 'http://' . ($_SERVER['HTTP_HOST']) . ($target);
  151. }
  152. /**
  153. * getPageParam
  154. *
  155. * @access public
  156. * @param boolean|integer $page (default: false)
  157. * @return string
  158. */
  159. public function getPageParam($page = false)
  160. {
  161. if ($page === false) {
  162. $page = (int) $this->_variables['current'];
  163. }
  164. $key = $this->_variables['key'];
  165. //return '?' . ($key) . '=' . ((int) $page);
  166. return 'page:' . ((int) $page);
  167. }
  168. /**
  169. * getPageUrl
  170. *
  171. * @access public
  172. * @param boolean|integer $page (default: false)
  173. * @return string
  174. */
  175. public function getPageUrl($page = false)
  176. {
  177. $target = $this->_variables['target'];
  178. if (empty($target)) {
  179. $target = $_SERVER['PHP_SELF'];
  180. }
  181. return 'http://' . ($_SERVER['HTTP_HOST']) . ($target) . ($this->getPageParam($page));
  182. }
  183. /**
  184. * getRelPrevNextLinkTags
  185. *
  186. * @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=1663744
  187. * @see http://googlewebmastercentral.blogspot.ca/2011/09/pagination-with-relnext-and-relprev.html
  188. * @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394
  189. * @access public
  190. * @return array
  191. */
  192. public function getRelPrevNextLinkTags()
  193. {
  194. // generate path
  195. $target = $this->_variables['target'];
  196. if (empty($target)) {
  197. $target = $_SERVER['PHP_SELF'];
  198. }
  199. $key = $this->_variables['key'];
  200. $params = $this->_variables['get'];
  201. $params[$key] = 'pgnmbr';
  202. $href = ($target) . '?' . http_build_query($params);
  203. $href = preg_replace(
  204. array('/=$/', '/=&/'),
  205. array('', '&'),
  206. $href
  207. );
  208. $href = 'http://' . ($_SERVER['HTTP_HOST']) . $href;
  209. // Pages
  210. $currentPage = (int) $this->_variables['current'];
  211. $numberOfPages = (
  212. (int) ceil(
  213. $this->_variables['total'] /
  214. $this->_variables['rpp']
  215. )
  216. );
  217. // On first page
  218. if ($currentPage === 1) {
  219. // There is a page after this one
  220. if ($numberOfPages > 1) {
  221. $href = str_replace('pgnmbr', 2, $href);
  222. return array(
  223. '<link rel="next" href="' . ($href) . '" />'
  224. );
  225. }
  226. return array();
  227. }
  228. // Store em
  229. $prevNextTags = array(
  230. '<link rel="prev" href="' . (str_replace('pgnmbr', $currentPage - 1, $href)) . '" />'
  231. );
  232. // There is a page after this one
  233. if ($numberOfPages > $currentPage) {
  234. array_push(
  235. $prevNextTags,
  236. '<link rel="next" href="' . (str_replace('pgnmbr', $currentPage + 1, $href)) . '" />'
  237. );
  238. }
  239. return $prevNextTags;
  240. }
  241. /**
  242. * parse
  243. *
  244. * Parses the pagination markup based on the parameters set and the
  245. * logic found in the render.inc.php file.
  246. *
  247. * @access public
  248. */
  249. public function parse()
  250. {
  251. // ensure required parameters were set
  252. $this->_check();
  253. // bring variables forward
  254. foreach ($this->_variables as $_name => $_value) {
  255. $$_name = $_value;
  256. }
  257. // buffer handling
  258. ob_start();
  259. include 'render.inc.php';
  260. $_response = ob_get_contents();
  261. ob_end_clean();
  262. return $_response;
  263. }
  264. /**
  265. * setClasses
  266. *
  267. * @see <http://twitter.github.com/bootstrap/components.html#pagination>
  268. * @access public
  269. * @param mixed $classes
  270. * @return void
  271. */
  272. public function setClasses($classes)
  273. {
  274. $this->_variables['classes'] = (array) $classes;
  275. }
  276. /**
  277. * setClean
  278. *
  279. * Sets the pagination to exclude page numbers, and only output
  280. * previous/next markup. The counter-method of this is self::setFull.
  281. *
  282. * @access public
  283. * @return void
  284. */
  285. public function setClean()
  286. {
  287. $this->_variables['clean'] = true;
  288. }
  289. /**
  290. * setCrumbs
  291. *
  292. * Sets the maximum number of 'crumbs' (eg. numerical page items)
  293. * available.
  294. *
  295. * @access public
  296. * @param integer $crumbs
  297. * @return void
  298. */
  299. public function setCrumbs($crumbs)
  300. {
  301. $this->_variables['crumbs'] = $crumbs;
  302. }
  303. /**
  304. * setCurrent
  305. *
  306. * Sets the current page being viewed.
  307. *
  308. * @access public
  309. * @param integer $current
  310. * @return void
  311. */
  312. public function setCurrent($current)
  313. {
  314. $this->_variables['current'] = $current;
  315. }
  316. /**
  317. * setFull
  318. *
  319. * See self::setClean for documentation.
  320. *
  321. * @access public
  322. * @return void
  323. */
  324. public function setFull()
  325. {
  326. $this->_variables['clean'] = false;
  327. }
  328. /**
  329. * setKey
  330. *
  331. * Sets the key of the <_GET> array that contains, and ought to contain,
  332. * paging information (eg. which page is being viewed).
  333. *
  334. * @access public
  335. * @param string $key
  336. * @return void
  337. */
  338. public function setKey($key)
  339. {
  340. $this->_variables['key'] = $key;
  341. }
  342. /**
  343. * setNext
  344. *
  345. * Sets the copy of the next anchor.
  346. *
  347. * @access public
  348. * @param string $str
  349. * @return void
  350. */
  351. public function setNext($str)
  352. {
  353. $this->_variables['next'] = $str;
  354. }
  355. /**
  356. * setPrevious
  357. *
  358. * Sets the copy of the previous anchor.
  359. *
  360. * @access public
  361. * @param string $str
  362. * @return void
  363. */
  364. public function setPrevious($str)
  365. {
  366. $this->_variables['previous'] = $str;
  367. }
  368. /**
  369. * setRPP
  370. *
  371. * Sets the number of records per page (used for determining total
  372. * number of pages).
  373. *
  374. * @access public
  375. * @param integer $rpp
  376. * @return void
  377. */
  378. public function setRPP($rpp)
  379. {
  380. $this->_variables['rpp'] = $rpp;
  381. }
  382. /**
  383. * setTarget
  384. *
  385. * Sets the leading path for anchors.
  386. *
  387. * @access public
  388. * @param string $target
  389. * @return void
  390. */
  391. public function setTarget($target)
  392. {
  393. $this->_variables['target'] = $target;
  394. }
  395. /**
  396. * setTotal
  397. *
  398. * Sets the total number of records available for pagination.
  399. *
  400. * @access public
  401. * @param integer $total
  402. * @return void
  403. */
  404. public function setTotal($total)
  405. {
  406. $this->_variables['total'] = $total;
  407. }
  408. /**
  409. * @custom setUrl
  410. * @param $get
  411. */
  412. public function setUrl($get)
  413. {
  414. $this->_variables['get'] = $get;
  415. }
  416. }