Completed
Push — master ( fb242c...3f99e9 )
by Timo
26:23 queued 04:18
created

AbstractFacetRenderer::getFacetOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 4
cp 0.5
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1.125
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Facet;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-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\Query\LinkBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ApacheSolrForTypo3\Solr\Facet\LinkBuilder.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
28
use ApacheSolrForTypo3\Solr\Search;
29
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
30
use ApacheSolrForTypo3\Solr\Template;
31
use ApacheSolrForTypo3\Solr\Util;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
34
35
/**
36
 * Facet renderer.
37
 *
38
 * @author Ingo Renner <[email protected]>
39
 */
40
abstract class AbstractFacetRenderer implements FacetRenderer
41
{
42
43
    /**
44
     * @var Search
45
     */
46
    protected $search;
47
48
    /**
49
     * The name of the facet being rendered
50
     *
51
     * @var string
52
     */
53
    protected $facetName;
54
55
    /**
56
     * The facet to render.
57
     *
58
     * @var Facet
59
     */
60
    protected $facet;
61
62
    /**
63
     * @var array
64
     */
65
    protected $facetConfiguration;
66
67
    /**
68
     * Configuration
69
     *
70
     * @var TypoScriptConfiguration
71
     */
72
    protected $solrConfiguration;
73
74
    /**
75
     * Template
76
     *
77
     * @var Template
78
     */
79
    protected $template = null;
80
81
    /**
82
     * Link target page id.
83
     *
84
     * @var int
85
     */
86
    protected $linkTargetPageId = 0;
87
88
    /**
89
     * Query link builder
90
     *
91
     * @var LinkBuilder
92
     */
93
    protected $queryLinkBuilder;
94
95
    /**
96
     * Constructor.
97
     *
98
     * @param Facet $facet The facet to render.
99
     */
100 18
    public function __construct(Facet $facet)
101
    {
102 18
        $this->search = GeneralUtility::makeInstance(Search::class);
103
104 18
        $this->facet = $facet;
105 18
        $this->facetName = $facet->getName();
106
107 18
        $this->solrConfiguration = Util::getSolrConfiguration();
108 18
        $this->facetConfiguration = $this->solrConfiguration->getSearchFacetingFacetByName($this->facetName);
109
110 18
        $this->linkTargetPageId = $GLOBALS['TSFE']->id;
111
112 18
        $this->queryLinkBuilder = GeneralUtility::makeInstance(LinkBuilder::class, $this->search->getQuery());
113 18
    }
114
115
    /**
116
     * Renders the complete facet.
117
     *
118
     * @return string Facet markup.
119
     */
120 18
    public function renderFacet()
121
    {
122
        // TODO must check whether $this->template is set
123
124 18
        $facetContent = '';
125
126 18
        $showEmptyFacets = $this->solrConfiguration->getSearchFacetingShowEmptyFacetsByName($this->facetName);
127
128
        // if the facet doesn't provide any options, don't render it unless
129
        // it is configured to be rendered nevertheless
130 18
        if (!$this->facet->isEmpty() || $showEmptyFacets) {
131 18
            $facetTemplate = clone $this->template;
132 18
            $facetTemplate->workOnSubpart('single_facet');
133
134 18
            $facetOptions = $this->renderFacetOptions();
135 18
            $facetTemplate->addSubpart('single_facet_option', $facetOptions);
136
137 18
            $facet = $this->getFacetProperties();
138
139
            // remove properties irrelevant for rendering in the template engine
140
            unset(
141 18
                $facet['renderingInstruction'],
142
                $facet['renderingInstruction.'],
143 18
                $facet[$facet['type'] . '.']
144
            );
145
146 18
            $facetTemplate->addVariable('facet', $facet);
147 18
            $facetContent = $facetTemplate->render();
148 18
        }
149
150 18
        return $facetContent;
151
    }
152
153
    /**
154
     * Renders a numeric range facet by providing a slider
155
     *
156
     * @return string
157
     */
158
    abstract protected function renderFacetOptions();
159
160
    /**
161
     * (non-PHPdoc)
162
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::getFacetProperties()
163
     */
164 18
    public function getFacetProperties()
165
    {
166 18
        $facet = $this->facetConfiguration;
167
168
        // TODO move these properties into ApacheSolrForTypo3\Solr\Facet\Facet and provide them via ArrayAccess interface
169
170 18
        $facet['name'] = $this->facetName;
171 18
        $facet['count'] = $this->getFacetOptionsCount();
172 18
        $facet['active'] = $this->facet->isActive() ? '1' : '0';
173 18
        $facet['empty'] = $this->facet->isEmpty() ? '1' : '0';
174 18
        $facet['reset_url'] = $this->buildResetFacetUrl();
175
176 18
        $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
177 18
        $facet['label'] = $contentObject->stdWrap(
178 18
            $this->facetConfiguration['label'],
179 18
            $this->facetConfiguration['label.']
180 18
        );
181
182 18
        $facet['type'] = 'default';
183 18
        if (!empty($this->facetConfiguration['type'])) {
184 18
            $facet['type'] = $this->facetConfiguration['type'];
185 18
        }
186
187 18
        return $facet;
188
    }
189
190
    /**
191
     * (non-PHPdoc)
192
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::getFacetOptions()
193
     */
194 18
    public function getFacetOptions()
195
    {
196 18
        return $this->facet->getOptionsRaw();
197
    }
198
199
    /**
200
     * (non-PHPdoc)
201
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::getFacetOptionsCount()
202
     */
203 18
    public function getFacetOptionsCount()
204
    {
205 18
        return $this->facet->getOptionsCount();
206
    }
207
208
    /**
209
     * (non-PHPdoc)
210
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::setTemplate()
211
     * @param Template $template
212
     */
213 18
    public function setTemplate(Template $template)
214
    {
215 18
        $this->template = $template;
216 18
    }
217
218
    /**
219
     * (non-PHPdoc)
220
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::setLinkTargetPageId()
221
     * @param int $linkTargetPageId
222
     */
223 18
    public function setLinkTargetPageId($linkTargetPageId)
224
    {
225 18
        $this->linkTargetPageId = intval($linkTargetPageId);
226 18
        $this->queryLinkBuilder->setLinkTargetPageId($this->linkTargetPageId);
227 18
    }
228
229
    /**
230
     * Builds the URL to reset all options of a facet - removing all its applied
231
     * filters from a result set.
232
     *
233
     * @return string  Url to remove a facet
234
     */
235 18
    protected function buildResetFacetUrl()
236
    {
237 18
        $resultParameters = GeneralUtility::_GPmerged('tx_solr');
238
239 18
        if (is_array($resultParameters['filter'])) {
240
            // urldecode the array to get the original representation
241 1
            $filterParameters = array_values((array)array_map('urldecode',
242 1
                $resultParameters['filter']));
243 1
            $filterParameters = array_unique($filterParameters);
244
245
            // find and remove all options for this facet
246 1
            foreach ($filterParameters as $key => $filter) {
247 1
                list($filterName, $filterValue) = explode(':', $filter);
0 ignored issues
show
Unused Code introduced by
The assignment to $filterValue is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
248 1
                if ($filterName == $this->facetName) {
249 1
                    unset($filterParameters[$key]);
250 1
                }
251 1
            }
252 1
            $filterParameters = array_map('urlencode', $filterParameters);
253
254 1
            $resetFacetUrl = $this->queryLinkBuilder->getQueryUrl(['filter' => $filterParameters]);
255 1
        } else {
256 17
            $resetFacetUrl = $this->queryLinkBuilder->getQueryUrl();
257
        }
258
259 18
        return $resetFacetUrl;
260
    }
261
}
262