tx_dlf_toc   B
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 305
rs 8.2857
c 0
b 0
f 0
wmc 39

3 Methods

Rating   Name   Duplication   Size   Complexity  
D getMenuEntry() 0 100 19
D makeMenuArray() 0 116 16
B main() 0 41 4
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
/**
13
 * Plugin 'DLF: Viewer' for the 'dlf' extension.
14
 *
15
 * @author	Sebastian Meyer <[email protected]>
16
 * @package	TYPO3
17
 * @subpackage	tx_dlf
18
 * @access	public
19
 */
20
class tx_dlf_toc extends tx_dlf_plugin {
21
22
    public $scriptRelPath = 'plugins/toc/class.tx_dlf_toc.php';
23
24
    /**
25
     * This holds the active entries according to the currently selected page
26
     *
27
     * @var	array
28
     * @access protected
29
     */
30
    protected $activeEntries = array ();
31
32
    /**
33
     * This builds an array for one menu entry
34
     *
35
     * @access	protected
36
     *
37
     * @param	array		$entry: The entry's array from tx_dlf_document->getLogicalStructure
38
     * @param	boolean		$recursive: Whether to include the child entries
39
     *
40
     * @return	array		HMENU array for menu entry
41
     */
42
    protected function getMenuEntry(array $entry, $recursive = FALSE) {
43
44
        $entryArray = array ();
45
46
        // Set "title", "volume", "type" and "pagination" from $entry array.
47
        $entryArray['title'] = $entry['label'];
48
49
        $entryArray['volume'] = $entry['volume'];
50
51
        $entryArray['orderlabel'] = $entry['orderlabel'];
52
53
        $entryArray['type'] = tx_dlf_helper::translate($entry['type'], 'tx_dlf_structures', $this->conf['pages']);
54
55
        $entryArray['pagination'] = $entry['pagination'];
56
57
        $entryArray['_OVERRIDE_HREF'] = '';
58
59
        $entryArray['doNotLinkIt'] = 1;
60
61
        $entryArray['ITEM_STATE'] = 'NO';
62
63
        // Build menu links based on the $entry['points'] array.
64
        if (!empty($entry['points']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($entry['points'])) {
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\MathUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
65
66
            $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(array ('page' => $entry['points']), TRUE, FALSE, $this->conf['targetPid']);
67
68
            $entryArray['doNotLinkIt'] = 0;
69
70
            if ($this->conf['basketButton']) {
71
72
                $entryArray['basketButtonHref'] = '<a href="'.$this->pi_linkTP_keepPIvars_url(array ('addToBasket' => 'toc', 'logId' => $entry['id'], 'startpage' => $entry['points']), TRUE, FALSE, $this->conf['targetBasket']).'">'.$this->pi_getLL('basketButton', '', TRUE).'</a>';
73
74
            }
75
76
        } elseif (!empty($entry['points']) && is_string($entry['points'])) {
77
78
            $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(array ('id' => $entry['points'], 'page' => 1), TRUE, FALSE, $this->conf['targetPid']);
79
80
            $entryArray['doNotLinkIt'] = 0;
81
82
            if ($this->conf['basketButton']) {
83
84
                $entryArray['basketButtonHref'] = '<a href="'.$this->pi_linkTP_keepPIvars_url(array ('addToBasket' => 'toc', 'logId' => $entry['id'], 'startpage' => $entry['points']), TRUE, FALSE, $this->conf['targetBasket']).'">'.$this->pi_getLL('basketButton', '', TRUE).'</a>';
85
86
            }
87
88
        } elseif (!empty($entry['targetUid'])) {
89
90
            $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(array ('id' => $entry['targetUid'], 'page' => 1), TRUE, FALSE, $this->conf['targetPid']);
91
92
            $entryArray['doNotLinkIt'] = 0;
93
94
            if ($this->conf['basketButton']) {
95
96
                $entryArray['basketButtonHref'] = '<a href="'.$this->pi_linkTP_keepPIvars_url(array ('addToBasket' => 'toc', 'logId' => $entry['id'], 'startpage' => $entry['targetUid']), TRUE, FALSE, $this->conf['targetBasket']).'">'.$this->pi_getLL('basketButton', '', TRUE).'</a>';
97
98
            }
99
100
        }
101
102
        // Set "ITEM_STATE" to "CUR" if this entry points to current page.
103
        if (in_array($entry['id'], $this->activeEntries)) {
104
105
            $entryArray['ITEM_STATE'] = 'CUR';
106
107
        }
108
109
        // Build sub-menu if available and called recursively.
110
        if ($recursive == TRUE && !empty($entry['children'])) {
111
112
            // Build sub-menu only if one of the following conditions apply:
113
            // 1. "expAll" is set for menu
114
            // 2. Current menu node is in rootline
115
            // 3. Current menu node points to another file
116
            // 4. Current menu node has no corresponding images
117
            if (!empty($this->conf['menuConf.']['expAll']) || $entryArray['ITEM_STATE'] == 'CUR' || is_string($entry['points']) || empty($this->doc->smLinks['l2p'][$entry['id']])) {
118
119
                $entryArray['_SUB_MENU'] = array ();
120
121
                foreach ($entry['children'] as $child) {
122
123
                    // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
124
                    if (in_array($child['id'], $this->activeEntries)) {
125
126
                        $entryArray['ITEM_STATE'] = 'ACT';
127
128
                    }
129
130
                    $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, TRUE);
131
132
                }
133
134
            }
135
136
            // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
137
            $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'].'IFSUB');
138
139
        }
140
141
        return $entryArray;
142
143
    }
144
145
    /**
146
     * The main method of the PlugIn
147
     *
148
     * @access	public
149
     *
150
     * @param	string		$content: The PlugIn content
151
     * @param	array		$conf: The PlugIn configuration
152
     *
153
     * @return	string		The content that is displayed on the website
154
     */
155
    public function main($content, $conf) {
156
157
        $this->init($conf);
158
159
        // Check for typoscript configuration to prevent fatal error.
160
        if (empty($this->conf['menuConf.'])) {
161
162
            if (TYPO3_DLOG) {
0 ignored issues
show
Bug introduced by
The constant TYPO3_DLOG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
163
164
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_toc->main('.$content.', [data])] Incomplete plugin configuration', $this->extKey, SYSLOG_SEVERITY_WARNING, $conf);
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\GeneralUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
165
166
            }
167
168
            return $content;
169
170
        }
171
172
        // Load template file.
173
        if (!empty($this->conf['templateFile'])) {
174
175
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATE###');
176
177
        } else {
178
179
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/plugins/toc/template.tmpl'), '###TEMPLATE###');
180
181
        }
182
183
        $TSconfig = array ();
184
185
        $TSconfig['special'] = 'userfunction';
186
187
        $TSconfig['special.']['userFunc'] = 'tx_dlf_toc->makeMenuArray';
188
189
        $TSconfig = tx_dlf_helper::array_merge_recursive_overrule($this->conf['menuConf.'], $TSconfig);
190
191
        $markerArray['###TOCMENU###'] = $this->cObj->HMENU($TSconfig);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$markerArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $markerArray = array(); before regardless.
Loading history...
192
193
        $content .= $this->cObj->substituteMarkerArray($this->template, $markerArray);
194
195
        return $this->pi_wrapInBaseClass($content);
196
197
    }
198
199
    /**
200
     * This builds a menu array for HMENU
201
     *
202
     * @access	public
203
     *
204
     * @param	string		$content: The PlugIn content
205
     * @param	array		$conf: The PlugIn configuration
206
     *
207
     * @return	array		HMENU array
208
     */
209
    public function makeMenuArray($content, $conf) {
210
211
        $this->init($conf);
212
213
        // Load current document.
214
        $this->loadDocument();
215
216
        if ($this->doc === NULL) {
217
218
            // Quit without doing anything if required variables are not set.
219
            return array ();
220
221
        } else {
222
223
            if (!empty($this->piVars['logicalPage'])) {
224
225
                $this->piVars['page'] = $this->doc->getPhysicalPage($this->piVars['logicalPage']);
0 ignored issues
show
Bug Best Practice introduced by
The property piVars does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
226
                // The logical page parameter should not appear again
227
                unset($this->piVars['logicalPage']);
228
229
                }
230
231
            // Set default values for page if not set.
232
            // $this->piVars['page'] may be integer or string (physical structure @ID)
233
            if ((int) $this->piVars['page'] > 0 || empty($this->piVars['page'])) {
234
235
                $this->piVars['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1);
236
237
            } else {
238
239
                $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure);
240
241
            }
242
243
            $this->piVars['double'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->piVars['double'], 0, 1, 0);
244
245
        }
246
247
        $menuArray = array ();
248
249
        // Does the document have physical elements or is it an external file?
250
        if ($this->doc->physicalStructure || !\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->doc->uid)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->doc->physicalStructure of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
251
252
            // Get all logical units the current page or track is a part of.
253
            if (!empty($this->piVars['page']) && $this->doc->physicalStructure) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->doc->physicalStructure of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
254
255
                $this->activeEntries = array_merge((array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[0]], (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]]);
256
257
                if (!empty($this->piVars['double']) && $this->piVars['page'] < $this->doc->numPages) {
258
259
                    $this->activeEntries = array_merge($this->activeEntries, (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page'] + 1]]);
260
261
                }
262
263
            }
264
265
            // Go through table of contents and create all menu entries.
266
            foreach ($this->doc->tableOfContents as $entry) {
267
268
                $menuArray[] = $this->getMenuEntry($entry, TRUE);
269
270
            }
271
272
        } else {
273
274
            // Go through table of contents and create top-level menu entries.
275
            foreach ($this->doc->tableOfContents as $entry) {
276
277
                $menuArray[] = $this->getMenuEntry($entry, FALSE);
278
279
            }
280
281
            // Get all child documents from database.
282
            $whereClause = 'tx_dlf_documents.partof='.intval($this->doc->uid).' AND tx_dlf_documents.structure=tx_dlf_structures.uid AND tx_dlf_structures.pid='.$this->doc->pid.tx_dlf_helper::whereClause('tx_dlf_documents').tx_dlf_helper::whereClause('tx_dlf_structures');
283
284
            if ($this->conf['excludeOther']) {
285
286
                $whereClause .= ' AND tx_dlf_documents.pid='.intval($this->conf['pages']);
287
288
            }
289
290
            // Build table of contents from database.
291
            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
292
                'tx_dlf_documents.uid AS uid,tx_dlf_documents.title AS title,tx_dlf_documents.volume AS volume,tx_dlf_structures.index_name AS type',
293
                'tx_dlf_documents,tx_dlf_structures',
294
                $whereClause,
295
                '',
296
                'tx_dlf_documents.volume_sorting',
297
                ''
298
            );
299
300
            if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
301
302
                $menuArray[0]['ITEM_STATE'] = 'CURIFSUB';
303
304
                $menuArray[0]['_SUB_MENU'] = array ();
305
306
                while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
307
308
                    $entry = array (
309
                        'label' => $resArray['title'],
310
                        'type' => $resArray['type'],
311
                        'volume' => $resArray['volume'],
312
                        'pagination' => '',
313
                        'targetUid' => $resArray['uid']
314
                    );
315
316
                    $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, FALSE);
317
318
                }
319
320
            }
321
322
        }
323
324
        return $menuArray;
325
326
    }
327
328
}
329