PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Paginator.php

https://bitbucket.org/michalmatoga/ebpl
PHP | 1173 lines | 540 code | 162 blank | 471 comment | 98 complexity | 5f4cd16282ec51a6ff2b00cc386d8a7e MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  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@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Paginator
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Paginator.php 24754 2012-05-05 02:30:56Z adamlundrigan $
  20. */
  21. /**
  22. * @see Zend_Loader_PluginLoader
  23. */
  24. require_once 'Zend/Loader/PluginLoader.php';
  25. /**
  26. * @see Zend_Json
  27. */
  28. require_once 'Zend/Json.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Paginator
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Paginator implements Countable, IteratorAggregate
  36. {
  37. /**
  38. * Specifies that the factory should try to detect the proper adapter type first
  39. *
  40. * @var string
  41. */
  42. const INTERNAL_ADAPTER = 'Zend_Paginator_Adapter_Internal';
  43. /**
  44. * The cache tag prefix used to namespace Paginator results in the cache
  45. *
  46. */
  47. const CACHE_TAG_PREFIX = 'Zend_Paginator_';
  48. /**
  49. * Adapter plugin loader
  50. *
  51. * @var Zend_Loader_PluginLoader
  52. */
  53. protected static $_adapterLoader = null;
  54. /**
  55. * Configuration file
  56. *
  57. * @var Zend_Config
  58. */
  59. protected static $_config = null;
  60. /**
  61. * Default scrolling style
  62. *
  63. * @var string
  64. */
  65. protected static $_defaultScrollingStyle = 'Sliding';
  66. /**
  67. * Default item count per page
  68. *
  69. * @var int
  70. */
  71. protected static $_defaultItemCountPerPage = 10;
  72. /**
  73. * Default number of local pages (i.e., the number of discretes
  74. * page numbers that will be displayed, including the current
  75. * page number)
  76. *
  77. * @var int
  78. */
  79. protected static $_defaultPageRange = 10;
  80. /**
  81. * Scrolling style plugin loader
  82. *
  83. * @var Zend_Loader_PluginLoader
  84. */
  85. protected static $_scrollingStyleLoader = null;
  86. /**
  87. * Cache object
  88. *
  89. * @var Zend_Cache_Core
  90. */
  91. protected static $_cache;
  92. /**
  93. * Enable or disable the cache by Zend_Paginator instance
  94. *
  95. * @var bool
  96. */
  97. protected $_cacheEnabled = true;
  98. /**
  99. * Adapter
  100. *
  101. * @var Zend_Paginator_Adapter_Interface
  102. */
  103. protected $_adapter = null;
  104. /**
  105. * Number of items in the current page
  106. *
  107. * @var integer
  108. */
  109. protected $_currentItemCount = null;
  110. /**
  111. * Current page items
  112. *
  113. * @var Traversable
  114. */
  115. protected $_currentItems = null;
  116. /**
  117. * Current page number (starting from 1)
  118. *
  119. * @var integer
  120. */
  121. protected $_currentPageNumber = 1;
  122. /**
  123. * Result filter
  124. *
  125. * @var Zend_Filter_Interface
  126. */
  127. protected $_filter = null;
  128. /**
  129. * Number of items per page
  130. *
  131. * @var integer
  132. */
  133. protected $_itemCountPerPage = null;
  134. /**
  135. * Number of pages
  136. *
  137. * @var integer
  138. */
  139. protected $_pageCount = null;
  140. /**
  141. * Number of local pages (i.e., the number of discrete page numbers
  142. * that will be displayed, including the current page number)
  143. *
  144. * @var integer
  145. */
  146. protected $_pageRange = null;
  147. /**
  148. * Pages
  149. *
  150. * @var array
  151. */
  152. protected $_pages = null;
  153. /**
  154. * View instance used for self rendering
  155. *
  156. * @var Zend_View_Interface
  157. */
  158. protected $_view = null;
  159. /**
  160. * Adds an adapter prefix path to the plugin loader.
  161. *
  162. * @param string $prefix
  163. * @param string $path
  164. */
  165. public static function addAdapterPrefixPath($prefix, $path)
  166. {
  167. self::getAdapterLoader()->addPrefixPath($prefix, $path);
  168. }
  169. /**
  170. * Adds an array of adapter prefix paths to the plugin
  171. * loader.
  172. *
  173. * <code>
  174. * $prefixPaths = array(
  175. * 'My_Paginator_Adapter' => 'My/Paginator/Adapter/',
  176. * 'Your_Paginator_Adapter' => 'Your/Paginator/Adapter/'
  177. * );
  178. * </code>
  179. *
  180. * @param array $prefixPaths
  181. */
  182. public static function addAdapterPrefixPaths(array $prefixPaths)
  183. {
  184. if (isset($prefixPaths['prefix']) && isset($prefixPaths['path'])) {
  185. self::addAdapterPrefixPath($prefixPaths['prefix'], $prefixPaths['path']);
  186. } else {
  187. foreach ($prefixPaths as $prefix => $path) {
  188. if (is_array($path) && isset($path['prefix']) && isset($path['path'])) {
  189. $prefix = $path['prefix'];
  190. $path = $path['path'];
  191. }
  192. self::addAdapterPrefixPath($prefix, $path);
  193. }
  194. }
  195. }
  196. /**
  197. * Adds a scrolling style prefix path to the plugin loader.
  198. *
  199. * @param string $prefix
  200. * @param string $path
  201. */
  202. public static function addScrollingStylePrefixPath($prefix, $path)
  203. {
  204. self::getScrollingStyleLoader()->addPrefixPath($prefix, $path);
  205. }
  206. /**
  207. * Adds an array of scrolling style prefix paths to the plugin
  208. * loader.
  209. *
  210. * <code>
  211. * $prefixPaths = array(
  212. * 'My_Paginator_ScrollingStyle' => 'My/Paginator/ScrollingStyle/',
  213. * 'Your_Paginator_ScrollingStyle' => 'Your/Paginator/ScrollingStyle/'
  214. * );
  215. * </code>
  216. *
  217. * @param array $prefixPaths
  218. */
  219. public static function addScrollingStylePrefixPaths(array $prefixPaths)
  220. {
  221. if (isset($prefixPaths['prefix']) && isset($prefixPaths['path'])) {
  222. self::addScrollingStylePrefixPath($prefixPaths['prefix'], $prefixPaths['path']);
  223. } else {
  224. foreach ($prefixPaths as $prefix => $path) {
  225. if (is_array($path) && isset($path['prefix']) && isset($path['path'])) {
  226. $prefix = $path['prefix'];
  227. $path = $path['path'];
  228. }
  229. self::addScrollingStylePrefixPath($prefix, $path);
  230. }
  231. }
  232. }
  233. /**
  234. * Factory.
  235. *
  236. * @param mixed $data
  237. * @param string $adapter
  238. * @param array $prefixPaths
  239. * @return Zend_Paginator
  240. */
  241. public static function factory($data, $adapter = self::INTERNAL_ADAPTER,
  242. array $prefixPaths = null)
  243. {
  244. if ($data instanceof Zend_Paginator_AdapterAggregate) {
  245. return new self($data->getPaginatorAdapter());
  246. } else {
  247. if ($adapter == self::INTERNAL_ADAPTER) {
  248. if (is_array($data)) {
  249. $adapter = 'Array';
  250. } else if ($data instanceof Zend_Db_Table_Select) {
  251. $adapter = 'DbTableSelect';
  252. } else if ($data instanceof Zend_Db_Select) {
  253. $adapter = 'DbSelect';
  254. } else if ($data instanceof Iterator) {
  255. $adapter = 'Iterator';
  256. } else if (is_integer($data)) {
  257. $adapter = 'Null';
  258. } else {
  259. $type = (is_object($data)) ? get_class($data) : gettype($data);
  260. /**
  261. * @see Zend_Paginator_Exception
  262. */
  263. require_once 'Zend/Paginator/Exception.php';
  264. throw new Zend_Paginator_Exception('No adapter for type ' . $type);
  265. }
  266. }
  267. $pluginLoader = self::getAdapterLoader();
  268. if (null !== $prefixPaths) {
  269. foreach ($prefixPaths as $prefix => $path) {
  270. $pluginLoader->addPrefixPath($prefix, $path);
  271. }
  272. }
  273. $adapterClassName = $pluginLoader->load($adapter);
  274. return new self(new $adapterClassName($data));
  275. }
  276. }
  277. /**
  278. * Returns the adapter loader. If it doesn't exist it's created.
  279. *
  280. * @return Zend_Loader_PluginLoader
  281. */
  282. public static function getAdapterLoader()
  283. {
  284. if (self::$_adapterLoader === null) {
  285. self::$_adapterLoader = new Zend_Loader_PluginLoader(
  286. array('Zend_Paginator_Adapter' => 'Zend/Paginator/Adapter')
  287. );
  288. }
  289. return self::$_adapterLoader;
  290. }
  291. /**
  292. * Set a global config
  293. *
  294. * @param Zend_Config $config
  295. */
  296. public static function setConfig(Zend_Config $config)
  297. {
  298. self::$_config = $config;
  299. $adapterPaths = $config->get('adapterpaths');
  300. if ($adapterPaths != null) {
  301. self::addAdapterPrefixPaths($adapterPaths->adapterpath->toArray());
  302. }
  303. $prefixPaths = $config->get('prefixpaths');
  304. if ($prefixPaths != null) {
  305. self::addScrollingStylePrefixPaths($prefixPaths->prefixpath->toArray());
  306. }
  307. $scrollingStyle = $config->get('scrollingstyle');
  308. if ($scrollingStyle != null) {
  309. self::setDefaultScrollingStyle($scrollingStyle);
  310. }
  311. }
  312. /**
  313. * Returns the default scrolling style.
  314. *
  315. * @return string
  316. */
  317. public static function getDefaultScrollingStyle()
  318. {
  319. return self::$_defaultScrollingStyle;
  320. }
  321. /**
  322. * Get the default item count per page
  323. *
  324. * @return int
  325. */
  326. public static function getDefaultItemCountPerPage()
  327. {
  328. return self::$_defaultItemCountPerPage;
  329. }
  330. /**
  331. * Set the default item count per page
  332. *
  333. * @param int $count
  334. */
  335. public static function setDefaultItemCountPerPage($count)
  336. {
  337. self::$_defaultItemCountPerPage = (int) $count;
  338. }
  339. /**
  340. * Get the default page range
  341. *
  342. * @return int
  343. */
  344. public static function getDefaultPageRange()
  345. {
  346. return self::$_defaultPageRange;
  347. }
  348. /**
  349. * Set the default page range
  350. *
  351. * @param int $count
  352. */
  353. public static function setDefaultPageRange($count)
  354. {
  355. self::$_defaultPageRange = (int) $count;
  356. }
  357. /**
  358. * Sets a cache object
  359. *
  360. * @param Zend_Cache_Core $cache
  361. */
  362. public static function setCache(Zend_Cache_Core $cache)
  363. {
  364. self::$_cache = $cache;
  365. }
  366. /**
  367. * Sets the default scrolling style.
  368. *
  369. * @param string $scrollingStyle
  370. */
  371. public static function setDefaultScrollingStyle($scrollingStyle = 'Sliding')
  372. {
  373. self::$_defaultScrollingStyle = $scrollingStyle;
  374. }
  375. /**
  376. * Returns the scrolling style loader. If it doesn't exist it's
  377. * created.
  378. *
  379. * @return Zend_Loader_PluginLoader
  380. */
  381. public static function getScrollingStyleLoader()
  382. {
  383. if (self::$_scrollingStyleLoader === null) {
  384. self::$_scrollingStyleLoader = new Zend_Loader_PluginLoader(
  385. array('Zend_Paginator_ScrollingStyle' => 'Zend/Paginator/ScrollingStyle')
  386. );
  387. }
  388. return self::$_scrollingStyleLoader;
  389. }
  390. /**
  391. * Constructor.
  392. *
  393. * @param Zend_Paginator_Adapter_Interface|Zend_Paginator_AdapterAggregate $adapter
  394. */
  395. public function __construct($adapter)
  396. {
  397. if ($adapter instanceof Zend_Paginator_Adapter_Interface) {
  398. $this->_adapter = $adapter;
  399. } else if ($adapter instanceof Zend_Paginator_AdapterAggregate) {
  400. $this->_adapter = $adapter->getPaginatorAdapter();
  401. } else {
  402. /**
  403. * @see Zend_Paginator_Exception
  404. */
  405. require_once 'Zend/Paginator/Exception.php';
  406. throw new Zend_Paginator_Exception(
  407. 'Zend_Paginator only accepts instances of the type ' .
  408. 'Zend_Paginator_Adapter_Interface or Zend_Paginator_AdapterAggregate.'
  409. );
  410. }
  411. $config = self::$_config;
  412. if ($config != null) {
  413. $setupMethods = array('ItemCountPerPage', 'PageRange');
  414. foreach ($setupMethods as $setupMethod) {
  415. $value = $config->get(strtolower($setupMethod));
  416. if ($value != null) {
  417. $setupMethod = 'set' . $setupMethod;
  418. $this->$setupMethod($value);
  419. }
  420. }
  421. }
  422. }
  423. /**
  424. * Serializes the object as a string. Proxies to {@link render()}.
  425. *
  426. * @return string
  427. */
  428. public function __toString()
  429. {
  430. try {
  431. $return = $this->render();
  432. return $return;
  433. } catch (Exception $e) {
  434. trigger_error($e->getMessage(), E_USER_WARNING);
  435. }
  436. return '';
  437. }
  438. /**
  439. * Enables/Disables the cache for this instance
  440. *
  441. * @param bool $enable
  442. * @return Zend_Paginator
  443. */
  444. public function setCacheEnabled($enable)
  445. {
  446. $this->_cacheEnabled = (bool)$enable;
  447. return $this;
  448. }
  449. /**
  450. * Returns the number of pages.
  451. *
  452. * @return integer
  453. */
  454. public function count()
  455. {
  456. if (!$this->_pageCount) {
  457. $this->_pageCount = $this->_calculatePageCount();
  458. }
  459. return $this->_pageCount;
  460. }
  461. /**
  462. * Returns the total number of items available. Uses cache if caching is enabled.
  463. *
  464. * @return integer
  465. */
  466. public function getTotalItemCount()
  467. {
  468. if (!$this->_cacheEnabled()) {
  469. return count($this->getAdapter());
  470. } else {
  471. $cacheId = md5($this->_getCacheInternalId(). '_itemCount');
  472. $itemCount = self::$_cache->load($cacheId);
  473. if ($itemCount === false) {
  474. $itemCount = count($this->getAdapter());
  475. self::$_cache->save($itemCount, $cacheId, array($this->_getCacheInternalId()));
  476. }
  477. return $itemCount;
  478. }
  479. }
  480. /**
  481. * Clear the page item cache.
  482. *
  483. * @param int $pageNumber
  484. * @return Zend_Paginator
  485. */
  486. public function clearPageItemCache($pageNumber = null)
  487. {
  488. if (!$this->_cacheEnabled()) {
  489. return $this;
  490. }
  491. if (null === $pageNumber) {
  492. foreach (self::$_cache->getIdsMatchingTags(array($this->_getCacheInternalId())) as $id) {
  493. if (preg_match('|'.self::CACHE_TAG_PREFIX."(\d+)_.*|", $id, $page)) {
  494. self::$_cache->remove($this->_getCacheId($page[1]));
  495. }
  496. }
  497. } else {
  498. $cleanId = $this->_getCacheId($pageNumber);
  499. self::$_cache->remove($cleanId);
  500. }
  501. return $this;
  502. }
  503. /**
  504. * Returns the absolute item number for the specified item.
  505. *
  506. * @param integer $relativeItemNumber Relative item number
  507. * @param integer $pageNumber Page number
  508. * @return integer
  509. */
  510. public function getAbsoluteItemNumber($relativeItemNumber, $pageNumber = null)
  511. {
  512. $relativeItemNumber = $this->normalizeItemNumber($relativeItemNumber);
  513. if ($pageNumber == null) {
  514. $pageNumber = $this->getCurrentPageNumber();
  515. }
  516. $pageNumber = $this->normalizePageNumber($pageNumber);
  517. return (($pageNumber - 1) * $this->getItemCountPerPage()) + $relativeItemNumber;
  518. }
  519. /**
  520. * Returns the adapter.
  521. *
  522. * @return Zend_Paginator_Adapter_Interface
  523. */
  524. public function getAdapter()
  525. {
  526. return $this->_adapter;
  527. }
  528. /**
  529. * Returns the number of items for the current page.
  530. *
  531. * @return integer
  532. */
  533. public function getCurrentItemCount()
  534. {
  535. if ($this->_currentItemCount === null) {
  536. $this->_currentItemCount = $this->getItemCount($this->getCurrentItems());
  537. }
  538. return $this->_currentItemCount;
  539. }
  540. /**
  541. * Returns the items for the current page.
  542. *
  543. * @return Traversable
  544. */
  545. public function getCurrentItems()
  546. {
  547. if ($this->_currentItems === null) {
  548. $this->_currentItems = $this->getItemsByPage($this->getCurrentPageNumber());
  549. }
  550. return $this->_currentItems;
  551. }
  552. /**
  553. * Returns the current page number.
  554. *
  555. * @return integer
  556. */
  557. public function getCurrentPageNumber()
  558. {
  559. return $this->normalizePageNumber($this->_currentPageNumber);
  560. }
  561. /**
  562. * Sets the current page number.
  563. *
  564. * @param integer $pageNumber Page number
  565. * @return Zend_Paginator $this
  566. */
  567. public function setCurrentPageNumber($pageNumber)
  568. {
  569. $this->_currentPageNumber = (integer) $pageNumber;
  570. $this->_currentItems = null;
  571. $this->_currentItemCount = null;
  572. return $this;
  573. }
  574. /**
  575. * Get the filter
  576. *
  577. * @return Zend_Filter_Interface
  578. */
  579. public function getFilter()
  580. {
  581. return $this->_filter;
  582. }
  583. /**
  584. * Set a filter chain
  585. *
  586. * @param Zend_Filter_Interface $filter
  587. * @return Zend_Paginator
  588. */
  589. public function setFilter(Zend_Filter_Interface $filter)
  590. {
  591. $this->_filter = $filter;
  592. return $this;
  593. }
  594. /**
  595. * Returns an item from a page. The current page is used if there's no
  596. * page sepcified.
  597. *
  598. * @param integer $itemNumber Item number (1 to itemCountPerPage)
  599. * @param integer $pageNumber
  600. * @return mixed
  601. */
  602. public function getItem($itemNumber, $pageNumber = null)
  603. {
  604. if ($pageNumber == null) {
  605. $pageNumber = $this->getCurrentPageNumber();
  606. } else if ($pageNumber < 0) {
  607. $pageNumber = ($this->count() + 1) + $pageNumber;
  608. }
  609. $page = $this->getItemsByPage($pageNumber);
  610. $itemCount = $this->getItemCount($page);
  611. if ($itemCount == 0) {
  612. /**
  613. * @see Zend_Paginator_Exception
  614. */
  615. require_once 'Zend/Paginator/Exception.php';
  616. throw new Zend_Paginator_Exception('Page ' . $pageNumber . ' does not exist');
  617. }
  618. if ($itemNumber < 0) {
  619. $itemNumber = ($itemCount + 1) + $itemNumber;
  620. }
  621. $itemNumber = $this->normalizeItemNumber($itemNumber);
  622. if ($itemNumber > $itemCount) {
  623. /**
  624. * @see Zend_Paginator_Exception
  625. */
  626. require_once 'Zend/Paginator/Exception.php';
  627. throw new Zend_Paginator_Exception('Page ' . $pageNumber . ' does not'
  628. . ' contain item number ' . $itemNumber);
  629. }
  630. return $page[$itemNumber - 1];
  631. }
  632. /**
  633. * Returns the number of items per page.
  634. *
  635. * @return integer
  636. */
  637. public function getItemCountPerPage()
  638. {
  639. if (empty($this->_itemCountPerPage)) {
  640. $this->_itemCountPerPage = self::getDefaultItemCountPerPage();
  641. }
  642. return $this->_itemCountPerPage;
  643. }
  644. /**
  645. * Sets the number of items per page.
  646. *
  647. * @param integer $itemCountPerPage
  648. * @return Zend_Paginator $this
  649. */
  650. public function setItemCountPerPage($itemCountPerPage = -1)
  651. {
  652. $this->_itemCountPerPage = (integer) $itemCountPerPage;
  653. if ($this->_itemCountPerPage < 1) {
  654. $this->_itemCountPerPage = $this->getTotalItemCount();
  655. }
  656. $this->_pageCount = $this->_calculatePageCount();
  657. $this->_currentItems = null;
  658. $this->_currentItemCount = null;
  659. return $this;
  660. }
  661. /**
  662. * Returns the number of items in a collection.
  663. *
  664. * @param mixed $items Items
  665. * @return integer
  666. */
  667. public function getItemCount($items)
  668. {
  669. $itemCount = 0;
  670. if (is_array($items) || $items instanceof Countable) {
  671. $itemCount = count($items);
  672. } else { // $items is something like LimitIterator
  673. $itemCount = iterator_count($items);
  674. }
  675. return $itemCount;
  676. }
  677. /**
  678. * Returns the items for a given page.
  679. *
  680. * @return Traversable
  681. */
  682. public function getItemsByPage($pageNumber)
  683. {
  684. $pageNumber = $this->normalizePageNumber($pageNumber);
  685. if ($this->_cacheEnabled()) {
  686. $data = self::$_cache->load($this->_getCacheId($pageNumber));
  687. if ($data !== false) {
  688. return $data;
  689. }
  690. }
  691. $offset = ($pageNumber - 1) * $this->getItemCountPerPage();
  692. $items = $this->_adapter->getItems($offset, $this->getItemCountPerPage());
  693. $filter = $this->getFilter();
  694. if ($filter !== null) {
  695. $items = $filter->filter($items);
  696. }
  697. if (!$items instanceof Traversable) {
  698. $items = new ArrayIterator($items);
  699. }
  700. if ($this->_cacheEnabled()) {
  701. self::$_cache->save($items, $this->_getCacheId($pageNumber), array($this->_getCacheInternalId()));
  702. }
  703. return $items;
  704. }
  705. /**
  706. * Returns a foreach-compatible iterator.
  707. *
  708. * @return Traversable
  709. */
  710. public function getIterator()
  711. {
  712. return $this->getCurrentItems();
  713. }
  714. /**
  715. * Returns the page range (see property declaration above).
  716. *
  717. * @return integer
  718. */
  719. public function getPageRange()
  720. {
  721. if (null === $this->_pageRange) {
  722. $this->_pageRange = self::getDefaultPageRange();
  723. }
  724. return $this->_pageRange;
  725. }
  726. /**
  727. * Sets the page range (see property declaration above).
  728. *
  729. * @param integer $pageRange
  730. * @return Zend_Paginator $this
  731. */
  732. public function setPageRange($pageRange)
  733. {
  734. $this->_pageRange = (integer) $pageRange;
  735. return $this;
  736. }
  737. /**
  738. * Returns the page collection.
  739. *
  740. * @param string $scrollingStyle Scrolling style
  741. * @return array
  742. */
  743. public function getPages($scrollingStyle = null)
  744. {
  745. if ($this->_pages === null) {
  746. $this->_pages = $this->_createPages($scrollingStyle);
  747. }
  748. return $this->_pages;
  749. }
  750. /**
  751. * Returns a subset of pages within a given range.
  752. *
  753. * @param integer $lowerBound Lower bound of the range
  754. * @param integer $upperBound Upper bound of the range
  755. * @return array
  756. */
  757. public function getPagesInRange($lowerBound, $upperBound)
  758. {
  759. $lowerBound = $this->normalizePageNumber($lowerBound);
  760. $upperBound = $this->normalizePageNumber($upperBound);
  761. $pages = array();
  762. for ($pageNumber = $lowerBound; $pageNumber <= $upperBound; $pageNumber++) {
  763. $pages[$pageNumber] = $pageNumber;
  764. }
  765. return $pages;
  766. }
  767. /**
  768. * Returns the page item cache.
  769. *
  770. * @return array
  771. */
  772. public function getPageItemCache()
  773. {
  774. $data = array();
  775. if ($this->_cacheEnabled()) {
  776. foreach (self::$_cache->getIdsMatchingTags(array($this->_getCacheInternalId())) as $id) {
  777. if (preg_match('|'.self::CACHE_TAG_PREFIX."(\d+)_.*|", $id, $page)) {
  778. $data[$page[1]] = self::$_cache->load($this->_getCacheId($page[1]));
  779. }
  780. }
  781. }
  782. return $data;
  783. }
  784. /**
  785. * Retrieves the view instance. If none registered, attempts to pull f
  786. * rom ViewRenderer.
  787. *
  788. * @return Zend_View_Interface|null
  789. */
  790. public function getView()
  791. {
  792. if ($this->_view === null) {
  793. /**
  794. * @see Zend_Controller_Action_HelperBroker
  795. */
  796. require_once 'Zend/Controller/Action/HelperBroker.php';
  797. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  798. if ($viewRenderer->view === null) {
  799. $viewRenderer->initView();
  800. }
  801. $this->_view = $viewRenderer->view;
  802. }
  803. return $this->_view;
  804. }
  805. /**
  806. * Sets the view object.
  807. *
  808. * @param Zend_View_Interface $view
  809. * @return Zend_Paginator
  810. */
  811. public function setView(Zend_View_Interface $view = null)
  812. {
  813. $this->_view = $view;
  814. return $this;
  815. }
  816. /**
  817. * Brings the item number in range of the page.
  818. *
  819. * @param integer $itemNumber
  820. * @return integer
  821. */
  822. public function normalizeItemNumber($itemNumber)
  823. {
  824. $itemNumber = (integer) $itemNumber;
  825. if ($itemNumber < 1) {
  826. $itemNumber = 1;
  827. }
  828. if ($itemNumber > $this->getItemCountPerPage()) {
  829. $itemNumber = $this->getItemCountPerPage();
  830. }
  831. return $itemNumber;
  832. }
  833. /**
  834. * Brings the page number in range of the paginator.
  835. *
  836. * @param integer $pageNumber
  837. * @return integer
  838. */
  839. public function normalizePageNumber($pageNumber)
  840. {
  841. $pageNumber = (integer) $pageNumber;
  842. if ($pageNumber < 1) {
  843. $pageNumber = 1;
  844. }
  845. $pageCount = $this->count();
  846. if ($pageCount > 0 && $pageNumber > $pageCount) {
  847. $pageNumber = $pageCount;
  848. }
  849. return $pageNumber;
  850. }
  851. /**
  852. * Renders the paginator.
  853. *
  854. * @param Zend_View_Interface $view
  855. * @return string
  856. */
  857. public function render(Zend_View_Interface $view = null)
  858. {
  859. if (null !== $view) {
  860. $this->setView($view);
  861. }
  862. $view = $this->getView();
  863. return $view->paginationControl($this);
  864. }
  865. /**
  866. * Returns the items of the current page as JSON.
  867. *
  868. * @return string
  869. */
  870. public function toJson()
  871. {
  872. $currentItems = $this->getCurrentItems();
  873. if ($currentItems instanceof Zend_Db_Table_Rowset_Abstract) {
  874. return Zend_Json::encode($currentItems->toArray());
  875. } else {
  876. return Zend_Json::encode($currentItems);
  877. }
  878. }
  879. /**
  880. * Tells if there is an active cache object
  881. * and if the cache has not been desabled
  882. *
  883. * @return bool
  884. */
  885. protected function _cacheEnabled()
  886. {
  887. return ((self::$_cache !== null) && $this->_cacheEnabled);
  888. }
  889. /**
  890. * Makes an Id for the cache
  891. * Depends on the adapter object and the page number
  892. *
  893. * Used to store item in cache from that Paginator instance
  894. * and that current page
  895. *
  896. * @param int $page
  897. * @return string
  898. */
  899. protected function _getCacheId($page = null)
  900. {
  901. if ($page === null) {
  902. $page = $this->getCurrentPageNumber();
  903. }
  904. return self::CACHE_TAG_PREFIX . $page . '_' . $this->_getCacheInternalId();
  905. }
  906. /**
  907. * Get the internal cache id
  908. * Depends on the adapter and the item count per page
  909. *
  910. * Used to tag that unique Paginator instance in cache
  911. *
  912. * @return string
  913. */
  914. protected function _getCacheInternalId()
  915. {
  916. $adapter = $this->getAdapter();
  917. if (method_exists($adapter, 'getCacheIdentifier')) {
  918. return md5(serialize(array(
  919. $adapter->getCacheIdentifier(), $this->getItemCountPerPage()
  920. )));
  921. } else {
  922. return md5(serialize(array(
  923. $adapter,
  924. $this->getItemCountPerPage()
  925. )));
  926. }
  927. }
  928. /**
  929. * Calculates the page count.
  930. *
  931. * @return integer
  932. */
  933. protected function _calculatePageCount()
  934. {
  935. return (integer) ceil($this->getTotalItemCount() / $this->getItemCountPerPage());
  936. }
  937. /**
  938. * Creates the page collection.
  939. *
  940. * @param string $scrollingStyle Scrolling style
  941. * @return stdClass
  942. */
  943. protected function _createPages($scrollingStyle = null)
  944. {
  945. $pageCount = $this->count();
  946. $currentPageNumber = $this->getCurrentPageNumber();
  947. $pages = new stdClass();
  948. $pages->pageCount = $pageCount;
  949. $pages->itemCountPerPage = $this->getItemCountPerPage();
  950. $pages->first = 1;
  951. $pages->current = $currentPageNumber;
  952. $pages->last = $pageCount;
  953. // Previous and next
  954. if ($currentPageNumber - 1 > 0) {
  955. $pages->previous = $currentPageNumber - 1;
  956. }
  957. if ($currentPageNumber + 1 <= $pageCount) {
  958. $pages->next = $currentPageNumber + 1;
  959. }
  960. // Pages in range
  961. $scrollingStyle = $this->_loadScrollingStyle($scrollingStyle);
  962. $pages->pagesInRange = $scrollingStyle->getPages($this);
  963. $pages->firstPageInRange = min($pages->pagesInRange);
  964. $pages->lastPageInRange = max($pages->pagesInRange);
  965. // Item numbers
  966. if ($this->getCurrentItems() !== null) {
  967. $pages->currentItemCount = $this->getCurrentItemCount();
  968. $pages->itemCountPerPage = $this->getItemCountPerPage();
  969. $pages->totalItemCount = $this->getTotalItemCount();
  970. $pages->firstItemNumber = (($currentPageNumber - 1) * $this->getItemCountPerPage()) + 1;
  971. $pages->lastItemNumber = $pages->firstItemNumber + $pages->currentItemCount - 1;
  972. }
  973. return $pages;
  974. }
  975. /**
  976. * Loads a scrolling style.
  977. *
  978. * @param string $scrollingStyle
  979. * @return Zend_Paginator_ScrollingStyle_Interface
  980. */
  981. protected function _loadScrollingStyle($scrollingStyle = null)
  982. {
  983. if ($scrollingStyle === null) {
  984. $scrollingStyle = self::$_defaultScrollingStyle;
  985. }
  986. switch (strtolower(gettype($scrollingStyle))) {
  987. case 'object':
  988. if (!$scrollingStyle instanceof Zend_Paginator_ScrollingStyle_Interface) {
  989. /**
  990. * @see Zend_View_Exception
  991. */
  992. require_once 'Zend/View/Exception.php';
  993. throw new Zend_View_Exception('Scrolling style must implement ' .
  994. 'Zend_Paginator_ScrollingStyle_Interface');
  995. }
  996. return $scrollingStyle;
  997. case 'string':
  998. $className = self::getScrollingStyleLoader()->load($scrollingStyle);
  999. return new $className();
  1000. case 'null':
  1001. // Fall through to default case
  1002. default:
  1003. /**
  1004. * @see Zend_View_Exception
  1005. */
  1006. require_once 'Zend/View/Exception.php';
  1007. throw new Zend_View_Exception('Scrolling style must be a class ' .
  1008. 'name or object implementing Zend_Paginator_ScrollingStyle_Interface');
  1009. }
  1010. }
  1011. }