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
08:52
created

TableOfContentsController::resolveMenuEntry()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 15
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
        // Is the document an external file?
162
        if (!MathUtility::canBeInterpretedAsInteger($this->requestData['id'])) {
163
            // Go through table of contents and create all menu entries.
164
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
165
                $menuArray[] = $this->getMenuEntryWithImage($entry, true);
166
            }
167
        }
168
        return $menuArray;
169
    }
170
171
    /**
172
     * This builds an array for one menu entry
173
     *
174
     * @access protected
175
     *
176
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
177
     * @param bool $recursive : Whether to include the child entries
178
     *
179
     * @return array HMENU array for menu entry
180
     */
181
    protected function getMenuEntry(array $entry, $recursive = false)
182
    {
183
        $entry = $this->resolveMenuEntry($entry);
184
185
        $entryArray = [];
186
        // Set "title", "volume", "type" and "pagination" from $entry array.
187
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
188
        $entryArray['volume'] = $entry['volume'];
189
        $entryArray['orderlabel'] = $entry['orderlabel'];
190
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
191
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
192
        $entryArray['_OVERRIDE_HREF'] = '';
193
        $entryArray['doNotLinkIt'] = 1;
194
        $entryArray['ITEM_STATE'] = 'NO';
195
        // Build menu links based on the $entry['points'] array.
196
        if (
197
            !empty($entry['points'])
198
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
199
        ) {
200
            $entryArray['page'] = $entry['points'];
201
202
            $entryArray['doNotLinkIt'] = 0;
203
            if ($this->settings['basketButton']) {
204
                $entryArray['basketButton'] = [
205
                    'logId' => $entry['id'],
206
                    'startpage' => $entry['points']
207
                ];
208
            }
209
        } elseif (
210
            !empty($entry['points'])
211
            && is_string($entry['points'])
212
        ) {
213
            $entryArray['id'] = $entry['points'];
214
            $entryArray['page'] = 1;
215
            $entryArray['doNotLinkIt'] = 0;
216
            if ($this->settings['basketButton']) {
217
                $entryArray['basketButton'] = [
218
                    'logId' => $entry['id'],
219
                    'startpage' => $entry['points']
220
                ];
221
            }
222
        } elseif (!empty($entry['targetUid'])) {
223
            $entryArray['id'] = $entry['targetUid'];
224
            $entryArray['page'] = 1;
225
            $entryArray['doNotLinkIt'] = 0;
226
            if ($this->settings['basketButton']) {
227
                $entryArray['basketButton'] = [
228
                    'logId' => $entry['id'],
229
                    'startpage' => $entry['targetUid']
230
                ];
231
            }
232
        }
233
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
234
        if (in_array($entry['id'], $this->activeEntries)) {
235
            $entryArray['ITEM_STATE'] = 'CUR';
236
        }
237
        // Build sub-menu if available and called recursively.
238
        if (
239
            $recursive === true
240
            && !empty($entry['children'])
241
        ) {
242
            // Build sub-menu only if one of the following conditions apply:
243
            // 1. Current menu node is in rootline
244
            // 2. Current menu node points to another file
245
            // 3. Current menu node has no corresponding images
246
            if (
247
                $entryArray['ITEM_STATE'] == 'CUR'
248
                || is_string($entry['points'])
249
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
250
            ) {
251
                $entryArray['_SUB_MENU'] = [];
252
                foreach ($entry['children'] as $child) {
253
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
254
                    if (in_array($child['id'], $this->activeEntries)) {
255
                        $entryArray['ITEM_STATE'] = 'ACT';
256
                    }
257
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
258
                }
259
            }
260
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
261
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
262
        }
263
        return $entryArray;
264
    }
265
266
    /**
267
     * If $entry references an external METS file (as mptr),
268
     * try to resolve its database UID and return an updated $entry.
269
     *
270
     * This is so that when linking from a child document back to its parent,
271
     * that link is via UID, so that subsequently the parent's TOC is built from database.
272
     *
273
     * @param array $entry
274
     * @return array
275
     */
276
    protected function resolveMenuEntry($entry)
277
    {
278
        // If the menu entry points to the parent document,
279
        // resolve to the parent UID set on indexation.
280
        $doc = $this->document->getDoc();
281
        if (
282
            $doc instanceof MetsDocument
283
            && $entry['points'] === $doc->parentHref
284
            && !empty($this->document->getPartof())
285
        ) {
286
            unset($entry['points']);
287
            $entry['targetUid'] = $this->document->getPartof();
288
        }
289
290
        return $entry;
291
    }
292
293
    protected function getMenuEntryWithImage(array $entry, $recursive = false)
294
    {
295
        $entryArray = [];
296
297
        // don't filter if the entry type is collection or search params are empty
298
        if ($entry['type'] != 'collection') {
299
            // currently only title filtering is defined
300
            if (!empty($this->requestData['title']) && !empty($this->requestData['types'])) {
301
                $label = strtolower($entry['label']);
302
                $title = strtolower($this->requestData['title']);
303
                if (!str_contains($label, $title) && !str_contains($entry['identifier'], $this->requestData['types'])) {
304
                    return $entryArray;
305
                }
306
            } else if (!empty($this->requestData['title'])) {
307
                $label = strtolower($entry['label']);
308
                $title = strtolower($this->requestData['title']);
309
                if (!str_contains($label, $title)) {
310
                    return $entryArray;
311
                }
312
            } else if (!empty($this->requestData['types'])) {
313
                if (!str_contains($entry['identifier'], $this->requestData['types'])) {
314
                    return $entryArray;
315
                }
316
            }
317
        }
318
319
        // Set "title", "volume", "type" and "pagination" from $entry array.
320
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
321
        $entryArray['orderlabel'] = $entry['orderlabel'];
322
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
323
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
324
        $entryArray['doNotLinkIt'] = 1;
325
        $entryArray['ITEM_STATE'] = 'HEADER';
326
327
        if ($entry['children'] == NULL) {
328
            $entryArray['description'] = $entry['description'];
329
            $entryArray['identifier'] = $entry['identifier'];
330
            $id = str_replace("LOG", "PHYS", $entry['id']);
331
            $entryArray['image'] = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$id]['files']['THUMBS']);
332
            $entryArray['doNotLinkIt'] = 0;
333
            // index.php?tx_dlf%5Bid%5D=http%3A%2F%2Flink_to_METS_file.xml
334
            $entryArray['urlId'] = GeneralUtility::_GET('id');
335
            $entryArray['urlXml'] = $entry['points'];
336
            $entryArray['ITEM_STATE'] = 'ITEM';
337
        }
338
339
        // Build sub-menu if available and called recursively.
340
        if (
341
            $recursive == true
342
            && !empty($entry['children'])
343
        ) {
344
            // Build sub-menu only if one of the following conditions apply:
345
            // 1. Current menu node points to another file
346
            // 2. Current menu node has no corresponding images
347
            if (
348
                is_string($entry['points'])
349
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
350
            ) {
351
                $entryArray['_SUB_MENU'] = [];
352
                foreach ($entry['children'] as $child) {
353
                    $menuEntry = $this->getMenuEntryWithImage($child);
354
                    if (!empty($menuEntry)) {
355
                        $entryArray['_SUB_MENU'][] = $menuEntry;
356
                    }
357
                }
358
            }
359
        }
360
        return $entryArray;
361
    }
362
363
    private function getTypes($entry) {
364
        $types = [];
365
        $index = 0;
366
367
        if (!empty($entry[0]['children'])) {
368
            foreach ($entry[0]['children'] as $child) {
369
                $type = $this->getType($child);
370
                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...
371
                    $types[$index] = $type;
372
                    $index++;
373
                }
374
            }
375
        }
376
377
        return $types;
378
    }
379
380
    private function getType($entry) {
381
        $type = $entry['identifier'];
382
        if (!empty($type)) {
383
            return strtok($type, ',');
384
        }
385
        return $type;
386
    }
387
}
388