Paginator.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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 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']) && 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)
  616. {
  617. $this->_itemCountPerPage = (integer) $itemCountPerPage;
  618. if ($this->_itemCountPerPage < 1) {
  619. $this->_itemCountPerPage = $this->getItemCountPerPage();
  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. 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. if ($pageNumber < 1) {
  803. $pageNumber = 1;
  804. }
  805. $pageCount = $this->count();
  806. if ($pageCount > 0 && $pageNumber > $pageCount) {
  807. $pageNumber = $pageCount;
  808. }
  809. return $pageNumber;
  810. }
  811. /**
  812. * Renders the paginator.
  813. *
  814. * @param Zend_View_Interface $view
  815. * @return string
  816. */
  817. public function render(Zend_View_Interface $view = null)
  818. {
  819. if (null !== $view) {
  820. $this->setView($view);
  821. }
  822. $view = $this->getView();
  823. return $view->paginationControl($this);
  824. }
  825. /**
  826. * Returns the items of the current page as JSON.
  827. *
  828. * @return string
  829. */
  830. public function toJson()
  831. {
  832. $currentItems = $this->getCurrentItems();
  833. if ($currentItems instanceof Zend_Db_Table_Rowset_Abstract) {
  834. return Zend_Json::encode($currentItems->toArray());
  835. } else {
  836. return Zend_Json::encode($currentItems);
  837. }
  838. }
  839. /**
  840. * Tells if there is an active cache object
  841. * and if the cache has not been desabled
  842. *
  843. * @return bool
  844. */
  845. protected function _cacheEnabled()
  846. {
  847. return ((self::$_cache !== null) && $this->_cacheEnabled);
  848. }
  849. /**
  850. * Makes an Id for the cache
  851. * Depends on the adapter object and the page number
  852. *
  853. * Used to store item in cache from that Paginator instance
  854. * and that current page
  855. *
  856. * @param int $page
  857. * @return string
  858. */
  859. protected function _getCacheId($page = null)
  860. {
  861. if ($page === null) {
  862. $page = $this->getCurrentPageNumber();
  863. }
  864. return self::CACHE_TAG_PREFIX . $page . '_' . $this->_getCacheInternalId();
  865. }
  866. /**
  867. * Get the internal cache id
  868. * Depends on the adapter and the item count per page
  869. *
  870. * Used to tag that unique Paginator instance in cache
  871. *
  872. * @return string
  873. */
  874. protected function _getCacheInternalId()
  875. {
  876. return md5(serialize($this->getAdapter()) . $this->getItemCountPerPage());
  877. }
  878. /**
  879. * Calculates the page count.
  880. *
  881. * @return integer
  882. */
  883. protected function _calculatePageCount()
  884. {
  885. return (integer) ceil($this->getAdapter()->count() / $this->getItemCountPerPage());
  886. }
  887. /**
  888. * Creates the page collection.
  889. *
  890. * @param string $scrollingStyle Scrolling style
  891. * @return stdClass
  892. */
  893. protected function _createPages($scrollingStyle = null)
  894. {
  895. $pageCount = $this->count();
  896. $currentPageNumber = $this->getCurrentPageNumber();
  897. $pages = new stdClass();
  898. $pages->pageCount = $pageCount;
  899. $pages->itemCountPerPage = $this->getItemCountPerPage();
  900. $pages->first = 1;
  901. $pages->current = $currentPageNumber;
  902. $pages->last = $pageCount;
  903. // Previous and next
  904. if ($currentPageNumber - 1 > 0) {
  905. $pages->previous = $currentPageNumber - 1;
  906. }
  907. if ($currentPageNumber + 1 <= $pageCount) {
  908. $pages->next = $currentPageNumber + 1;
  909. }
  910. // Pages in range
  911. $scrollingStyle = $this->_loadScrollingStyle($scrollingStyle);
  912. $pages->pagesInRange = $scrollingStyle->getPages($this);
  913. $pages->firstPageInRange = min($pages->pagesInRange);
  914. $pages->lastPageInRange = max($pages->pagesInRange);
  915. // Item numbers
  916. if ($this->getCurrentItems() !== null) {
  917. $pages->currentItemCount = $this->getCurrentItemCount();
  918. $pages->itemCountPerPage = $this->getItemCountPerPage();
  919. $pages->totalItemCount = $this->getTotalItemCount();
  920. $pages->firstItemNumber = (($currentPageNumber - 1) * $this->getItemCountPerPage()) + 1;
  921. $pages->lastItemNumber = $pages->firstItemNumber + $pages->currentItemCount - 1;
  922. }
  923. return $pages;
  924. }
  925. /**
  926. * Loads a scrolling style.
  927. *
  928. * @param string $scrollingStyle
  929. * @return Zend_Paginator_ScrollingStyle_Interface
  930. */
  931. protected function _loadScrollingStyle($scrollingStyle = null)
  932. {
  933. if ($scrollingStyle === null) {
  934. $scrollingStyle = self::$_defaultScrollingStyle;
  935. }
  936. switch (strtolower(gettype($scrollingStyle))) {
  937. case 'object':
  938. if (!$scrollingStyle instanceof Zend_Paginator_ScrollingStyle_Interface) {
  939. /**
  940. * @see Zend_View_Exception
  941. */
  942. require_once 'Zend/View/Exception.php';
  943. throw new Zend_View_Exception('Scrolling style must implement ' .
  944. 'Zend_Paginator_ScrollingStyle_Interface');
  945. }
  946. return $scrollingStyle;
  947. case 'string':
  948. $className = self::getScrollingStyleLoader()->load($scrollingStyle);
  949. return new $className();
  950. case 'null':
  951. // Fall through to default case
  952. default:
  953. /**
  954. * @see Zend_View_Exception
  955. */
  956. require_once 'Zend/View/Exception.php';
  957. throw new Zend_View_Exception('Scrolling style must be a class ' .
  958. 'name or object implementing Zend_Paginator_ScrollingStyle_Interface');
  959. }
  960. }
  961. }