Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c2a726...77fa83 )
by Sebastian
18s queued 13s
created

ListView::getSortingForm()   F

Complexity

Conditions 16
Paths 1729

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 42
rs 1.4
cc 16
nc 1729
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Plugin;
14
15
use Kitodo\Dlf\Common\Document;
16
use Kitodo\Dlf\Common\DocumentList;
17
use Kitodo\Dlf\Common\Helper;
18
use Kitodo\Dlf\Common\Solr;
19
use TYPO3\CMS\Core\Database\ConnectionPool;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
/**
23
 * Plugin 'List View' for the 'dlf' extension
24
 *
25
 * @author Sebastian Meyer <[email protected]>
26
 * @author Henrik Lochmann <[email protected]>
27
 * @author Frank Ulrich Weber <[email protected]>
28
 * @package TYPO3
29
 * @subpackage dlf
30
 * @access public
31
 */
32
class ListView extends \Kitodo\Dlf\Common\AbstractPlugin
33
{
34
    public $scriptRelPath = 'Classes/Plugin/ListView.php';
35
36
    /**
37
     * This holds the field wrap of the metadata
38
     *
39
     * @var array
40
     * @access private
41
     */
42
    private $fieldwrap = [];
43
44
    /**
45
     * This holds the list
46
     *
47
     * @var \Kitodo\Dlf\Common\DocumentList
48
     * @access protected
49
     */
50
    protected $list;
51
52
    /**
53
     * Array of sorted metadata
54
     *
55
     * @var array
56
     * @access protected
57
     */
58
    protected $metadata = [];
59
60
    /**
61
     * Array of sortable metadata
62
     *
63
     * @var array
64
     * @access protected
65
     */
66
    protected $sortables = [];
67
68
    /**
69
     * Renders the page browser
70
     *
71
     * @access protected
72
     *
73
     * @return string The rendered page browser ready for output
74
     */
75
    protected function getPageBrowser()
76
    {
77
        // Get overall number of pages.
78
        $maxPages = intval(ceil($this->list->metadata['options']['numberOfToplevelHits'] / $this->conf['limit']));
79
        // Return empty pagebrowser if there is just one page.
80
        if ($maxPages < 2) {
81
            return '';
82
        }
83
        // Get separator.
84
        $separator = '<span class="separator">' . $this->pi_getLL('separator', ' - ', true) . '</span>';
85
        // Add link to previous page.
86
        if ($this->piVars['pointer'] > 0) {
87
            $output = $this->pi_linkTP_keepPIvars($this->pi_getLL('prevPage', '&lt;', true), ['pointer' => $this->piVars['pointer'] - 1], true) . $separator;
88
        } else {
89
            $output = '<span>' . $this->pi_getLL('prevPage', '&lt;', true) . '</span>' . $separator;
90
        }
91
        $i = 0;
92
        $skip = null;
93
        // Add links to pages.
94
        while ($i < $maxPages) {
95
            if ($i < 3 || ($i > $this->piVars['pointer'] - 3 && $i < $this->piVars['pointer'] + 3) || $i > $maxPages - 4) {
96
                if ($this->piVars['pointer'] != $i) {
97
                    $output .= $this->pi_linkTP_keepPIvars(sprintf($this->pi_getLL('page', '%d', true), $i + 1), ['pointer' => $i], true) . $separator;
98
                } else {
99
                    $output .= '<span class="active">' . sprintf($this->pi_getLL('page', '%d', true), $i + 1) . '</span>' . $separator;
100
                }
101
                $skip = true;
102
            } elseif ($skip === true) {
103
                $output .= '<span class="skip">' . $this->pi_getLL('skip', '...', true) . '</span>' . $separator;
104
                $skip = false;
105
            }
106
            $i++;
107
        }
108
        // Add link to next page.
109
        if ($this->piVars['pointer'] < $maxPages - 1) {
110
            $output .= $this->pi_linkTP_keepPIvars($this->pi_getLL('nextPage', '&gt;', true), ['pointer' => $this->piVars['pointer'] + 1], true);
111
        } else {
112
            $output .= '<span>' . $this->pi_getLL('nextPage', '&gt;', true) . '</span>';
113
        }
114
        return $output;
115
    }
116
117
    /**
118
     * Renders one entry of the list
119
     *
120
     * @access protected
121
     *
122
     * @param int $number: The number of the entry
123
     * @param string $template: Parsed template subpart
124
     *
125
     * @return string The rendered entry ready for output
126
     */
127
    protected function getEntry($number, $template)
128
    {
129
        $markerArray['###NUMBER###'] = ($this->piVars['pointer'] * $this->conf['limit']) + $number + 1;
130
        $markerArray['###METADATA###'] = '';
131
        $markerArray['###THUMBNAIL###'] = '';
132
        $markerArray['###PREVIEW###'] = '';
133
        $subpart = '';
134
        $imgAlt = '';
135
        $noTitle = $this->pi_getLL('noTitle');
136
        $metadata = $this->list[$number]['metadata'];
137
        foreach ($this->metadata as $index_name => $metaConf) {
138
            if (!empty($metadata[$index_name])) {
139
                $parsedValue = '';
140
                $fieldwrap = $this->getFieldWrap($index_name, $metaConf['wrap']);
141
                do {
142
                    $value = @array_shift($metadata[$index_name]);
143
                    // Link title to pageview.
144
                    if ($index_name == 'title') {
145
                        // Get title of parent document if needed.
146
                        if (empty($value) && $this->conf['getTitle']) {
147
                            $superiorTitle = Document::getTitle($this->list[$number]['uid'], true);
148
                            if (!empty($superiorTitle)) {
149
                                $value = '[' . $superiorTitle . ']';
150
                            }
151
                        }
152
                        // Set fake title if still not present.
153
                        if (empty($value)) {
154
                            $value = $noTitle;
155
                        }
156
                        $imgAlt = htmlspecialchars($value);
157
                        $additionalParams = [
158
                            'id' => $this->list[$number]['uid'],
159
                            'page' => $this->list[$number]['page']
160
                        ];
161
                        if (!empty($this->piVars['logicalPage'])) {
162
                            $additionalParams['logicalPage'] = $this->piVars['logicalPage'];
163
                        }
164
                        $conf = [
165
                            'useCacheHash' => 1,
166
                            'parameter' => $this->conf['targetPid'],
167
                            'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0,
168
                            'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http'],
169
                            'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', true, false)
170
                        ];
171
                        $value = $this->cObj->typoLink(htmlspecialchars($value), $conf);
172
                    } elseif ($index_name == 'owner' && !empty($value)) {
173
                        // Translate name of holding library.
174
                        $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_libraries', $this->conf['pages']));
175
                    } elseif ($index_name == 'type' && !empty($value)) {
176
                        // Translate document type.
177
                        $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_structures', $this->conf['pages']));
178
                    } elseif ($index_name == 'language' && !empty($value)) {
179
                        // Translate ISO 639 language code.
180
                        $value = htmlspecialchars(Helper::getLanguageName($value));
181
                    } elseif (!empty($value)) {
182
                        $value = htmlspecialchars($value);
183
                    }
184
                    $value = $this->cObj->stdWrap($value, $fieldwrap['value.']);
185
                    if (!empty($value)) {
186
                        $parsedValue .= $value;
187
                    }
188
                } while (count($metadata[$index_name]));
189
                if (!empty($parsedValue)) {
190
                    $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']);
191
                    $field .= $parsedValue;
192
                    $markerArray['###METADATA###'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']);
193
                }
194
            }
195
        }
196
        // Add thumbnail.
197
        if (!empty($this->list[$number]['thumbnail'])) {
198
            $markerArray['###THUMBNAIL###'] = '<img alt="' . $imgAlt . '" src="' . $this->list[$number]['thumbnail'] . '" />';
199
        }
200
        // Add preview.
201
        if (!empty($this->list[$number]['preview'])) {
202
            $markerArray['###PREVIEW###'] = $this->list[$number]['preview'];
203
        }
204
        if (!empty($this->list[$number]['subparts'])) {
205
            $subpart = $this->getSubEntries($number, $template);
206
        }
207
        // Basket button.
208
        $markerArray['###BASKETBUTTON###'] = '';
209
        if (!empty($this->conf['basketButton']) && !empty($this->conf['targetBasket'])) {
210
            $additionalParams = ['id' => $this->list[$number]['uid'], 'startpage' => $this->list[$number]['page'], 'addToBasket' => 'list'];
211
            $conf = [
212
                'useCacheHash' => 1,
213
                'parameter' => $this->conf['targetBasket'],
214
                'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0,
215
                'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http'],
216
                'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', true, false)
217
            ];
218
            $link = $this->cObj->typoLink($this->pi_getLL('addBasket', '', true), $conf);
219
            $markerArray['###BASKETBUTTON###'] = $link;
220
        }
221
        return $this->templateService->substituteMarkerArray($this->templateService->substituteSubpart($template['entry'], '###SUBTEMPLATE###', $subpart, true), $markerArray);
222
    }
223
224
    /**
225
     * Returns the parsed fieldwrap of a metadata
226
     *
227
     * @access private
228
     *
229
     * @param string $index_name: The index name of a metadata
230
     * @param string $wrap: The configured metadata wrap
231
     *
232
     * @return array The parsed fieldwrap
233
     */
234
    private function getFieldWrap($index_name, $wrap)
235
    {
236
        if (isset($this->fieldwrap[$index_name])) {
237
            return $this->fieldwrap[$index_name];
238
        } else {
239
            return $this->fieldwrap[$index_name] = $this->parseTS($wrap);
240
        }
241
    }
242
243
    /**
244
     * Renders sorting dialog
245
     *
246
     * @access protected
247
     *
248
     * @return string The rendered sorting dialog ready for output
249
     */
250
    protected function getSortingForm()
251
    {
252
        // Return nothing if there are no sortable metadata fields.
253
        if (!count($this->sortables)) {
254
            return '';
255
        }
256
        // Set class prefix.
257
        $prefix = Helper::getUnqualifiedClassName(get_class($this));
258
        // Configure @action URL for form.
259
        $linkConf = [
260
            'parameter' => $GLOBALS['TSFE']->id,
261
            'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0,
262
            'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http']
263
        ];
264
        if (!empty($this->piVars['logicalPage'])) {
265
            $linkConf['additionalParams'] = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, ['logicalPage' => $this->piVars['logicalPage']], '', true, false);
266
        }
267
        // Build HTML form.
268
        $sorting = '<form action="' . $this->cObj->typoLink_URL($linkConf) . '" method="get"><div><input type="hidden" name="id" value="' . $GLOBALS['TSFE']->id . '" />';
269
        foreach ($this->piVars as $piVar => $value) {
270
            if ($piVar != 'order' && $piVar != 'DATA' && !empty($value)) {
271
                $sorting .= '<input type="hidden" name="' . $this->prefixId . '[' . $piVar . ']" value="' . $value . '" />';
272
            }
273
        }
274
        // Select sort field.
275
        $uniqId = uniqid($prefix . '-');
276
        $sorting .= '<label for="' . $uniqId . '">' . $this->pi_getLL('orderBy', '', true) . '</label><select id="' . $uniqId . '" name="' . $this->prefixId . '[order]" onchange="javascript:this.form.submit();">';
277
        // Add relevance sorting if this is a search result list.
278
        if ($this->list->metadata['options']['source'] == 'search') {
279
            $sorting .= '<option value="score"' . (($this->list->metadata['options']['order'] == 'score') ? ' selected="selected"' : '') . '>' . $this->pi_getLL('relevance', '', true) . '</option>';
280
        }
281
        foreach ($this->sortables as $index_name => $label) {
282
            $sorting .= '<option value="' . $index_name . '"' . (($this->list->metadata['options']['order'] == $index_name) ? ' selected="selected"' : '') . '>' . htmlspecialchars($label) . '</option>';
283
        }
284
        $sorting .= '</select>';
285
        // Select sort direction.
286
        $uniqId = uniqid($prefix . '-');
287
        $sorting .= '<label for="' . $uniqId . '">' . $this->pi_getLL('direction', '', true) . '</label><select id="' . $uniqId . '" name="' . $this->prefixId . '[asc]" onchange="javascript:this.form.submit();">';
288
        $sorting .= '<option value="1" ' . ($this->list->metadata['options']['order.asc'] ? ' selected="selected"' : '') . '>' . $this->pi_getLL('direction.asc', '', true) . '</option>';
289
        $sorting .= '<option value="0" ' . (!$this->list->metadata['options']['order.asc'] ? ' selected="selected"' : '') . '>' . $this->pi_getLL('direction.desc', '', true) . '</option>';
290
        $sorting .= '</select></div></form>';
291
        return $sorting;
292
    }
293
294
    /**
295
     * Renders all sub-entries of one entry
296
     *
297
     * @access protected
298
     *
299
     * @param int $number: The number of the entry
300
     * @param string $template: Parsed template subpart
301
     *
302
     * @return string The rendered entries ready for output
303
     */
304
    protected function getSubEntries($number, $template)
305
    {
306
        $content = '';
307
        $noTitle = $this->pi_getLL('noTitle');
308
        $highlight_word = preg_replace('/\s\s+/', ';', $this->list->metadata['searchString']);
309
        foreach ($this->list[$number]['subparts'] as $subpart) {
310
            $markerArray['###SUBMETADATA###'] = '';
311
            $markerArray['###SUBTHUMBNAIL###'] = '';
312
            $markerArray['###SUBPREVIEW###'] = '';
313
            $imgAlt = '';
314
            foreach ($this->metadata as $index_name => $metaConf) {
315
                $parsedValue = '';
316
                $fieldwrap = $this->getFieldWrap($index_name, $metaConf['wrap']);
317
                do {
318
                    $value = @array_shift($subpart['metadata'][$index_name]);
319
                    // Link title to pageview.
320
                    if ($index_name == 'title') {
321
                        // Get title of parent document if needed.
322
                        if (empty($value) && $this->conf['getTitle']) {
323
                            $superiorTitle = Document::getTitle($subpart['uid'], true);
324
                            if (!empty($superiorTitle)) {
325
                                $value = '[' . $superiorTitle . ']';
326
                            }
327
                        }
328
                        // Set fake title if still not present.
329
                        if (empty($value)) {
330
                            $value = $noTitle;
331
                        }
332
                        $imgAlt = htmlspecialchars($value);
333
                        $additionalParams = [
334
                            'id' => $subpart['uid'],
335
                            'page' => $subpart['page'],
336
                            'highlight_word' => $highlight_word
337
                        ];
338
                        if (!empty($this->piVars['logicalPage'])) {
339
                            $additionalParams['logicalPage'] = $this->piVars['logicalPage'];
340
                        }
341
                        $conf = [
342
                            // we don't want cHash in case of search parameters
343
                            'useCacheHash' => empty($this->list->metadata['searchString']) ? 1 : 0,
344
                            'parameter' => $this->conf['targetPid'],
345
                            'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0,
346
                            'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http'],
347
                            'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', true, false)
348
                        ];
349
                        $value = $this->cObj->typoLink(htmlspecialchars($value), $conf);
350
                    } elseif ($index_name == 'owner' && !empty($value)) {
351
                        // Translate name of holding library.
352
                        $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_libraries', $this->conf['pages']));
353
                    } elseif ($index_name == 'type' && !empty($value)) {
354
                        // Translate document type.
355
                        $_value = $value;
356
                        $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_structures', $this->conf['pages']));
357
                        // Add page number for single pages.
358
                        if ($_value == 'page') {
359
                            $value .= ' ' . intval($subpart['page']);
360
                        }
361
                    } elseif ($index_name == 'language' && !empty($value)) {
362
                        // Translate ISO 639 language code.
363
                        $value = htmlspecialchars(Helper::getLanguageName($value));
364
                    } elseif (!empty($value)) {
365
                        $value = htmlspecialchars($value);
366
                    }
367
                    $value = $this->cObj->stdWrap($value, $fieldwrap['value.']);
368
                    if (!empty($value)) {
369
                        $parsedValue .= $value;
370
                    }
371
                } while (is_array($subpart['metadata'][$index_name]) && count($subpart['metadata'][$index_name]) > 0);
372
                if (!empty($parsedValue)) {
373
                    $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']);
374
                    $field .= $parsedValue;
375
                    $markerArray['###SUBMETADATA###'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']);
376
                }
377
            }
378
            // Add thumbnail.
379
            if (!empty($subpart['thumbnail'])) {
380
                $markerArray['###SUBTHUMBNAIL###'] = '<img alt="' . $imgAlt . '" src="' . $subpart['thumbnail'] . '" />';
381
            }
382
            // Add preview.
383
            if (!empty($subpart['preview'])) {
384
                $markerArray['###SUBPREVIEW###'] = $subpart['preview'];
385
            }
386
            // Basket button
387
            $markerArray['###SUBBASKETBUTTON###'] = '';
388
            if (!empty($this->conf['basketButton']) && !empty($this->conf['targetBasket'])) {
389
                $additionalParams = ['id' => $this->list[$number]['uid'], 'startpage' => $subpart['page'], 'endpage' => $subpart['page'], 'logId' => $subpart['sid'], 'addToBasket' => 'subentry'];
390
                $conf = [
391
                    'useCacheHash' => 1,
392
                    'parameter' => $this->conf['targetBasket'],
393
                    'forceAbsoluteUrl' => !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0,
394
                    'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http'],
395
                    'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', true, false)
396
                ];
397
                $link = $this->cObj->typoLink($this->pi_getLL('addBasket', '', true), $conf);
398
                $markerArray['###SUBBASKETBUTTON###'] = $link;
399
            }
400
            $content .= $this->templateService->substituteMarkerArray($template['subentry'], $markerArray);
401
        }
402
        return $this->templateService->substituteSubpart($this->templateService->getSubpart($this->template, '###SUBTEMPLATE###'), '###SUBENTRY###', $content, true);
403
    }
404
405
    /**
406
     * Get metadata configuration from database
407
     *
408
     * @access protected
409
     *
410
     * @return void
411
     */
412
    protected function loadConfig()
413
    {
414
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
415
            ->getQueryBuilderForTable('tx_dlf_metadata');
416
417
        $result = $queryBuilder
418
            ->select(
419
                'tx_dlf_metadata.index_name AS index_name',
420
                'tx_dlf_metadata.wrap AS wrap',
421
                'tx_dlf_metadata.is_listed AS is_listed',
422
                'tx_dlf_metadata.is_sortable AS is_sortable'
423
            )
424
            ->from('tx_dlf_metadata')
425
            ->where(
426
                $queryBuilder->expr()->orX(
427
                    $queryBuilder->expr()->eq('tx_dlf_metadata.is_listed', 1),
428
                    $queryBuilder->expr()->eq('tx_dlf_metadata.is_sortable', 1)
429
                ),
430
                $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($this->conf['pages'])),
431
                Helper::whereExpression('tx_dlf_metadata')
432
            )
433
            ->orderBy('tx_dlf_metadata.sorting')
434
            ->execute();
435
436
        while ($resArray = $result->fetch()) {
437
            if ($resArray['is_listed']) {
438
                $this->metadata[$resArray['index_name']] = [
439
                    'wrap' => $resArray['wrap'],
440
                    'label' => Helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages'])
441
                ];
442
            }
443
            if ($resArray['is_sortable']) {
444
                $this->sortables[$resArray['index_name']] = Helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages']);
445
            }
446
        }
447
    }
448
449
    /**
450
     * The main method of the PlugIn
451
     *
452
     * @access public
453
     *
454
     * @param string $content: The PlugIn content
455
     * @param array $conf: The PlugIn configuration
456
     *
457
     * @return string The content that is displayed on the website
458
     */
459
    public function main($content, $conf)
460
    {
461
        $this->init($conf);
462
        // Don't cache the output.
463
        $this->setCache(false);
464
        // Load the list.
465
        $this->list = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(DocumentList::class);
466
        $currentEntry = $this->piVars['pointer'] * $this->conf['limit'];
467
        $lastEntry = ($this->piVars['pointer'] + 1) * $this->conf['limit'];
468
        // Check if it's a list of database records or Solr documents.
469
        if (
470
            !empty($this->list->metadata['options']['source'])
471
            && $this->list->metadata['options']['source'] == 'collection'
472
            && ((!empty($this->piVars['order']) && $this->piVars['order'] != $this->list->metadata['options']['order'])
473
                || (isset($this->piVars['asc']) && $this->piVars['asc'] != $this->list->metadata['options']['order.asc']))
474
        ) {
475
            // Order list by given field.
476
            $this->list->sort($this->piVars['order'], (bool) $this->piVars['asc']);
477
            // Update list's metadata.
478
            $listMetadata = $this->list->metadata;
479
            $listMetadata['options']['order'] = $this->piVars['order'];
480
            $listMetadata['options']['order.asc'] = (bool) $this->piVars['asc'];
481
            $this->list->metadata = $listMetadata;
482
            // Save updated list.
483
            $this->list->save();
484
            // Reset pointer.
485
            $this->piVars['pointer'] = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property piVars does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
486
        } elseif (!empty($this->list->metadata['options']['source']) && $this->list->metadata['options']['source'] == 'search') {
487
            // Update list's metadata
488
            $listMetadata = $this->list->metadata;
489
            // Sort the list if applicable.
490
            if ((!empty($this->piVars['order']) && $this->piVars['order'] != $listMetadata['options']['order'])
491
                || (isset($this->piVars['asc']) && $this->piVars['asc'] != $listMetadata['options']['order.asc'])
492
            ) {
493
                // Update list's metadata.
494
                $listMetadata['options']['params']['sort'] = [$this->piVars['order'] . "_sorting" => (bool) $this->piVars['asc'] ? 'asc' : 'desc'];
495
                $listMetadata['options']['order'] = $this->piVars['order'];
496
                $listMetadata['options']['order.asc'] = (bool) $this->piVars['asc'];
497
                // Reset pointer.
498
                $this->piVars['pointer'] = 0;
499
            }
500
            // Set some query parameters
501
            $listMetadata['options']['params']['start'] = $currentEntry;
502
            $listMetadata['options']['params']['rows'] = $this->conf['limit'];
503
            // Search only if the query params have changed.
504
            if ($listMetadata['options']['params'] != $this->list->metadata['options']['params']) {
505
                // Instantiate search object.
506
                $solr = Solr::getInstance($this->list->metadata['options']['core']);
507
                if (!$solr->ready) {
508
                    Helper::devLog('Apache Solr not available', DEVLOG_SEVERITY_ERROR);
509
                    return $content;
510
                }
511
                // Set search parameters.
512
                $solr->cPid = $listMetadata['options']['pid'];
513
                $solr->params = $listMetadata['options']['params'];
514
                // Perform search.
515
                $this->list = $solr->search();
516
            }
517
            // Add list description
518
            $listMetadata['description'] = '<p class="tx-dlf-search-numHits">' . htmlspecialchars(sprintf($this->pi_getLL('hits', ''), $this->list->metadata['options']['numberOfHits'], $this->list->metadata['options']['numberOfToplevelHits'])) . '</p>';
519
            $this->list->metadata = $listMetadata;
520
            // Save updated list.
521
            $this->list->save();
522
            $currentEntry = 0;
523
            $lastEntry = $this->conf['limit'];
524
        }
525
        // Load template file.
526
        $this->getTemplate();
527
        $subpartArray['entry'] = $this->templateService->getSubpart($this->template, '###ENTRY###');
528
        $subpartArray['subentry'] = $this->templateService->getSubpart($this->template, '###SUBENTRY###');
529
        // Set some variable defaults.
530
        if (!empty($this->piVars['pointer']) && (($this->piVars['pointer'] * $this->conf['limit']) + 1) <= $this->list->metadata['options']['numberOfToplevelHits']) {
531
            $this->piVars['pointer'] = max(intval($this->piVars['pointer']), 0);
532
        } else {
533
            $this->piVars['pointer'] = 0;
534
        }
535
        // Load metadata configuration.
536
        $this->loadConfig();
537
        for ($currentEntry, $lastEntry; $currentEntry < $lastEntry; $currentEntry++) {
538
            if (empty($this->list[$currentEntry])) {
539
                break;
540
            } else {
541
                $content .= $this->getEntry($currentEntry, $subpartArray);
542
            }
543
        }
544
        $markerArray['###LISTTITLE###'] = $this->list->metadata['label'];
545
        $markerArray['###LISTDESCRIPTION###'] = $this->list->metadata['description'];
546
        if (!empty($this->list->metadata['thumbnail'])) {
547
            $markerArray['###LISTTHUMBNAIL###'] = '<img alt="" src="' . $this->list->metadata['thumbnail'] . '" />';
548
        } else {
549
            $markerArray['###LISTTHUMBNAIL###'] = '';
550
        }
551
        if ($currentEntry) {
552
            $currentEntry = ($this->piVars['pointer'] * $this->conf['limit']) + 1;
553
            $lastEntry = ($this->piVars['pointer'] * $this->conf['limit']) + $this->conf['limit'];
554
            $markerArray['###COUNT###'] = htmlspecialchars(sprintf($this->pi_getLL('count'), $currentEntry, $lastEntry < $this->list->metadata['options']['numberOfToplevelHits'] ? $lastEntry : $this->list->metadata['options']['numberOfToplevelHits'], $this->list->metadata['options']['numberOfToplevelHits']));
555
        } else {
556
            $markerArray['###COUNT###'] = $this->pi_getLL('nohits', '', true);
557
        }
558
        $markerArray['###PAGEBROWSER###'] = $this->getPageBrowser();
559
        $markerArray['###SORTING###'] = $this->getSortingForm();
560
        $content = $this->templateService->substituteMarkerArray($this->templateService->substituteSubpart($this->template, '###ENTRY###', $content, true), $markerArray);
561
        return $this->pi_wrapInBaseClass($content);
562
    }
563
}
564