Paginator.php 27 KB

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