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
Push — master ( 118576...18bf9c )
by Sebastian
14:55
created

tx_dlf_collection::showCollectionList()   F

Complexity

Conditions 25
Paths 4920

Size

Total Lines 195
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 25
eloc 82
c 2
b 0
f 0
nc 4920
nop 0
dl 0
loc 195
rs 2

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
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
/**
13
 * Plugin 'DLF: Collection' for the 'dlf' extension.
14
 *
15
 * @author	Sebastian Meyer <[email protected]>
16
 * @package	TYPO3
17
 * @subpackage	tx_dlf
18
 * @access	public
19
 */
20
class tx_dlf_collection extends tx_dlf_plugin {
21
22
    public $scriptRelPath = 'plugins/collection/class.tx_dlf_collection.php';
23
24
    /**
25
     * This holds the hook objects
26
     *
27
     * @var	array
28
     * @access protected
29
     */
30
    protected $hookObjects = array ();
31
32
    /**
33
     * The main method of the PlugIn
34
     *
35
     * @access	public
36
     *
37
     * @param	string		$content: The PlugIn content
38
     * @param	array		$conf: The PlugIn configuration
39
     *
40
     * @return	string		The content that is displayed on the website
41
     */
42
    public function main($content, $conf) {
43
44
        $this->init($conf);
45
46
        // Turn cache on.
47
        $this->setCache(TRUE);
48
49
        // Quit without doing anything if required configuration variables are not set.
50
        if (empty($this->conf['pages'])) {
51
52
            if (TYPO3_DLOG) {
53
54
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_collection->main('.$content.', [data])] Incomplete plugin configuration', $this->extKey, SYSLOG_SEVERITY_WARNING, $conf);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
56
            }
57
58
            return $content;
59
60
        }
61
62
        // Load template file.
63
        if (!empty($this->conf['templateFile'])) {
64
65
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATE###');
66
67
        } else {
68
69
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/plugins/collection/template.tmpl'), '###TEMPLATE###');
70
71
        }
72
73
        // Get hook objects.
74
        $this->hookObjects = tx_dlf_helper::getHookObjects($this->scriptRelPath);
75
76
        if (!empty($this->piVars['collection'])) {
77
78
            $this->showSingleCollection(intval($this->piVars['collection']));
79
80
        } else {
81
82
            $content .= $this->showCollectionList();
83
84
        }
85
86
        return $this->pi_wrapInBaseClass($content);
87
88
    }
89
90
    /**
91
     * Builds a collection list
92
     *
93
     * @access	protected
94
     *
95
     * @return	string		The list of collections ready to output
96
     */
97
    protected function showCollectionList() {
98
99
        $additionalWhere = '';
100
101
        $orderBy = 'tx_dlf_collections.label';
102
103
        // Handle collections set by configuration.
104
        if ($this->conf['collections']) {
105
106
            if (count(explode(',', $this->conf['collections'])) == 1 && empty($this->conf['dont_show_single'])) {
107
108
                $this->showSingleCollection(intval(trim($this->conf['collections'], ' ,')));
109
110
            }
111
112
            $additionalWhere .= ' AND tx_dlf_collections.uid IN ('.$GLOBALS['TYPO3_DB']->cleanIntList($this->conf['collections']).')';
113
114
            $orderBy = 'FIELD(tx_dlf_collections.uid, '.$GLOBALS['TYPO3_DB']->cleanIntList($this->conf['collections']).')';
115
116
        }
117
118
        // Should user-defined collections be shown?
119
        if (empty($this->conf['show_userdefined'])) {
120
121
            $additionalWhere .= ' AND tx_dlf_collections.fe_cruser_id=0';
122
123
        } elseif ($this->conf['show_userdefined'] > 0) {
124
125
            if (!empty($GLOBALS['TSFE']->fe_user->user['uid'])) {
126
127
                $additionalWhere .= ' AND tx_dlf_collections.fe_cruser_id='.intval($GLOBALS['TSFE']->fe_user->user['uid']);
128
129
            } else {
130
131
                $additionalWhere .= ' AND NOT tx_dlf_collections.fe_cruser_id=0';
132
133
            }
134
135
        }
136
137
        // Get collections.
138
        $result = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
139
            'tx_dlf_collections.uid AS uid,tx_dlf_collections.pid AS pid,tx_dlf_collections.sys_language_uid AS sys_language_uid,tx_dlf_collections.label AS label,tx_dlf_collections.thumbnail AS thumbnail,tx_dlf_collections.description AS description,tx_dlf_collections.priority AS priority,COUNT(tx_dlf_documents.uid) AS titles',
140
            'tx_dlf_documents',
141
            'tx_dlf_relations',
142
            'tx_dlf_collections',
143
            'AND tx_dlf_collections.pid='.intval($this->conf['pages']).' AND tx_dlf_documents.partof=0 AND tx_dlf_relations.ident='.$GLOBALS['TYPO3_DB']->fullQuoteStr('docs_colls', 'tx_dlf_relations').$additionalWhere.tx_dlf_helper::whereClause('tx_dlf_documents').tx_dlf_helper::whereClause('tx_dlf_collections').' AND (tx_dlf_collections.sys_language_uid IN (-1,0) OR (tx_dlf_collections.sys_language_uid = '.$GLOBALS['TSFE']->sys_language_uid.' AND tx_dlf_collections.l18n_parent = 0))',
144
            'tx_dlf_collections.uid',
145
            $orderBy,
146
            ''
147
        );
148
149
        $count = $GLOBALS['TYPO3_DB']->sql_num_rows($result);
150
151
        $content = '';
152
153
        if ($count == 1 && empty($this->conf['dont_show_single'])) {
154
155
            $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
156
157
            $this->showSingleCollection(intval($resArray['uid']));
158
159
        } elseif ($count > 0) {
160
161
            // Get number of volumes per collection.
162
            $resultVolumes = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
163
                'tx_dlf_collections.uid AS uid,COUNT(tx_dlf_documents.uid) AS volumes',
164
                'tx_dlf_documents',
165
                'tx_dlf_relations',
166
                'tx_dlf_collections',
167
                'AND tx_dlf_collections.pid='.intval($this->conf['pages']).' AND NOT tx_dlf_documents.uid IN (SELECT DISTINCT tx_dlf_documents.partof FROM tx_dlf_documents WHERE NOT tx_dlf_documents.partof=0'.tx_dlf_helper::whereClause('tx_dlf_documents').') AND tx_dlf_relations.ident='.$GLOBALS['TYPO3_DB']->fullQuoteStr('docs_colls', 'tx_dlf_relations').$additionalWhere.tx_dlf_helper::whereClause('tx_dlf_documents').tx_dlf_helper::whereClause('tx_dlf_collections'),
168
                'tx_dlf_collections.uid',
169
                '',
170
                ''
171
            );
172
173
            $volumes = array ();
174
175
            while ($resArrayVolumes = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($resultVolumes)) {
176
177
                $volumes[$resArrayVolumes['uid']] = $resArrayVolumes['volumes'];
178
179
            }
180
181
            // Process results.
182
            while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
183
184
                if (is_array($resArray) && $resArray['sys_language_uid'] != $GLOBALS['TSFE']->sys_language_content && $GLOBALS['TSFE']->sys_language_contentOL) {
185
186
                    $resArray = $GLOBALS['TSFE']->sys_page->getRecordOverlay('tx_dlf_collections', $resArray, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL);
187
188
                }
189
190
                // Generate random but unique array key taking priority into account.
191
                do {
192
193
                    $_key = ($resArray['priority'] * 1000) + mt_rand(0, 1000);
194
195
                } while (!empty($markerArray[$_key]));
196
197
                // Merge plugin variables with new set of values.
198
                $additionalParams = array ('collection' => $resArray['uid']);
199
200
                if (is_array($this->piVars)) {
201
202
                    $piVars = $this->piVars;
203
204
                    unset($piVars['DATA']);
205
206
                    $additionalParams = tx_dlf_helper::array_merge_recursive_overrule($piVars, $additionalParams);
207
208
                }
209
210
                // Build typolink configuration array.
211
                $conf = array (
212
                    'useCacheHash' => 1,
213
                    'parameter' => $GLOBALS['TSFE']->id,
214
                    'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', TRUE, FALSE)
215
                );
216
217
                // Link collection's title to list view.
218
                $markerArray[$_key]['###TITLE###'] = $this->cObj->typoLink(htmlspecialchars($resArray['label']), $conf);
219
220
                // Add feed link if applicable.
221
                if (!empty($this->conf['targetFeed'])) {
222
223
                    $img = '<img src="'.\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::siteRelPath($this->extKey).'res/icons/txdlffeeds.png" alt="'.$this->pi_getLL('feedAlt', '', TRUE).'" title="'.$this->pi_getLL('feedTitle', '', TRUE).'" />';
224
225
                    $markerArray[$_key]['###FEED###'] = $this->pi_linkTP($img, array ($this->prefixId => array ('collection' => $resArray['uid'])), FALSE, $this->conf['targetFeed']);
226
227
                } else {
228
229
                    $markerArray[$_key]['###FEED###'] = '';
230
231
                }
232
233
                // Add thumbnail.
234
                if (!empty($resArray['thumbnail'])) {
235
236
                    $markerArray[$_key]['###THUMBNAIL###'] = '<img alt="" title="'.htmlspecialchars($resArray['label']).'" src="'.$resArray['thumbnail'].'" />';
237
238
                } else {
239
240
                    $markerArray[$_key]['###THUMBNAIL###'] = '';
241
242
                }
243
244
                // Add description.
245
                $markerArray[$_key]['###DESCRIPTION###'] = $this->pi_RTEcssText($resArray['description']);
246
247
                // Build statistic's output.
248
                $labelTitles = $this->pi_getLL(($resArray['titles'] > 1 ? 'titles' : 'title'), '', FALSE);
249
250
                $markerArray[$_key]['###COUNT_TITLES###'] = htmlspecialchars($resArray['titles'].$labelTitles);
251
252
                $labelVolumes = $this->pi_getLL(($volumes[$resArray['uid']] > 1 ? 'volumes' : 'volume'), '', FALSE);
253
254
                $markerArray[$_key]['###COUNT_VOLUMES###'] = htmlspecialchars($volumes[$resArray['uid']].$labelVolumes);
255
256
            }
257
258
            // Randomize sorting?
259
            if (!empty($this->conf['randomize'])) {
260
261
                ksort($markerArray, SORT_NUMERIC);
262
263
                // Don't cache the output.
264
                $this->setCache(FALSE);
265
266
            }
267
268
            $entry = $this->cObj->getSubpart($this->template, '###ENTRY###');
269
270
            foreach ($markerArray as $marker) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $markerArray does not seem to be defined for all execution paths leading up to this point.
Loading history...
271
272
                $content .= $this->cObj->substituteMarkerArray($entry, $marker);
273
274
            }
275
276
            // Hook for getting custom collection hierarchies/subentries (requested by SBB).
277
            foreach ($this->hookObjects as $hookObj) {
278
279
                if (method_exists($hookObj, 'showCollectionList_getCustomCollectionList')) {
280
281
                    $hookObj->showCollectionList_getCustomCollectionList($this, $this->conf['templateFile'], $content, $markerArray);
282
283
                }
284
285
            }
286
287
            return $this->cObj->substituteSubpart($this->template, '###ENTRY###', $content, TRUE);
288
289
        }
290
291
        return $content;
292
293
    }
294
295
    /**
296
     * Builds a collection's list
297
     *
298
     * @access	protected
299
     *
300
     * @param	integer		$id: The collection's UID
301
     *
302
     * @return	void
303
     */
304
    protected function showSingleCollection($id) {
305
306
        // Should user-defined collections be shown?
307
        if (empty($this->conf['show_userdefined'])) {
308
309
            $additionalWhere = ' AND tx_dlf_collections.fe_cruser_id=0';
310
311
        } elseif ($this->conf['show_userdefined'] > 0) {
312
313
            $additionalWhere = ' AND NOT tx_dlf_collections.fe_cruser_id=0';
314
315
        }
316
317
        // Get all documents in collection.
318
        $result = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
319
            'tx_dlf_collections.uid AS uid,tx_dlf_collections.pid AS pid,tx_dlf_collections.sys_language_uid AS sys_language_uid,tx_dlf_collections.index_name AS index_name,tx_dlf_collections.label AS label,tx_dlf_collections.description AS description,tx_dlf_collections.thumbnail AS collThumb,tx_dlf_collections.fe_cruser_id AS userid,tx_dlf_documents.uid AS docUid,tx_dlf_documents.metadata_sorting AS metadata_sorting,tx_dlf_documents.volume_sorting AS volume_sorting,tx_dlf_documents.partof AS partof',
320
            'tx_dlf_documents',
321
            'tx_dlf_relations',
322
            'tx_dlf_collections',
323
            'AND tx_dlf_collections.uid='.intval($id).' AND tx_dlf_collections.pid='.intval($this->conf['pages']).' AND tx_dlf_relations.ident='.$GLOBALS['TYPO3_DB']->fullQuoteStr('docs_colls', 'tx_dlf_relations').$additionalWhere.tx_dlf_helper::whereClause('tx_dlf_documents').tx_dlf_helper::whereClause('tx_dlf_collections'),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $additionalWhere does not seem to be defined for all execution paths leading up to this point.
Loading history...
324
            '',
325
            'tx_dlf_documents.title_sorting ASC',
326
            ''
327
        );
328
329
        $toplevel = array ();
330
331
        $subparts = array ();
332
333
        $listMetadata = array ();
334
335
        // Process results.
336
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
337
338
            if (empty($l10nOverlay)) {
339
340
                $l10nOverlay = $GLOBALS['TSFE']->sys_page->getRecordOverlay('tx_dlf_collections', $resArray, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL);
341
342
            }
343
344
            if (empty($listMetadata)) {
345
346
                $listMetadata = array (
347
                    'label' => !empty($l10nOverlay['label']) ? htmlspecialchars($l10nOverlay['label']) : htmlspecialchars($resArray['label']),
348
                    'description' => !empty($l10nOverlay['description']) ? $this->pi_RTEcssText($l10nOverlay['description']) : $this->pi_RTEcssText($resArray['description']),
349
                    'thumbnail' => htmlspecialchars($resArray['collThumb']),
350
                    'options' => array (
351
                        'source' => 'collection',
352
                        'select' => $id,
353
                        'userid' => $resArray['userid'],
354
                        'params' => array ('fq' => array ('collection_faceting:("'.$resArray['index_name'].'")')),
355
                        'core' => '',
356
                        'pid' => $this->conf['pages'],
357
                        'order' => 'title',
358
                        'order.asc' => TRUE
359
                    )
360
                );
361
362
            }
363
364
            // Split toplevel documents from volumes.
365
            if ($resArray['partof'] == 0) {
366
367
                // Prepare document's metadata for sorting.
368
                $sorting = unserialize($resArray['metadata_sorting']);
369
370
                if (!empty($sorting['type']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($sorting['type'])) {
371
372
                    $sorting['type'] = tx_dlf_helper::getIndexName($sorting['type'], 'tx_dlf_structures', $this->conf['pages']);
373
374
                }
375
376
                if (!empty($sorting['owner']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($sorting['owner'])) {
377
378
                    $sorting['owner'] = tx_dlf_helper::getIndexName($sorting['owner'], 'tx_dlf_libraries', $this->conf['pages']);
379
380
                }
381
382
                if (!empty($sorting['collection']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($sorting['collection'])) {
383
384
                    $sorting['collection'] = tx_dlf_helper::getIndexName($sorting['collection'], 'tx_dlf_collections', $this->conf['pages']);
385
386
                }
387
388
                $toplevel[$resArray['docUid']] = array (
389
                    'u' => $resArray['docUid'],
390
                    'h' => '',
391
                    's' => $sorting,
392
                    'p' => array ()
393
                );
394
395
            } else {
396
397
                $subparts[$resArray['partof']][$resArray['volume_sorting']] = $resArray['docUid'];
398
399
            }
400
401
        }
402
403
        // Add volumes to the corresponding toplevel documents.
404
        foreach ($subparts as $partof => $parts) {
405
406
            if (!empty($toplevel[$partof])) {
407
408
                ksort($parts);
409
410
                foreach ($parts as $part) {
411
412
                    $toplevel[$partof]['p'][] = array ('u' => $part);
413
414
                }
415
416
            }
417
418
        }
419
420
        // Save list of documents.
421
        $list = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_dlf_list');
422
423
        $list->reset();
424
425
        $list->add(array_values($toplevel));
426
427
        $list->metadata = $listMetadata;
428
429
        $list->save();
430
431
        // Clean output buffer.
432
        \TYPO3\CMS\Core\Utility\GeneralUtility::cleanOutputBuffers();
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Core\Utility\G...y::cleanOutputBuffers() has been deprecated: since TYPO3 CMS 7, will be removed in CMS 8, use ob_* functions directly or self::flushOutputBuffers ( Ignorable by Annotation )

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

432
        /** @scrutinizer ignore-deprecated */ \TYPO3\CMS\Core\Utility\GeneralUtility::cleanOutputBuffers();

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...
433
434
        // Send headers.
435
        header('Location: '.\TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl($this->cObj->typoLink_URL(array ('parameter' => $this->conf['targetPid']))));
436
437
        // Flush output buffer and end script processing.
438
        ob_end_flush();
439
440
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
441
442
    }
443
444
}
445