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:39
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
        unset($requestData['__referrer'], $requestData['__trustedProperties']);
156
157
        // Check for typoscript configuration to prevent fatal error.
158
        if (empty($this->pluginConf['menuConf.'])) {
159
            $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

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