Completed
Push — master ( 2575d6...b16bbe )
by Timo
120:07 queued 86:28
created

getResultsPerPageOptions()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3.0155

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 22
cts 25
cp 0.88
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 3
nop 0
crap 3.0155
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Plugin\Results;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Plugin\CommandPluginBase;
28
use ApacheSolrForTypo3\Solr\Plugin\PluginCommand;
29
use ApacheSolrForTypo3\Solr\Query\LinkBuilder;
30
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Results per page switch view command
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
class ResultsPerPageSwitchCommand implements PluginCommand
39
{
40
41
    /**
42
     * Parent plugin
43
     *
44
     * @var CommandPluginBase
45
     */
46
    protected $parentPlugin;
47
48
    /**
49
     * Configuration
50
     *
51
     * @var TypoScriptConfiguration
52
     */
53
    protected $configuration;
54
55
    /**
56
     * Constructor.
57
     *
58
     * @param CommandPluginBase $parentPlugin Parent plugin object.
59
     */
60 17
    public function __construct(CommandPluginBase $parentPlugin)
61
    {
62 17
        $this->parentPlugin = $parentPlugin;
63 17
        $this->configuration = $parentPlugin->typoScriptConfiguration;
64 17
    }
65
66
    /**
67
     * @return array|null
68
     */
69 17
    public function execute()
70
    {
71 17
        $markers = [];
72
73 17
        $selectOptions = $this->getResultsPerPageOptions();
74 17
        if (!empty($selectOptions)) {
75 17
            $queryLinkBuilder = GeneralUtility::makeInstance(LinkBuilder::class,
76 17
                $this->parentPlugin->getSearchResultSetService()->getSearch()->getQuery());
77 17
            $queryLinkBuilder->setLinkTargetPageId($this->parentPlugin->getLinkTargetPageId());
78
            $form = [
79 17
                'action' => $queryLinkBuilder->getQueryUrl()
80 17
            ];
81
82 17
            $markers['loop_options|option'] = $selectOptions;
83 17
            $markers['form'] = $form;
84 17
        } else {
85
            $markers = null;
86
        }
87
88 17
        return $markers;
89
    }
90
91
    /**
92
     * Generates the options for the results per page switch.
93
     *
94
     * @return array Array of results per page switch options.
95
     */
96 17
    public function getResultsPerPageOptions()
97
    {
98 17
        $resultsPerPageOptions = [];
99
100 17
        $resultsPerPageSwitchOptions = $this->configuration->getSearchResultsPerPageSwitchOptionsAsArray();
101 17
        $currentNumberOfResultsShown = $this->parentPlugin->getSearchResultSetService()->getLastResultSet()->getResultsPerPage();
102
103 17
        $queryLinkBuilder = GeneralUtility::makeInstance(LinkBuilder::class,
104 17
            $this->parentPlugin->getSearchResultSetService()->getSearch()->getQuery());
105 17
        $queryLinkBuilder->removeUnwantedUrlParameter('resultsPerPage');
106 17
        $queryLinkBuilder->setLinkTargetPageId($this->parentPlugin->getLinkTargetPageId());
107
108 17
        foreach ($resultsPerPageSwitchOptions as $option) {
109 17
            $selected = '';
110 17
            $selectedClass = '';
111
112 17
            if ($option == $currentNumberOfResultsShown) {
113 17
                $selected = ' selected="selected"';
114 17
                $selectedClass = ' class="currentNumberOfResults"';
115 17
            }
116
117 17
            $resultsPerPageOptions[] = [
118 17
                'value' => $option,
119 17
                'selected' => $selected,
120 17
                'selectedClass' => $selectedClass,
121 17
                'url' => $queryLinkBuilder->getQueryUrl(['resultsPerPage' => $option]),
122
            ];
123 17
        }
124
125 17
        return $resultsPerPageOptions;
126
    }
127
}
128