Completed
Push — master ( ce3674...ce3674 )
by
unknown
15s queued 12s
created

TableOfContentsController::mainAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 8
rs 10
c 2
b 0
f 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\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
     * @var array
38
     */
39
    protected $pluginConf;
40
41
    /**
42
     * SearchController constructor.
43
     */
44
    public function __construct()
45
    {
46
        parent::__construct();
47
        // Read plugin TS configuration.
48
        // TODO: This is more or less obsolete - the table of document plugin does not use the TypoScript anymore
49
        $this->pluginConf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_dlf_tableofcontents.'];
50
        $this->initialize();
51
    }
52
53
    /**
54
     * This builds an array for one menu entry
55
     *
56
     * @access protected
57
     *
58
     * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure
59
     * @param bool $recursive : Whether to include the child entries
60
     *
61
     * @return array HMENU array for menu entry
62
     */
63
    protected function getMenuEntry(array $entry, $recursive = false)
64
    {
65
        $entryArray = [];
66
        // Set "title", "volume", "type" and "pagination" from $entry array.
67
        $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel'];
68
        $entryArray['volume'] = $entry['volume'];
69
        $entryArray['orderlabel'] = $entry['orderlabel'];
70
        $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']);
71
        $entryArray['pagination'] = htmlspecialchars($entry['pagination']);
72
        $entryArray['_OVERRIDE_HREF'] = '';
73
        $entryArray['doNotLinkIt'] = 1;
74
        $entryArray['ITEM_STATE'] = 'NO';
75
        // Build menu links based on the $entry['points'] array.
76
        if (
77
            !empty($entry['points'])
78
            && MathUtility::canBeInterpretedAsInteger($entry['points'])
79
        ) {
80
            $entryArray['page'] = $entry['points'];
81
82
            $entryArray['doNotLinkIt'] = 0;
83
            if ($this->settings['basketButton']) {
84
                $entryArray['basketButton'] = [
85
                    'logId' => $entry['id'],
86
                    'startpage' => $entry['points']
87
                ];
88
            }
89
        } elseif (
90
            !empty($entry['points'])
91
            && is_string($entry['points'])
92
        ) {
93
            $entryArray['id'] = $entry['points'];
94
            $entryArray['page'] = 1;
95
            $entryArray['doNotLinkIt'] = 0;
96
            if ($this->settings['basketButton']) {
97
                $entryArray['basketButton'] = [
98
                    'logId' => $entry['id'],
99
                    'startpage' => $entry['points']
100
                ];
101
            }
102
        } elseif (!empty($entry['targetUid'])) {
103
            $entryArray['id'] = $entry['targetUid'];
104
            $entryArray['page'] = 1;
105
            $entryArray['doNotLinkIt'] = 0;
106
            if ($this->settings['basketButton']) {
107
                $entryArray['basketButton'] = [
108
                    'logId' => $entry['id'],
109
                    'startpage' => $entry['targetUid']
110
                ];
111
            }
112
        }
113
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
114
        if (in_array($entry['id'], $this->activeEntries)) {
115
            $entryArray['ITEM_STATE'] = 'CUR';
116
        }
117
        // Build sub-menu if available and called recursively.
118
        if (
119
            $recursive === true
120
            && !empty($entry['children'])
121
        ) {
122
            // Build sub-menu only if one of the following conditions apply:
123
            // 1. "expAll" is set for menu
124
            // 2. Current menu node is in rootline
125
            // 3. Current menu node points to another file
126
            // 4. Current menu node has no corresponding images
127
            if (
128
                !empty($this->pluginConf['menuConf.']['expAll'])
129
                || $entryArray['ITEM_STATE'] == 'CUR'
130
                || is_string($entry['points'])
131
                || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']])
132
            ) {
133
                $entryArray['_SUB_MENU'] = [];
134
                foreach ($entry['children'] as $child) {
135
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
136
                    if (in_array($child['id'], $this->activeEntries)) {
137
                        $entryArray['ITEM_STATE'] = 'ACT';
138
                    }
139
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true);
140
                }
141
            }
142
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
143
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB');
144
        }
145
        return $entryArray;
146
    }
147
148
    /**
149
     * The main method of the plugin
150
     *
151
     * @return void
152
     */
153
    public function mainAction()
154
    {
155
        // Check for typoscript configuration to prevent fatal error.
156
        if (empty($this->settings['menuConf'])) {
157
            $this->logger->warning('Incomplete plugin configuration');
0 ignored issues
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

157
            $this->logger->/** @scrutinizer ignore-call */ 
158
                           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...
158
        }
159
160
        $this->view->assign('toc', $this->makeMenuArray());
161
    }
162
163
    /**
164
     * This builds a menu array for HMENU
165
     *
166
     * @access public
167
     * @return array HMENU array
168
     */
169
    public function makeMenuArray()
170
    {
171
        // Load current document.
172
        $this->loadDocument($this->requestData);
173
        if (
174
            $this->document === null
175
            || $this->document->getDoc() === null
176
        ) {
177
            // Quit without doing anything if required variables are not set.
178
            return [];
179
        } else {
180
            if (!empty($this->requestData['logicalPage'])) {
181
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
182
                // The logical page parameter should not appear again
183
                unset($this->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) $this->requestData['page'] > 0
189
                || empty($this->requestData['page'])
190
            ) {
191
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'],
192
                    1, $this->document->getDoc()->numPages, 1);
193
            } else {
194
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
195
            }
196
            $this->requestData['double'] = MathUtility::forceIntegerInRange($this->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->document->getDoc()->physicalStructure)
203
            || !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...
204
        ) {
205
            // Get all logical units the current page or track is a part of.
206
            if (
207
                !empty($this->requestData['page'])
208
                && !empty($this->document->getDoc()->physicalStructure)
209
            ) {
210
                $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
211
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
212
                if (
213
                    !empty($this->requestData['double'])
214
                    && $this->requestData['page'] < $this->document->getDoc()->numPages
215
                ) {
216
                    $this->activeEntries = array_merge($this->activeEntries,
217
                        (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
218
                }
219
            }
220
            // Go through table of contents and create all menu entries.
221
            foreach ($this->document->getDoc()->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->document->getDoc()->tableOfContents as $entry) {
227
                $menuArray[] = $this->getMenuEntry($entry, false);
228
            }
229
            // Build table of contents from database.
230
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
231
232
            $allResults = $result->fetchAll();
233
234
            if (count($allResults) > 0) {
235
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
236
                $menuArray[0]['_SUB_MENU'] = [];
237
                foreach ($allResults as $resArray) {
238
                    $entry = [
239
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
240
                        'type' => $resArray['type'],
241
                        'volume' => $resArray['volume'],
242
                        'orderlabel' => $resArray['mets_orderlabel'],
243
                        'pagination' => '',
244
                        'targetUid' => $resArray['uid']
245
                    ];
246
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
247
                }
248
            }
249
        }
250
        return $menuArray;
251
    }
252
}
253