Completed
Push — master ( e93629...dcd410 )
by
unknown
19s queued 15s
created

TableOfContentsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
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
     * 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
        $this->view->assign('toc', $this->makeMenuArray());
137
    }
138
139
    /**
140
     * This builds a menu array for HMENU
141
     *
142
     * @access public
143
     * @return array HMENU array
144
     */
145
    public function makeMenuArray()
146
    {
147
        // Load current document.
148
        $this->loadDocument($this->requestData);
149
        if (
150
            $this->document === null
151
            || $this->document->getDoc() === null
152
        ) {
153
            // Quit without doing anything if required variables are not set.
154
            return [];
155
        } else {
156
            if (!empty($this->requestData['logicalPage'])) {
157
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
158
                // The logical page parameter should not appear again
159
                unset($this->requestData['logicalPage']);
160
            }
161
            // Set default values for page if not set.
162
            // $this->piVars['page'] may be integer or string (physical structure @ID)
163
            if (
164
                (int) $this->requestData['page'] > 0
165
                || empty($this->requestData['page'])
166
            ) {
167
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'],
168
                    1, $this->document->getDoc()->numPages, 1);
169
            } else {
170
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
171
            }
172
            $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'],
173
                0, 1, 0);
174
        }
175
        $menuArray = [];
176
        // Does the document have physical elements or is it an external file?
177
        if (
178
            !empty($this->document->getDoc()->physicalStructure)
179
            || !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...
180
        ) {
181
            // Get all logical units the current page or track is a part of.
182
            if (
183
                !empty($this->requestData['page'])
184
                && !empty($this->document->getDoc()->physicalStructure)
185
            ) {
186
                $this->activeEntries = array_merge((array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[0]],
187
                    (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]]);
188
                if (
189
                    !empty($this->requestData['double'])
190
                    && $this->requestData['page'] < $this->document->getDoc()->numPages
191
                ) {
192
                    $this->activeEntries = array_merge($this->activeEntries,
193
                        (array) $this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page'] + 1]]);
194
                }
195
            }
196
            // Go through table of contents and create all menu entries.
197
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
198
                $menuArray[] = $this->getMenuEntry($entry, true);
199
            }
200
        } else {
201
            // Go through table of contents and create top-level menu entries.
202
            foreach ($this->document->getDoc()->tableOfContents as $entry) {
203
                $menuArray[] = $this->getMenuEntry($entry, false);
204
            }
205
            // Build table of contents from database.
206
            $result = $this->documentRepository->getTableOfContentsFromDb($this->document->getUid(), $this->document->getPid(), $this->settings);
207
208
            $allResults = $result->fetchAll();
209
210
            if (count($allResults) > 0) {
211
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
212
                $menuArray[0]['_SUB_MENU'] = [];
213
                foreach ($allResults as $resArray) {
214
                    $entry = [
215
                        'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'],
216
                        'type' => $resArray['type'],
217
                        'volume' => $resArray['volume'],
218
                        'orderlabel' => $resArray['mets_orderlabel'],
219
                        'pagination' => '',
220
                        'targetUid' => $resArray['uid']
221
                    ];
222
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false);
223
                }
224
            }
225
        }
226
        return $menuArray;
227
    }
228
}
229