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 (#852)
by Beatrycze
07:08 queued 03:29
created

TableOfContentsController::isAuthorFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
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
     * This holds the list of authors for autocomplete
39
     *
40
     * @var array
41
     * @access protected
42
     */
43
    protected $authors = [];
44
45
    /**
46
     * This holds the list of titles for autocomplete
47
     *
48
     * @var array
49
     * @access protected
50
     */
51
    protected $titles = [];
52
53
    /**
54
     * The main method of the plugin
55
     *
56
     * @return void
57
     */
58
    public function mainAction()
59
    {
60
        // Load current document.
61
        $this->loadDocument($this->requestData);
62
        if (
63
            $this->document === null
64
            || $this->document->getDoc() === null
65
        ) {
66
            // Quit without doing anything if required variables are not set.
67
            return;
68
        } else {
69
            if (!empty($this->requestData['logicalPage'])) {
70
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
71
                // The logical page parameter should not appear again
72
                unset($this->requestData['logicalPage']);
73
            }
74
            if ($this->document->getDoc()->tableOfContents[0]['type'] == 'collection') {
75
                $this->view->assign('currentList', $this->requestData['id']);
76
                if (isset($this->requestData['transform'])) {
77
                    $this->view->assign('transform', $this->requestData['transform']);
78
                } else {
79
                    $this->view->assign('transform', 'something');
80
                }
81
                $this->view->assign('type', 'collection');
82
                $this->view->assign('types', $this->getTypes($this->document->getDoc()->tableOfContents));
83
                $this->view->assign('toc', $this->makeMenuFor3DObjects());
84
                $this->sortAuthors();
85
                $this->view->assign('authors', $this->authors);
86
                natcasesort($this->titles);
87
                $this->view->assign('titles', $this->titles);
88
            } else {
89
                $this->view->assign('type', 'other');
90
                $this->view->assign('toc', $this->makeMenuArray());
91
            }
92
        }
93
    }
94
95
    /**
96
     * This builds a menu array for HMENU
97
     *
98
     * @access protected
99
     * @return array HMENU array
100
     */
101
    protected function makeMenuArray()
102
    {
103
        // Set default values for page if not set.
104
        // $this->requestData['page'] may be integer or string (physical structure @ID)
105
        if (
106
            (int) $this->requestData['page'] > 0
107
            || empty($this->requestData['page'])
108
        ) {
109
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
110
        } else {
111
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
112
        }
113
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
114
        $menuArray = [];
115
        // Does the document have physical elements or is it an external file?
116
        if (
117
            !empty($this->document->getDoc()->physicalStructure)
118
            || !MathUtility::canBeInterpretedAsInteger($this->requestData['id'])
119
        ) {
120
            // Get all logical units the current page or track is a part of.
121
            if (
122
                !empty($this->requestData['page'])
123
                && !empty($this->document->getDoc()->physicalStructure)
124
            ) {
125
                $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
126
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
127
                if (
128
                    !empty($this->requestData['double'])
129
                    && $this->requestData['page'] < $this->document->getDoc()->numPages
130
                ) {
131
                    $this->activeEntries = array_merge($this->activeEntries,
132
                        (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
133
                }
134
            }
135
            // Go through table of contents and create all menu entries.
136
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
137
                $menuArray[] = $this->getMenuEntry($entry, true);
138
            }
139
        } else {
140
            // Go through table of contents and create top-level menu entries.
141
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
142
                $menuArray[] = $this->getMenuEntry($entry, false);
143
            }
144
            // Build table of contents from database.
145
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
146
147
            $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

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