Paginator.php 29 KB

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