Passed
Pull Request — master (#97)
by Ralf
03:18
created

RelatedListTool   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B main() 0 90 9
A compareByOrderVolumeTitle() 0 5 1
A getRelatedItems() 0 30 4
1
<?php
2
3
namespace EWW\Dpf\Plugins\RelatedListTool;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
/**
19
 * Plugin 'DPF: RElatedListTool' for the 'dlf / dpf' extension.
20
 *
21
 * @author    Sebastian Meyer <[email protected]>
22
 * @author    Alexander Bigga <[email protected]>
23
 * @package    TYPO3
24
 * @subpackage    tx_dpf
25
 * @access    public
26
 */
27
class RelatedListTool extends \tx_dlf_plugin
0 ignored issues
show
Bug introduced by
The type tx_dlf_plugin 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...
28
{
29
30
    /**
31
     * The main method of the PlugIn
32
     *
33
     * @access    public
34
     *
35
     * @param    string        $content: The PlugIn content
36
     * @param    array        $conf: The PlugIn configuration
37
     *
38
     * @return    string        The content that is displayed on the website
39
     */
40
    public function main($content, $conf)
41
    {
42
43
        $this->init($conf);
44
45
        // get the tx_dpf.settings too
46
        // Flexform wins over TS
47
        $dpfTSconfig = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_dpf.'];
48
49
        if (is_array($dpfTSconfig['settings.'])) {
50
51
            \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($dpfTSconfig['settings.'], $this->conf, true, false);
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\ArrayUtility 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...
52
            $this->conf = $dpfTSconfig['settings.'];
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...
53
54
        }
55
56
        // Load current document.
57
        $this->loadDocument();
58
59
        if ($this->doc === null) {
60
61
            // Quit without doing anything if required variables are not set.
62
            return $content;
63
64
        }
65
66
        // Load template file.
67
        if (!empty($this->conf['templateFile'])) {
68
69
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATE###');
0 ignored issues
show
Bug Best Practice introduced by
The property template does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
71
        } else {
72
73
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dpf/Classes/Plugins/RelatedListTool/template.tmpl'), '###TEMPLATE###');
74
75
        }
76
77
        $subpartArray['items'] = $this->cObj->getSubpart($this->template, '###ITEMS###');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$subpartArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subpartArray = array(); before regardless.
Loading history...
78
79
        $relatedItems = $this->getRelatedItems();
80
81
        $content = '';
82
83
        if (!empty($relatedItems)) {
84
            foreach ($relatedItems as $key => $value) {
85
                // set link
86
                if ($value['type'] == 'local') {
87
                    $confApi = array(
88
                        'useCacheHash'     => 0,
89
                        'parameter'        => $this->conf['apiPid'],
90
                        'additionalParams' => '&tx_dpf[qid]=' . $value['docId'] . '&tx_dpf[action]=mets',
91
                        'forceAbsoluteUrl' => true,
92
                    );
93
94
                    $metsApiUrl = urlencode($this->cObj->typoLink_URL($confApi));
95
96
                    $conf = array(
97
                        'useCacheHash'     => 1,
98
                        'parameter'        => $GLOBALS['TSFE']->page['uid'],
99
                        'additionalParams' => '&tx_dlf[id]=' . $metsApiUrl,
100
                        'forceAbsoluteUrl' => true,
101
                    );
102
103
                } elseif ($value['type'] == 'urn') {
104
                    // use urn link
105
                    $conf = array(
106
                        'useCacheHash'     => 0,
107
                        'parameter'        => 'http://nbn-resolving.de/' . $value['docId'],
108
                        'forceAbsoluteUrl' => true,
109
                    );
110
111
                } else {
112
113
                    $conf = array(
114
                        'useCacheHash'     => 0,
115
                        'forceAbsoluteUrl' => true,
116
                    );
117
118
                }
119
120
                $title = $value['title'] ? $value['title'] : $value['docId'];
121
122
                // replace uid with URI to dpf API
123
                $markerArray['###ITEM###'] = $this->cObj->typoLink($title, $conf);
124
125
                $content .= $this->cObj->substituteMarkerArray($subpartArray['items'], $markerArray);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $markerArray seems to be defined later in this foreach loop on line 123. Are you sure it is defined here?
Loading history...
126
            }
127
        }
128
129
        return $this->cObj->substituteSubpart($this->template, '###ITEMS###', $content, true);
130
131
    }
132
133
    private function compareByOrderVolumeTitle($a, $b)
134
    {
135
        $s1 = join(' ', array($a['order'], $a['volume'], $a['title']));
136
        $s2 = join(' ', array($b['order'], $b['volume'], $b['title']));
137
        return strnatcmp($s1, $s2);
138
    }
139
140
    public function getRelatedItems()
141
    {
142
        $xPath = '//mods:relatedItem[@type="constituent"]';
143
144
        $items = $this->doc->mets->xpath($xPath);
145
146
        $relatedItems = array();
147
148
        foreach ($items as $index => $relatedItemXmlElement) {
149
            $relatedItemXmlElement->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
150
151
            $type = (string) $relatedItemXmlElement->xpath('mods:identifier/@type')[0];
152
            $title = (string) $relatedItemXmlElement->xpath('mods:titleInfo/mods:title')[0];
153
            $docId = (string) $relatedItemXmlElement->xpath('mods:identifier[@type="' . $type . '"]')[0];
154
            $order = (string) $relatedItemXmlElement->xpath('mods:part/@order')[0];
155
            $volume = (string) $relatedItemXmlElement->xpath('mods:part[@type="volume" or @type="issue"]/mods:detail/mods:number')[0];
156
157
            $element = array();
158
            $element['type'] = $type;
159
            $element['title'] = $title;
160
            $element['docId'] = $docId;
161
            $element['order'] = (!empty($order)) ? $order : null;
162
            $element['volume'] = (!empty($volume)) ? $volume : null;
163
164
            $relatedItems[$index] = $element;
165
        }
166
167
        usort($relatedItems, array('EWW\Dpf\Plugins\RelatedListTool\RelatedListTool', 'compareByOrderVolumeTitle'));
168
169
        return $relatedItems;
170
    }
171
172
}
173
174