Guest User

PaginateController.php

a guest
Sep 8th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.77 KB | None | 0 0
  1. <?php
  2. namespace Emmar\MsPartners\ViewHelpers\Widget\Controller;
  3. //namespace TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller;
  4.  
  5. /*                                                                        *
  6.  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
  7.  *                                                                        *
  8.  * It is free software; you can redistribute it and/or modify it under    *
  9.  * the terms of the GNU Lesser General Public License, either version 3   *
  10.  *  of the License, or (at your option) any later version.                *
  11.  *                                                                        *
  12.  *                                                                        *
  13.  * This script is distributed in the hope that it will be useful, but     *
  14.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
  15.  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
  16.  * General Public License for more details.                               *
  17.  *                                                                        *
  18.  * You should have received a copy of the GNU Lesser General Public       *
  19.  * License along with the script.                                         *
  20.  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
  21.  *                                                                        *
  22.  * The TYPO3 project - inspiring people to share!                         *
  23.  *                                                                        */
  24. class PaginateController extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController {
  25.  
  26.     /**
  27.      * @var array
  28.      */
  29.     protected $configuration = array(
  30.             'itemsPerPage' => 10,
  31.             'insertAbove' => FALSE,
  32.             'insertBelow' => TRUE,
  33.             'maximumNumberOfLinks' => 99,
  34.             'additionalParams' => array()
  35.             );
  36.        
  37.     /**
  38.      * @var array
  39.      */
  40.     protected $filter = array(
  41.             );
  42.        
  43.     /**
  44.      * @var \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
  45.      */
  46.     protected $objects;
  47.  
  48.     /**
  49.      * @var integer
  50.      */
  51.     protected $currentPage = 1;
  52.  
  53.     /**
  54.      * @var integer
  55.      */
  56.     protected $maximumNumberOfLinks = 99;
  57.  
  58.     /**
  59.      * @var integer
  60.      */
  61.     protected $numberOfPages = 1;
  62.  
  63.     /**
  64.      * @return void
  65.      */
  66.     public function initializeAction() {
  67.         $this->objects = $this->widgetConfiguration['objects'];
  68.         $this->filter = $this->widgetConfiguration['filter'];
  69.         $this->configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($this->configuration, $this->widgetConfiguration['configuration'], TRUE);
  70.         $this->numberOfPages = ceil(count($this->objects) / (integer) $this->configuration['itemsPerPage']);
  71.         $this->maximumNumberOfLinks = (integer) $this->configuration['maximumNumberOfLinks'];
  72.     }
  73.  
  74.     /**
  75.      * @param integer $currentPage
  76.      * @return void
  77.      */
  78.     public function indexAction($currentPage = 1) {
  79.         // set current page
  80.         $this->currentPage = (integer) $currentPage;
  81.         if ($this->currentPage < 1) {
  82.             $this->currentPage = 1;
  83.         }
  84.         if ($this->currentPage > $this->numberOfPages) {
  85.             // set $modifiedObjects to NULL if the page does not exist
  86.             $modifiedObjects = NULL;
  87.         } else {
  88.             // modify query
  89.             $itemsPerPage = (integer) $this->configuration['itemsPerPage'];
  90.             $query = $this->objects->getQuery();
  91.             $query->setLimit($itemsPerPage);
  92.             if ($this->currentPage > 1) {
  93.                 $query->setOffset((integer) ($itemsPerPage * ($this->currentPage - 1)));
  94.             }
  95.             $modifiedObjects = $query->execute();
  96.         }
  97.         $this->view->assign('contentArguments', array(
  98.             $this->widgetConfiguration['as'] => $modifiedObjects
  99.         ));
  100.         $this->view->assign('configuration', $this->configuration);
  101.         $this->view->assign('filter', $this->filter);
  102.         $this->view->assign('pagination', $this->buildPagination());
  103.     }
  104.  
  105.     /**
  106.      * If a certain number of links should be displayed, adjust before and after
  107.      * amounts accordingly.
  108.      *
  109.      * @return void
  110.      */
  111.     protected function calculateDisplayRange() {
  112.         $maximumNumberOfLinks = $this->maximumNumberOfLinks;
  113.         if ($maximumNumberOfLinks > $this->numberOfPages) {
  114.             $maximumNumberOfLinks = $this->numberOfPages;
  115.         }
  116.         $delta = floor($maximumNumberOfLinks / 2);
  117.         $this->displayRangeStart = $this->currentPage - $delta;
  118.         $this->displayRangeEnd = $this->currentPage + $delta + ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
  119.         if ($this->displayRangeStart < 1) {
  120.             $this->displayRangeEnd -= $this->displayRangeStart - 1;
  121.         }
  122.         if ($this->displayRangeEnd > $this->numberOfPages) {
  123.             $this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages;
  124.         }
  125.         $this->displayRangeStart = (integer) max($this->displayRangeStart, 1);
  126.         $this->displayRangeEnd = (integer) min($this->displayRangeEnd, $this->numberOfPages);
  127.     }
  128.  
  129.     /**
  130.      * Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage"
  131.      *
  132.      * @return array
  133.      */
  134.     protected function buildPagination() {
  135.         $this->calculateDisplayRange();
  136.         $pages = array();
  137.         for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) {
  138.             $pages[] = array('number' => $i, 'isCurrent' => $i === $this->currentPage);
  139.         }
  140.         $pagination = array(
  141.             'pages' => $pages,
  142.             'current' => $this->currentPage,
  143.             'numberOfPages' => $this->numberOfPages,
  144.             'displayRangeStart' => $this->displayRangeStart,
  145.             'displayRangeEnd' => $this->displayRangeEnd,
  146.             'hasLessPages' => $this->displayRangeStart > 2,
  147.             'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages
  148.         );
  149.         if ($this->currentPage < $this->numberOfPages) {
  150.             $pagination['nextPage'] = $this->currentPage + 1;
  151.         }
  152.         if ($this->currentPage > 1) {
  153.             $pagination['previousPage'] = $this->currentPage - 1;
  154.         }
  155.         return $pagination;
  156.     }
  157.  
  158. }
  159.  
  160. ?>
Advertisement
Add Comment
Please, Sign In to add comment