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:10
created

TableOfContentsController::mainAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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