Passed
Push — master ( 757b9a...3f7054 )
by
unknown
05:20 queued 02:36
created

DocumentTypeFunctionProvider::loadDocument()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 21
c 5
b 1
f 0
dl 0
loc 31
rs 9.2728
cc 5
nc 5
nop 1
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\ExpressionLanguage;
14
15
use Kitodo\Dlf\Common\Document;
16
use Kitodo\Dlf\Common\Helper;
17
use Kitodo\Dlf\Common\IiifManifest;
18
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
19
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Core\Database\ConnectionPool;
22
23
/**
24
 * Provider class for additional "getDocmentType" function to the ExpressionLanguage.
25
 *
26
 * @author Alexander Bigga <[email protected]>
27
 * @package TYPO3
28
 * @subpackage dlf
29
 * @access public
30
 */
31
class DocumentTypeFunctionProvider implements ExpressionFunctionProviderInterface
32
{
33
    /**
34
     * This holds the extension's parameter prefix
35
     * @see \Kitodo\Dlf\Common\AbstractPlugin
36
     *
37
     * @var string
38
     * @access protected
39
     */
40
    protected $prefixId = 'tx_dlf';
41
42
    /**
43
     * @return ExpressionFunction[] An array of Function instances
44
     */
45
    public function getFunctions()
46
    {
47
        return [
48
            $this->getDocumentTypeFunction(),
49
        ];
50
    }
51
52
    /**
53
     * Shortcut function to access field values
54
     *
55
     * @return \Symfony\Component\ExpressionLanguage\ExpressionFunction
56
     */
57
    protected function getDocumentTypeFunction(): ExpressionFunction
58
    {
59
        return new ExpressionFunction(
60
            'getDocumentType',
61
            function () {
62
                // Not implemented, we only use the evaluator
63
            },
64
            function ($arguments, $cPid) {
65
                /** @var RequestWrapper $requestWrapper */
66
                $requestWrapper = $arguments['request'];
67
                $queryParams = $requestWrapper->getQueryParams();
68
69
                $type = 'undefined';
70
71
                // Load document with current plugin parameters.
72
                $doc = $this->loadDocument($queryParams[$this->prefixId]);
73
                if ($doc === null) {
74
                    return $type;
75
                }
76
                $metadata = $doc->getTitledata($cPid);
77
                if (!empty($metadata['type'][0])) {
78
                    // Calendar plugin does not support IIIF (yet). Abort for all newspaper related types.
79
                    if (
80
                        $doc instanceof IiifManifest
81
                        && array_search($metadata['type'][0], ['newspaper', 'ephemera', 'year', 'issue']) !== false
82
                    ) {
83
                        return $type;
84
                    }
85
                    $type = $metadata['type'][0];
86
                }
87
                return $type;
88
            });
89
    }
90
91
    /**
92
     * Loads the current document
93
     * @see \Kitodo\Dlf\Common\AbstractPlugin->loadDocument()
94
     *
95
     * @access protected
96
     *
97
     * @param array $piVars The current plugin variables containing a document identifier
98
     *
99
     * @return \Kitodo\Dlf\Common\Document Instance of the current document
100
     */
101
    protected function loadDocument(array $piVars)
102
    {
103
        // Check for required variable.
104
        if (!empty($piVars['id'])) {
105
            // Get instance of document.
106
            $doc = Document::getInstance($piVars['id']);
107
            if ($doc->ready) {
108
                return $doc;
109
            } else {
110
                Helper::devLog('Failed to load document with UID ' . $piVars['id'], DEVLOG_SEVERITY_WARNING);
111
            }
112
        } elseif (!empty($piVars['recordId'])) {
113
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
114
                ->getQueryBuilderForTable('tx_dlf_documents');
115
116
            // Get UID of document with given record identifier.
117
            $result = $queryBuilder
118
                ->select('tx_dlf_documents.uid AS uid')
119
                ->from('tx_dlf_documents')
120
                ->where(
121
                    $queryBuilder->expr()->eq('tx_dlf_documents.record_id', $queryBuilder->expr()->literal($piVars['recordId'])),
122
                    Helper::whereExpression('tx_documents')
123
                )
124
                ->setMaxResults(1)
125
                ->execute();
126
127
            if ($resArray = $result->fetch()) {
128
                // Try to load document.
129
                return $this->loadDocument(['id' => $resArray['uid']]);
130
            } else {
131
                Helper::devLog('Failed to load document with record ID "' . $piVars['recordId'] . '"', DEVLOG_SEVERITY_WARNING);
132
            }
133
        }
134
    }
135
}
136