Completed
Push — master ( 694264...44bc0a )
by Timo
11s
created

AbstractFacetRenderer::renderFacetOptions()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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\Template;
30
use ApacheSolrForTypo3\Solr\Util;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
33
34
/**
35
 * Facet renderer.
36
 *
37
 * @author Ingo Renner <[email protected]>
38
 */
39
abstract class AbstractFacetRenderer implements FacetRenderer
40
{
41
42
    /**
43
     * @var Search
44
     */
45
    protected $search;
46
47
    /**
48
     * The name of the facet being rendered
49
     *
50
     * @var string
51
     */
52
    protected $facetName;
53
54
    /**
55
     * The facet to render.
56
     *
57
     * @var Facet
58
     */
59
    protected $facet;
60
61
    /**
62
     * @var array
63
     */
64
    protected $facetConfiguration;
65
66
    /**
67
     * @var array
68
     */
69
    protected $solrConfiguration;
70
71
    /**
72
     * Template
73
     *
74
     * @var Template
75
     */
76
    protected $template = null;
77
78
    /**
79
     * Link target page id.
80
     *
81
     * @var int
82
     */
83
    protected $linkTargetPageId = 0;
84
85
    /**
86
     * Query link builder
87
     *
88
     * @var LinkBuilder
89
     */
90
    protected $queryLinkBuilder;
91
92
    /**
93
     * Constructor.
94
     *
95
     * @param Facet $facet The facet to render.
96
     */
97 18
    public function __construct(Facet $facet)
98
    {
99 18
        $this->search = GeneralUtility::makeInstance(Search::class);
100
101 18
        $this->facet = $facet;
102 18
        $this->facetName = $facet->getName();
103
104 18
        $this->solrConfiguration = Util::getSolrConfiguration();
0 ignored issues
show
Documentation Bug introduced by
It seems like \ApacheSolrForTypo3\Solr...:getSolrConfiguration() of type object<ApacheSolrForTypo...ypoScriptConfiguration> is incompatible with the declared type array of property $solrConfiguration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105 18
        $this->facetConfiguration = $this->solrConfiguration->getSearchFacetingFacetByName($this->facetName);
106
107 18
        $this->linkTargetPageId = $GLOBALS['TSFE']->id;
108
109 18
        $this->queryLinkBuilder = GeneralUtility::makeInstance(LinkBuilder::class, $this->search->getQuery());
110 18
    }
111
112
    /**
113
     * Renders the complete facet.
114
     *
115
     * @return string Facet markup.
116
     */
117 18
    public function renderFacet()
118
    {
119
        // TODO must check whether $this->template is set
120
121 18
        $facetContent = '';
122
123 18
        $showEmptyFacets = $this->solrConfiguration->getSearchFacetingShowEmptyFacetsByName($this->facetName);
0 ignored issues
show
Bug introduced by
The method getSearchFacetingShowEmptyFacetsByName cannot be called on $this->solrConfiguration (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
124
125
        // if the facet doesn't provide any options, don't render it unless
126
        // it is configured to be rendered nevertheless
127 18
        if (!$this->facet->isEmpty() || $showEmptyFacets) {
128 18
            $facetTemplate = clone $this->template;
129 18
            $facetTemplate->workOnSubpart('single_facet');
130
131 18
            $facetOptions = $this->renderFacetOptions();
132 18
            $facetTemplate->addSubpart('single_facet_option', $facetOptions);
133
134 18
            $facet = $this->getFacetProperties();
135
136
            // remove properties irrelevant for rendering in the template engine
137
            unset(
138 18
                $facet['renderingInstruction'],
139 18
                $facet['renderingInstruction.'],
140 18
                $facet[$facet['type'] . '.']
141
            );
142
143 18
            $facetTemplate->addVariable('facet', $facet);
144 18
            $facetContent = $facetTemplate->render();
145
        }
146
147 18
        return $facetContent;
148
    }
149
150
    /**
151
     * Renders a numeric range facet by providing a slider
152
     *
153
     * @return string
154
     */
155
    abstract protected function renderFacetOptions();
156
157
    /**
158
     * (non-PHPdoc)
159
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::getFacetProperties()
160
     */
161 18
    public function getFacetProperties()
162
    {
163 18
        $facet = $this->facetConfiguration;
164
165
        // TODO move these properties into ApacheSolrForTypo3\Solr\Facet\Facet and provide them via ArrayAccess interface
166
167 18
        $facet['name'] = $this->facetName;
168 18
        $facet['count'] = $this->getFacetOptionsCount();
169 18
        $facet['active'] = $this->facet->isActive() ? '1' : '0';
170 18
        $facet['empty'] = $this->facet->isEmpty() ? '1' : '0';
171 18
        $facet['reset_url'] = $this->buildResetFacetUrl();
172
173 18
        $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
174 18
        $facet['label'] = $contentObject->stdWrap(
175 18
            $this->facetConfiguration['label'],
176 18
            $this->facetConfiguration['label.']
177
        );
178
179 18
        $facet['type'] = 'default';
180 18
        if (!empty($this->facetConfiguration['type'])) {
181 18
            $facet['type'] = $this->facetConfiguration['type'];
182
        }
183
184 18
        return $facet;
185
    }
186
187
    /**
188
     * (non-PHPdoc)
189
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::getFacetOptions()
190
     */
191 18
    public function getFacetOptions()
192
    {
193 18
        return $this->facet->getOptionsRaw();
194
    }
195
196
    /**
197
     * (non-PHPdoc)
198
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::getFacetOptionsCount()
199
     */
200 18
    public function getFacetOptionsCount()
201
    {
202 18
        return $this->facet->getOptionsCount();
203
    }
204
205
    /**
206
     * (non-PHPdoc)
207
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::setTemplate()
208
     * @param Template $template
209
     */
210 18
    public function setTemplate(Template $template)
211
    {
212 18
        $this->template = $template;
213 18
    }
214
215
    /**
216
     * (non-PHPdoc)
217
     * @see \ApacheSolrForTypo3\Solr\Facet\FacetRenderer::setLinkTargetPageId()
218
     * @param int $linkTargetPageId
219
     */
220 18
    public function setLinkTargetPageId($linkTargetPageId)
221
    {
222 18
        $this->linkTargetPageId = intval($linkTargetPageId);
223 18
        $this->queryLinkBuilder->setLinkTargetPageId($this->linkTargetPageId);
224 18
    }
225
226
    /**
227
     * Builds the URL to reset all options of a facet - removing all its applied
228
     * filters from a result set.
229
     *
230
     * @return string  Url to remove a facet
231
     */
232 18
    protected function buildResetFacetUrl()
233
    {
234 18
        $resultParameters = GeneralUtility::_GPmerged('tx_solr');
235
236 18
        if (is_array($resultParameters['filter'])) {
237
            // urldecode the array to get the original representation
238 1
            $filterParameters = array_values((array)array_map('urldecode',
239 1
                $resultParameters['filter']));
240 1
            $filterParameters = array_unique($filterParameters);
241
242
            // find and remove all options for this facet
243 1
            foreach ($filterParameters as $key => $filter) {
244 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...
245 1
                if ($filterName == $this->facetName) {
246 1
                    unset($filterParameters[$key]);
247
                }
248
            }
249 1
            $filterParameters = array_map('urlencode', $filterParameters);
250
251 1
            $resetFacetUrl = $this->queryLinkBuilder->getQueryUrl(array('filter' => $filterParameters));
252
        } else {
253 17
            $resetFacetUrl = $this->queryLinkBuilder->getQueryUrl();
254
        }
255
256 18
        return $resetFacetUrl;
257
    }
258
}
259