tx_dlf_metadata::printMetadata()   F
last analyzed

Complexity

Conditions 39
Paths > 20000

Size

Total Lines 214
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 39
eloc 83
nc 25520
nop 1
dl 0
loc 214
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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: Metadata' for the 'dlf' extension.
14
 *
15
 * @author	Sebastian Meyer <[email protected]>
16
 * @author	Siegfried Schweizer <[email protected]>
17
 * @package	TYPO3
18
 * @subpackage	tx_dlf
19
 * @access	public
20
 */
21
class tx_dlf_metadata extends tx_dlf_plugin {
22
23
    public $scriptRelPath = 'plugins/metadata/class.tx_dlf_metadata.php';
24
25
    /**
26
     * This holds the hook objects
27
     *
28
     * @var	array
29
     * @access protected
30
     */
31
    protected $hookObjects = array ();
32
33
    /**
34
     * The main method of the PlugIn
35
     *
36
     * @access	public
37
     *
38
     * @param	string		$content: The PlugIn content
39
     * @param	array		$conf: The PlugIn configuration
40
     *
41
     * @return	string		The content that is displayed on the website
42
     */
43
    public function main($content, $conf) {
44
45
        $this->init($conf);
46
47
        // Turn cache on.
48
        $this->setCache(TRUE);
49
50
        // Load current document.
51
        $this->loadDocument();
52
53
        if ($this->doc === NULL) {
54
55
            // Quit without doing anything if required variables are not set.
56
            return $content;
57
58
        } else {
59
60
            // Set default values if not set.
61
            if (!isset($this->conf['rootline'])) {
62
63
                $this->conf['rootline'] = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property conf does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
65
            }
66
67
        }
68
69
        $metadata = array ();
70
71
        if ($this->conf['rootline'] < 2) {
72
73
            // Get current structure's @ID.
74
            $ids = array ();
75
76
            if (!empty($this->doc->physicalStructure[$this->piVars['page']]) && !empty($this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]])) {
77
78
                foreach ($this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]] as $logId) {
79
80
                    $count = count($this->doc->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@ID="'.$logId.'"]/ancestor::*'));
81
82
                    $ids[$count][] = $logId;
83
84
                }
85
86
            }
87
88
            ksort($ids);
89
90
            reset($ids);
91
92
            // Check if we should display all metadata up to the root.
93
            if ($this->conf['rootline'] == 1) {
94
95
                foreach ($ids as $id) {
96
97
                    foreach ($id as $sid) {
98
99
                        $data = $this->doc->getMetadata($sid, $this->conf['pages']);
100
101
                        if (!empty($data)) {
102
103
                            $data['_id'] = $sid;
104
105
                            $metadata[] = $data;
106
107
                        }
108
109
                    }
110
111
                }
112
113
            } else {
114
115
                $id = array_pop($ids);
116
117
                if (is_array($id)) {
118
119
                    foreach ($id as $sid) {
120
121
                        $data = $this->doc->getMetadata($sid, $this->conf['pages']);
122
123
                        if (!empty($data)) {
124
125
                            $data['_id'] = $sid;
126
127
                            $metadata[] = $data;
128
129
                        }
130
131
                    }
132
133
                }
134
135
            }
136
137
        }
138
139
        // Get titledata?
140
        if (empty($metadata) || ($this->conf['rootline'] == 1 && $metadata[0]['_id'] != $this->doc->toplevelId)) {
141
142
            $data = $this->doc->getTitleData($this->conf['pages']);
143
144
            $data['_id'] = $this->doc->toplevelId;
145
146
            array_unshift($metadata, $data);
147
148
        }
149
150
        if (empty($metadata)) {
151
152
            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...
153
154
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_metadata->main('.$content.', [data])] No metadata found for document with UID "'.$this->doc->uid.'"', $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...
155
156
            }
157
158
            return $content;
159
160
        }
161
162
        ksort($metadata);
163
164
        // Get hook objects.
165
        $this->hookObjects = tx_dlf_helper::getHookObjects($this->scriptRelPath);
166
167
        // Hook for getting a customized title bar (requested by SBB).
168
        foreach ($this->hookObjects as $hookObj) {
169
170
            if (method_exists($hookObj, 'main_customizeTitleBarGetCustomTemplate')) {
171
172
                $hookObj->main_customizeTitleBarGetCustomTemplate($this, $metadata);
173
174
            }
175
176
        }
177
178
        $content .= $this->printMetadata($metadata);
179
180
        return $this->pi_wrapInBaseClass($content);
181
182
    }
183
184
    /**
185
     * Prepares the metadata array for output
186
     *
187
     * @access	protected
188
     *
189
     * @param	array		$metadataArray: The metadata array
190
     *
191
     * @return	string		The metadata array ready for output
192
     */
193
    protected function printMetadata(array $metadataArray) {
194
195
        // Load template file.
196
        if (!empty($this->conf['templateFile'])) {
197
198
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATE###');
199
200
        } else {
201
202
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/plugins/metadata/template.tmpl'), '###TEMPLATE###');
203
204
        }
205
206
        $output = '';
207
208
        $subpart['block'] = $this->cObj->getSubpart($this->template, '###BLOCK###');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$subpart was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subpart = array(); before regardless.
Loading history...
209
210
        // Get list of metadata to show.
211
        $metaList = array ();
212
213
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
214
            'tx_dlf_metadata.index_name AS index_name,tx_dlf_metadata.is_listed AS is_listed,tx_dlf_metadata.wrap AS wrap',
215
            'tx_dlf_metadata',
216
            'tx_dlf_metadata.pid='.intval($this->conf['pages']).tx_dlf_helper::whereClause('tx_dlf_metadata').' AND (sys_language_uid IN (-1,0) OR (sys_language_uid = '.$GLOBALS['TSFE']->sys_language_uid.' AND l18n_parent = 0))',
217
            '',
218
            'tx_dlf_metadata.sorting',
219
            ''
220
        );
221
222
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
223
224
            if (is_array($resArray) && $resArray['sys_language_uid'] != $GLOBALS['TSFE']->sys_language_content && $GLOBALS['TSFE']->sys_language_contentOL) {
225
226
                    $resArray = $GLOBALS['TSFE']->sys_page->getRecordOverlay('tx_dlf_metadata', $resArray, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL);
227
228
                }
229
230
                if ($resArray) {
231
                    // get correct language uid for translated realurl link
232
                    $link_uid = ($resArray['_LOCALIZED_UID']) ? $resArray['_LOCALIZED_UID'] : $resArray['uid'];
0 ignored issues
show
Unused Code introduced by
The assignment to $link_uid is dead and can be removed.
Loading history...
233
234
                    // do stuff with the row entry data	like built HTML or prepare further usage
235
                    if ($this->conf['showFull'] || $resArray['is_listed']) {
236
237
                        $metaList[$resArray['index_name']] = array (
238
                            'wrap' => $resArray['wrap'],
239
                            'label' => tx_dlf_helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages'])
240
                        );
241
242
                    }
243
244
                }
245
246
        }
247
248
        // Get list of collections to show.
249
        $collList = array ();
250
251
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
252
            'tx_dlf_collections.index_name AS index_name',
253
            'tx_dlf_collections',
254
            'tx_dlf_collections.pid='.intval($this->conf['pages']).tx_dlf_helper::whereClause('tx_dlf_collections'),
255
            '',
256
            '',
257
            ''
258
        );
259
260
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
261
262
            $collList[] = $resArray['index_name'];
263
264
        }
265
266
        // Save original data array.
267
        $cObjData = $this->cObj->data;
268
269
        // Parse the metadata arrays.
270
        foreach ($metadataArray as $metadata) {
271
272
            $markerArray['###METADATA###'] = '';
273
274
            // Reset content object's data array.
275
            $this->cObj->data = $cObjData;
276
277
            // Load all the metadata values into the content object's data array.
278
            foreach ($metadata as $index_name => $value) {
279
280
                if (is_array($value)) {
281
282
                    $this->cObj->data[$index_name] = implode($this->conf['separator'], $value);
283
284
                } else {
285
286
                    $this->cObj->data[$index_name] = $value;
287
288
                }
289
290
            }
291
292
            // Process each metadate.
293
            foreach ($metaList as $index_name => $metaConf) {
294
295
                $parsedValue = '';
296
297
                $fieldwrap = $this->parseTS($metaConf['wrap']);
298
299
                do {
300
301
                    $value = @array_shift($metadata[$index_name]);
302
303
                    if ($index_name == 'title') {
304
305
                        // Get title of parent document if needed.
306
                        if (empty($value) && $this->conf['getTitle'] && $this->doc->parentId) {
307
308
                            $superiorTitle = tx_dlf_document::getTitle($this->doc->parentId, TRUE);
309
310
                            if (!empty($superiorTitle)) {
311
312
                                $value = '['.$superiorTitle.']';
313
314
                            }
315
316
                        }
317
318
                        if (!empty($value)) {
319
320
                            $value = htmlspecialchars($value);
321
322
                            // Link title to pageview.
323
                            if ($this->conf['linkTitle'] && $metadata['_id']) {
324
325
                                $details = $this->doc->getLogicalStructure($metadata['_id']);
326
327
                                $value = $this->pi_linkTP($value, array ($this->prefixId => array ('id' => $this->doc->uid, 'page' => (!empty($details['points']) ? intval($details['points']) : 1))), TRUE, $this->conf['targetPid']);
328
329
                            }
330
331
                        }
332
333
                    } elseif ($index_name == 'owner' && !empty($value)) {
334
335
                        // Translate name of holding library.
336
                        $value = htmlspecialchars(tx_dlf_helper::translate($value, 'tx_dlf_libraries', $this->conf['pages']));
337
338
                    } elseif ($index_name == 'type' && !empty($value)) {
339
340
                        // Translate document type.
341
                        $value = htmlspecialchars(tx_dlf_helper::translate($value, 'tx_dlf_structures', $this->conf['pages']));
342
343
                    } elseif ($index_name == 'collection' && !empty($value)) {
344
345
                        // Check if collections isn't hidden.
346
                        if (in_array($value, $collList)) {
347
348
                            // Translate collection.
349
                            $value = htmlspecialchars(tx_dlf_helper::translate($value, 'tx_dlf_collections', $this->conf['pages']));
350
351
                        } else {
352
353
                            $value = '';
354
355
                        }
356
357
                    } elseif ($index_name == 'language' && !empty($value)) {
358
359
                        // Translate ISO 639 language code.
360
                        $value = htmlspecialchars(tx_dlf_helper::getLanguageName($value));
361
362
                    } elseif (!empty($value)) {
363
364
                        // Sanitize value for output.
365
                        $value = htmlspecialchars($value);
366
367
                    }
368
369
                    // Hook for getting a customized value (requested by SBB).
370
                    foreach ($this->hookObjects as $hookObj) {
371
372
                        if (method_exists($hookObj, 'printMetadata_customizeMetadata')) {
373
374
                            $hookObj->printMetadata_customizeMetadata($value);
375
376
                        }
377
378
                    }
379
380
                    $value = $this->cObj->stdWrap($value, $fieldwrap['value.']);
381
382
                    if (!empty($value)) {
383
384
                        $parsedValue .= $value;
385
386
                    }
387
388
                } while (count($metadata[$index_name]));
389
390
                if (!empty($parsedValue)) {
391
392
                    $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']);
393
394
                    $field .= $parsedValue;
395
396
                    $markerArray['###METADATA###'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']);
397
398
                }
399
400
            }
401
402
            $output .= $this->cObj->substituteMarkerArray($subpart['block'], $markerArray);
403
404
        }
405
406
        return $this->cObj->substituteSubpart($this->template, '###BLOCK###', $output, TRUE);
407
408
    }
409
410
}
411