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 (#837)
by Beatrycze
04:04
created

TableOfContentsController::makeMenuFor3DObjects()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 3
b 0
f 0
nc 4
nop 0
dl 0
loc 22
rs 9.5555
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
namespace Kitodo\Dlf\Controller;
13
14
use Kitodo\Dlf\Common\Helper;
15
use TYPO3\CMS\Core\Utility\MathUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18
/**
19
 * Controller class for plugin 'Table Of Contents'.
20
 *
21
 * @author Sebastian Meyer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class TableOfContentsController extends AbstractController
27
{
28
    /**
29
     * This holds the active entries according to the currently selected page
30
     *
31
     * @var array
32
     * @access protected
33
     */
34
    protected $activeEntries = [];
35
36
    /**
37
     * This holds the list of authors for autocomplete
38
     *
39
     * @var array
40
     * @access protected
41
     */
42
    protected $authors = [];
43
44
    /**
45
     * This holds the list of titles for autocomplete
46
     *
47
     * @var array
48
     * @access protected
49
     */
50
    protected $titles = [];
51
52
    /**
53
     * The main method of the plugin
54
     *
55
     * @return void
56
     */
57
    public function mainAction()
58
    {
59
        // Load current document.
60
        $this->loadDocument($this->requestData);
61
        if (
62
            $this->document === null
63
            || $this->document->getDoc() === null
64
        ) {
65
            // Quit without doing anything if required variables are not set.
66
            return;
67
        } else {
68
            if (!empty($this->requestData['logicalPage'])) {
69
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
70
                // The logical page parameter should not appear again
71
                unset($this->requestData['logicalPage']);
72
            }
73
            if ($this->document->getDoc()->tableOfContents[0]['type'] == 'collection') {
74
                $this->view->assign('currentList', $this->requestData['id']);
75
                if (isset($this->requestData['transform'])) {
76
                    $this->view->assign('transform', $this->requestData['transform']);
77
                } else {
78
                    $this->view->assign('transform', 'something');
79
                }
80
                $this->view->assign('type', 'collection');
81
                $this->view->assign('types', $this->getTypes($this->document->getDoc()->tableOfContents));
82
                $this->view->assign('toc', $this->makeMenuFor3DObjects());
83
                $this->view->assign('authors', $this->authors);
84
                $this->view->assign('titles', $this->titles);
85
            } else {
86
                $this->view->assign('type', 'other');
87
                $this->view->assign('toc', $this->makeMenuArray());
88
            }
89
        }
90
    }
91
92
    /**
93
     * This builds a menu array for HMENU
94
     *
95
     * @access protected
96
     * @return array HMENU array
97
     */
98
    protected function makeMenuArray()
99
    {
100
        // Set default values for page if not set.
101
        // $this->requestData['page'] may be integer or string (physical structure @ID)
102
        if (
103
            (int) $this->requestData['page'] > 0
104
            || empty($this->requestData['page'])
105
        ) {
106
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
107
        } else {
108
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
109
        }
110
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
111
        $menuArray = [];
112
        // Does the document have physical elements or is it an external file?
113
        if (
114
            !empty($this->document->getDoc()->physicalStructure)
115
            || !MathUtility::canBeInterpretedAsInteger($this->requestData['id'])
116
        ) {
117
            // Get all logical units the current page or track is a part of.
118
            if (
119
                !empty($this->requestData['page'])
120
                && !empty($this->document->getDoc()->physicalStructure)
121
            ) {
122
                $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
123
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
124
                if (
125
                    !empty($this->requestData['double'])
126
                    && $this->requestData['page'] < $this->document->getDoc()->numPages
127
                ) {
128
                    $this->activeEntries = array_merge($this->activeEntries,
129
                        (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
130
                }
131
            }
132
            // Go through table of contents and create all menu entries.
133
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
134
                $menuArray[] = $this->getMenuEntry($entry, true);
135
            }
136
        } else {
137
            // Go through table of contents and create top-level menu entries.
138
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
139
                $menuArray[] = $this->getMenuEntry($entry, false);
140
            }
141
            // Build table of contents from database.
142
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
143
144
            $allResults = $result->fetchAll();
0 ignored issues
show
Bug introduced by
The method fetchAll() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

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

144
            /** @scrutinizer ignore-call */ 
145
            $allResults = $result->fetchAll();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
146
            if (count($allResults) > 0) {
147
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
148
                $menuArray[0]['_SUB_MENU'] = [];
149
                foreach ($allResults as $resArray) {
150
                    $entry = [
151
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
152
                        'type' => $resArray['type'],
153
                        'volume' => $resArray['volume'],
154
                        'orderlabel' => $resArray['mets_orderlabel'],
155
                        'pagination' => '',
156
                        'targetUid' => $resArray['uid']
157
                    ];
158
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
159
                }
160
            }
161
        }
162
        return $menuArray;
163
    }
164
165
    /**
166
     * This builds a menu for list of 3D objects
167
     *
168
     * @access protected
169
     *
170
     * @param string $content: The PlugIn content
171
     * @param array $conf: The PlugIn configuration
172
     *
173
     * @return array HMENU array
174
     */
175
    protected function makeMenuFor3DObjects()
176
    {
177
        // Set default values for page if not set.
178
        // $this->requestData['page'] may be integer or string (physical structure @ID)
179
        if (
180
            (int) $this->requestData['page'] > 0
181
            || empty($this->requestData['page'])
182
        ) {
183
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
184
        } else {
185
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
186
        }
187
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
188
        $menuArray = [];
189
        // Is the document an external file?
190
        if (!MathUtility::canBeInterpretedAsInteger($this->requestData['id'])) {
191
            // Go through table of contents and create all menu entries.
192
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
193
                $menuArray[] = $this->getMenuEntryWithImage($entry, true);
194
            }
195
        }
196
        return $menuArray;
197
    }
198
199
    /**
200
     * This builds an array for one menu entry
201
     *
202
     * @access protected
203
     *
204
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
205
     * @param bool $recursive : Whether to include the child entries
206
     *
207
     * @return array HMENU array for menu entry
208
     */
209
    protected function getMenuEntry(array $entry, $recursive = false)
210
    {
211
        $entryArray = [];
212
        // Set "title", "volume", "type" and "pagination" from $entry array.
213
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
214
        $entryArray['volume'] = $entry['volume'];
215
        $entryArray['orderlabel'] = $entry['orderlabel'];
216
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
217
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
218
        $entryArray['_OVERRIDE_HREF'] = '';
219
        $entryArray['doNotLinkIt'] = 1;
220
        $entryArray['ITEM_STATE'] = 'NO';
221
        // Build menu links based on the $entry['points'] array.
222
        if (
223
            !empty($entry['points'])
224
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
225
        ) {
226
            $entryArray['page'] = $entry['points'];
227
228
            $entryArray['doNotLinkIt'] = 0;
229
            if ($this->settings['basketButton']) {
230
                $entryArray['basketButton'] = [
231
                    'logId' => $entry['id'],
232
                    'startpage' => $entry['points']
233
                ];
234
            }
235
        } elseif (
236
            !empty($entry['points'])
237
            && is_string($entry['points'])
238
        ) {
239
            $entryArray['id'] = $entry['points'];
240
            $entryArray['page'] = 1;
241
            $entryArray['doNotLinkIt'] = 0;
242
            if ($this->settings['basketButton']) {
243
                $entryArray['basketButton'] = [
244
                    'logId' => $entry['id'],
245
                    'startpage' => $entry['points']
246
                ];
247
            }
248
        } elseif (!empty($entry['targetUid'])) {
249
            $entryArray['id'] = $entry['targetUid'];
250
            $entryArray['page'] = 1;
251
            $entryArray['doNotLinkIt'] = 0;
252
            if ($this->settings['basketButton']) {
253
                $entryArray['basketButton'] = [
254
                    'logId' => $entry['id'],
255
                    'startpage' => $entry['targetUid']
256
                ];
257
            }
258
        }
259
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
260
        if (in_array($entry['id'], $this->activeEntries)) {
261
            $entryArray['ITEM_STATE'] = 'CUR';
262
        }
263
        // Build sub-menu if available and called recursively.
264
        if (
265
            $recursive === true
266
            && !empty($entry['children'])
267
        ) {
268
            // Build sub-menu only if one of the following conditions apply:
269
            // 1. Current menu node is in rootline
270
            // 2. Current menu node points to another file
271
            // 3. Current menu node has no corresponding images
272
            if (
273
                $entryArray['ITEM_STATE'] == 'CUR'
274
                || is_string($entry['points'])
275
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
276
            ) {
277
                $entryArray['_SUB_MENU'] = [];
278
                foreach ($entry['children'] as $child) {
279
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
280
                    if (in_array($child['id'], $this->activeEntries)) {
281
                        $entryArray['ITEM_STATE'] = 'ACT';
282
                    }
283
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
284
                }
285
            }
286
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
287
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
288
        }
289
        return $entryArray;
290
    }
291
292
    /**
293
     * This builds an array for one 3D menu entry
294
     *
295
     * @access protected
296
     *
297
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
298
     * @param bool $recursive : Whether to include the child entries
299
     *
300
     * @return array HMENU array for 3D menu entry
301
     */
302
    protected function getMenuEntryWithImage(array $entry, $recursive = false)
303
    {
304
        $entryArray = [];
305
306
        // don't filter if the entry type is collection
307
        if ($entry['type'] != 'collection') {
308
            if (!$this->isFound($entry)) {
309
                return $entryArray;
310
            }
311
        }
312
313
        // Set "title", "volume", "type" and "pagination" from $entry array.
314
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
315
        $entryArray['orderlabel'] = $entry['orderlabel'];
316
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
317
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
318
        $entryArray['doNotLinkIt'] = 1;
319
        $entryArray['ITEM_STATE'] = 'HEADER';
320
321
        if ($entry['children'] == NULL) {
322
            $entryArray['description'] = $entry['description'];
323
            $entryArray['identifier'] = $entry['identifier'];
324
            $id = str_replace("LOG", "PHYS", $entry['id']);
325
            $entryArray['image'] = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$id]['files']['THUMBS']);
326
            $entryArray['doNotLinkIt'] = 0;
327
            // index.php?tx_dlf%5Bid%5D=http%3A%2F%2Flink_to_METS_file.xml
328
            $entryArray['urlId'] = GeneralUtility::_GET('id');
329
            $entryArray['urlXml'] = $entry['points'];
330
            $entryArray['ITEM_STATE'] = 'ITEM';
331
332
            $this->addAuthorToAutocomplete($entryArray['author']);
333
            $this->addTitleToAutocomplete($entryArray['title']);
334
        }
335
336
        // Build sub-menu if available and called recursively.
337
        if (
338
            $recursive == true
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
339
            && !empty($entry['children'])
340
        ) {
341
            // Build sub-menu only if one of the following conditions apply:
342
            // 1. Current menu node points to another file
343
            // 2. Current menu node has no corresponding images
344
            if (
345
                is_string($entry['points'])
346
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
347
            ) {
348
                $entryArray['_SUB_MENU'] = [];
349
                foreach ($entry['children'] as $child) {
350
                    $menuEntry = $this->getMenuEntryWithImage($child);
351
                    if (!empty($menuEntry)) {
352
                        $entryArray['_SUB_MENU'][] = $menuEntry;
353
                    }
354
                }
355
            }
356
        }
357
        return $entryArray;
358
    }
359
360
    /**
361
     * Check or possible combinations of requested params.
362
     *
363
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
364
     *
365
     * @return bool true if found, false otherwise
366
     */
367
    private function isFound($entry) {
368
        if (!empty($this->requestData['title'] && !empty($this->requestData['types']) && !empty($this->requestData['author']))) {
369
            return $this->isTitleFound($entry) && $this->isTypeFound($entry) && $this->isAuthorFound($entry);
370
        } else if (!empty($this->requestData['title']) && !empty($this->requestData['author'])) {
371
            return $this->isTitleFound($entry) && $this->isAuthorFound($entry);
372
        } else if (!empty($this->requestData['title']) && !empty($this->requestData['types'])) {
373
            return $this->isTitleFound($entry) && $this->isTypeFound($entry);
374
        } else if (!empty($this->requestData['author']) && !empty($this->requestData['types'])) {
375
            return $this->isAuthorFound($entry) && $this->isTypeFound($entry);
376
        } else if (!empty($this->requestData['title'])) {
377
            return $this->isTitleFound($entry);
378
        } else if (!empty($this->requestData['types'])) {
379
            return $this->isTypeFound($entry);
380
        } else if (!empty($this->requestData['author'])) {
381
            return $this->isAuthorFound($entry);
382
        } else {
383
            // no parameters so entry is matching
384
            return true;
385
        }
386
    }
387
388
    /**
389
     * Check if author is found.
390
     *
391
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
392
     *
393
     * @return bool true if found, false otherwise
394
     */
395
    private function isAuthorFound($entry) {
396
        $value = strtolower($entry['author']);
397
        $author = strtolower($this->requestData['author']);
398
        return str_contains($value, $author);
399
    }
400
401
    /**
402
     * Check if title is found.
403
     *
404
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
405
     *
406
     * @return bool true if found, false otherwise
407
     */
408
    private function isTitleFound($entry) {
409
        $value = strtolower($entry['label']);
410
        $title = strtolower($this->requestData['title']);
411
        return str_contains($value, $title);
412
    }
413
414
    /**
415
     * Check if type is found.
416
     *
417
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
418
     *
419
     * @return bool true if found, false otherwise
420
     */
421
    private function isTypeFound($entry) {
422
        return str_contains($entry['identifier'], $this->requestData['types']);
423
    }
424
425
    /**
426
     * Add author to the authors autocomplete array.
427
     *
428
     * @param string $author : author to be inserted to the authors autocomplete array
429
     *
430
     * @return void
431
     */
432
    private function addAuthorToAutocomplete($author) {
433
        if ($author != NULL && !(in_array($author, $this->authors))) {
434
            // additional check if actually not more than 1 author is included
435
            if (strpos($author, ',') !== false) {
436
                $authors = explode(",", $author);
437
                foreach ($authors as $value) {
438
                    if (!(in_array(trim($value), $this->authors))) {
439
                        $this->authors[] = trim($value);
440
                    }
441
                }
442
            } else {
443
                $this->authors[] = $author;
444
            }
445
        }
446
    }
447
448
    /**
449
     * Add title to the titles autocomplete array.
450
     *
451
     * @param string $title : title to be inserted to the titles autocomplete array
452
     *
453
     * @return void
454
     */
455
    private function addTitleToAutocomplete($title) {
456
        if (!(in_array($title, $this->titles)) && $title != NULL) {
457
            $this->titles[] = $title;
458
        }
459
    }
460
461
    /**
462
     * Get all types.
463
     *
464
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
465
     *
466
     * @return array of object types
467
     */
468
    private function getTypes($entry) {
469
        $types = [];
470
        $index = 0;
471
472
        if (!empty($entry[0]['children'])) {
473
            foreach ($entry[0]['children'] as $child) {
474
                $type = $this->getType($child);
475
                if (!(in_array($type, $types)) && $type != NULL) {
476
                    $types[$index] = $type;
477
                    $index++;
478
                }
479
            }
480
        }
481
482
        return $types;
483
    }
484
485
    /**
486
     * Get single type for given entry.
487
     *
488
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
489
     *
490
     * @return string type name without number
491
     */
492
    private function getType($entry) {
493
        $type = $entry['identifier'];
494
        if (!empty($type)) {
495
            return strtok($type, ',');
496
        }
497
        return $type;
498
    }
499
}
500