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.
16 * @package Zend_Paginator
17 * @copyright Copyright (c) 2005-2009 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 17631 2009-08-16 12:29:46Z norm2782 $
23 * @see Zend_Loader_PluginLoader
25 require_once 'Zend/Loader/PluginLoader.php';
30 require_once 'Zend/Json.php';
34 * @package Zend_Paginator
35 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
36 * @license http://framework.zend.com/license/new-bsd New BSD License
38 class Zend_Paginator
implements Countable
, IteratorAggregate
41 * Specifies that the factory should try to detect the proper adapter type first
45 const INTERNAL_ADAPTER
= 'Zend_Paginator_Adapter_Internal';
48 * The cache tag prefix used to namespace Paginator results in the cache
51 const CACHE_TAG_PREFIX
= 'Zend_Paginator_';
54 * Adapter plugin loader
56 * @var Zend_Loader_PluginLoader
58 protected static $_adapterLoader = null;
65 protected static $_config = null;
68 * Default scrolling style
72 protected static $_defaultScrollingStyle = 'Sliding';
75 * Default item count per page
79 protected static $_defaultItemCountPerPage = 10;
82 * Scrolling style plugin loader
84 * @var Zend_Loader_PluginLoader
86 protected static $_scrollingStyleLoader = null;
91 * @var Zend_Cache_Core
93 protected static $_cache;
96 * Enable or desable the cache by Zend_Paginator instance
100 protected $_cacheEnabled = true;
105 * @var Zend_Paginator_Adapter_Interface
107 protected $_adapter = null;
110 * Number of items in the current page
114 protected $_currentItemCount = null;
121 protected $_currentItems = null;
124 * Current page number (starting from 1)
128 protected $_currentPageNumber = 1;
133 * @var Zend_Filter_Interface
135 protected $_filter = null;
138 * Number of items per page
142 protected $_itemCountPerPage = null;
149 protected $_pageCount = null;
152 * Number of local pages (i.e., the number of discrete page numbers
153 * that will be displayed, including the current page number)
157 protected $_pageRange = 10;
164 protected $_pages = null;
167 * View instance used for self rendering
169 * @var Zend_View_Interface
171 protected $_view = null;
174 * Adds an adapter prefix path to the plugin loader.
176 * @param string $prefix
177 * @param string $path
179 public static function addAdapterPrefixPath($prefix, $path)
181 self
::getAdapterLoader()->addPrefixPath($prefix, $path);
185 * Adds an array of adapter prefix paths to the plugin
189 * $prefixPaths = array(
190 * 'My_Paginator_Adapter' => 'My/Paginator/Adapter/',
191 * 'Your_Paginator_Adapter' => 'Your/Paginator/Adapter/'
195 * @param array $prefixPaths
197 public static function addAdapterPrefixPaths(array $prefixPaths)
199 if (isset($prefixPaths['prefix']) && isset($prefixPaths['path'])) {
200 self
::addAdapterPrefixPath($prefixPaths['prefix'], $prefixPaths['path']);
202 foreach ($prefixPaths as $prefix => $path) {
203 if (is_array($path) && isset($path['prefix']) && isset($path['path'])) {
204 $prefix = $path['prefix'];
205 $path = $path['path'];
208 self
::addAdapterPrefixPath($prefix, $path);
214 * Adds a scrolling style prefix path to the plugin loader.
216 * @param string $prefix
217 * @param string $path
219 public static function addScrollingStylePrefixPath($prefix, $path)
221 self
::getScrollingStyleLoader()->addPrefixPath($prefix, $path);
225 * Adds an array of scrolling style prefix paths to the plugin
229 * $prefixPaths = array(
230 * 'My_Paginator_ScrollingStyle' => 'My/Paginator/ScrollingStyle/',
231 * 'Your_Paginator_ScrollingStyle' => 'Your/Paginator/ScrollingStyle/'
235 * @param array $prefixPaths
237 public static function addScrollingStylePrefixPaths(array $prefixPaths)
239 if (isset($prefixPaths['prefix']) && isset($prefixPaths['path'])) {
240 self
::addScrollingStylePrefixPath($prefixPaths['prefix'], $prefixPaths['path']);
242 foreach ($prefixPaths as $prefix => $path) {
243 if (is_array($path) && isset($path['prefix']) && isset($path['path'])) {
244 $prefix = $path['prefix'];
245 $path = $path['path'];
248 self
::addScrollingStylePrefixPath($prefix, $path);
257 * @param string $adapter
258 * @param array $prefixPaths
259 * @return Zend_Paginator
261 public static function factory($data, $adapter = self
::INTERNAL_ADAPTER
,
262 array $prefixPaths = null)
264 if ($data instanceof Zend_Paginator_AdapterAggregate
) {
265 return new self($data->getPaginatorAdapter());
267 if ($adapter == self
::INTERNAL_ADAPTER
) {
268 if (is_array($data)) {
270 } else if ($data instanceof Zend_Db_Table_Select
) {
271 $adapter = 'DbTableSelect';
272 } else if ($data instanceof Zend_Db_Select
) {
273 $adapter = 'DbSelect';
274 } else if ($data instanceof Iterator
) {
275 $adapter = 'Iterator';
276 } else if (is_integer($data)) {
279 $type = (is_object($data)) ?
get_class($data) : gettype($data);
282 * @see Zend_Paginator_Exception
284 require_once 'Zend/Paginator/Exception.php';
286 throw new Zend_Paginator_Exception('No adapter for type ' . $type);
290 $pluginLoader = self
::getAdapterLoader();
292 if (null !== $prefixPaths) {
293 foreach ($prefixPaths as $prefix => $path) {
294 $pluginLoader->addPrefixPath($prefix, $path);
298 $adapterClassName = $pluginLoader->load($adapter);
300 return new self(new $adapterClassName($data));
305 * Returns the adapter loader. If it doesn't exist it's created.
307 * @return Zend_Loader_PluginLoader
309 public static function getAdapterLoader()
311 if (self
::$_adapterLoader === null) {
312 self
::$_adapterLoader = new Zend_Loader_PluginLoader(
313 array('Zend_Paginator_Adapter' => 'Zend/Paginator/Adapter')
317 return self
::$_adapterLoader;
321 * Set a global config
323 * @param Zend_Config $config
325 public static function setConfig(Zend_Config
$config)
327 self
::$_config = $config;
329 $adapterPaths = $config->get('adapterpaths');
331 if ($adapterPaths != null) {
332 self
::addAdapterPrefixPaths($adapterPaths->adapterpath
->toArray());
335 $prefixPaths = $config->get('prefixpaths');
337 if ($prefixPaths != null) {
338 self
::addScrollingStylePrefixPaths($prefixPaths->prefixpath
->toArray());
341 $scrollingStyle = $config->get('scrollingstyle');
343 if ($scrollingStyle != null) {
344 self
::setDefaultScrollingStyle($scrollingStyle);
349 * Returns the default scrolling style.
353 public static function getDefaultScrollingStyle()
355 return self
::$_defaultScrollingStyle;
359 * Get the default item count per page
363 public static function getDefaultItemCountPerPage()
365 return self
::$_defaultItemCountPerPage;
369 * Set the default item count per page
373 public static function setDefaultItemCountPerPage($count)
375 self
::$_defaultItemCountPerPage = (int) $count;
379 * Sets a cache object
381 * @param Zend_Cache_Core $cache
383 public static function setCache(Zend_Cache_Core
$cache)
385 self
::$_cache = $cache;
389 * Sets the default scrolling style.
391 * @param string $scrollingStyle
393 public static function setDefaultScrollingStyle($scrollingStyle = 'Sliding')
395 self
::$_defaultScrollingStyle = $scrollingStyle;
399 * Returns the scrolling style loader. If it doesn't exist it's
402 * @return Zend_Loader_PluginLoader
404 public static function getScrollingStyleLoader()
406 if (self
::$_scrollingStyleLoader === null) {
407 self
::$_scrollingStyleLoader = new Zend_Loader_PluginLoader(
408 array('Zend_Paginator_ScrollingStyle' => 'Zend/Paginator/ScrollingStyle')
412 return self
::$_scrollingStyleLoader;
418 * @param Zend_Paginator_Adapter_Interface|Zend_Paginator_AdapterAggregate $adapter
420 public function __construct($adapter)
422 if ($adapter instanceof Zend_Paginator_Adapter_Interface
) {
423 $this->_adapter
= $adapter;
424 } else if ($adapter instanceof Zend_Paginator_AdapterAggregate
) {
425 $this->_adapter
= $adapter->getPaginatorAdapter();
428 * @see Zend_Paginator_Exception
430 require_once 'Zend/Paginator/Exception.php';
432 throw new Zend_Paginator_Exception(
433 'Zend_Paginator only accepts instances of the type ' .
434 'Zend_Paginator_Adapter_Interface or Zend_Paginator_AdapterAggregate.'
438 $config = self
::$_config;
440 if ($config != null) {
441 $setupMethods = array('ItemCountPerPage', 'PageRange');
443 foreach ($setupMethods as $setupMethod) {
444 $value = $config->get(strtolower($setupMethod));
446 if ($value != null) {
447 $setupMethod = 'set' . $setupMethod;
448 $this->$setupMethod($value);
455 * Serializes the object as a string. Proxies to {@link render()}.
459 public function __toString()
462 $return = $this->render();
464 } catch (Exception
$e) {
465 trigger_error($e->getMessage(), E_USER_WARNING
);
472 * Enables/Disables the cache for this instance
474 * @param bool $enable
475 * @return Zend_Paginator
477 public function setCacheEnabled($enable)
479 $this->_cacheEnabled
= (bool)$enable;
484 * Returns the number of pages.
488 public function count()
490 if (!$this->_pageCount
) {
491 $this->_pageCount
= $this->_calculatePageCount();
494 return $this->_pageCount
;
498 * Returns the total number of items available.
502 public function getTotalItemCount()
504 return count($this->getAdapter());
508 * Clear the page item cache.
510 * @param int $pageNumber
511 * @return Zend_Paginator
513 public function clearPageItemCache($pageNumber = null)
515 if (!$this->_cacheEnabled()) {
519 if (null === $pageNumber) {
520 $cleanTags = self
::CACHE_TAG_PREFIX
;
521 foreach (self
::$_cache->getIdsMatchingTags(array($this->_getCacheInternalId())) as $id) {
522 if (preg_match('|'.self
::CACHE_TAG_PREFIX
."(\d+)_.*|", $id, $page)) {
523 self
::$_cache->remove($this->_getCacheId($page[1]));
527 $cleanId = $this->_getCacheId($pageNumber);
528 self
::$_cache->remove($cleanId);
534 * Returns the absolute item number for the specified item.
536 * @param integer $relativeItemNumber Relative item number
537 * @param integer $pageNumber Page number
540 public function getAbsoluteItemNumber($relativeItemNumber, $pageNumber = null)
542 $relativeItemNumber = $this->normalizeItemNumber($relativeItemNumber);
544 if ($pageNumber == null) {
545 $pageNumber = $this->getCurrentPageNumber();
548 $pageNumber = $this->normalizePageNumber($pageNumber);
550 return (($pageNumber - 1) * $this->getItemCountPerPage()) +
$relativeItemNumber;
554 * Returns the adapter.
556 * @return Zend_Paginator_Adapter_Interface
558 public function getAdapter()
560 return $this->_adapter
;
564 * Returns the number of items for the current page.
568 public function getCurrentItemCount()
570 if ($this->_currentItemCount
=== null) {
571 $this->_currentItemCount
= $this->getItemCount($this->getCurrentItems());
574 return $this->_currentItemCount
;
578 * Returns the items for the current page.
580 * @return Traversable
582 public function getCurrentItems()
584 if ($this->_currentItems
=== null) {
585 $this->_currentItems
= $this->getItemsByPage($this->getCurrentPageNumber());
588 return $this->_currentItems
;
592 * Returns the current page number.
596 public function getCurrentPageNumber()
598 return $this->normalizePageNumber($this->_currentPageNumber
);
602 * Sets the current page number.
604 * @param integer $pageNumber Page number
605 * @return Zend_Paginator $this
607 public function setCurrentPageNumber($pageNumber)
609 $this->_currentPageNumber
= (integer) $pageNumber;
610 $this->_currentItems
= null;
611 $this->_currentItemCount
= null;
619 * @return Zend_Filter_Interface
621 public function getFilter()
623 return $this->_filter
;
629 * @param Zend_Filter_Interface $filter
630 * @return Zend_Paginator
632 public function setFilter(Zend_Filter_Interface
$filter)
634 $this->_filter
= $filter;
640 * Returns an item from a page. The current page is used if there's no
643 * @param integer $itemNumber Item number (1 to itemCountPerPage)
644 * @param integer $pageNumber
647 public function getItem($itemNumber, $pageNumber = null)
649 $itemNumber = $this->normalizeItemNumber($itemNumber);
651 if ($pageNumber == null) {
652 $pageNumber = $this->getCurrentPageNumber();
655 $page = $this->getItemsByPage($pageNumber);
656 $itemCount = $this->getItemCount($page);
658 if ($itemCount == 0) {
660 * @see Zend_Paginator_Exception
662 require_once 'Zend/Paginator/Exception.php';
664 throw new Zend_Paginator_Exception('Page ' . $pageNumber . ' does not exist');
667 if ($itemNumber > $itemCount) {
669 * @see Zend_Paginator_Exception
671 require_once 'Zend/Paginator/Exception.php';
673 throw new Zend_Paginator_Exception('Page ' . $pageNumber . ' does not'
674 . ' contain item number ' . $itemNumber);
677 return $page[$itemNumber - 1];
681 * Returns the number of items per page.
685 public function getItemCountPerPage()
687 if (empty($this->_itemCountPerPage
)) {
688 $this->_itemCountPerPage
= self
::getDefaultItemCountPerPage();
691 return $this->_itemCountPerPage
;
695 * Sets the number of items per page.
697 * @param integer $itemCountPerPage
698 * @return Zend_Paginator $this
700 public function setItemCountPerPage($itemCountPerPage)
702 $this->_itemCountPerPage
= (integer) $itemCountPerPage;
703 if ($this->_itemCountPerPage
< 1) {
704 $this->_itemCountPerPage
= $this->getItemCountPerPage();
706 $this->_pageCount
= $this->_calculatePageCount();
707 $this->_currentItems
= null;
708 $this->_currentItemCount
= null;
714 * Returns the number of items in a collection.
716 * @param mixed $items Items
719 public function getItemCount($items)
723 if (is_array($items) ||
$items instanceof Countable
) {
724 $itemCount = count($items);
725 } else { // $items is something like LimitIterator
726 $itemCount = iterator_count($items);
733 * Returns the items for a given page.
735 * @return Traversable
737 public function getItemsByPage($pageNumber)
739 $pageNumber = $this->normalizePageNumber($pageNumber);
741 if ($this->_cacheEnabled()) {
742 $data = self
::$_cache->load($this->_getCacheId($pageNumber));
743 if ($data !== false) {
748 $offset = ($pageNumber - 1) * $this->getItemCountPerPage();
750 $items = $this->_adapter
->getItems($offset, $this->getItemCountPerPage());
752 $filter = $this->getFilter();
754 if ($filter !== null) {
755 $items = $filter->filter($items);
758 if (!$items instanceof Traversable
) {
759 $items = new ArrayIterator($items);
762 if ($this->_cacheEnabled()) {
763 self
::$_cache->save($items, $this->_getCacheId($pageNumber), array($this->_getCacheInternalId()));
770 * Returns a foreach-compatible iterator.
772 * @return Traversable
774 public function getIterator()
776 return $this->getCurrentItems();
780 * Returns the page range (see property declaration above).
784 public function getPageRange()
786 return $this->_pageRange
;
790 * Sets the page range (see property declaration above).
792 * @param integer $pageRange
793 * @return Zend_Paginator $this
795 public function setPageRange($pageRange)
797 $this->_pageRange
= (integer) $pageRange;
803 * Returns the page collection.
805 * @param string $scrollingStyle Scrolling style
808 public function getPages($scrollingStyle = null)
810 if ($this->_pages
=== null) {
811 $this->_pages
= $this->_createPages($scrollingStyle);
814 return $this->_pages
;
818 * Returns a subset of pages within a given range.
820 * @param integer $lowerBound Lower bound of the range
821 * @param integer $upperBound Upper bound of the range
824 public function getPagesInRange($lowerBound, $upperBound)
826 $lowerBound = $this->normalizePageNumber($lowerBound);
827 $upperBound = $this->normalizePageNumber($upperBound);
831 for ($pageNumber = $lowerBound; $pageNumber <= $upperBound; $pageNumber++
) {
832 $pages[$pageNumber] = $pageNumber;
839 * Returns the page item cache.
843 public function getPageItemCache()
846 if ($this->_cacheEnabled()) {
847 foreach (self
::$_cache->getIdsMatchingTags(array($this->_getCacheInternalId())) as $id) {
848 if (preg_match('|'.self
::CACHE_TAG_PREFIX
."(\d+)_.*|", $id, $page)) {
849 $data[$page[1]] = self
::$_cache->load($this->_getCacheId($page[1]));
857 * Retrieves the view instance. If none registered, attempts to pull f
860 * @return Zend_View_Interface|null
862 public function getView()
864 if ($this->_view
=== null) {
866 * @see Zend_Controller_Action_HelperBroker
868 require_once 'Zend/Controller/Action/HelperBroker.php';
870 $viewRenderer = Zend_Controller_Action_HelperBroker
::getStaticHelper('viewRenderer');
871 if ($viewRenderer->view
=== null) {
872 $viewRenderer->initView();
874 $this->_view
= $viewRenderer->view
;
881 * Sets the view object.
883 * @param Zend_View_Interface $view
884 * @return Zend_Paginator
886 public function setView(Zend_View_Interface
$view = null)
888 $this->_view
= $view;
894 * Brings the item number in range of the page.
896 * @param integer $itemNumber
899 public function normalizeItemNumber($itemNumber)
901 if ($itemNumber < 1) {
905 if ($itemNumber > $this->getItemCountPerPage()) {
906 $itemNumber = $this->getItemCountPerPage();
913 * Brings the page number in range of the paginator.
915 * @param integer $pageNumber
918 public function normalizePageNumber($pageNumber)
920 if ($pageNumber < 1) {
924 $pageCount = $this->count();
926 if ($pageCount > 0 && $pageNumber > $pageCount) {
927 $pageNumber = $pageCount;
934 * Renders the paginator.
936 * @param Zend_View_Interface $view
939 public function render(Zend_View_Interface
$view = null)
941 if (null !== $view) {
942 $this->setView($view);
945 $view = $this->getView();
947 return $view->paginationControl($this);
951 * Returns the items of the current page as JSON.
955 public function toJson()
957 $currentItems = $this->getCurrentItems();
959 if ($currentItems instanceof Zend_Db_Table_Rowset_Abstract
) {
960 return Zend_Json
::encode($currentItems->toArray());
962 return Zend_Json
::encode($currentItems);
967 * Tells if there is an active cache object
968 * and if the cache has not been desabled
972 protected function _cacheEnabled()
974 return ((self
::$_cache !== null) && $this->_cacheEnabled
);
978 * Makes an Id for the cache
979 * Depends on the adapter object and the page number
981 * Used to store item in cache from that Paginator instance
982 * and that current page
987 protected function _getCacheId($page = null)
989 if ($page === null) {
990 $page = $this->getCurrentPageNumber();
992 return self
::CACHE_TAG_PREFIX
. $page . '_' . $this->_getCacheInternalId();
996 * Get the internal cache id
997 * Depends on the adapter and the item count per page
999 * Used to tag that unique Paginator instance in cache
1003 protected function _getCacheInternalId()
1005 return md5(serialize($this->getAdapter()) . $this->getItemCountPerPage());
1009 * Calculates the page count.
1013 protected function _calculatePageCount()
1015 return (integer) ceil($this->getAdapter()->count() / $this->getItemCountPerPage());
1019 * Creates the page collection.
1021 * @param string $scrollingStyle Scrolling style
1024 protected function _createPages($scrollingStyle = null)
1026 $pageCount = $this->count();
1027 $currentPageNumber = $this->getCurrentPageNumber();
1029 $pages = new stdClass();
1030 $pages->pageCount
= $pageCount;
1031 $pages->itemCountPerPage
= $this->getItemCountPerPage();
1033 $pages->current
= $currentPageNumber;
1034 $pages->last
= $pageCount;
1036 // Previous and next
1037 if ($currentPageNumber - 1 > 0) {
1038 $pages->previous
= $currentPageNumber - 1;
1041 if ($currentPageNumber +
1 <= $pageCount) {
1042 $pages->next
= $currentPageNumber +
1;
1046 $scrollingStyle = $this->_loadScrollingStyle($scrollingStyle);
1047 $pages->pagesInRange
= $scrollingStyle->getPages($this);
1048 $pages->firstPageInRange
= min($pages->pagesInRange
);
1049 $pages->lastPageInRange
= max($pages->pagesInRange
);
1052 if ($this->getCurrentItems() !== null) {
1053 $pages->currentItemCount
= $this->getCurrentItemCount();
1054 $pages->itemCountPerPage
= $this->getItemCountPerPage();
1055 $pages->totalItemCount
= $this->getTotalItemCount();
1056 $pages->firstItemNumber
= (($currentPageNumber - 1) * $this->getItemCountPerPage()) +
1;
1057 $pages->lastItemNumber
= $pages->firstItemNumber +
$pages->currentItemCount
- 1;
1064 * Loads a scrolling style.
1066 * @param string $scrollingStyle
1067 * @return Zend_Paginator_ScrollingStyle_Interface
1069 protected function _loadScrollingStyle($scrollingStyle = null)
1071 if ($scrollingStyle === null) {
1072 $scrollingStyle = self
::$_defaultScrollingStyle;
1075 switch (strtolower(gettype($scrollingStyle))) {
1077 if (!$scrollingStyle instanceof Zend_Paginator_ScrollingStyle_Interface
) {
1079 * @see Zend_View_Exception
1081 require_once 'Zend/View/Exception.php';
1083 throw new Zend_View_Exception('Scrolling style must implement ' .
1084 'Zend_Paginator_ScrollingStyle_Interface');
1087 return $scrollingStyle;
1090 $className = self
::getScrollingStyleLoader()->load($scrollingStyle);
1092 return new $className();
1095 // Fall through to default case
1099 * @see Zend_View_Exception
1101 require_once 'Zend/View/Exception.php';
1103 throw new Zend_View_Exception('Scrolling style must be a class ' .
1104 'name or object implementing Zend_Paginator_ScrollingStyle_Interface');