PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Paginator.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 1152 lines | 523 code | 158 blank | 471 comment | 93 complexity | 0d2e35572c6161d319347c703d8ee5c0 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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.
  463. *
  464. * @return integer
  465. */
  466. public function getTotalItemCount()
  467. {
  468. return count($this->getAdapter());
  469. }
  470. /**
  471. * Clear the page item cache.
  472. *
  473. * @param int $pageNumber
  474. * @return Zend_Paginator
  475. */
  476. public function clearPageItemCache($pageNumber = null)
  477. {
  478. if (!$this->_cacheEnabled()) {
  479. return $this;
  480. }
  481. if (null === $pageNumber) {
  482. foreach (self::$_cache->getIdsMatchingTags(array($this->_getCacheInternalId())) as $id) {
  483. if (preg_match('|'.self::CACHE_TAG_PREFIX."(\d+)_.*|", $id, $page)) {
  484. self::$_cache->remove($this->_getCacheId($page[1]));
  485. }
  486. }
  487. } else {
  488. $cleanId = $this->_getCacheId($pageNumber);
  489. self::$_cache->remove($cleanId);
  490. }
  491. return $this;
  492. }
  493. /**
  494. * Returns the absolute item number for the specified item.
  495. *
  496. * @param integer $relativeItemNumber Relative item number
  497. * @param integer $pageNumber Page number
  498. * @return integer
  499. */
  500. public function getAbsoluteItemNumber($relativeItemNumber, $pageNumber = null)
  501. {
  502. $relativeItemNumber = $this->normalizeItemNumber($relativeItemNumber);
  503. if ($pageNumber == null) {
  504. $pageNumber = $this->getCurrentPageNumber();
  505. }
  506. $pageNumber = $this->normalizePageNumber($pageNumber);
  507. return (($pageNumber - 1) * $this->getItemCountPerPage()) + $relativeItemNumber;
  508. }
  509. /**
  510. * Returns the adapter.
  511. *
  512. * @return Zend_Paginator_Adapter_Interface
  513. */
  514. public function getAdapter()
  515. {
  516. return $this->_adapter;
  517. }
  518. /**
  519. * Returns the number of items for the current page.
  520. *
  521. * @return integer
  522. */
  523. public function getCurrentItemCount()
  524. {
  525. if ($this->_currentItemCount === null) {
  526. $this->_currentItemCount = $this->getItemCount($this->getCurrentItems());
  527. }
  528. return $this->_currentItemCount;
  529. }
  530. /**
  531. * Returns the items for the current page.
  532. *
  533. * @return Traversable
  534. */
  535. public function getCurrentItems()
  536. {
  537. if ($this->_currentItems === null) {
  538. $this->_currentItems = $this->getItemsByPage($this->getCurrentPageNumber());
  539. }
  540. return $this->_currentItems;
  541. }
  542. /**
  543. * Returns the current page number.
  544. *
  545. * @return integer
  546. */
  547. public function getCurrentPageNumber()
  548. {
  549. return $this->normalizePageNumber($this->_currentPageNumber);
  550. }
  551. /**
  552. * Sets the current page number.
  553. *
  554. * @param integer $pageNumber Page number
  555. * @return Zend_Paginator $this
  556. */
  557. public function setCurrentPageNumber($pageNumber)
  558. {
  559. $this->_currentPageNumber = (integer) $pageNumber;
  560. $this->_currentItems = null;
  561. $this->_currentItemCount = null;
  562. return $this;
  563. }
  564. /**
  565. * Get the filter
  566. *
  567. * @return Zend_Filter_Interface
  568. */
  569. public function getFilter()
  570. {
  571. return $this->_filter;
  572. }
  573. /**
  574. * Set a filter chain
  575. *
  576. * @param Zend_Filter_Interface $filter
  577. * @return Zend_Paginator
  578. */
  579. public function setFilter(Zend_Filter_Interface $filter)
  580. {
  581. $this->_filter = $filter;
  582. return $this;
  583. }
  584. /**
  585. * Returns an item from a page. The current page is used if there's no
  586. * page sepcified.
  587. *
  588. * @param integer $itemNumber Item number (1 to itemCountPerPage)
  589. * @param integer $pageNumber
  590. * @return mixed
  591. */
  592. public function getItem($itemNumber, $pageNumber = null)
  593. {
  594. if ($pageNumber == null) {
  595. $pageNumber = $this->getCurrentPageNumber();
  596. } else if ($pageNumber < 0) {
  597. $pageNumber = ($this->count() + 1) + $pageNumber;
  598. }
  599. $page = $this->getItemsByPage($pageNumber);
  600. $itemCount = $this->getItemCount($page);
  601. if ($itemCount == 0) {
  602. /**
  603. * @see Zend_Paginator_Exception
  604. */
  605. require_once 'Zend/Paginator/Exception.php';
  606. throw new Zend_Paginator_Exception('Page ' . $pageNumber . ' does not exist');
  607. }
  608. if ($itemNumber < 0) {
  609. $itemNumber = ($itemCount + 1) + $itemNumber;
  610. }
  611. $itemNumber = $this->normalizeItemNumber($itemNumber);
  612. if ($itemNumber > $itemCount) {
  613. /**
  614. * @see Zend_Paginator_Exception
  615. */
  616. require_once 'Zend/Paginator/Exception.php';
  617. throw new Zend_Paginator_Exception('Page ' . $pageNumber . ' does not'
  618. . ' contain item number ' . $itemNumber);
  619. }
  620. return $page[$itemNumber - 1];
  621. }
  622. /**
  623. * Returns the number of items per page.
  624. *
  625. * @return integer
  626. */
  627. public function getItemCountPerPage()
  628. {
  629. if (empty($this->_itemCountPerPage)) {
  630. $this->_itemCountPerPage = self::getDefaultItemCountPerPage();
  631. }
  632. return $this->_itemCountPerPage;
  633. }
  634. /**
  635. * Sets the number of items per page.
  636. *
  637. * @param integer $itemCountPerPage
  638. * @return Zend_Paginator $this
  639. */
  640. public function setItemCountPerPage($itemCountPerPage = -1)
  641. {
  642. $this->_itemCountPerPage = (integer) $itemCountPerPage;
  643. if ($this->_itemCountPerPage < 1) {
  644. $this->_itemCountPerPage = $this->getTotalItemCount();
  645. }
  646. $this->_pageCount = $this->_calculatePageCount();
  647. $this->_currentItems = null;
  648. $this->_currentItemCount = null;
  649. return $this;
  650. }
  651. /**
  652. * Returns the number of items in a collection.
  653. *
  654. * @param mixed $items Items
  655. * @return integer
  656. */
  657. public function getItemCount($items)
  658. {
  659. $itemCount = 0;
  660. if (is_array($items) || $items instanceof Countable) {
  661. $itemCount = count($items);
  662. } else { // $items is something like LimitIterator
  663. $itemCount = iterator_count($items);
  664. }
  665. return $itemCount;
  666. }
  667. /**
  668. * Returns the items for a given page.
  669. *
  670. * @return Traversable
  671. */
  672. public function getItemsByPage($pageNumber)
  673. {
  674. $pageNumber = $this->normalizePageNumber($pageNumber);
  675. if ($this->_cacheEnabled()) {
  676. $data = self::$_cache->load($this->_getCacheId($pageNumber));
  677. if ($data !== false) {
  678. return $data;
  679. }
  680. }
  681. $offset = ($pageNumber - 1) * $this->getItemCountPerPage();
  682. $items = $this->_adapter->getItems($offset, $this->getItemCountPerPage());
  683. $filter = $this->getFilter();
  684. if ($filter !== null) {
  685. $items = $filter->filter($items);
  686. }
  687. if (!$items instanceof Traversable) {
  688. $items = new ArrayIterator($items);
  689. }
  690. if ($this->_cacheEnabled()) {
  691. self::$_cache->save($items, $this->_getCacheId($pageNumber), array($this->_getCacheInternalId()));
  692. }
  693. return $items;
  694. }
  695. /**
  696. * Returns a foreach-compatible iterator.
  697. *
  698. * @return Traversable
  699. */
  700. public function getIterator()
  701. {
  702. return $this->getCurrentItems();
  703. }
  704. /**
  705. * Returns the page range (see property declaration above).
  706. *
  707. * @return integer
  708. */
  709. public function getPageRange()
  710. {
  711. if (null === $this->_pageRange) {
  712. $this->_pageRange = self::getDefaultPageRange();
  713. }
  714. return $this->_pageRange;
  715. }
  716. /**
  717. * Sets the page range (see property declaration above).
  718. *
  719. * @param integer $pageRange
  720. * @return Zend_Paginator $this
  721. */
  722. public function setPageRange($pageRange)
  723. {
  724. $this->_pageRange = (integer) $pageRange;
  725. return $this;
  726. }
  727. /**
  728. * Returns the page collection.
  729. *
  730. * @param string $scrollingStyle Scrolling style
  731. * @return array
  732. */
  733. public function getPages($scrollingStyle = null)
  734. {
  735. if ($this->_pages === null) {
  736. $this->_pages = $this->_createPages($scrollingStyle);
  737. }
  738. return $this->_pages;
  739. }
  740. /**
  741. * Returns a subset of pages within a given range.
  742. *
  743. * @param integer $lowerBound Lower bound of the range
  744. * @param integer $upperBound Upper bound of the range
  745. * @return array
  746. */
  747. public function getPagesInRange($lowerBound, $upperBound)
  748. {
  749. $lowerBound = $this->normalizePageNumber($lowerBound);
  750. $upperBound = $this->normalizePageNumber($upperBound);
  751. $pages = array();
  752. for ($pageNumber = $lowerBound; $pageNumber <= $upperBound; $pageNumber++) {
  753. $pages[$pageNumber] = $pageNumber;
  754. }
  755. return $pages;
  756. }
  757. /**
  758. * Returns the page item cache.
  759. *
  760. * @return array
  761. */
  762. public function getPageItemCache()
  763. {
  764. $data = array();
  765. if ($this->_cacheEnabled()) {
  766. foreach (self::$_cache->getIdsMatchingTags(array($this->_getCacheInternalId())) as $id) {
  767. if (preg_match('|'.self::CACHE_TAG_PREFIX."(\d+)_.*|", $id, $page)) {
  768. $data[$page[1]] = self::$_cache->load($this->_getCacheId($page[1]));
  769. }
  770. }
  771. }
  772. return $data;
  773. }
  774. /**
  775. * Retrieves the view instance. If none registered, attempts to pull f
  776. * rom ViewRenderer.
  777. *
  778. * @return Zend_View_Interface|null
  779. */
  780. public function getView()
  781. {
  782. if ($this->_view === null) {
  783. /**
  784. * @see Zend_Controller_Action_HelperBroker
  785. */
  786. require_once 'Zend/Controller/Action/HelperBroker.php';
  787. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  788. if ($viewRenderer->view === null) {
  789. $viewRenderer->initView();
  790. }
  791. $this->_view = $viewRenderer->view;
  792. }
  793. return $this->_view;
  794. }
  795. /**
  796. * Sets the view object.
  797. *
  798. * @param Zend_View_Interface $view
  799. * @return Zend_Paginator
  800. */
  801. public function setView(Zend_View_Interface $view = null)
  802. {
  803. $this->_view = $view;
  804. return $this;
  805. }
  806. /**
  807. * Brings the item number in range of the page.
  808. *
  809. * @param integer $itemNumber
  810. * @return integer
  811. */
  812. public function normalizeItemNumber($itemNumber)
  813. {
  814. $itemNumber = (integer) $itemNumber;
  815. if ($itemNumber < 1) {
  816. $itemNumber = 1;
  817. }
  818. if ($itemNumber > $this->getItemCountPerPage()) {
  819. $itemNumber = $this->getItemCountPerPage();
  820. }
  821. return $itemNumber;
  822. }
  823. /**
  824. * Brings the page number in range of the paginator.
  825. *
  826. * @param integer $pageNumber
  827. * @return integer
  828. */
  829. public function normalizePageNumber($pageNumber)
  830. {
  831. $pageNumber = (integer) $pageNumber;
  832. if ($pageNumber < 1) {
  833. $pageNumber = 1;
  834. }
  835. $pageCount = $this->count();
  836. if ($pageCount > 0 && $pageNumber > $pageCount) {
  837. $pageNumber = $pageCount;
  838. }
  839. return $pageNumber;
  840. }
  841. /**
  842. * Renders the paginator.
  843. *
  844. * @param Zend_View_Interface $view
  845. * @return string
  846. */
  847. public function render(Zend_View_Interface $view = null)
  848. {
  849. if (null !== $view) {
  850. $this->setView($view);
  851. }
  852. $view = $this->getView();
  853. return $view->paginationControl($this);
  854. }
  855. /**
  856. * Returns the items of the current page as JSON.
  857. *
  858. * @return string
  859. */
  860. public function toJson()
  861. {
  862. $currentItems = $this->getCurrentItems();
  863. if ($currentItems instanceof Zend_Db_Table_Rowset_Abstract) {
  864. return Zend_Json::encode($currentItems->toArray());
  865. } else {
  866. return Zend_Json::encode($currentItems);
  867. }
  868. }
  869. /**
  870. * Tells if there is an active cache object
  871. * and if the cache has not been desabled
  872. *
  873. * @return bool
  874. */
  875. protected function _cacheEnabled()
  876. {
  877. return ((self::$_cache !== null) && $this->_cacheEnabled);
  878. }
  879. /**
  880. * Makes an Id for the cache
  881. * Depends on the adapter object and the page number
  882. *
  883. * Used to store item in cache from that Paginator instance
  884. * and that current page
  885. *
  886. * @param int $page
  887. * @return string
  888. */
  889. protected function _getCacheId($page = null)
  890. {
  891. if ($page === null) {
  892. $page = $this->getCurrentPageNumber();
  893. }
  894. return self::CACHE_TAG_PREFIX . $page . '_' . $this->_getCacheInternalId();
  895. }
  896. /**
  897. * Get the internal cache id
  898. * Depends on the adapter and the item count per page
  899. *
  900. * Used to tag that unique Paginator instance in cache
  901. *
  902. * @return string
  903. */
  904. protected function _getCacheInternalId()
  905. {
  906. return md5(serialize(array(
  907. $this->getAdapter(),
  908. $this->getItemCountPerPage()
  909. )));
  910. }
  911. /**
  912. * Calculates the page count.
  913. *
  914. * @return integer
  915. */
  916. protected function _calculatePageCount()
  917. {
  918. return (integer) ceil($this->getAdapter()->count() / $this->getItemCountPerPage());
  919. }
  920. /**
  921. * Creates the page collection.
  922. *
  923. * @param string $scrollingStyle Scrolling style
  924. * @return stdClass
  925. */
  926. protected function _createPages($scrollingStyle = null)
  927. {
  928. $pageCount = $this->count();
  929. $currentPageNumber = $this->getCurrentPageNumber();
  930. $pages = new stdClass();
  931. $pages->pageCount = $pageCount;
  932. $pages->itemCountPerPage = $this->getItemCountPerPage();
  933. $pages->first = 1;
  934. $pages->current = $currentPageNumber;
  935. $pages->last = $pageCount;
  936. // Previous and next
  937. if ($currentPageNumber - 1 > 0) {
  938. $pages->previous = $currentPageNumber - 1;
  939. }
  940. if ($currentPageNumber + 1 <= $pageCount) {
  941. $pages->next = $currentPageNumber + 1;
  942. }
  943. // Pages in range
  944. $scrollingStyle = $this->_loadScrollingStyle($scrollingStyle);
  945. $pages->pagesInRange = $scrollingStyle->getPages($this);
  946. $pages->firstPageInRange = min($pages->pagesInRange);
  947. $pages->lastPageInRange = max($pages->pagesInRange);
  948. // Item numbers
  949. if ($this->getCurrentItems() !== null) {
  950. $pages->currentItemCount = $this->getCurrentItemCount();
  951. $pages->itemCountPerPage = $this->getItemCountPerPage();
  952. $pages->totalItemCount = $this->getTotalItemCount();
  953. $pages->firstItemNumber = (($currentPageNumber - 1) * $this->getItemCountPerPage()) + 1;
  954. $pages->lastItemNumber = $pages->firstItemNumber + $pages->currentItemCount - 1;
  955. }
  956. return $pages;
  957. }
  958. /**
  959. * Loads a scrolling style.
  960. *
  961. * @param string $scrollingStyle
  962. * @return Zend_Paginator_ScrollingStyle_Interface
  963. */
  964. protected function _loadScrollingStyle($scrollingStyle = null)
  965. {
  966. if ($scrollingStyle === null) {
  967. $scrollingStyle = self::$_defaultScrollingStyle;
  968. }
  969. switch (strtolower(gettype($scrollingStyle))) {
  970. case 'object':
  971. if (!$scrollingStyle instanceof Zend_Paginator_ScrollingStyle_Interface) {
  972. /**
  973. * @see Zend_View_Exception
  974. */
  975. require_once 'Zend/View/Exception.php';
  976. throw new Zend_View_Exception('Scrolling style must implement ' .
  977. 'Zend_Paginator_ScrollingStyle_Interface');
  978. }
  979. return $scrollingStyle;
  980. case 'string':
  981. $className = self::getScrollingStyleLoader()->load($scrollingStyle);
  982. return new $className();
  983. case 'null':
  984. // Fall through to default case
  985. default:
  986. /**
  987. * @see Zend_View_Exception
  988. */
  989. require_once 'Zend/View/Exception.php';
  990. throw new Zend_View_Exception('Scrolling style must be a class ' .
  991. 'name or object implementing Zend_Paginator_ScrollingStyle_Interface');
  992. }
  993. }
  994. }