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

TableOfContentsController::getType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

127
            /** @scrutinizer ignore-call */ 
128
            $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...
128
129
            if (count($allResults) > 0) {
130
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
131
                $menuArray[0]['_SUB_MENU'] = [];
132
                foreach ($allResults as $resArray) {
133
                    $entry = [
134
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
135
                        'type' => $resArray['type'],
136
                        'volume' => $resArray['volume'],
137
                        'orderlabel' => $resArray['mets_orderlabel'],
138
                        'pagination' => '',
139
                        'targetUid' => $resArray['uid']
140
                    ];
141
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
142
                }
143
            }
144
        }
145
        return $menuArray;
146
    }
147
148
    /**
149
     * This builds a menu for list of 3D objects
150
     *
151
     * @access protected
152
     *
153
     * @param string $content: The PlugIn content
154
     * @param array $conf: The PlugIn configuration
155
     *
156
     * @return array HMENU array
157
     */
158
    protected function makeMenuFor3DObjects()
159
    {
160
        $menuArray = [];
161
162
        // Go through table of contents and create all menu entries.
163
        foreach ($this->document->getDoc()->tableOfContents as $entry) {
164
            $menuArray[] = $this->getMenuEntryWithImage($entry, true);
165
        }
166
        return $menuArray;
167
    }
168
169
    /**
170
     * This builds an array for one menu entry
171
     *
172
     * @access protected
173
     *
174
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
175
     * @param bool $recursive : Whether to include the child entries
176
     *
177
     * @return array HMENU array for menu entry
178
     */
179
    protected function getMenuEntry(array $entry, $recursive = false)
180
    {
181
        $entry = $this->resolveMenuEntry($entry);
182
183
        $entryArray = [];
184
        // Set "title", "volume", "type" and "pagination" from $entry array.
185
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
186
        $entryArray['volume'] = $entry['volume'];
187
        $entryArray['orderlabel'] = $entry['orderlabel'];
188
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
189
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
190
        $entryArray['_OVERRIDE_HREF'] = '';
191
        $entryArray['doNotLinkIt'] = 1;
192
        $entryArray['ITEM_STATE'] = 'NO';
193
        // Build menu links based on the $entry['points'] array.
194
        if (
195
            !empty($entry['points'])
196
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
197
        ) {
198
            $entryArray['page'] = $entry['points'];
199
200
            $entryArray['doNotLinkIt'] = 0;
201
            if ($this->settings['basketButton']) {
202
                $entryArray['basketButton'] = [
203
                    'logId' => $entry['id'],
204
                    'startpage' => $entry['points']
205
                ];
206
            }
207
        } elseif (
208
            !empty($entry['points'])
209
            && is_string($entry['points'])
210
        ) {
211
            $entryArray['id'] = $entry['points'];
212
            $entryArray['page'] = 1;
213
            $entryArray['doNotLinkIt'] = 0;
214
            if ($this->settings['basketButton']) {
215
                $entryArray['basketButton'] = [
216
                    'logId' => $entry['id'],
217
                    'startpage' => $entry['points']
218
                ];
219
            }
220
        } elseif (!empty($entry['targetUid'])) {
221
            $entryArray['id'] = $entry['targetUid'];
222
            $entryArray['page'] = 1;
223
            $entryArray['doNotLinkIt'] = 0;
224
            if ($this->settings['basketButton']) {
225
                $entryArray['basketButton'] = [
226
                    'logId' => $entry['id'],
227
                    'startpage' => $entry['targetUid']
228
                ];
229
            }
230
        }
231
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
232
        if (in_array($entry['id'], $this->activeEntries)) {
233
            $entryArray['ITEM_STATE'] = 'CUR';
234
        }
235
        // Build sub-menu if available and called recursively.
236
        if (
237
            $recursive === true
238
            && !empty($entry['children'])
239
        ) {
240
            // Build sub-menu only if one of the following conditions apply:
241
            // 1. Current menu node is in rootline
242
            // 2. Current menu node points to another file
243
            // 3. Current menu node has no corresponding images
244
            if (
245
                $entryArray['ITEM_STATE'] == 'CUR'
246
                || is_string($entry['points'])
247
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
248
            ) {
249
                $entryArray['_SUB_MENU'] = [];
250
                foreach ($entry['children'] as $child) {
251
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
252
                    if (in_array($child['id'], $this->activeEntries)) {
253
                        $entryArray['ITEM_STATE'] = 'ACT';
254
                    }
255
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
256
                }
257
            }
258
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
259
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
260
        }
261
        return $entryArray;
262
    }
263
264
    /**
265
     * If $entry references an external METS file (as mptr),
266
     * try to resolve its database UID and return an updated $entry.
267
     *
268
     * This is so that when linking from a child document back to its parent,
269
     * that link is via UID, so that subsequently the parent's TOC is built from database.
270
     *
271
     * @param array $entry
272
     * @return array
273
     */
274
    protected function resolveMenuEntry($entry)
275
    {
276
        // If the menu entry points to the parent document,
277
        // resolve to the parent UID set on indexation.
278
        $doc = $this->document->getDoc();
279
        if (
280
            $doc instanceof MetsDocument
281
            && $entry['points'] === $doc->parentHref
282
            && !empty($this->document->getPartof())
283
        ) {
284
            unset($entry['points']);
285
            $entry['targetUid'] = $this->document->getPartof();
286
        }
287
288
        return $entry;
289
    }
290
291
    /**
292
     * This builds an array for one 3D menu entry
293
     *
294
     * @access protected
295
     *
296
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
297
     * @param bool $recursive : Whether to include the child entries
298
     *
299
     * @return array HMENU array for 3D menu entry
300
     */
301
    protected function getMenuEntryWithImage(array $entry, $recursive = false)
302
    {
303
        $entryArray = [];
304
305
        // don't filter if the entry type is collection
306
        if ($entry['type'] != 'collection') {
307
            if (!$this->isFound($entry)) {
308
                return $entryArray;
309
            }
310
        }
311
312
        // Set "title", "volume", "type" and "pagination" from $entry array.
313
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
314
        $entryArray['orderlabel'] = $entry['orderlabel'];
315
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
316
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
317
        $entryArray['doNotLinkIt'] = 1;
318
        $entryArray['ITEM_STATE'] = 'HEADER';
319
320
        if ($entry['children'] === null) {
321
            $entryArray['description'] = $entry['description'];
322
            $id = $this->document->getDoc()->smLinks['l2p'][$entry['id']][0];
323
            $entryArray['image'] = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$id]['files']['THUMBS']);
324
            $entryArray['doNotLinkIt'] = 0;
325
            // index.php?tx_dlf%5Bid%5D=http%3A%2F%2Flink_to_METS_file.xml
326
            $entryArray['urlId'] = GeneralUtility::_GET('id');
327
            $entryArray['urlXml'] = $entry['points'];
328
            $entryArray['ITEM_STATE'] = 'ITEM';
329
        }
330
331
        // Build sub-menu if available and called recursively.
332
        if (
333
            $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...
334
            && !empty($entry['children'])
335
        ) {
336
            // Build sub-menu only if one of the following conditions apply:
337
            // 1. Current menu node points to another file
338
            // 2. Current menu node has no corresponding images
339
            if (
340
                is_string($entry['points'])
341
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
342
            ) {
343
                $entryArray['_SUB_MENU'] = [];
344
                foreach ($entry['children'] as $child) {
345
                    $menuEntry = $this->getMenuEntryWithImage($child);
346
                    if (!empty($menuEntry)) {
347
                        $entryArray['_SUB_MENU'][] = $menuEntry;
348
                    }
349
                }
350
            }
351
        }
352
        return $entryArray;
353
    }
354
355
    /**
356
     * Check or possible combinations of requested params.
357
     *
358
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
359
     *
360
     * @return bool true if found, false otherwise
361
     */
362
    private function isFound($entry) {
363
        if (!empty($this->requestData['title'] && !empty($this->requestData['types']) && !empty($this->requestData['author']))) {
364
            return $this->isTitleFound($entry) && $this->isTypeFound($entry) && $this->isAuthorFound($entry);
365
        } else if (!empty($this->requestData['title']) && !empty($this->requestData['author'])) {
366
            return $this->isTitleFound($entry) && $this->isAuthorFound($entry);
367
        } else if (!empty($this->requestData['title']) && !empty($this->requestData['types'])) {
368
            return $this->isTitleFound($entry) && $this->isTypeFound($entry);
369
        } else if (!empty($this->requestData['author']) && !empty($this->requestData['types'])) {
370
            return $this->isAuthorFound($entry) && $this->isTypeFound($entry);
371
        } else if (!empty($this->requestData['title'])) {
372
            return $this->isTitleFound($entry);
373
        } else if (!empty($this->requestData['types'])) {
374
            return $this->isTypeFound($entry);
375
        } else if (!empty($this->requestData['author'])) {
376
            return $this->isAuthorFound($entry);
377
        } else {
378
            // no parameters so entry is matching
379
            return true;
380
        }
381
    }
382
383
    /**
384
     * Check if author is found.
385
     *
386
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
387
     *
388
     * @return bool true if found, false otherwise
389
     */
390
    private function isAuthorFound($entry) {
391
        $value = strtolower($entry['author']);
392
        $author = strtolower($this->requestData['author']);
393
        return str_contains($value, $author);
394
    }
395
396
    /**
397
     * Check if title is found.
398
     *
399
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
400
     *
401
     * @return bool true if found, false otherwise
402
     */
403
    private function isTitleFound($entry) {
404
        $value = strtolower($entry['label']);
405
        $title = strtolower($this->requestData['title']);
406
        return str_contains($value, $title);
407
    }
408
409
    /**
410
     * Check if type is found.
411
     *
412
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
413
     *
414
     * @return bool true if found, false otherwise
415
     */
416
    private function isTypeFound($entry) {
417
        return str_contains($entry['identifier'], $this->requestData['types']);
418
    }
419
420
    /**
421
     * Get all types.
422
     *
423
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
424
     *
425
     * @return array of object types
426
     */
427
    private function getTypes($entry) {
428
        $types = [];
429
        $index = 0;
430
431
        if (!empty($entry[0]['children'])) {
432
            foreach ($entry[0]['children'] as $child) {
433
                $type = $this->getType($child);
434
                if (!(in_array($type, $types)) && $type != NULL) {
435
                    $types[$index] = $type;
436
                    $index++;
437
                }
438
            }
439
        }
440
441
        return $types;
442
    }
443
444
    /**
445
     * Get single type for given entry.
446
     *
447
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
448
     *
449
     * @return string type name without number
450
     */
451
    private function getType($entry) {
452
        $type = $entry['identifier'];
453
        if (!empty($type)) {
454
            return strtok($type, ',');
455
        }
456
        return $type;
457
    }
458
}
459