Completed
Pull Request — master (#1346)
by Timo
33:05
created

ResultPaginateController::indexAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\Widget\AbstractWidgetController;
18
use TYPO3\CMS\Core\Utility\ArrayUtility;
19
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
20
21
/**
22
 * Class ResultPaginateController
23
 *
24
 * @author Frans Saris <[email protected]>
25
 * @author Timo Hund <[email protected]>
26
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller
27
 */
28
class ResultPaginateController extends AbstractWidgetController
29
{
30
31
    /**
32
     * @var array
33
     */
34
    protected $configuration = ['insertAbove' => true, 'insertBelow' => true, 'maximumNumberOfLinks' => 10, 'addQueryStringMethod' => ''];
35
36
    /**
37
     * @var \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet
38
     */
39
    protected $resultSet;
40
41
    /**
42
     * @var int
43
     */
44
    protected $currentPage = 1;
45
46
    /**
47
     * @var int
48
     */
49
    protected $displayRangeStart;
50
51
    /**
52
     * @var int
53
     */
54
    protected $displayRangeEnd;
55
56
    /**
57
     * @var int
58
     */
59
    protected $maximumNumberOfLinks = 99;
60
61
    /**
62
     * @var int
63
     */
64
    protected $numberOfPages = 1;
65
66
    /**
67
     * @return void
68
     */
69
    public function initializeAction()
70
    {
71
        $this->resultSet = $this->widgetConfiguration['resultSet'];
72
73
        ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], false);
74
        $this->configuration['itemsPerPage'] = (int)$this->resultSet->getUsedSearch()->getQuery()->getResultsPerPage();
75
        $this->numberOfPages = (int)ceil($this->resultSet->getUsedSearch()->getNumberOfResults() / $this->configuration['itemsPerPage']);
76
        $this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks'];
77
    }
78
79
    /**
80
     * @param \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext $controllerContext
81
     * @return \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext
82
     */
83
    protected function setActiveSearchResultSet($controllerContext) {
84
        $controllerContext->setSearchResultSet($this->resultSet);
85
        return $controllerContext;
86
    }
87
88
    /**
89
     * @return void
90
     */
91
    public function indexAction()
92
    {
93
        // set current page
94
        $this->currentPage = $this->resultSet->getUsedPage() + 1;
95
        if ($this->currentPage < 1) {
96
            $this->currentPage = 1;
97
        }
98
        $this->view->assign('contentArguments', [$this->widgetConfiguration['as'] => $this->getDocuments(), 'pagination' => $this->buildPagination()]);
99
        $this->view->assign('configuration', $this->configuration);
100
        $this->view->assign('resultSet', $this->resultSet);
101
    }
102
103
    /**
104
     * If a certain number of links should be displayed, adjust before and after
105
     * amounts accordingly.
106
     *
107
     * @return void
108
     */
109
    protected function calculateDisplayRange()
110
    {
111
        $maximumNumberOfLinks = $this->maximumNumberOfLinks;
112
        if ($maximumNumberOfLinks > $this->numberOfPages) {
113
            $maximumNumberOfLinks = $this->numberOfPages;
114
        }
115
        $delta = floor($maximumNumberOfLinks / 2);
116
        $this->displayRangeStart = $this->currentPage - $delta;
0 ignored issues
show
Documentation Bug introduced by
The property $displayRangeStart was declared of type integer, but $this->currentPage - $delta is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
117
        $this->displayRangeEnd = $this->currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
0 ignored issues
show
Documentation Bug introduced by
The property $displayRangeEnd was declared of type integer, but $this->currentPage + $de...inks % 2 === 0 ? 1 : 0) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
118
        if ($this->displayRangeStart < 1) {
119
            $this->displayRangeEnd -= $this->displayRangeStart - 1;
120
        }
121
        if ($this->displayRangeEnd > $this->numberOfPages) {
122
            $this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages;
123
        }
124
        $this->displayRangeStart = (int)max($this->displayRangeStart, 1);
125
        $this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
126
    }
127
128
    /**
129
     * Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage"
130
     *
131
     * @return array
132
     */
133
    protected function buildPagination()
134
    {
135
        $this->calculateDisplayRange();
136
        $pages = [];
137
        for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) {
138
            $pages[] = ['number' => $i, 'isCurrent' => $i === $this->currentPage];
139
        }
140
        $pagination = ['pages' => $pages, 'current' => $this->currentPage, 'numberOfPages' => $this->numberOfPages, 'displayRangeStart' => $this->displayRangeStart, 'displayRangeEnd' => $this->displayRangeEnd, 'hasLessPages' => $this->displayRangeStart > 2, 'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages];
141
        if ($this->currentPage < $this->numberOfPages) {
142
            $pagination['nextPage'] = $this->currentPage + 1;
143
        }
144
        if ($this->currentPage > 1) {
145
            $pagination['previousPage'] = $this->currentPage - 1;
146
        }
147
        return $pagination;
148
    }
149
150
    /**
151
     * @return \Apache_Solr_Document[]
152
     */
153
    protected function getDocuments()
154
    {
155
        $extbaseFrameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
156
        if (!empty($extbaseFrameworkConfiguration['features']['useRawDocuments'])) {
157
            return $this->resultSet->getUsedSearch()->getResultDocumentsRaw();
158
        } else {
159
            return $this->resultSet->getUsedSearch()->getResultDocumentsEscaped();
160
        }
161
    }
162
}
163