Completed
Push — master ( ce3674...ce3674 )
by
unknown
15s queued 12s
created

ToolboxController::pdfdownloadtool()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 18
nc 5
nop 0
dl 0
loc 31
rs 8.4444
c 1
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 'Toolbox'.
20
 *
21
 * @author Sebastian Meyer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class ToolboxController extends AbstractController
27
{
28
    /**
29
     * The main method of the plugin
30
     *
31
     * @return void
32
     */
33
    public function mainAction()
34
    {
35
        // Load current document.
36
        $this->loadDocument($this->requestData);
37
38
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
39
        $this->view->assign('double', $this->requestData['double']);
40
41
        $tools = explode(',', $this->settings['tools']);
42
        // Add the tools to the toolbox.
43
        foreach ($tools as $tool) {
44
            $tool = trim(str_replace('tx_dlf_', '', $tool));
45
            $this->$tool();
46
            $this->view->assign($tool, true);
47
        }
48
    }
49
50
    /**
51
     * Renders the annotation tool
52
     *
53
     * @return void
54
     */
55
    public function annotationtool()
56
    {
57
        if (
58
            $this->document === null
59
            || $this->document->getDoc()->numPages < 1
60
        ) {
61
            // Quit without doing anything if required variables are not set.
62
            return;
63
        } else {
64
            if (!empty($this->requestData['logicalPage'])) {
65
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
66
                // The logical page parameter should not appear again
67
                unset($this->requestData['logicalPage']);
68
            }
69
            // Set default values if not set.
70
            // $this->requestData['page'] may be integer or string (physical structure @ID)
71
            if (
72
                (int) $this->requestData['page'] > 0
73
                || empty($this->requestData['page'])
74
            ) {
75
                $this->requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
76
            } else {
77
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
78
            }
79
        }
80
81
        $annotationContainers = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['annotationContainers'];
82
        if (
83
            $annotationContainers != null
84
            && sizeof($annotationContainers) > 0
85
        ) {
86
            $this->view->assign('annotationTool', true);
87
        } else {
88
            $this->view->assign('annotationTool', false);
89
        }
90
    }
91
92
    /**
93
     * Renders the fulltext download tool
94
     *
95
     * @return void
96
     */
97
    public function fulltextdownloadtool()
98
    {
99
        if (
100
            $this->document === null
101
            || $this->document->getDoc()->numPages < 1
102
            || empty($this->extConf['fileGrpFulltext'])
103
        ) {
104
            // Quit without doing anything if required variables are not set.
105
            return;
106
        } else {
107
            if (!empty($this->requestData['logicalPage'])) {
108
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
109
                // The logical page parameter should not appear again
110
                unset($this->requestData['logicalPage']);
111
            }
112
            // Set default values if not set.
113
            // $this->requestData['page'] may be integer or string (physical structure @ID)
114
            if (
115
                (int) $this->requestData['page'] > 0
116
                || empty($this->requestData['page'])
117
            ) {
118
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
119
            } else {
120
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
121
            }
122
        }
123
        // Get text download.
124
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
125
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
126
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) {
127
                $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
128
                break;
129
            }
130
        }
131
        if (!empty($fullTextFile)) {
132
            $this->view->assign('fulltextDownload', true);
133
        } else {
134
            $this->view->assign('fulltextDownload', false);
135
        }
136
    }
137
138
    /**
139
     * Renders the fulltext tool
140
     *
141
     * @return void
142
     */
143
    public function fulltexttool()
144
    {
145
        if (
146
            $this->document === null
147
            || $this->document->getDoc()->numPages < 1
148
            || empty($this->extConf['fileGrpFulltext'])
149
        ) {
150
            // Quit without doing anything if required variables are not set.
151
            return;
152
        } else {
153
            if (!empty($this->requestData['logicalPage'])) {
154
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
155
                // The logical page parameter should not appear again
156
                unset($this->requestData['logicalPage']);
157
            }
158
            // Set default values if not set.
159
            // $this->requestData['page'] may be integer or string (physical structure @ID)
160
            if (
161
                (int) $this->requestData['page'] > 0
162
                || empty($this->requestData['page'])
163
            ) {
164
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
165
            } else {
166
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
167
            }
168
        }
169
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
170
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
171
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) {
172
                $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
173
                break;
174
            }
175
        }
176
        if (!empty($fullTextFile)) {
177
            $this->view->assign('fulltext', true);
178
            $this->view->assign('activateFullTextInitially', MathUtility::forceIntegerInRange($this->settings['activateFullTextInitially'], 0, 1, 0));
179
        } else {
180
            $this->view->assign('fulltext', false);
181
        }
182
    }
183
184
    /**
185
     * Renders the image download tool
186
     *
187
     * @return void
188
     */
189
    public function imagedownloadtool()
190
    {
191
        if (
192
            $this->document === null
193
            || $this->document->getDoc()->numPages < 1
194
            || empty($this->settings['fileGrpsImageDownload'])
195
        ) {
196
            // Quit without doing anything if required variables are not set.
197
            return;
198
        } else {
199
            if (!empty($this->requestData['logicalPage'])) {
200
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
201
                // The logical page parameter should not appear again
202
                unset($this->requestData['logicalPage']);
203
            }
204
            // Set default values if not set.
205
            // $this->requestData['page'] may be integer or string (physical structure @ID)
206
            if (
207
                (int) $this->requestData['page'] > 0
208
                || empty($this->requestData['page'])
209
            ) {
210
                $this->requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
211
            } else {
212
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
213
            }
214
        }
215
        $imageArray = [];
216
        // Get left or single page download.
217
        $imageArray[0] = $this->getImage($this->requestData['page']);
218
        if ($this->requestData['double'] == 1) {
219
            $imageArray[1] = $this->getImage($this->requestData['page'] + 1);
220
        }
221
        $this->view->assign('imageDownload', $imageArray);
222
    }
223
224
    /**
225
     * Get image's URL and MIME type
226
     *
227
     * @access protected
228
     *
229
     * @param int $page: Page number
230
     *
231
     * @return array Array of image links and image format information
232
     */
233
    protected function getImage($page)
234
    {
235
        $image = [];
236
        // Get @USE value of METS fileGrp.
237
        $fileGrps = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']);
238
        while ($fileGrp = @array_pop($fileGrps)) {
239
            // Get image link.
240
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp])) {
241
                $image['url'] = $this->document->getDoc()->getDownloadLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp]);
242
                $image['mimetype'] = $this->document->getDoc()->getFileMimeType($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp]);
243
                switch ($image['mimetype']) {
244
                    case 'image/jpeg':
245
                        $mimetypeLabel = ' (JPG)';
246
                        break;
247
                    case 'image/tiff':
248
                        $mimetypeLabel = ' (TIFF)';
249
                        break;
250
                    default:
251
                        $mimetypeLabel = '';
252
                }
253
                $image['mimetypeLabel'] = $mimetypeLabel;
254
                break;
255
            } else {
256
                $this->logger->warning('File not found in fileGrp "' . $fileGrp . '"');
0 ignored issues
show
Bug introduced by
The method warning() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

256
                $this->logger->/** @scrutinizer ignore-call */ 
257
                               warning('File not found in fileGrp "' . $fileGrp . '"');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
257
            }
258
        }
259
        return $image;
260
    }
261
262
    /**
263
     * Renders the image manipulation tool
264
     *
265
     * @return void
266
     */
267
    public function imagemanipulationtool()
268
    {
269
        // Set parent element for initialization.
270
        $parentContainer = !empty($this->settings['parentContainer']) ? $this->settings['parentContainer'] : '.tx-dlf-imagemanipulationtool';
271
272
        $this->view->assign('imageManipulation', true);
273
        $this->view->assign('parentContainer', $parentContainer);
274
    }
275
276
    /**
277
     * Renders the PDF download tool
278
     *
279
     * @return void
280
     */
281
    public function pdfdownloadtool()
282
    {
283
        if (
284
            $this->document === null
285
            || $this->document->getDoc() === null
286
            || $this->document->getDoc()->numPages < 1
287
            || empty($this->extConf['fileGrpDownload'])
288
        ) {
289
            // Quit without doing anything if required variables are not set.
290
            return;
291
        } else {
292
            if (!empty($this->requestData['logicalPage'])) {
293
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
294
                // The logical page parameter should not appear again
295
                unset($this->requestData['logicalPage']);
296
            }
297
            // Set default values if not set.
298
            // $this->requestData['page'] may be integer or string (physical structure @ID)
299
            if (
300
                (int) $this->requestData['page'] > 0
301
                || empty($this->requestData['page'])
302
            ) {
303
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
304
            } else {
305
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
306
            }
307
        }
308
        // Get single page downloads.
309
        $this->view->assign('pageLinks', $this->getPageLink());
310
        // Get work download.
311
        $this->view->assign('workLink', $this->getWorkLink());
312
    }
313
314
    /**
315
     * Get page's download link
316
     *
317
     * @access protected
318
     *
319
     * @return array Link to downloadable page
320
     */
321
    protected function getPageLink()
322
    {
323
        $page1Link = '';
324
        $page2Link = '';
325
        $pageLinkArray = [];
326
        $pageNumber = $this->requestData['page'];
327
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
328
        // Get image link.
329
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
330
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber]]['files'][$fileGrpDownload])) {
331
                $page1Link = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber]]['files'][$fileGrpDownload]);
332
                // Get second page, too, if double page view is activated.
333
                if (
334
                    $this->requestData['double']
335
                    && $pageNumber < $this->document->getDoc()->numPages
336
                    && !empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload])
337
                ) {
338
                    $page2Link = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]);
339
                }
340
                break;
341
            }
342
        }
343
        if (
344
            empty($page1Link)
345
            && empty($page2Link)
346
        ) {
347
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
348
        }
349
350
        if (!empty($page1Link)) {
351
            $pageLinkArray[0] = $page1Link;
352
        }
353
        if (!empty($page2Link)) {
354
            $pageLinkArray[1] = $page2Link;
355
        }
356
        return $pageLinkArray;
357
    }
358
359
    /**
360
     * Get work's download link
361
     *
362
     * @access protected
363
     *
364
     * @return string Link to downloadable work
365
     */
366
    protected function getWorkLink()
367
    {
368
        $workLink = '';
369
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
370
        // Get work link.
371
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
372
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[0]]['files'][$fileGrpDownload])) {
373
                $workLink = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[0]]['files'][$fileGrpDownload]);
374
                break;
375
            } else {
376
                $details = $this->document->getDoc()->getLogicalStructure($this->document->getDoc()->toplevelId);
377
                if (!empty($details['files'][$fileGrpDownload])) {
378
                    $workLink = $this->document->getDoc()->getFileLocation($details['files'][$fileGrpDownload]);
379
                    break;
380
                }
381
            }
382
        }
383
        if (!empty($workLink)) {
384
            $workLink = $workLink;
385
        } else {
386
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
387
        }
388
        return $workLink;
389
    }
390
391
    /**
392
     * Renders the searchInDocument tool
393
     *
394
     * @return void
395
     */
396
    public function searchindocumenttool()
397
    {
398
        if (
399
            $this->document === null
400
            || $this->document->getDoc()->numPages < 1
401
            || empty($this->extConf['fileGrpFulltext'])
402
            || empty($this->settings['solrcore'])
403
        ) {
404
            // Quit without doing anything if required variables are not set.
405
            return;
406
        } else {
407
            if (!empty($this->requestData['logicalPage'])) {
408
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
409
                // The logical page parameter should not appear again
410
                unset($this->requestData['logicalPage']);
411
            }
412
            // Set default values if not set.
413
            // $this->requestData['page'] may be integer or string (physical structure @ID)
414
            if (
415
                (int) $this->requestData['page'] > 0
416
                || empty($this->requestData['page'])
417
            ) {
418
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
419
            } else {
420
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
421
            }
422
        }
423
424
        // Quit if no fulltext file is present
425
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
426
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
427
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) {
428
                $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
429
                break;
430
            }
431
        }
432
        if (empty($fullTextFile)) {
433
            return;
434
        }
435
436
        // Fill markers.
437
        $viewArray = [
438
            'LABEL_QUERY_URL' => $this->settings['queryInputName'],
439
            'LABEL_START' => $this->settings['startInputName'],
440
            'LABEL_ID' => $this->settings['idInputName'],
441
            'LABEL_PAGE_URL' => $this->settings['pageInputName'],
442
            'LABEL_HIGHLIGHT_WORD' => $this->settings['highlightWordInputName'],
443
            'LABEL_ENCRYPTED' => $this->settings['encryptedInputName'],
444
            'CURRENT_DOCUMENT' => $this->getCurrentDocumentId(),
445
            'SOLR_ENCRYPTED' => $this->getEncryptedCoreName() ? : ''
446
        ];
447
448
        $this->view->assign('searchInDocument', $viewArray);
449
    }
450
451
    /**
452
     * Get current document id. As default the uid will be used.
453
     * In case there is defined documentIdUrlSchema then the id will
454
     * extracted from this URL.
455
     *
456
     * @access protected
457
     *
458
     * @return string with current document id
459
     */
460
    protected function getCurrentDocumentId()
461
    {
462
        $id = $this->document->getUid();
463
464
        if ($id !== null && $id > 0) {
465
            // we found the document uid
466
            return (string) $id;
467
        } else {
468
            $id = $this->requestData['id'];
469
            if (!GeneralUtility::isValidUrl($id)) {
470
                // we found no valid URI --> something unexpected we cannot search within.
471
                return '';
472
            }
473
        }
474
475
        // example: https://host.de/items/*id*/record
476
        if (!empty($this->settings['documentIdUrlSchema'])) {
477
            $arr = explode('*', $this->settings['documentIdUrlSchema']);
478
479
            if (count($arr) == 2) {
480
                $id = explode($arr[0], $id)[0];
481
            } else if (count($arr) == 3) {
482
                $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id));
483
                $id = substr($sub, 0, strpos($sub, $arr[2]));
484
            }
485
        }
486
        return $id;
487
    }
488
489
    /**
490
     * Get the encrypted Solr core name
491
     *
492
     * @access protected
493
     *
494
     * @return string with encrypted core name
495
     */
496
    protected function getEncryptedCoreName()
497
    {
498
        // Get core name.
499
        $name = Helper::getIndexNameFromUid($this->settings['solrcore'], 'tx_dlf_solrcores');
500
        // Encrypt core name.
501
        if (!empty($name)) {
502
            $name = Helper::encrypt($name);
503
        }
504
        return $name;
505
    }
506
}
507