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.
Passed
Pull Request — master (#329)
by Sebastian
03:28
created

ListView::getSubEntries()   F

Complexity

Conditions 25
Paths 969

Size

Total Lines 91
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 66
nc 969
nop 2
dl 0
loc 91
rs 0.0429
c 0
b 0
f 0

How to fix   Long Method    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
namespace Kitodo\Dlf\Plugins;
3
4
/**
5
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
6
 *
7
 * This file is part of the Kitodo and TYPO3 projects.
8
 *
9
 * @license GNU General Public License version 3 or later.
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 */
13
14
use Kitodo\Dlf\Common\Document;
15
use Kitodo\Dlf\Common\DocumentList;
16
use Kitodo\Dlf\Common\Helper;
17
use Kitodo\Dlf\Common\Solr;
18
19
/**
20
 * Plugin 'List View' for the 'dlf' extension
21
 *
22
 * @author Sebastian Meyer <[email protected]>
23
 * @author Henrik Lochmann <[email protected]>
24
 * @author Frank Ulrich Weber <[email protected]>
25
 * @package TYPO3
26
 * @subpackage dlf
27
 * @access public
28
 */
29
class ListView extends \Kitodo\Dlf\Common\AbstractPlugin {
30
    public $scriptRelPath = 'Classes/Plugins/ListView.php';
31
32
    /**
33
     * This holds the field wrap of the metadata
34
     *
35
     * @var array
36
     * @access private
37
     */
38
    private $fieldwrap = [];
39
40
    /**
41
     * This holds the list
42
     *
43
     * @var \Kitodo\Dlf\Common\DocumentList
44
     * @access protected
45
     */
46
    protected $list;
47
48
    /**
49
     * Array of sorted metadata
50
     *
51
     * @var array
52
     * @access protected
53
     */
54
    protected $metadata = [];
55
56
    /**
57
     * Array of sortable metadata
58
     *
59
     * @var array
60
     * @access protected
61
     */
62
    protected $sortables = [];
63
64
    /**
65
     * Renders the page browser
66
     *
67
     * @access protected
68
     *
69
     * @return string The rendered page browser ready for output
70
     */
71
    protected function getPageBrowser() {
72
        // Get overall number of pages.
73
        $maxPages = intval(ceil($this->list->metadata['options']['numberOfToplevelHits'] / $this->conf['limit']));
0 ignored issues
show
Bug Best Practice introduced by
The property $metadata is declared protected in Kitodo\Dlf\Common\DocumentList. Since you implement __get, consider adding a @property or @property-read.
Loading history...
74
        // Return empty pagebrowser if there is just one page.
75
        if ($maxPages < 2) {
76
            return '';
77
        }
78
        // Get separator.
79
        $separator = $this->pi_getLL('separator', ' - ', TRUE);
80
        // Add link to previous page.
81
        if ($this->piVars['pointer'] > 0) {
82
            $output = $this->pi_linkTP_keepPIvars($this->pi_getLL('prevPage', '&lt;', TRUE), ['pointer' => $this->piVars['pointer'] - 1], TRUE).$separator;
83
        } else {
84
            $output = $this->pi_getLL('prevPage', '&lt;', TRUE).$separator;
85
        }
86
        $i = 0;
87
        $skip = NULL;
88
        // Add links to pages.
89
        while ($i < $maxPages) {
90
            if ($i < 3 || ($i > $this->piVars['pointer'] - 3 && $i < $this->piVars['pointer'] + 3) || $i > $maxPages - 4) {
91
                if ($this->piVars['pointer'] != $i) {
92
                    $output .= $this->pi_linkTP_keepPIvars(sprintf($this->pi_getLL('page', '%d', TRUE), $i + 1), ['pointer' => $i], TRUE).$separator;
93
                } else {
94
                    $output .= sprintf($this->pi_getLL('page', '%d', TRUE), $i + 1).$separator;
95
                }
96
                $skip = TRUE;
97
            } elseif ($skip === TRUE) {
98
                $output .= $this->pi_getLL('skip', '...', TRUE).$separator;
99
                $skip = FALSE;
100
            }
101
            $i++;
102
        }
103
        // Add link to next page.
104
        if ($this->piVars['pointer'] < $maxPages - 1) {
105
            $output .= $this->pi_linkTP_keepPIvars($this->pi_getLL('nextPage', '&gt;', TRUE), ['pointer' => $this->piVars['pointer'] + 1], TRUE);
106
        } else {
107
            $output .= $this->pi_getLL('nextPage', '&gt;', TRUE);
108
        }
109
        return $output;
110
    }
111
112
    /**
113
     * Renders one entry of the list
114
     *
115
     * @access protected
116
     *
117
     * @param integer $number: The number of the entry
118
     * @param string $template: Parsed template subpart
119
     *
120
     * @return string The rendered entry ready for output
121
     */
122
    protected function getEntry($number, $template) {
123
        $markerArray['###NUMBER###'] = $number + 1;
1 ignored issue
show
Comprehensibility Best Practice introduced by
$markerArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $markerArray = array(); before regardless.
Loading history...
124
        $markerArray['###METADATA###'] = '';
125
        $markerArray['###THUMBNAIL###'] = '';
126
        $markerArray['###PREVIEW###'] = '';
127
        $subpart = '';
128
        $imgAlt = '';
129
        $noTitle = $this->pi_getLL('noTitle');
130
        $metadata = $this->list[$number]['metadata'];
131
        foreach ($this->metadata as $index_name => $metaConf) {
132
            $parsedValue = '';
133
            $fieldwrap = $this->getFieldWrap($index_name, $metaConf['wrap']);
134
            do {
135
                $value = @array_shift($metadata[$index_name]);
136
                // Link title to pageview.
137
                if ($index_name == 'title') {
138
                    // Get title of parent document if needed.
139
                    if (empty($value) && $this->conf['getTitle']) {
140
                        $superiorTitle = Document::getTitle($this->list[$number]['uid'], TRUE);
141
                        if (!empty($superiorTitle)) {
142
                            $value = '['.$superiorTitle.']';
143
                        }
144
                    }
145
                    // Set fake title if still not present.
146
                    if (empty($value)) {
147
                        $value = $noTitle;
148
                    }
149
                    $imgAlt = htmlspecialchars($value);
150
                    $additionalParams = [
151
                        'id' => $this->list[$number]['uid'],
152
                        'page' => $this->list[$number]['page']
153
                    ];
154
                    if (!empty($this->piVars['logicalPage'])) {
155
                        $additionalParams['logicalPage'] = $this->piVars['logicalPage'];
156
                    }
157
                    $conf = [
158
                        'useCacheHash' => 1,
159
                        'parameter' => $this->conf['targetPid'],
160
                        'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', TRUE, FALSE)
161
                    ];
162
                    $value = $this->cObj->typoLink(htmlspecialchars($value), $conf);
163
                } elseif ($index_name == 'owner' && !empty($value)) { // Translate name of holding library.
164
                    $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_libraries', $this->conf['pages']));
165
                } elseif ($index_name == 'type' && !empty($value)) { // Translate document type.
166
                    $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_structures', $this->conf['pages']));
167
                } elseif ($index_name == 'language' && !empty($value)) { // Translate ISO 639 language code.
168
                    $value = htmlspecialchars(Helper::getLanguageName($value));
169
                } elseif (!empty($value)) {
170
                    $value = htmlspecialchars($value);
171
                }
172
                $value = $this->cObj->stdWrap($value, $fieldwrap['value.']);
173
                if (!empty($value)) {
174
                    $parsedValue .= $value;
175
                }
176
            } while (count($metadata[$index_name]));
177
            if (!empty($parsedValue)) {
178
                $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']);
179
                $field .= $parsedValue;
180
                $markerArray['###METADATA###'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']);
181
            }
182
        }
183
        // Add thumbnail.
184
        if (!empty($this->list[$number]['thumbnail'])) {
185
            $markerArray['###THUMBNAIL###'] = '<img alt="'.$imgAlt.'" src="'.$this->list[$number]['thumbnail'].'" />';
186
        }
187
        // Add preview.
188
        if (!empty($this->list[$number]['preview'])) {
189
            $markerArray['###PREVIEW###'] = $this->list[$number]['preview'];
190
        }
191
        if (!empty($this->list[$number]['subparts'])) {
192
            $subpart = $this->getSubEntries($number, $template);
193
        }
194
        // Basket button.
195
        $markerArray['###BASKETBUTTON###'] = '';
196
        if (!empty($this->conf['basketButton']) && !empty($this->conf['targetBasket'])) {
197
            $additionalParams = ['id' => $this->list[$number]['uid'], 'startpage' => $this->list[$number]['page'], 'addToBasket' => 'list'];
198
            $conf = [
199
                'useCacheHash' => 1,
200
                'parameter' => $this->conf['targetBasket'],
201
                'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', TRUE, FALSE)
202
            ];
203
            $link = $this->cObj->typoLink($this->pi_getLL('addBasket', '', TRUE), $conf);
204
            $markerArray['###BASKETBUTTON###'] = $link;
205
        }
206
        return $this->cObj->substituteMarkerArray($this->cObj->substituteSubpart($template['entry'], '###SUBTEMPLATE###', $subpart, TRUE), $markerArray);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...substituteMarkerArray() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

206
        return /** @scrutinizer ignore-deprecated */ $this->cObj->substituteMarkerArray($this->cObj->substituteSubpart($template['entry'], '###SUBTEMPLATE###', $subpart, TRUE), $markerArray);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...er::substituteSubpart() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

206
        return $this->cObj->substituteMarkerArray(/** @scrutinizer ignore-deprecated */ $this->cObj->substituteSubpart($template['entry'], '###SUBTEMPLATE###', $subpart, TRUE), $markerArray);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
207
    }
208
209
    /**
210
     * Returns the parsed fieldwrap of a metadata
211
     *
212
     * @access private
213
     *
214
     * @param string $index_name: The index name of a metadata
215
     * @param string $wrap: The configured metadata wrap
216
     *
217
     * @return array The parsed fieldwrap
218
     */
219
    private function getFieldWrap($index_name, $wrap) {
220
        if (isset($this->fieldwrap[$index_name])) {
221
            return $this->fieldwrap[$index_name];
222
        } else {
223
            return $this->fieldwrap[$index_name] = $this->parseTS($wrap);
224
        }
225
    }
226
227
    /**
228
     * Renders sorting dialog
229
     *
230
     * @access protected
231
     *
232
     * @return string The rendered sorting dialog ready for output
233
     */
234
    protected function getSortingForm() {
235
        // Return nothing if there are no sortable metadata fields.
236
        if (!count($this->sortables)) {
237
            return '';
238
        }
239
        // Set class prefix.
240
        $prefix = str_replace('_', '-', get_class($this));
241
        // Configure @action URL for form.
242
        $linkConf = [
243
            'parameter' => $GLOBALS['TSFE']->id
244
        ];
245
        if (!empty($this->piVars['logicalPage'])) {
246
            $linkConf['additionalParams'] = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, ['logicalPage' => $this->piVars['logicalPage']], '', TRUE, FALSE);
247
        }
248
        // Build HTML form.
249
        $sorting = '<form action="'.$this->cObj->typoLink_URL($linkConf).'" method="get"><div><input type="hidden" name="id" value="'.$GLOBALS['TSFE']->id.'" />';
250
        foreach ($this->piVars as $piVar => $value) {
251
            if ($piVar != 'order' && $piVar != 'DATA' && !empty($value)) {
252
                $sorting .= '<input type="hidden" name="'.$this->prefixId.'['.$piVar.']" value="'.$value.'" />';
253
            }
254
        }
255
        // Select sort field.
256
        $uniqId = uniqid($prefix.'-');
257
        $sorting .= '<label for="'.$uniqId.'">'.$this->pi_getLL('orderBy', '', TRUE).'</label><select id="'.$uniqId.'" name="'.$this->prefixId.'[order]" onchange="javascript:this.form.submit();">';
258
        // Add relevance sorting if this is a search result list.
259
        if ($this->list->metadata['options']['source'] == 'search') {
0 ignored issues
show
Bug Best Practice introduced by
The property $metadata is declared protected in Kitodo\Dlf\Common\DocumentList. Since you implement __get, consider adding a @property or @property-read.
Loading history...
260
            $sorting .= '<option value="score"'.(($this->list->metadata['options']['order'] == 'score') ? ' selected="selected"' : '').'>'.$this->pi_getLL('relevance', '', TRUE).'</option>';
261
        }
262
        foreach ($this->sortables as $index_name => $label) {
263
            $sorting .= '<option value="'.$index_name.'"'.(($this->list->metadata['options']['order'] == $index_name) ? ' selected="selected"' : '').'>'.htmlspecialchars($label).'</option>';
264
        }
265
        $sorting .= '</select>';
266
        // Select sort direction.
267
        $uniqId = uniqid($prefix.'-');
268
        $sorting .= '<label for="'.$uniqId.'">'.$this->pi_getLL('direction', '', TRUE).'</label><select id="'.$uniqId.'" name="'.$this->prefixId.'[asc]" onchange="javascript:this.form.submit();">';
269
        $sorting .= '<option value="1" '.($this->list->metadata['options']['order.asc'] ? ' selected="selected"' : '').'>'.$this->pi_getLL('direction.asc', '', TRUE).'</option>';
270
        $sorting .= '<option value="0" '.(!$this->list->metadata['options']['order.asc'] ? ' selected="selected"' : '').'>'.$this->pi_getLL('direction.desc', '', TRUE).'</option>';
271
        $sorting .= '</select></div></form>';
272
        return $sorting;
273
    }
274
275
    /**
276
     * Renders all sub-entries of one entry
277
     *
278
     * @access protected
279
     *
280
     * @param integer $number: The number of the entry
281
     * @param string $template: Parsed template subpart
282
     *
283
     * @return string The rendered entries ready for output
284
     */
285
    protected function getSubEntries($number, $template) {
286
        $content = '';
287
        $noTitle = $this->pi_getLL('noTitle');
288
        $highlight_word = preg_replace('/\s\s+/', ';', $this->list->metadata['searchString']);
0 ignored issues
show
Bug Best Practice introduced by
The property $metadata is declared protected in Kitodo\Dlf\Common\DocumentList. Since you implement __get, consider adding a @property or @property-read.
Loading history...
289
        foreach ($this->list[$number]['subparts'] as $subpart) {
290
            $markerArray['###SUBMETADATA###'] = '';
291
            $markerArray['###SUBTHUMBNAIL###'] = '';
292
            $markerArray['###SUBPREVIEW###'] = '';
293
            $imgAlt = '';
294
            foreach ($this->metadata as $index_name => $metaConf) {
295
                $parsedValue = '';
296
                $fieldwrap = $this->getFieldWrap($index_name, $metaConf['wrap']);
297
                do {
298
                    $value = @array_shift($subpart['metadata'][$index_name]);
299
                    // Link title to pageview.
300
                    if ($index_name == 'title') {
301
                        // Get title of parent document if needed.
302
                        if (empty($value) && $this->conf['getTitle']) {
303
                            $superiorTitle = Document::getTitle($subpart['uid'], TRUE);
304
                            if (!empty($superiorTitle)) {
305
                                $value = '['.$superiorTitle.']';
306
                            }
307
                        }
308
                        // Set fake title if still not present.
309
                        if (empty($value)) {
310
                            $value = $noTitle;
311
                        }
312
                        $imgAlt = htmlspecialchars($value);
313
                        $additionalParams = [
314
                            'id' => $subpart['uid'],
315
                            'page' => $subpart['page'],
316
                            'highlight_word' => $highlight_word
317
                        ];
318
                        if (!empty($this->piVars['logicalPage'])) {
319
                            $additionalParams['logicalPage'] = $this->piVars['logicalPage'];
320
                        }
321
                        $conf = [
322
                            // we don't want cHash in case of search parameters
323
                            'useCacheHash' => empty($this->list->metadata['searchString']) ? 1 : 0,
324
                            'parameter' => $this->conf['targetPid'],
325
                            'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', TRUE, FALSE)
326
                        ];
327
                        $value = $this->cObj->typoLink(htmlspecialchars($value), $conf);
328
                    } elseif ($index_name == 'owner' && !empty($value)) { // Translate name of holding library.
329
                        $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_libraries', $this->conf['pages']));
330
                    } elseif ($index_name == 'type' && !empty($value)) { // Translate document type.
331
                        $_value = $value;
332
                        $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_structures', $this->conf['pages']));
333
                        // Add page number for single pages.
334
                        if ($_value == 'page') {
335
                            $value .= ' '.intval($subpart['page']);
336
                        }
337
                    } elseif ($index_name == 'language' && !empty($value)) { // Translate ISO 639 language code.
338
                        $value = htmlspecialchars(Helper::getLanguageName($value));
339
                    } elseif (!empty($value)) {
340
                        $value = htmlspecialchars($value);
341
                    }
342
                    $value = $this->cObj->stdWrap($value, $fieldwrap['value.']);
343
                    if (!empty($value)) {
344
                        $parsedValue .= $value;
345
                    }
346
                } while (count($subpart['metadata'][$index_name]));
347
                if (!empty($parsedValue)) {
348
                    $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']);
349
                    $field .= $parsedValue;
350
                    $markerArray['###SUBMETADATA###'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']);
351
                }
352
            }
353
            // Add thumbnail.
354
            if (!empty($subpart['thumbnail'])) {
355
                $markerArray['###SUBTHUMBNAIL###'] = '<img alt="'.$imgAlt.'" src="'.$subpart['thumbnail'].'" />';
356
            }
357
            // Add preview.
358
            if (!empty($subpart['preview'])) {
359
                $markerArray['###SUBPREVIEW###'] = $subpart['preview'];
360
            }
361
            // Basket button
362
            $markerArray['###SUBBASKETBUTTON###'] = '';
363
            if (!empty($this->conf['basketButton']) && !empty($this->conf['targetBasket'])) {
364
                $additionalParams = ['id' => $this->list[$number]['uid'], 'startpage' => $subpart['page'], 'endpage' => $subpart['page'], 'logId' => $subpart['sid'], 'addToBasket' => 'subentry'];
365
                $conf = [
366
                    'useCacheHash' => 1,
367
                    'parameter' => $this->conf['targetBasket'],
368
                    'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', TRUE, FALSE)
369
                ];
370
                $link = $this->cObj->typoLink($this->pi_getLL('addBasket', '', TRUE), $conf);
371
                $markerArray['###SUBBASKETBUTTON###'] = $link;
372
            }
373
            $content .= $this->cObj->substituteMarkerArray($template['subentry'], $markerArray);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...substituteMarkerArray() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

373
            $content .= /** @scrutinizer ignore-deprecated */ $this->cObj->substituteMarkerArray($template['subentry'], $markerArray);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
374
        }
375
        return $this->cObj->substituteSubpart($this->cObj->getSubpart($this->template, '###SUBTEMPLATE###'), '###SUBENTRY###', $content, TRUE);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...er::substituteSubpart() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

375
        return /** @scrutinizer ignore-deprecated */ $this->cObj->substituteSubpart($this->cObj->getSubpart($this->template, '###SUBTEMPLATE###'), '###SUBENTRY###', $content, TRUE);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...tRenderer::getSubpart() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

375
        return $this->cObj->substituteSubpart(/** @scrutinizer ignore-deprecated */ $this->cObj->getSubpart($this->template, '###SUBTEMPLATE###'), '###SUBENTRY###', $content, TRUE);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
376
    }
377
378
    /**
379
     * Get metadata configuration from database
380
     *
381
     * @access protected
382
     *
383
     * @return void
384
     */
385
    protected function loadConfig() {
386
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
387
            'tx_dlf_metadata.index_name AS index_name,tx_dlf_metadata.wrap AS wrap,tx_dlf_metadata.is_listed AS is_listed,tx_dlf_metadata.is_sortable AS is_sortable',
388
            'tx_dlf_metadata',
389
            '(tx_dlf_metadata.is_listed=1 OR tx_dlf_metadata.is_sortable=1)'
390
                .' AND tx_dlf_metadata.pid='.intval($this->conf['pages'])
391
                .Helper::whereClause('tx_dlf_metadata'),
392
            '',
393
            'tx_dlf_metadata.sorting ASC',
394
            ''
395
        );
396
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
397
            if ($resArray['is_listed']) {
398
                $this->metadata[$resArray['index_name']] = [
399
                    'wrap' => $resArray['wrap'],
400
                    'label' => Helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages'])
401
                ];
402
            }
403
            if ($resArray['is_sortable']) {
404
                $this->sortables[$resArray['index_name']] = Helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages']);
405
            }
406
        }
407
    }
408
409
    /**
410
     * The main method of the PlugIn
411
     *
412
     * @access public
413
     *
414
     * @param string $content: The PlugIn content
415
     * @param array $conf: The PlugIn configuration
416
     *
417
     * @return string The content that is displayed on the website
418
     */
419
    public function main($content, $conf) {
420
        $this->init($conf);
421
        // Don't cache the output.
422
        $this->setCache(FALSE);
423
        // Load the list.
424
        $this->list = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(DocumentList::class);
425
        $currentEntry = $this->piVars['pointer'] * $this->conf['limit'];
426
        $lastEntry = ($this->piVars['pointer'] + 1) * $this->conf['limit'];
427
        // Check if it's a list of database records or Solr documents.
428
        if (!empty($this->list->metadata['options']['source'])
429
            && $this->list->metadata['options']['source'] == 'collection'
430
            && ((!empty($this->piVars['order']) && $this->piVars['order'] != $this->list->metadata['options']['order'])
431
                || (isset($this->piVars['asc']) && $this->piVars['asc'] != $this->list->metadata['options']['order.asc']))) {
432
            // Order list by given field.
433
            $this->list->sort($this->piVars['order'], (boolean) $this->piVars['asc']);
434
            // Update list's metadata.
435
            $listMetadata = $this->list->metadata;
436
            $listMetadata['options']['order'] = $this->piVars['order'];
437
            $listMetadata['options']['order.asc'] = (boolean) $this->piVars['asc'];
438
            $this->list->metadata = $listMetadata;
439
            // Save updated list.
440
            $this->list->save();
441
            // Reset pointer.
442
            $this->piVars['pointer'] = 0;
443
        } elseif (!empty($this->list->metadata['options']['source']) && $this->list->metadata['options']['source'] == 'search') {
444
            // Update list's metadata
445
            $listMetadata = $this->list->metadata;
446
            // Sort the list if applicable.
447
            if ((!empty($this->piVars['order']) && $this->piVars['order'] != $listMetadata['options']['order'])
448
                || (isset($this->piVars['asc']) && $this->piVars['asc'] != $listMetadata['options']['order.asc'])) {
449
                // Update list's metadata.
450
                $listMetadata['options']['params']['sort'] = [$this->piVars['order']."_sorting" => (boolean) $this->piVars['asc']?'asc':'desc'];
451
                $listMetadata['options']['order'] = $this->piVars['order'];
452
                $listMetadata['options']['order.asc'] = (boolean) $this->piVars['asc'];
453
                // Reset pointer.
454
                $this->piVars['pointer'] = 0;
455
            }
456
            // Set some query parameters
457
            $listMetadata['options']['params']['start'] = $currentEntry;
458
            $listMetadata['options']['params']['rows'] = $this->conf['limit'];
459
            // Search only if the query params have changed.
460
            if ($listMetadata['options']['params'] != $this->list->metadata['options']['params']) {
461
                // Instantiate search object.
462
                $solr = Solr::getInstance($this->list->metadata['options']['core']);
463
                if (!$solr->ready) {
0 ignored issues
show
Bug Best Practice introduced by
The property $ready is declared protected in Kitodo\Dlf\Common\Solr. Since you implement __get, consider adding a @property or @property-read.
Loading history...
464
                    Helper::devLog('Apache Solr not available', DEVLOG_SEVERITY_ERROR);
465
                    return $content;
466
                }
467
                // Set search parameters.
468
                $solr->cPid =  $listMetadata['options']['pid'];
0 ignored issues
show
Bug Best Practice introduced by
The property $cPid is declared protected in Kitodo\Dlf\Common\Solr. Since you implement __set, consider adding a @property or @property-write.
Loading history...
469
                $solr->params = $listMetadata['options']['params'];
0 ignored issues
show
Bug Best Practice introduced by
The property $params is declared protected in Kitodo\Dlf\Common\Solr. Since you implement __set, consider adding a @property or @property-write.
Loading history...
470
                // Perform search.
471
                $this->list = $solr->search();
472
            }
473
            // Add list description
474
            $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>';
0 ignored issues
show
Bug Best Practice introduced by
The property $metadata is declared protected in Kitodo\Dlf\Common\DocumentList. Since you implement __get, consider adding a @property or @property-read.
Loading history...
475
            $this->list->metadata = $listMetadata;
476
            // Save updated list.
477
            $this->list->save();
478
            $currentEntry = 0;
479
            $lastEntry = $this->conf['limit'];
480
        }
481
        // Load template file.
482
        $this->getTemplate();
483
        $subpartArray['entry'] = $this->cObj->getSubpart($this->template, '###ENTRY###');
1 ignored issue
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...tRenderer::getSubpart() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

483
        $subpartArray['entry'] = /** @scrutinizer ignore-deprecated */ $this->cObj->getSubpart($this->template, '###ENTRY###');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Comprehensibility Best Practice introduced by
$subpartArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subpartArray = array(); before regardless.
Loading history...
484
        $subpartArray['subentry'] = $this->cObj->getSubpart($this->template, '###SUBENTRY###');
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...tRenderer::getSubpart() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

484
        $subpartArray['subentry'] = /** @scrutinizer ignore-deprecated */ $this->cObj->getSubpart($this->template, '###SUBENTRY###');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
485
        // Set some variable defaults.
486
        if (!empty($this->piVars['pointer']) && (($this->piVars['pointer'] * $this->conf['limit']) + 1) <= $this->list->metadata['options']['numberOfToplevelHits']) {
487
            $this->piVars['pointer'] = max(intval($this->piVars['pointer']), 0);
488
        } else {
489
            $this->piVars['pointer'] = 0;
490
        }
491
        // Load metadata configuration.
492
        $this->loadConfig();
493
        for ($currentEntry, $lastEntry; $currentEntry < $lastEntry; $currentEntry++) {
494
            if (empty($this->list[$currentEntry])) {
495
                break;
496
            } else {
497
                $content .= $this->getEntry($currentEntry, $subpartArray);
498
            }
499
        }
500
        $markerArray['###LISTTITLE###'] = $this->list->metadata['label'];
1 ignored issue
show
Comprehensibility Best Practice introduced by
$markerArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $markerArray = array(); before regardless.
Loading history...
501
        $markerArray['###LISTDESCRIPTION###'] = $this->list->metadata['description'];
502
        if (!empty($this->list->metadata['thumbnail'])) {
503
            $markerArray['###LISTTHUMBNAIL###'] = '<img alt="" src="'.$this->list->metadata['thumbnail'].'" />';
504
        } else {
505
            $markerArray['###LISTTHUMBNAIL###'] = '';
506
        }
507
        if ($currentEntry) {
508
            $currentEntry =  ($this->piVars['pointer'] * $this->conf['limit']) + 1;
509
            $lastEntry = ($this->piVars['pointer'] * $this->conf['limit']) + $this->conf['limit'];
510
            $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']));
511
        } else {
512
            $markerArray['###COUNT###'] = $this->pi_getLL('nohits', '', TRUE);
513
        }
514
        $markerArray['###PAGEBROWSER###'] = $this->getPageBrowser();
515
        $markerArray['###SORTING###'] = $this->getSortingForm();
516
        $content = $this->cObj->substituteMarkerArray($this->cObj->substituteSubpart($this->template, '###ENTRY###', $content, TRUE), $markerArray);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...substituteMarkerArray() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

516
        $content = /** @scrutinizer ignore-deprecated */ $this->cObj->substituteMarkerArray($this->cObj->substituteSubpart($this->template, '###ENTRY###', $content, TRUE), $markerArray);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...er::substituteSubpart() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

516
        $content = $this->cObj->substituteMarkerArray(/** @scrutinizer ignore-deprecated */ $this->cObj->substituteSubpart($this->template, '###ENTRY###', $content, TRUE), $markerArray);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
517
        return $this->pi_wrapInBaseClass($content);
518
    }
519
}
520