Paginator.php 29 KB

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