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 (#715)
by Alexander
03:35
created

TableOfContentsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 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\Document;
15
use Kitodo\Dlf\Common\Helper;
16
use TYPO3\CMS\Core\Utility\MathUtility;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Core\Database\ConnectionPool;
19
20
/**
21
 * Controller for plugin 'Table Of Contents' for the 'dlf' extension
22
 *
23
 * @author Sebastian Meyer <[email protected]>
24
 * @package TYPO3
25
 * @subpackage dlf
26
 * @access public
27
 */
28
class TableOfContentsController extends AbstractController
29
{
30
    /**
31
     * This holds the active entries according to the currently selected page
32
     *
33
     * @var array
34
     * @access protected
35
     */
36
    protected $activeEntries = [];
37
38
    /**
39
     * @var array
40
     */
41
    protected $pluginConf;
42
43
    /**
44
     * SearchController constructor.
45
     */
46
    public function __construct()
47
    {
48
        // Read plugin TS configuration.
49
        $this->pluginConf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_dlf_tableofcontents.'];
50
    }
51
52
    /**
53
     * This builds an array for one menu entry
54
     *
55
     * @access protected
56
     *
57
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Document->getLogicalStructure
58
     * @param bool $recursive : Whether to include the child entries
59
     *
60
     * @return array HMENU array for menu entry
61
     */
62
    protected function getMenuEntry(array $entry, $recursive = false)
63
    {
64
        $entryArray = [];
65
        // Set "title", "volume", "type" and "pagination" from $entry array.
66
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
67
        $entryArray['volume'] = $entry['volume'];
68
        $entryArray['orderlabel'] = $entry['orderlabel'];
69
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['pages']);
70
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
71
        $entryArray['_OVERRIDE_HREF'] = '';
72
        $entryArray['doNotLinkIt'] = 1;
73
        $entryArray['ITEM_STATE'] = 'NO';
74
        // Build menu links based on the $entry['points'] array.
75
        if (
76
            !empty($entry['points'])
77
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
78
        ) {
79
            $entryArray['page'] = $entry['points'];
80
81
            $entryArray['doNotLinkIt'] = 0;
82
            if ($this->settings['basketButton']) {
83
                $entryArray['basketButton'] = [
84
                    'logId' => $entry['id'],
85
                    'startpage' => $entry['points']
86
                ];
87
            }
88
        } elseif (
89
            !empty($entry['points'])
90
            && is_string($entry['points'])
91
        ) {
92
            $entryArray['id'] = $entry['points'];
93
            $entryArray['page'] = 1;
94
            $entryArray['doNotLinkIt'] = 0;
95
            if ($this->settings['basketButton']) {
96
                $entryArray['basketButton'] = [
97
                    'logId' => $entry['id'],
98
                    'startpage' => $entry['points']
99
                ];
100
            }
101
        } elseif (!empty($entry['targetUid'])) {
102
            $entryArray['id'] = $entry['targetUid'];
103
            $entryArray['page'] = 1;
104
            $entryArray['doNotLinkIt'] = 0;
105
            if ($this->settings['basketButton']) {
106
                $entryArray['basketButton'] = [
107
                    'logId' => $entry['id'],
108
                    'startpage' => $entry['targetUid']
109
                ];
110
            }
111
        }
112
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
113
        if (in_array($entry['id'], $this->activeEntries)) {
114
            $entryArray['ITEM_STATE'] = 'CUR';
115
        }
116
        // Build sub-menu if available and called recursively.
117
        if (
118
            $recursive === true
119
            && !empty($entry['children'])
120
        ) {
121
            // Build sub-menu only if one of the following conditions apply:
122
            // 1. "expAll" is set for menu
123
            // 2. Current menu node is in rootline
124
            // 3. Current menu node points to another file
125
            // 4. Current menu node has no corresponding images
126
            if (
127
                !empty($this->pluginConf['menuConf.']['expAll'])
128
                || $entryArray['ITEM_STATE'] == 'CUR'
129
                || is_string($entry['points'])
130
                || empty($this->doc->smLinks['l2p'][$entry['id']])
131
            ) {
132
                $entryArray['_SUB_MENU'] = [];
133
                foreach ($entry['children'] as $child) {
134
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
135
                    if (in_array($child['id'], $this->activeEntries)) {
136
                        $entryArray['ITEM_STATE'] = 'ACT';
137
                    }
138
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
139
                }
140
            }
141
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
142
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
143
        }
144
        return $entryArray;
145
    }
146
147
    /**
148
     * The main method of the plugin
149
     *
150
     * @return void
151
     */
152
    public function mainAction()
153
    {
154
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
155
156
        // Check for typoscript configuration to prevent fatal error.
157
        if (empty($this->settings['menuConf'])) {
158
            $this->logger->warning('Incomplete plugin configuration');
1 ignored issue
show
Bug introduced by
The method warning() does not exist on null. ( Ignorable by Annotation )

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

158
            $this->logger->/** @scrutinizer ignore-call */ 
159
                           warning('Incomplete plugin configuration');

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...
159
        }
160
161
        $this->view->assign('toc', $this->makeMenuArray($requestData));
162
    }
163
164
    /**
165
     * This builds a menu array for HMENU
166
     *
167
     * @access public
168
     * @param array $requestData
169
     * @return array HMENU array
170
     */
171
    public function makeMenuArray($requestData)
172
    {
173
        // Load current document.
174
        $this->loadDocument($requestData);
175
        if ($this->doc === null) {
176
            // Quit without doing anything if required variables are not set.
177
            return [];
178
        } else {
179
            if (!empty($requestData['logicalPage'])) {
180
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
181
                // The logical page parameter should not appear again
182
                unset($requestData['logicalPage']);
183
            }
184
            // Set default values for page if not set.
185
            // $this->piVars['page'] may be integer or string (physical structure @ID)
186
            if (
187
                (int) $requestData['page'] > 0
188
                || empty($requestData['page'])
189
            ) {
190
                $requestData['page'] = MathUtility::forceIntegerInRange((int) $requestData['page'],
191
                    1, $this->doc->numPages, 1);
192
            } else {
193
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
194
            }
195
            $requestData['double'] = MathUtility::forceIntegerInRange($requestData['double'],
196
                0, 1, 0);
197
        }
198
        $menuArray = [];
199
        // Does the document have physical elements or is it an external file?
200
        if (
201
            !empty($this->doc->physicalStructure)
202
            || !MathUtility::canBeInterpretedAsInteger($this->doc->uid)
203
        ) {
204
            // Get all logical units the current page or track is a part of.
205
            if (
206
                !empty($requestData['page'])
207
                && !empty($this->doc->physicalStructure)
208
            ) {
209
                $this->activeEntries = array_merge((array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[0]],
210
                    (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$requestData['page']]]);
211
                if (
212
                    !empty($requestData['double'])
213
                    && $requestData['page'] < $this->doc->numPages
214
                ) {
215
                    $this->activeEntries = array_merge($this->activeEntries,
216
                        (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$requestData['page'] + 1]]);
217
                }
218
            }
219
            // Go through table of contents and create all menu entries.
220
            foreach ($this->doc->tableOfContents as $entry) {
221
                $menuArray[] = $this->getMenuEntry($entry, true);
222
            }
223
        } else {
224
            // Go through table of contents and create top-level menu entries.
225
            foreach ($this->doc->tableOfContents as $entry) {
226
                $menuArray[] = $this->getMenuEntry($entry, false);
227
            }
228
            // Build table of contents from database.
229
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
230
                ->getQueryBuilderForTable('tx_dlf_documents');
231
232
            $excludeOtherWhere = '';
233
            if ($this->settings['excludeOther']) {
234
                $excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($this->settings['pages']);
235
            }
236
            // Check if there are any metadata to suggest.
237
            $result = $queryBuilder
238
                ->select(
239
                    'tx_dlf_documents.uid AS uid',
240
                    'tx_dlf_documents.title AS title',
241
                    'tx_dlf_documents.volume AS volume',
242
                    'tx_dlf_documents.mets_label AS mets_label',
243
                    'tx_dlf_documents.mets_orderlabel AS mets_orderlabel',
244
                    'tx_dlf_structures_join.index_name AS type'
245
                )
246
                ->innerJoin(
247
                    'tx_dlf_documents',
248
                    'tx_dlf_structures',
249
                    'tx_dlf_structures_join',
250
                    $queryBuilder->expr()->eq(
251
                        'tx_dlf_structures_join.uid',
252
                        'tx_dlf_documents.structure'
253
                    )
254
                )
255
                ->from('tx_dlf_documents')
256
                ->where(
257
                    $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($this->doc->uid)),
258
                    $queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($this->doc->pid)),
259
                    $excludeOtherWhere
260
                )
261
                ->addOrderBy('tx_dlf_documents.volume_sorting')
262
                ->addOrderBy('tx_dlf_documents.mets_orderlabel')
263
                ->execute();
264
265
            $allResults = $result->fetchAll();
266
267
            if (count($allResults) > 0) {
268
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
269
                $menuArray[0]['_SUB_MENU'] = [];
270
                foreach ($allResults as $resArray) {
271
                    $entry = [
272
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
273
                        'type' => $resArray['type'],
274
                        'volume' => $resArray['volume'],
275
                        'orderlabel' => $resArray['mets_orderlabel'],
276
                        'pagination' => '',
277
                        'targetUid' => $resArray['uid']
278
                    ];
279
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
280
                }
281
            }
282
        }
283
        return $menuArray;
284
    }
285
}
286