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

TableOfContentsController::makeMenuFor3DObjects()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 27
rs 9.2222
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 TYPO3\CMS\Core\Utility\MathUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
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
     * This builds an array for one menu entry
38
     *
39
     * @access protected
40
     *
41
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
42
     * @param bool $recursive : Whether to include the child entries
43
     *
44
     * @return array HMENU array for menu entry
45
     */
46
    protected function getMenuEntry(array $entry, $recursive = false)
47
    {
48
        $entryArray = [];
49
        // Set "title", "volume", "type" and "pagination" from $entry array.
50
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
51
        $entryArray['volume'] = $entry['volume'];
52
        $entryArray['orderlabel'] = $entry['orderlabel'];
53
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
54
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
55
        $entryArray['_OVERRIDE_HREF'] = '';
56
        $entryArray['doNotLinkIt'] = 1;
57
        $entryArray['ITEM_STATE'] = 'NO';
58
        // Build menu links based on the $entry['points'] array.
59
        if (
60
            !empty($entry['points'])
61
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
62
        ) {
63
            $entryArray['page'] = $entry['points'];
64
65
            $entryArray['doNotLinkIt'] = 0;
66
            if ($this->settings['basketButton']) {
67
                $entryArray['basketButton'] = [
68
                    'logId' => $entry['id'],
69
                    'startpage' => $entry['points']
70
                ];
71
            }
72
        } elseif (
73
            !empty($entry['points'])
74
            && is_string($entry['points'])
75
        ) {
76
            $entryArray['id'] = $entry['points'];
77
            $entryArray['page'] = 1;
78
            $entryArray['doNotLinkIt'] = 0;
79
            if ($this->settings['basketButton']) {
80
                $entryArray['basketButton'] = [
81
                    'logId' => $entry['id'],
82
                    'startpage' => $entry['points']
83
                ];
84
            }
85
        } elseif (!empty($entry['targetUid'])) {
86
            $entryArray['id'] = $entry['targetUid'];
87
            $entryArray['page'] = 1;
88
            $entryArray['doNotLinkIt'] = 0;
89
            if ($this->settings['basketButton']) {
90
                $entryArray['basketButton'] = [
91
                    'logId' => $entry['id'],
92
                    'startpage' => $entry['targetUid']
93
                ];
94
            }
95
        }
96
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
97
        if (in_array($entry['id'], $this->activeEntries)) {
98
            $entryArray['ITEM_STATE'] = 'CUR';
99
        }
100
        // Build sub-menu if available and called recursively.
101
        if (
102
            $recursive === true
103
            && !empty($entry['children'])
104
        ) {
105
            // Build sub-menu only if one of the following conditions apply:
106
            // 1. Current menu node is in rootline
107
            // 2. Current menu node points to another file
108
            // 3. Current menu node has no corresponding images
109
            if (
110
                $entryArray['ITEM_STATE'] == 'CUR'
111
                || is_string($entry['points'])
112
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
113
            ) {
114
                $entryArray['_SUB_MENU'] = [];
115
                foreach ($entry['children'] as $child) {
116
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
117
                    if (in_array($child['id'], $this->activeEntries)) {
118
                        $entryArray['ITEM_STATE'] = 'ACT';
119
                    }
120
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
121
                }
122
            }
123
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
124
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
125
        }
126
        return $entryArray;
127
    }
128
129
    /**
130
     * The main method of the plugin
131
     *
132
     * @return void
133
     */
134
    public function mainAction()
135
    {
136
        // Load current document.
137
        $this->loadDocument($this->requestData);
138
        if (
139
            $this->document === null
140
            || $this->document->getDoc() === null
141
        ) {
142
            // Quit without doing anything if required variables are not set.
143
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type void.
Loading history...
144
        } else {
145
            if ($this->document->getDoc()->metadataArray['LOG_0000']['type'][0] != 'collection') {
146
                $this->view->assign('toc', $this->makeMenuArray());
147
            } else {
148
                $this->view->assign('toc', $this->makeMenuFor3DObjects());
0 ignored issues
show
Bug introduced by
The call to Kitodo\Dlf\Controller\Ta...:makeMenuFor3DObjects() has too few arguments starting with content. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
                $this->view->assign('toc', $this->/** @scrutinizer ignore-call */ makeMenuFor3DObjects());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
149
            }
150
        }
151
    }
152
153
    /**
154
     * This builds a menu array for HMENU
155
     *
156
     * @access public
157
     * @return array HMENU array
158
     */
159
    public function makeMenuArray()
160
    {
161
        if (!empty($this->requestData['logicalPage'])) {
162
             $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
163
            // The logical page parameter should not appear again
164
            unset($this->requestData['logicalPage']);
165
        }
166
        // Set default values for page if not set.
167
        // $this->requestData['page'] may be integer or string (physical structure @ID)
168
        if (
169
            (int) $this->requestData['page'] > 0
170
            || empty($this->requestData['page'])
171
        ) {
172
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
173
        } else {
174
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
175
        }
176
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
177
        $menuArray = [];
178
        // Does the document have physical elements or is it an external file?
179
        if (
180
            !empty($this->document->getDoc()->physicalStructure)
181
            || !MathUtility::canBeInterpretedAsInteger($this->document->getDoc()->uid)
0 ignored issues
show
Bug Best Practice introduced by
The property uid does not exist on Kitodo\Dlf\Common\Doc. Since you implemented __get, consider adding a @property annotation.
Loading history...
182
        ) {
183
            // Get all logical units the current page or track is a part of.
184
            if (
185
                !empty($this->requestData['page'])
186
                && !empty($this->document->getDoc()->physicalStructure)
187
            ) {
188
                $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
189
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
190
                if (
191
                    !empty($this->requestData['double'])
192
                    && $this->requestData['page'] < $this->document->getDoc()->numPages
193
                ) {
194
                    $this->activeEntries = array_merge($this->activeEntries,
195
                        (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
196
                }
197
            }
198
            // Go through table of contents and create all menu entries.
199
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
200
                $menuArray[] = $this->getMenuEntry($entry, true);
201
            }
202
        } else {
203
            // Go through table of contents and create top-level menu entries.
204
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
205
                $menuArray[] = $this->getMenuEntry($entry, false);
206
            }
207
            // Build table of contents from database.
208
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
209
210
            $allResults = $result->fetchAll();
211
212
            if (count($allResults) > 0) {
213
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
214
                $menuArray[0]['_SUB_MENU'] = [];
215
                foreach ($allResults as $resArray) {
216
                    $entry = [
217
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
218
                        'type' => $resArray['type'],
219
                        'volume' => $resArray['volume'],
220
                        'orderlabel' => $resArray['mets_orderlabel'],
221
                        'pagination' => '',
222
                        'targetUid' => $resArray['uid']
223
                    ];
224
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
225
                }
226
            }
227
        }
228
        return $menuArray;
229
    }
230
231
    /**
232
     * This builds a menu for list of 3D objects
233
     *
234
     * @access public
235
     *
236
     * @param string $content: The PlugIn content
237
     * @param array $conf: The PlugIn configuration
238
     *
239
     * @return array HMENU array
240
     */
241
    public function makeMenuFor3DObjects($content, $conf)
0 ignored issues
show
Unused Code introduced by
The parameter $conf is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

241
    public function makeMenuFor3DObjects($content, /** @scrutinizer ignore-unused */ $conf)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $content is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

241
    public function makeMenuFor3DObjects(/** @scrutinizer ignore-unused */ $content, $conf)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
242
    {
243
        if (!empty($this->requestData['logicalPage'])) {
244
            $this->requestData['page'] = $this->doc->getPhysicalPage($this->requestData['logicalPage']);
0 ignored issues
show
Bug Best Practice introduced by
The property doc does not exist on Kitodo\Dlf\Controller\TableOfContentsController. Did you maybe forget to declare it?
Loading history...
245
            // The logical page parameter should not appear again
246
            unset($this->requestData['logicalPage']);
247
        }
248
        // Set default values for page if not set.
249
        // $this->requestData['page'] may be integer or string (physical structure @ID)
250
        if (
251
            (int) $this->requestData['page'] > 0
252
            || empty($this->requestData['page'])
253
        ) {
254
            $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
255
        } else {
256
            $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
257
        }
258
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
259
        $menuArray = [];
260
        // Is the document an external file?
261
        if (!MathUtility::canBeInterpretedAsInteger($this->document->getDoc()->uid)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uid does not exist on Kitodo\Dlf\Common\Doc. Since you implemented __get, consider adding a @property annotation.
Loading history...
262
            // Go through table of contents and create all menu entries.
263
            foreach ($this->doc->tableOfContents as $entry) {
264
                $menuArray[] = $this->getMenuEntryWithImage($entry, true);
265
            }
266
        }
267
        return $menuArray;
268
    }
269
270
    protected function getMenuEntryWithImage(array $entry, $recursive = false)
271
    {
272
        $entryArray = [];
273
        // Set "title", "volume", "type" and "pagination" from $entry array.
274
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
275
        $entryArray['orderlabel'] = $entry['orderlabel'];
276
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
277
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
278
        $entryArray['doNotLinkIt'] = 1;
279
        $entryArray['ITEM_STATE'] = 'HEADER';
280
281
        if ($entry['children'] == NULL) {
282
            $entryArray['description'] = $entry['description'];
283
            $entryArray['image'] = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[1]]['files']['THUMBS']);
284
            $entryArray['doNotLinkIt'] = 0;
285
            // index.php?tx_dlf%5Bid%5D=http%3A%2F%2Flink_to_METS_file.xml
286
            $entryArray['_OVERRIDE_HREF'] = '/index.php?id=' . GeneralUtility::_GET('id') . '&tx_dlf%5Bid%5D=' . urlencode($entry['points']);
287
            $entryArray['ITEM_STATE'] = 'ITEM';
288
        }
289
290
        // Build sub-menu if available and called recursively.
291
        if (
292
            $recursive == true
293
            && !empty($entry['children'])
294
        ) {
295
            // Build sub-menu only if one of the following conditions apply:
296
            // 1. Current menu node points to another file
297
            // 2. Current menu node has no corresponding images
298
            if (
299
                is_string($entry['points'])
300
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
301
            ) {
302
                $entryArray['_SUB_MENU'] = [];
303
                foreach ($entry['children'] as $child) {
304
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntryWithImage($child);
305
                }
306
            }
307
        }
308
        return $entryArray;
309
    }
310
}
311