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 (#833)
by Beatrycze
03:30
created

TableOfContentsController::getTypes()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.6111
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
    protected function getMenuEntryWithImage(array $entry, $recursive = false)
292
    {
293
        $entryArray = [];
294
295
        // don't filter if the entry type is collection or search params are empty
296
        if ($entry['type'] != 'collection') {
297
            // currently only title filtering is defined
298
            if (!empty($this->requestData['title']) && !empty($this->requestData['types'])) {
299
                $label = strtolower($entry['label']);
300
                $title = strtolower($this->requestData['title']);
301
                if (!str_contains($label, $title) && !str_contains($entry['identifier'], $this->requestData['types'])) {
302
                    return $entryArray;
303
                }
304
            } else if (!empty($this->requestData['title'])) {
305
                $label = strtolower($entry['label']);
306
                $title = strtolower($this->requestData['title']);
307
                if (!str_contains($label, $title)) {
308
                    return $entryArray;
309
                }
310
            } else if (!empty($this->requestData['types'])) {
311
                if (!str_contains($entry['identifier'], $this->requestData['types'])) {
312
                    return $entryArray;
313
                }
314
            }
315
        }
316
317
        // Set "title", "volume", "type" and "pagination" from $entry array.
318
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
319
        $entryArray['orderlabel'] = $entry['orderlabel'];
320
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
321
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
322
        $entryArray['doNotLinkIt'] = 1;
323
        $entryArray['ITEM_STATE'] = 'HEADER';
324
325
        if ($entry['children'] === null) {
326
            $entryArray['description'] = $entry['description'];
327
            $id = $this->document->getDoc()->smLinks['l2p'][$entry['id']][0];
328
            $entryArray['image'] = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$id]['files']['THUMBS']);
329
            $entryArray['doNotLinkIt'] = 0;
330
            // index.php?tx_dlf%5Bid%5D=http%3A%2F%2Flink_to_METS_file.xml
331
            $entryArray['urlId'] = GeneralUtility::_GET('id');
332
            $entryArray['urlXml'] = $entry['points'];
333
            $entryArray['ITEM_STATE'] = 'ITEM';
334
        }
335
336
        // Build sub-menu if available and called recursively.
337
        if (
338
            $recursive == true
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
    private function getTypes($entry) {
361
        $types = [];
362
        $index = 0;
363
364
        if (!empty($entry[0]['children'])) {
365
            foreach ($entry[0]['children'] as $child) {
366
                $type = $this->getType($child);
367
                if (!(in_array($type, $types)) && $type != NULL) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $type of type mixed|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
368
                    $types[$index] = $type;
369
                    $index++;
370
                }
371
            }
372
        }
373
374
        return $types;
375
    }
376
377
    private function getType($entry) {
378
        $type = $entry['identifier'];
379
        if (!empty($type)) {
380
            return strtok($type, ',');
381
        }
382
        return $type;
383
    }
384
}
385