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 ( e3d09d...c5a926 )
by
unknown
03:19
created

TableOfContentsController::sortMenuForNewspapers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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