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

/magento/app/code/core/Mage/Page/Block/Html/Pager.php

https://bitbucket.org/jit_bec/shopifine
PHP | 592 lines | 346 code | 69 blank | 177 comment | 32 complexity | c2776c8d77507f4a74161dd3234f8d45 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Page
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Html page block
  28. *
  29. * @category Mage
  30. * @package Mage_Page
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. *
  33. * @todo separate order, mode and pager
  34. */
  35. class Mage_Page_Block_Html_Pager extends Mage_Core_Block_Template
  36. {
  37. protected $_collection = null;
  38. protected $_pageVarName = 'p';
  39. protected $_limitVarName = 'limit';
  40. protected $_availableLimit = array(10=>10,20=>20,50=>50);
  41. protected $_dispersion = 3;
  42. protected $_displayPages = 5;
  43. protected $_showPerPage = true;
  44. protected $_limit = null;
  45. protected $_outputRequired = true;
  46. /**
  47. * Pages quantity per frame
  48. * @var int
  49. */
  50. protected $_frameLength = 5;
  51. /**
  52. * Next/previous page position relatively to the current frame
  53. * @var int
  54. */
  55. protected $_jump = 5;
  56. /**
  57. * Frame initialization flag
  58. * @var bool
  59. */
  60. protected $_frameInitialized = false;
  61. /**
  62. * Start page position in frame
  63. * @var int
  64. */
  65. protected $_frameStart;
  66. /**
  67. * Finish page position in frame
  68. * @var int
  69. */
  70. protected $_frameEnd;
  71. protected function _construct()
  72. {
  73. parent::_construct();
  74. $this->setData('show_amounts', true);
  75. $this->setData('use_container', true);
  76. $this->setTemplate('page/html/pager.phtml');
  77. }
  78. public function getCurrentPage()
  79. {
  80. if ($page = (int) $this->getRequest()->getParam($this->getPageVarName())) {
  81. return $page;
  82. }
  83. return 1;
  84. }
  85. public function getLimit()
  86. {
  87. if ($this->_limit !== null) {
  88. return $this->_limit;
  89. }
  90. $limits = $this->getAvailableLimit();
  91. if ($limit = $this->getRequest()->getParam($this->getLimitVarName())) {
  92. if (isset($limits[$limit])) {
  93. return $limit;
  94. }
  95. }
  96. $limits = array_keys($limits);
  97. return $limits[0];
  98. }
  99. /**
  100. * Setter for limit items per page
  101. *
  102. * @param int $limit
  103. * @return Mage_Page_Block_Html_Pager
  104. */
  105. public function setLimit($limit)
  106. {
  107. $this->_limit = $limit;
  108. return $this;
  109. }
  110. public function setCollection($collection)
  111. {
  112. $this->_collection = $collection
  113. ->setCurPage($this->getCurrentPage());
  114. // If not int - then not limit
  115. if ((int) $this->getLimit()) {
  116. $this->_collection->setPageSize($this->getLimit());
  117. }
  118. $this->_setFrameInitialized(false);
  119. return $this;
  120. }
  121. /**
  122. * @return Mage_Core_Model_Mysql4_Collection_Abstract
  123. */
  124. public function getCollection()
  125. {
  126. return $this->_collection;
  127. }
  128. public function setPageVarName($varName)
  129. {
  130. $this->_pageVarName = $varName;
  131. return $this;
  132. }
  133. public function getPageVarName()
  134. {
  135. return $this->_pageVarName;
  136. }
  137. public function setShowPerPage($varName)
  138. {
  139. $this->_showPerPage=$varName;
  140. return $this;
  141. }
  142. public function getShowPerPage()
  143. {
  144. if(sizeof($this->getAvailableLimit())<=1) {
  145. return false;
  146. }
  147. return $this->_showPerPage;
  148. }
  149. public function setLimitVarName($varName)
  150. {
  151. $this->_limitVarName = $varName;
  152. return $this;
  153. }
  154. public function getLimitVarName()
  155. {
  156. return $this->_limitVarName;
  157. }
  158. public function setAvailableLimit(array $limits)
  159. {
  160. $this->_availableLimit = $limits;
  161. }
  162. public function getAvailableLimit()
  163. {
  164. return $this->_availableLimit;
  165. }
  166. public function getFirstNum()
  167. {
  168. $collection = $this->getCollection();
  169. return $collection->getPageSize()*($collection->getCurPage()-1)+1;
  170. }
  171. public function getLastNum()
  172. {
  173. $collection = $this->getCollection();
  174. return $collection->getPageSize()*($collection->getCurPage()-1)+$collection->count();
  175. }
  176. public function getTotalNum()
  177. {
  178. return $this->getCollection()->getSize();
  179. }
  180. public function isFirstPage()
  181. {
  182. return $this->getCollection()->getCurPage() == 1;
  183. }
  184. public function getLastPageNum()
  185. {
  186. return $this->getCollection()->getLastPageNumber();
  187. }
  188. public function isLastPage()
  189. {
  190. return $this->getCollection()->getCurPage() >= $this->getLastPageNum();
  191. }
  192. public function isLimitCurrent($limit)
  193. {
  194. return $limit == $this->getLimit();
  195. }
  196. public function isPageCurrent($page)
  197. {
  198. return $page == $this->getCurrentPage();
  199. }
  200. public function getPages()
  201. {
  202. $collection = $this->getCollection();
  203. $pages = array();
  204. if ($collection->getLastPageNumber() <= $this->_displayPages) {
  205. $pages = range(1, $collection->getLastPageNumber());
  206. }
  207. else {
  208. $half = ceil($this->_displayPages / 2);
  209. if ($collection->getCurPage() >= $half && $collection->getCurPage() <= $collection->getLastPageNumber() - $half) {
  210. $start = ($collection->getCurPage() - $half) + 1;
  211. $finish = ($start + $this->_displayPages) - 1;
  212. }
  213. elseif ($collection->getCurPage() < $half) {
  214. $start = 1;
  215. $finish = $this->_displayPages;
  216. }
  217. elseif ($collection->getCurPage() > ($collection->getLastPageNumber() - $half)) {
  218. $finish = $collection->getLastPageNumber();
  219. $start = $finish - $this->_displayPages + 1;
  220. }
  221. $pages = range($start, $finish);
  222. }
  223. return $pages;
  224. }
  225. public function getFirstPageUrl()
  226. {
  227. return $this->getPageUrl(1);
  228. }
  229. public function getPreviousPageUrl()
  230. {
  231. return $this->getPageUrl($this->getCollection()->getCurPage(-1));
  232. }
  233. public function getNextPageUrl()
  234. {
  235. return $this->getPageUrl($this->getCollection()->getCurPage(+1));
  236. }
  237. public function getLastPageUrl()
  238. {
  239. return $this->getPageUrl($this->getCollection()->getLastPageNumber());
  240. }
  241. public function getPageUrl($page)
  242. {
  243. return $this->getPagerUrl(array($this->getPageVarName()=>$page));
  244. }
  245. public function getLimitUrl($limit)
  246. {
  247. return $this->getPagerUrl(array($this->getLimitVarName()=>$limit));
  248. }
  249. public function getPagerUrl($params=array())
  250. {
  251. $urlParams = array();
  252. $urlParams['_current'] = true;
  253. $urlParams['_escape'] = true;
  254. $urlParams['_use_rewrite'] = true;
  255. $urlParams['_query'] = $params;
  256. return $this->getUrl('*/*/*', $urlParams);
  257. }
  258. /**
  259. * Getter for $_frameStart
  260. *
  261. * @return int
  262. */
  263. public function getFrameStart()
  264. {
  265. $this->_initFrame();
  266. return $this->_frameStart;
  267. }
  268. /**
  269. * Getter for $_frameEnd
  270. *
  271. * @return int
  272. */
  273. public function getFrameEnd()
  274. {
  275. $this->_initFrame();
  276. return $this->_frameEnd;
  277. }
  278. /**
  279. * Return array of pages in frame
  280. *
  281. * @return array
  282. */
  283. public function getFramePages()
  284. {
  285. $start = $this->getFrameStart();
  286. $end = $this->getFrameEnd();
  287. return range($start, $end);
  288. }
  289. /**
  290. * Return page number of Previous jump
  291. *
  292. * @return int
  293. */
  294. public function getPreviousJumpPage()
  295. {
  296. if (!$this->getJump()) {
  297. return null;
  298. }
  299. $frameStart = $this->getFrameStart();
  300. if ($frameStart - 1 > 1) {
  301. return max(2, $frameStart - $this->getJump());
  302. }
  303. return null;
  304. }
  305. /**
  306. * Prepare URL for Previous Jump
  307. *
  308. * @return string
  309. */
  310. public function getPreviousJumpUrl()
  311. {
  312. return $this->getPageUrl($this->getPreviousJumpPage());
  313. }
  314. /**
  315. * Return page number of Next jump
  316. *
  317. * @return int
  318. */
  319. public function getNextJumpPage()
  320. {
  321. if (!$this->getJump()) {
  322. return null;
  323. }
  324. $frameEnd = $this->getFrameEnd();
  325. if ($this->getLastPageNum() - $frameEnd > 1) {
  326. return min($this->getLastPageNum() - 1, $frameEnd + $this->getJump());
  327. }
  328. return null;
  329. }
  330. /**
  331. * Prepare URL for Next Jump
  332. *
  333. * @return string
  334. */
  335. public function getNextJumpUrl()
  336. {
  337. return $this->getPageUrl($this->getNextJumpPage());
  338. }
  339. /**
  340. * Getter for $_frameLength
  341. *
  342. * @return int
  343. */
  344. public function getFrameLength()
  345. {
  346. return $this->_frameLength;
  347. }
  348. /**
  349. * Getter for $_jump
  350. *
  351. * @return int
  352. */
  353. public function getJump()
  354. {
  355. return $this->_jump;
  356. }
  357. /**
  358. * Setter for $_frameLength
  359. *
  360. * @param int $frame
  361. * @return Mage_Page_Block_Html_Pager
  362. */
  363. public function setFrameLength($frame)
  364. {
  365. $frame = abs(intval($frame));
  366. if ($frame == 0) {
  367. $frame = $this->_frameLength;
  368. }
  369. if ($this->getFrameLength() != $frame) {
  370. $this->_setFrameInitialized(false);
  371. $this->_frameLength = $frame;
  372. }
  373. return $this;
  374. }
  375. /**
  376. * Setter for $_jump
  377. *
  378. * @param int $jump
  379. * @return Mage_Page_Block_Html_Pager
  380. */
  381. public function setJump($jump)
  382. {
  383. $jump = abs(intval($jump));
  384. if ($this->getJump() != $jump) {
  385. $this->_setFrameInitialized(false);
  386. $this->_jump = $jump;
  387. }
  388. return $this;
  389. }
  390. /**
  391. * Whether to show first page in pagination or not
  392. *
  393. * @return bool
  394. */
  395. public function canShowFirst()
  396. {
  397. return $this->getJump() > 1 && $this->getFrameStart() > 1;
  398. }
  399. /**
  400. * Whether to show last page in pagination or not
  401. *
  402. * @return bool
  403. */
  404. public function canShowLast()
  405. {
  406. return $this->getJump() > 1 && $this->getFrameEnd() < $this->getLastPageNum();
  407. }
  408. /**
  409. * Whether to show link to Previous Jump
  410. *
  411. * @return bool
  412. */
  413. public function canShowPreviousJump()
  414. {
  415. return $this->getPreviousJumpPage() !== null;
  416. }
  417. /**
  418. * Whether to show link to Next Jump
  419. *
  420. * @return bool
  421. */
  422. public function canShowNextJump()
  423. {
  424. return $this->getNextJumpPage() !== null;
  425. }
  426. /**
  427. * Initialize frame data, such as frame start, frame start etc.
  428. *
  429. * @return Mage_Page_Block_Html_Pager
  430. */
  431. protected function _initFrame()
  432. {
  433. if (!$this->isFrameInitialized()) {
  434. $start = 0;
  435. $end = 0;
  436. $collection = $this->getCollection();
  437. if ($collection->getLastPageNumber() <= $this->getFrameLength()) {
  438. $start = 1;
  439. $end = $collection->getLastPageNumber();
  440. }
  441. else {
  442. $half = ceil($this->getFrameLength() / 2);
  443. if ($collection->getCurPage() >= $half && $collection->getCurPage() <= $collection->getLastPageNumber() - $half) {
  444. $start = ($collection->getCurPage() - $half) + 1;
  445. $end = ($start + $this->getFrameLength()) - 1;
  446. }
  447. elseif ($collection->getCurPage() < $half) {
  448. $start = 1;
  449. $end = $this->getFrameLength();
  450. }
  451. elseif ($collection->getCurPage() > ($collection->getLastPageNumber() - $half)) {
  452. $end = $collection->getLastPageNumber();
  453. $start = $end - $this->getFrameLength() + 1;
  454. }
  455. }
  456. $this->_frameStart = $start;
  457. $this->_frameEnd = $end;
  458. $this->_setFrameInitialized(true);
  459. }
  460. return $this;
  461. }
  462. /**
  463. * Setter for flag _frameInitialized
  464. *
  465. * @param bool $flag
  466. * @return Mage_Page_Block_Html_Pager
  467. */
  468. protected function _setFrameInitialized($flag)
  469. {
  470. $this->_frameInitialized = (bool)$flag;
  471. return $this;
  472. }
  473. /**
  474. * Check if frame data was initialized
  475. *
  476. * @return Mage_Page_Block_Html_Pager
  477. */
  478. public function isFrameInitialized()
  479. {
  480. return $this->_frameInitialized;
  481. }
  482. /**
  483. * Getter for alternative text for Previous link in pagination frame
  484. *
  485. * @return string
  486. */
  487. public function getAnchorTextForPrevious()
  488. {
  489. return Mage::getStoreConfig('design/pagination/anchor_text_for_previous');
  490. }
  491. /**
  492. * Getter for alternative text for Next link in pagination frame
  493. *
  494. * @return string
  495. */
  496. public function getAnchorTextForNext()
  497. {
  498. return Mage::getStoreConfig('design/pagination/anchor_text_for_next');
  499. }
  500. /**
  501. * Set whether output of the pager is mandatory
  502. *
  503. * @param bool $isRequired
  504. * @return Mage_Page_Block_Html_Pager
  505. */
  506. public function setIsOutputRequired($isRequired)
  507. {
  508. $this->_outputRequired = (bool)$isRequired;
  509. return $this;
  510. }
  511. /**
  512. * Determine whether the pagination should be eventually rendered
  513. *
  514. * @return string
  515. */
  516. protected function _toHtml()
  517. {
  518. if ($this->_outputRequired || $this->getTotalNum() > $this->getLimit()) {
  519. return parent::_toHtml();
  520. }
  521. return '';
  522. }
  523. }