Completed
Push — master ( 1f4c79...e8d8b4 )
by
unknown
22s queued 12s
created

SearchInDocumentTool::getActionUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 15
rs 10
cc 3
nc 4
nop 0
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Plugin\Tools;
14
15
use Kitodo\Dlf\Common\Helper;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Core\Utility\MathUtility;
18
19
/**
20
 * SearchInDocument tool for the plugin 'Toolbox' of the 'dlf' extension
21
 *
22
 * @author Sebastian Meyer <[email protected]>
23
 * @author Alexander Bigga <[email protected]>
24
 * @author Beatrycze Volk <[email protected]>
25
 * @package TYPO3
26
 * @subpackage dlf
27
 * @access public
28
 */
29
class SearchInDocumentTool extends \Kitodo\Dlf\Common\AbstractPlugin
30
{
31
    public $scriptRelPath = 'Classes/Plugin/Tools/SearchInDocumentTool.php';
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
46
        $this->init($conf);
47
48
        // Merge configuration with conf array of toolbox.
49
        if (!empty($this->cObj->data['conf'])) {
50
            $this->conf = Helper::mergeRecursiveWithOverrule($this->cObj->data['conf'], $this->conf);
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...
51
        }
52
53
        // Load current document.
54
        $this->loadDocument();
55
        if (
56
            $this->doc === null
57
            || $this->doc->numPages < 1
58
            || empty($this->conf['fileGrpFulltext'])
59
            || empty($this->conf['solrcore'])
60
        ) {
61
            // Quit without doing anything if required variables are not set.
62
            return $content;
63
        } else {
64
            if (!empty($this->piVars['logicalPage'])) {
65
                $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...
66
                // The logical page parameter should not appear again
67
                unset($this->piVars['logicalPage']);
68
            }
69
            // Set default values if not set.
70
            // $this->piVars['page'] may be integer or string (physical structure @ID)
71
            if (
72
                (int) $this->piVars['page'] > 0
73
                || empty($this->piVars['page'])
74
            ) {
75
                $this->piVars['page'] = MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1);
76
            } else {
77
                $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure);
78
            }
79
        }
80
81
        // Quit if no fulltext file is present
82
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->conf['fileGrpFulltext']);
83
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
84
            if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->piVars['page']]]['files'][$fileGrpFulltext])) {
85
                $fullTextFile = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->piVars['page']]]['files'][$fileGrpFulltext];
86
                break;
87
            }
88
        }
89
        if (empty($fullTextFile)) {
90
            return $content;
91
        }
92
93
        // Load template file.
94
        $this->getTemplate();
95
96
        // Fill markers.
97
        $markerArray = [
98
            '###ACTION_URL###' => $this->getActionUrl(),
99
            '###LABEL_QUERY###' => htmlspecialchars($this->pi_getLL('label.query')),
100
            '###LABEL_DELETE_SEARCH###' => htmlspecialchars($this->pi_getLL('label.delete_search')),
101
            '###LABEL_LOADING###' => htmlspecialchars($this->pi_getLL('label.loading')),
102
            '###LABEL_SUBMIT###' => htmlspecialchars($this->pi_getLL('label.submit')),
103
            '###LABEL_SEARCH_IN_DOCUMENT###' => htmlspecialchars($this->pi_getLL('label.searchInDocument')),
104
            '###LABEL_NEXT###' => htmlspecialchars($this->pi_getLL('label.next')),
105
            '###LABEL_PREVIOUS###' => htmlspecialchars($this->pi_getLL('label.previous')),
106
            '###LABEL_PAGE###' => htmlspecialchars($this->pi_getLL('label.logicalPage')),
107
            '###LABEL_NORESULT###' => htmlspecialchars($this->pi_getLL('label.noresult')),
108
            '###LABEL_QUERY_URL###' => $this->conf['queryInputName'],
109
            '###LABEL_START###' => $this->conf['startInputName'],
110
            '###LABEL_ID###' => $this->conf['idInputName'],
111
            '###LABEL_PAGE_URL###' => $this->conf['pageInputName'],
112
            '###LABEL_HIGHLIGHT_WORD###' => $this->conf['highlightWordInputName'],
113
            '###LABEL_ENCRYPTED###' => $this->conf['encryptedInputName'],
114
            '###CURRENT_DOCUMENT###' => $this->getCurrentDocumentId(),
115
            '###SOLR_ENCRYPTED###' => $this->getEncryptedCoreName() ? : ''
116
        ];
117
118
        $content .= $this->templateService->substituteMarkerArray($this->template, $markerArray);
119
        return $this->pi_wrapInBaseClass($content);
120
    }
121
122
    /**
123
     * Get the action url for search form
124
     *
125
     * @access protected
126
     *
127
     * @return string with action url for search form
128
     */
129
    protected function getActionUrl()
130
    {
131
        // Configure @action URL for form.
132
        $linkConf = [
133
            'parameter' => $GLOBALS['TSFE']->id,
134
            'forceAbsoluteUrl' => 1,
135
            'forceAbsoluteUrl.' => ['scheme' => !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http']
136
        ];
137
138
        $actionUrl = $this->cObj->typoLink_URL($linkConf);
139
140
        if (!empty($this->conf['searchUrl'])) {
141
            $actionUrl = $this->conf['searchUrl'];
142
        }
143
        return $actionUrl;
144
    }
145
146
    /**
147
     * Get current document id
148
     *
149
     * @access protected
150
     *
151
     * @return string with current document id
152
     */
153
    protected function getCurrentDocumentId()
154
    {
155
        $id = $this->doc->uid;
156
157
        if (!empty($this->conf['documentIdUrlSchema'])) {
158
            $arr = explode('*', $this->conf['documentIdUrlSchema']);
159
160
            if (count($arr) == 2) {
161
                $id = explode($arr[0], $id)[0];
162
            } else if (count($arr) == 3) {
163
                $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id));
164
                $id = substr($sub, 0, strpos($sub, $arr[2]));
165
            }
166
        }
167
        return $id;
168
    }
169
170
    /**
171
     * Get the encrypted Solr core name
172
     *
173
     * @access protected
174
     *
175
     * @return string with encrypted core name
176
     */
177
    protected function getEncryptedCoreName()
178
    {
179
        // Get core name.
180
        $name = Helper::getIndexNameFromUid($this->conf['solrcore'], 'tx_dlf_solrcores');
181
        // Encrypt core name.
182
        if (!empty($name)) {
183
            $name = Helper::encrypt($name);
184
        }
185
        return $name;
186
    }
187
}
188