File indexing completed on 2024-04-28 05:58:57

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  **/
0022 
0023 class Local_Paginator extends Zend_Paginator
0024 {
0025     /** @var  integer */
0026     protected $totalItemCount;
0027 
0028     /**
0029      * Returns the total number of items available.
0030      *
0031      * @return integer
0032      */
0033     public function getTotalItemCount()
0034     {
0035         return $this->totalItemCount;
0036     }
0037 
0038     public function setTotalItemCount($count)
0039     {
0040         $this->totalItemCount = (integer)$count;
0041         $this->_pageCount     = $this->_calculatePageCount();
0042         return $this;
0043     }
0044 
0045     /**
0046      * Returns the items for the current page.
0047      *
0048      * @return Traversable
0049      */
0050     public function getCurrentItems()
0051     {
0052         if ($this->_currentItems === null) {
0053             $this->_currentItems = $this->getItems();
0054         }
0055 
0056         return $this->_currentItems;
0057     }
0058 
0059     /**
0060      * Returns the items for a given page.
0061      *
0062      * @return Traversable
0063      */
0064     public function getItems()
0065     {
0066 
0067         $items = $this->_adapter->getItems(0, $this->getItemCountPerPage());
0068 
0069         $filter = $this->getFilter();
0070 
0071         if ($filter !== null) {
0072             $items = $filter->filter($items);
0073         }
0074 
0075         if (!$items instanceof Traversable) {
0076             $items = new ArrayIterator($items);
0077         }
0078 
0079         return $items;
0080     }
0081 
0082     /**
0083      * Factory.
0084      *
0085      * @param  mixed $data
0086      * @param string $adapter
0087      * @param  array $prefixPaths
0088      * @return Local_Paginator
0089      * @throws Zend_Loader_PluginLoader_Exception
0090      * @throws Zend_Paginator_Exception
0091      */
0092     public static function factory($data, $adapter = self::INTERNAL_ADAPTER,
0093         array $prefixPaths = null)
0094     {
0095         if ($data instanceof Zend_Paginator_AdapterAggregate) {
0096             return new self($data->getPaginatorAdapter());
0097         } else {
0098             if ($adapter == self::INTERNAL_ADAPTER) {
0099                 if (is_array($data)) {
0100                     $adapter = 'Array';
0101                 } else if ($data instanceof Zend_Db_Table_Select) {
0102                     $adapter = 'DbTableSelect';
0103                 } else if ($data instanceof Zend_Db_Select) {
0104                     $adapter = 'DbSelect';
0105                 } else if ($data instanceof Iterator) {
0106                     $adapter = 'Iterator';
0107                 } else if (is_integer($data)) {
0108                     $adapter = 'Null';
0109                 } else {
0110                     $type = (is_object($data)) ? get_class($data) : gettype($data);
0111 
0112                     /**
0113                      * @see Zend_Paginator_Exception
0114                      */
0115                     // require_once 'Zend/Paginator/Exception.php';
0116 
0117                     throw new Zend_Paginator_Exception('No adapter for type ' . $type);
0118                 }
0119             }
0120 
0121             $pluginLoader = self::getAdapterLoader();
0122 
0123             if (null !== $prefixPaths) {
0124                 foreach ($prefixPaths as $prefix => $path) {
0125                     $pluginLoader->addPrefixPath($prefix, $path);
0126                 }
0127             }
0128 
0129             $adapterClassName = $pluginLoader->load($adapter);
0130 
0131             return new self(new $adapterClassName($data));
0132         }
0133     }
0134 
0135 }