TYPO3 API  SVNRelease
PaginateController.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00025  */
00026 class Tx_Fluid_ViewHelpers_Widget_Controller_PaginateController extends Tx_Fluid_Core_Widget_AbstractWidgetController {
00027 
00028     /**
00029      * @var array
00030      */
00031     protected $configuration = array('itemsPerPage' => 10, 'insertAbove' => FALSE, 'insertBelow' => TRUE);
00032 
00033     /**
00034      * @var Tx_Extbase_Persistence_QueryResultInterface
00035      */
00036     protected $objects;
00037 
00038     /**
00039      * @var integer
00040      */
00041     protected $currentPage = 1;
00042 
00043     /**
00044      * @var integer
00045      */
00046     protected $numberOfPages = 1;
00047 
00048     /**
00049      * @return void
00050      */
00051     public function initializeAction() {
00052         $this->objects = $this->widgetConfiguration['objects'];
00053         $this->configuration = t3lib_div::array_merge_recursive_overrule($this->configuration, $this->widgetConfiguration['configuration'], TRUE);
00054         $this->numberOfPages = ceil(count($this->objects) / (integer)$this->configuration['itemsPerPage']);
00055     }
00056 
00057     /**
00058      * @param integer $currentPage
00059      * @return void
00060      */
00061     public function indexAction($currentPage = 1) {
00062             // set current page
00063         $this->currentPage = (integer)$currentPage;
00064         if ($this->currentPage < 1) {
00065             $this->currentPage = 1;
00066         } elseif ($this->currentPage > $this->numberOfPages) {
00067             $this->currentPage = $this->numberOfPages;
00068         }
00069 
00070             // modify query
00071         $itemsPerPage = (integer)$this->configuration['itemsPerPage'];
00072         $query = $this->objects->getQuery();
00073         $query->setLimit($itemsPerPage);
00074         if ($this->currentPage > 1) {
00075             $query->setOffset((integer)($itemsPerPage * ($this->currentPage - 1)));
00076         }
00077         $modifiedObjects = $query->execute();
00078 
00079         $this->view->assign('contentArguments', array(
00080             $this->widgetConfiguration['as'] => $modifiedObjects
00081         ));
00082         $this->view->assign('configuration', $this->configuration);
00083         $this->view->assign('pagination', $this->buildPagination());
00084     }
00085 
00086     /**
00087      * Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage"
00088      *
00089      * @return array
00090      */
00091     protected function buildPagination() {
00092         $pages = array();
00093         for ($i = 1; $i <= $this->numberOfPages; $i++) {
00094             $pages[] = array('number' => $i, 'isCurrent' => ($i === $this->currentPage));
00095         }
00096         $pagination = array(
00097             'pages' => $pages,
00098             'current' => $this->currentPage,
00099             'numberOfPages' => $this->numberOfPages,
00100         );
00101         if ($this->currentPage < $this->numberOfPages) {
00102             $pagination['nextPage'] = $this->currentPage + 1;
00103         }
00104         if ($this->currentPage > 1) {
00105             $pagination['previousPage'] = $this->currentPage - 1;
00106         }
00107         return $pagination;
00108     }
00109 }
00110 
00111 ?>