Extbase / Fluid: Widget Paginate ViewHelper 处理数组及ObjectStorage

来源:互联网 发布:mac的image文件夹在哪 编辑:程序博客网 时间:2024/06/16 01:39
1、typo3conf/ext/ExtensionXY/Classes/ViewHelpers/PaginateViewHelper.php

<?phpnamespace TYPO3\ExtensionXY\ViewHelpers;class PaginateViewHelper extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper {     /**     * @var \TYPO3\ExtensionXY\Controller\PaginateController     */    protected $controller;     /**     * Injection of widget controller     *      * @param \TYPO3\ExtensionXY\Controller\PaginateController $controller     * @return void     */    public function injectController(\TYPO3\ExtensionXY\Controller\PaginateController $controller) {        $this->controller = $controller;    }     /**     * The render method of widget     *     * @param mixed $objects \TYPO3\CMS\ExtBase\Persistence\QueryResultInterface,     *        \TYPO3\CMS\ExtBase\Persistence\ObjectStorage object or array     * @param string $as     * @param array $configuration     * @return string     */    public function render($objects, $as, array $configuration = array('itemsPerPage' => 10, 'insertAbove' => FALSE, 'insertBelow' => TRUE)) {        return $this->initiateSubRequest();    }}








2、typo3conf/ext/ExtensionXY/Classes/Controller/PaginateController.php

<?phpnamespace TYPO3\ExtensionXY\Controller;class PaginateController extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController {    /**     * @var array     */    protected $configuration = array('itemsPerPage' => 5, 'insertAbove' => FALSE, 'insertBelow' => TRUE, 'maximumVisiblePages' => 7);     /**     * @var array     */    protected $objects;     /**     * @var integer     */    protected $currentPage = 1;     /**     * @var integer     */    protected $numberOfPages = 1;     /**     * @var integer     */    protected $itemsPerPage = 0;     /**     * Initialize Action of the widget controller     *     * @return void     */    public function initializeAction() {        $this->objects = $this->widgetConfiguration['objects'];        $this->configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($this->configuration, $this->widgetConfiguration['configuration'], TRUE);    }     /**     * Returns the items per page     *     * @return integer the items per page     */    public function getItemsPerPage() {        return $this->itemsPerPage;    }     /**     * Sets the items per page     *     * @param integer $itemsPerPage the items per page     *     * @return void     */    public function setItemsPerPage($itemsPerPage) {        if (empty($itemsPerPage)) {            $this->itemsPerPage = (integer)$this->configuration['itemsPerPage'];        } else {            $this->itemsPerPage = $itemsPerPage;        }    }     /**     * Returns the number of pages     *     * @return integer     */    public function getNumberOfPages() {        return $this->numberOfPages;    }     /**     * Sets the number of pages     *     * @param integer $numberOfPages the number of pages     *     * @return void     */    public function setNumberOfPages($numberOfPages) {        $this->numberOfPages = $numberOfPages;    }     /**     * Returns the current page     *     * @return integer the current page     */    public function getCurrentPage() {        return $this->currentPage;    }     /**     * Sets the current page and limits it between 1 and $this->numberOfPages.     *     * @param integer $currentPage the current page     *     * @return void     */    public function setCurrentPage($currentPage) {        $this->currentPage = $currentPage;        if ($this->currentPage < 1) {            $this->currentPage = 1;        } elseif ($this->currentPage > $this->numberOfPages) {            $this->currentPage = $this->numberOfPages;        }    }     /**     * Returns all visible objects within a range, depending on itemsPerPage and the currentPage.     *     * @param \TYPO3\CMS\ExtBase\Persistence\QueryResult|array $objects the list of objects     *     * @return array<mixed> the list of visible objects     */    public function getVisibleObjects($objects) {        $i = 0;        $modifiedObjects = array();        $indexMin = $this->itemsPerPage * ($this->currentPage - 1);        $indexMax = $this->itemsPerPage * $this->currentPage - 1;         foreach ($objects as $object) {            if ($i >= $indexMin && $i <= $indexMax) {                $modifiedObjects[] = $object;            }            $i++;        }         return $modifiedObjects;    }     /**     * Index action of the widget controller     *     * @param integer $currentPage     * @param integer $itemsPerPage     *     * @return void     */    public function indexAction($currentPage = 1, $itemsPerPage = 0) {        $this->setItemsPerPage($itemsPerPage);        $this->setNumberOfPages(intval(ceil(count($this->objects) / (integer)$this->itemsPerPage)));        $this->setCurrentPage((integer)$currentPage);         $this->view->assign('contentArguments', array(            $this->widgetConfiguration['as'] => $this->getVisibleObjects($this->objects)        ));        $this->view->assign('configuration', $this->configuration);        if($this->numberOfPages >= 2) {            $this->view->assign('pagination', $this->buildPagination());                   }        $this->view->assign('itemsPerPage', $this->itemsPerPage);    }     /**     * Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage"     *     * @return array     */    protected function buildPagination() {        $sidePageCount = intval($this->configuration['maximumVisiblePages']) >> 1;         $firstPage = max($this->currentPage - $sidePageCount, 1);        $lastPage = min($firstPage + $sidePageCount * 2, $this->numberOfPages);        $firstPage = max($lastPage - $sidePageCount * 2, 1);         $pages = array();        foreach (range($firstPage, $lastPage) as $index) {            $pages[] = array(                'number' => $index,                'isCurrent' => ($index === $this->currentPage)            );        }         $pagination = array(            'pages' => $pages,            'current' => $this->currentPage,            'numberOfPages' => $this->numberOfPages,            'itemsPerPage' => $this->itemsPerPage,            'firstPage' => 1,            'lastPage' => $this->numberOfPages,            'isFirstPage' => ($this->currentPage == 1),            'isLastPage' => ($this->currentPage == $this->numberOfPages)        );         return $this->addPreviousAndNextPage($pagination);    }     /**     * Adds the nextPage and the previousPage to the pagination array     *     * @param array $pagination the pagination array which get previous and     *        next page     *     * @return array the pagination array which contains some meta data and     *         another array which are the pages     */    protected function addPreviousAndNextPage($pagination) {        if ($this->currentPage < $this->numberOfPages) {            $pagination['nextPage'] = $this->currentPage + 1;        }        if ($this->currentPage > 1) {            $pagination['previousPage'] = $this->currentPage - 1;        }         return $pagination;    }}




3、typo3conf/ext/ExtensionXY/Resources/Private/Templates/Paginate/Index.html

<f:if condition="{configuration.insertAbove}">    <f:render section="paginator" arguments="{pagination: pagination}" /></f:if> <f:renderChildren arguments="{contentArguments}" /> <f:if condition="{configuration.insertBelow}">    <f:render section="paginator" arguments="{pagination: pagination}" /></f:if> <f:section name="paginator">    <ul class="f3-widget-paginator">        <!-- First -->        <f:if condition="{f:translate(key:'pagination_first')}">            <f:if condition="{pagination.isFirstPage}">                <f:then>                    <li class="first">                        <span><f:translate key="pagination_first" /></span>                    </li>                </f:then>                <f:else>                    <li class="first">                        <f:widget.link><f:translate key="pagination_first" /></f:widget.link>                    </li>                </f:else>            </f:if>        </f:if>         <!-- Previous -->        <f:if condition="{f:translate(key:'pagination_previous')}">            <f:if condition="{pagination.previousPage}">                <f:then>                    <li class="previous">                        <f:if condition="{pagination.previousPage} > 1">                            <f:then>                                <!-- <f:translate key="previous" /> -->                                <f:widget.link arguments="{currentPage: pagination.previousPage}"><f:translate key="pagination_previous" /></f:widget.link>                            </f:then>                            <f:else>                                <f:widget.link><f:translate key="pagination_previous" /></f:widget.link>                            </f:else>                        </f:if>                    </li>                </f:then>                <f:else>                    <li class="previous">                        <span><f:translate key="pagination_previous" /></span>                    </li>                </f:else>            </f:if>        </f:if>         <!-- Numbered pages -->        <f:for each="{pagination.pages}" as="page">            <f:if condition="{page.isCurrent}">                <f:then>                    <li class="current">                        <span>{page.number}</span>                    </li>                </f:then>                <f:else>                    <li>                        <f:if condition="{page.number} > 1">                            <f:then>                                <f:widget.link arguments="{currentPage: page.number}">{page.number}</f:widget.link>                            </f:then>                            <f:else>                                <f:widget.link>{page.number}</f:widget.link>                            </f:else>                        </f:if>                    </li>                </f:else>            </f:if>        </f:for>         <!-- Next -->        <f:if condition="{f:translate(key:'pagination_next')}">            <li class="next">                <f:if condition="{pagination.nextPage}">                    <f:then>                        <f:widget.link arguments="{currentPage: pagination.nextPage}"><f:translate key="pagination_next" /></f:widget.link>                    </f:then>                    <f:else>                        <span><f:translate key="pagination_next" /></span>                    </f:else>                </f:if>            </li>        </f:if>         <!-- Last -->        <f:if condition="{f:translate(key:'pagination_last')}">            <li class="last">                <f:if condition="{pagination.isLastPage}">                    <f:then>                        <span><f:translate key="pagination_last" /></span>                    </f:then>                    <f:else>                        <f:widget.link arguments="{currentPage: pagination.lastPage}"><f:translate key="pagination_last" /></f:widget.link>                    </f:else>                </f:if>            </li>        </f:if>    </ul></f:section>





4、使用
{namespace m=TYPO3\ExtensionXY\ViewHelpers}<m:Paginate objects="{paginateObjektOderArray}" as="paginatedObjekt" configuration="{itemsPerPage: 5, insertAbove: 1, insertBelow: 1}">


0 0
原创粉丝点击