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
Push — master ( 87be4c...109c48 )
by
unknown
09:58
created

TableOfContentsController::getAllLogicalUnits()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 3
nop 0
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
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
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
     * The main method of the plugin
38
     *
39
     * @access public
40
     *
41
     * @return void
42
     */
43
    public function mainAction()
44
    {
45
        // Load current document.
46
        $this->loadDocument();
47
        if ($this->isDocMissing()) {
48
            // Quit without doing anything if required variables are not set.
49
            return;
50
        } else {
51
            $this->setPage();
52
53
            $this->view->assign('toc', $this->makeMenuArray());
54
        }
55
    }
56
57
    /**
58
     * This builds a menu array for HMENU
59
     *
60
     * @access private
61
     *
62
     * @return array HMENU array
63
     */
64
    private function makeMenuArray()
65
    {
66
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
67
        $menuArray = [];
68
        // Does the document have physical elements or is it an external file?
69
        if (
70
            !empty($this->document->getDoc()->physicalStructure)
71
            || !MathUtility::canBeInterpretedAsInteger($this->requestData['id'])
72
        ) {
73
            $this->getAllLogicalUnits();
74
            // Go through table of contents and create all menu entries.
75
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
76
                $menuArray[] = $this->getMenuEntry($entry, true);
77
            }
78
        } else {
79
            // Go through table of contents and create top-level menu entries.
80
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
81
                $menuArray[] = $this->getMenuEntry($entry, false);
82
            }
83
            // Build table of contents from database.
84
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
85
86
            $allResults = $result->fetchAllAssociative();
87
88
            if (count($allResults) > 0) {
89
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
90
                $menuArray[0]['_SUB_MENU'] = [];
91
                foreach ($allResults as $resArray) {
92
                    $entry = [
93
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
94
                        'type' => $resArray['type'],
95
                        'volume' => $resArray['volume'],
96
                        'year' => $resArray['year'],
97
                        'orderlabel' => $resArray['mets_orderlabel'],
98
                        'pagination' => '',
99
                        'targetUid' => $resArray['uid']
100
                    ];
101
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
102
                }
103
            }
104
        }
105
        $this->sortMenu($menuArray);
106
        return $menuArray;
107
    }
108
109
    /**
110
     * This builds an array for one menu entry
111
     *
112
     * @access private
113
     *
114
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
115
     * @param bool $recursive : Whether to include the child entries
116
     *
117
     * @return array HMENU array for menu entry
118
     */
119
    private function getMenuEntry(array $entry, $recursive = false)
120
    {
121
        $entry = $this->resolveMenuEntry($entry);
122
123
        $entryArray = [];
124
        // Set "title", "volume", "type" and "pagination" from $entry array.
125
        $entryArray['title'] = $this->setTitle($entry);
126
        $entryArray['volume'] = $entry['volume'];
127
        $entryArray['year'] = $entry['year'];
128
        $entryArray['orderlabel'] = $entry['orderlabel'];
129
        $entryArray['type'] = $this->getTranslatedType($entry['type']);
130
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
131
        $entryArray['_OVERRIDE_HREF'] = '';
132
        $entryArray['doNotLinkIt'] = 1;
133
        $entryArray['ITEM_STATE'] = 'NO';
134
135
        // Build menu links based on the $entry['points'] array.
136
        if (
137
            !empty($entry['points'])
138
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
139
        ) {
140
            $entryArray['page'] = $entry['points'];
141
142
            $entryArray['doNotLinkIt'] = 0;
143
            if ($this->settings['basketButton']) {
144
                $entryArray['basketButton'] = [
145
                    'logId' => $entry['id'],
146
                    'startpage' => $entry['points']
147
                ];
148
            }
149
        } elseif (
150
            !empty($entry['points'])
151
            && is_string($entry['points'])
152
        ) {
153
            $entryArray['id'] = $entry['points'];
154
            $entryArray['page'] = 1;
155
            $entryArray['doNotLinkIt'] = 0;
156
            if ($this->settings['basketButton']) {
157
                $entryArray['basketButton'] = [
158
                    'logId' => $entry['id'],
159
                    'startpage' => $entry['points']
160
                ];
161
            }
162
        } elseif (!empty($entry['targetUid'])) {
163
            $entryArray['id'] = $entry['targetUid'];
164
            $entryArray['page'] = 1;
165
            $entryArray['doNotLinkIt'] = 0;
166
            if ($this->settings['basketButton']) {
167
                $entryArray['basketButton'] = [
168
                    'logId' => $entry['id'],
169
                    'startpage' => $entry['targetUid']
170
                ];
171
            }
172
        }
173
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
174
        if (in_array($entry['id'], $this->activeEntries)) {
175
            $entryArray['ITEM_STATE'] = 'CUR';
176
        }
177
        // Build sub-menu if available and called recursively.
178
        if (
179
            $recursive === true
180
            && !empty($entry['children'])
181
        ) {
182
            // Build sub-menu only if one of the following conditions apply:
183
            // 1. Current menu node is in rootline
184
            // 2. Current menu node points to another file
185
            // 3. Current menu node has no corresponding images
186
            if (
187
                $entryArray['ITEM_STATE'] == 'CUR'
188
                || is_string($entry['points'])
189
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
190
            ) {
191
                $entryArray['_SUB_MENU'] = [];
192
                foreach ($entry['children'] as $child) {
193
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
194
                    if (in_array($child['id'], $this->activeEntries)) {
195
                        $entryArray['ITEM_STATE'] = 'ACT';
196
                    }
197
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
198
                }
199
            }
200
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
201
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
202
        }
203
        return $entryArray;
204
    }
205
206
    /**
207
     * If $entry references an external METS file (as mptr),
208
     * try to resolve its database UID and return an updated $entry.
209
     *
210
     * This is so that when linking from a child document back to its parent,
211
     * that link is via UID, so that subsequently the parent's TOC is built from database.
212
     *
213
     * @access private
214
     *
215
     * @param array $entry
216
     * @return array
217
     */
218
    private function resolveMenuEntry($entry)
219
    {
220
        // If the menu entry points to the parent document,
221
        // resolve to the parent UID set on indexation.
222
        $doc = $this->document->getDoc();
223
        if (
224
            $doc instanceof MetsDocument
225
            && ($entry['points'] === $doc->parentHref || $this->isMultiElement($entry['type']))
226
            && !empty($this->document->getPartof())
227
        ) {
228
            unset($entry['points']);
229
            $entry['targetUid'] = $this->document->getPartof();
230
        }
231
232
        return $entry;
233
    }
234
235
    /**
236
     * Get all logical units the current page or track is a part of.
237
     *
238
     * @access private
239
     *
240
     * @return void
241
     */
242
    private function getAllLogicalUnits() {
243
        if (
244
            !empty($this->requestData['page'])
245
            && !empty($this->document->getDoc()->physicalStructure)
246
        ) {
247
            $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
248
                (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
249
            if (
250
                !empty($this->requestData['double'])
251
                && $this->requestData['page'] < $this->document->getDoc()->numPages
252
            ) {
253
                $this->activeEntries = array_merge($this->activeEntries,
254
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
255
            }
256
        }
257
    }
258
259
    /**
260
     * Get translated type of entry.
261
     *
262
     * @access private
263
     *
264
     * @param string $type
265
     *
266
     * @return string
267
     */
268
    private function getTranslatedType($type) {
269
        return Helper::translate($type, 'tx_dlf_structures', $this->settings['storagePid']);
270
    }
271
272
    /**
273
     * Check if element has type 'multivolume_work' or 'multipart_manuscript'.
274
     * For Kitodo.Production prior to version 3.x, hierarchical child documents
275
     * always come with their own METS file for their parent document, even
276
     * if multiple documents in fact have the same parent. To make sure that all
277
     * of them point to the same parent document in Kitodo.Presentation, we
278
     * need some workaround here.
279
     *
280
     * @todo Should be removed when Kitodo.Production 2.x is no longer supported.
281
     *
282
     * @access private
283
     *
284
     * @param string $type
285
     *
286
     * @return bool
287
     */
288
    private function isMultiElement($type) {
289
        return $type === 'multivolume_work' || $type === 'multipart_manuscript';
290
    }
291
    /**
292
     * Set title from entry.
293
     *
294
     * @access private
295
     *
296
     * @param array $entry
297
     *
298
     * @return string
299
     */
300
    private function setTitle($entry) {
301
        if (empty($entry['label']) && empty($entry['orderlabel'])) {
302
            foreach ($this->settings['titleReplacements'] as $titleReplacement) {
303
                if ($entry['type'] == $titleReplacement['type']) {
304
                    $fields = explode(",", $titleReplacement['fields']);
305
                    $title = '';
306
                    foreach ($fields as $field) {
307
                        if ($field == 'type') {
308
                            $title .= $this->getTranslatedType($entry['type']) . ' ';
309
                        } else {
310
                            $title .= $entry[$field] . ' ';
311
                        }
312
                    }
313
        
314
                    return trim($title);
315
                }
316
            }
317
        }
318
        return $entry['label'] ?: $entry['orderlabel'];
319
    }
320
321
    /**
322
     * Sort menu by orderlabel.
323
     *
324
     * @access private
325
     *
326
     * @param array &$menu
327
     *
328
     * @return void
329
     */
330
    private function sortMenu(&$menu) {
331
        if ($menu[0]['type'] == $this->getTranslatedType("newspaper")) {
332
            $this->sortSubMenu($menu);
333
        }
334
        if ($menu[0]['type'] == $this->getTranslatedType("year")) {
335
            $this->sortSubMenu($menu);
336
        }
337
    }
338
339
    /**
340
     * Sort sub menu e.g years of the newspaper by orderlabel.
341
     *
342
     * @access private
343
     *
344
     * @param array &$menu
345
     *
346
     * @return void
347
     */
348
    private function sortSubMenu(&$menu) {
349
        usort($menu[0]['_SUB_MENU'], function ($firstElement, $secondElement) {
350
            if (!empty($firstElement['orderlabel'])) {
351
                return $firstElement['orderlabel'] <=> $secondElement['orderlabel'];
352
            }
353
            return $firstElement['year'] <=> $secondElement['year'];
354
        });
355
    }
356
}
357