Completed
Push — master ( 366a5b...a17b2a )
by
unknown
17:34 queued 14:29
created

AbstractPlugin::main()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
c 0
b 0
f 0
1
<?php
2
namespace Kitodo\Dlf\Common;
3
4
/**
5
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
6
 *
7
 * This file is part of the Kitodo and TYPO3 projects.
8
 *
9
 * @license GNU General Public License version 3 or later.
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 */
13
14
/**
15
 * Abstract plugin class for the 'dlf' extension
16
 *
17
 * @author Sebastian Meyer <[email protected]>
18
 * @package TYPO3
19
 * @subpackage dlf
20
 * @access public
21
 * @abstract
22
 */
23
abstract class AbstractPlugin extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin {
24
    public $extKey = 'dlf';
25
    public $prefixId = 'tx_dlf';
26
    public $scriptRelPath = 'Classes/Common/AbstractPlugin.php';
27
    // Plugins are cached by default (@see setCache()).
28
    public $pi_USER_INT_obj = FALSE;
29
    public $pi_checkCHash = TRUE;
30
31
    /**
32
     * This holds the current document
33
     *
34
     * @var \Kitodo\Dlf\Common\Document
35
     * @access protected
36
     */
37
    protected $doc;
38
39
    /**
40
     * This holds the plugin's parsed template
41
     *
42
     * @var string
43
     * @access protected
44
     */
45
    protected $template = '';
46
47
    /**
48
     * Read and parse the template file
49
     *
50
     * @access protected
51
     *
52
     * @param string $part: Name of the subpart to load
0 ignored issues
show
Bug introduced by
There is no parameter named $part:. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
53
     *
54
     * @return void
55
     */
56
    protected function getTemplate($part = '###TEMPLATE###') {
57
        if (!empty($this->conf['templateFile'])) {
58
            // Load template file from configuration.
59
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), $part);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Frontend\Conte...enderer::fileResource() has been deprecated with message: since TYPO3 v8, will be removed in TYPO3 v9, use file_get_contents() directly

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method TYPO3\CMS\Frontend\Conte...tRenderer::getSubpart() has been deprecated with message: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
60
        } else {
61
            // Load default template file.
62
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/Resources/Private/Templates/'.Helper::getUnqualifiedClassName(get_class($this)).'.tmpl'), $part);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Frontend\Conte...enderer::fileResource() has been deprecated with message: since TYPO3 v8, will be removed in TYPO3 v9, use file_get_contents() directly

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method TYPO3\CMS\Frontend\Conte...tRenderer::getSubpart() has been deprecated with message: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
63
        }
64
    }
65
66
    /**
67
     * All the needed configuration values are stored in class variables
68
     * Priority: Flexforms > TS-Templates > Extension Configuration > ext_localconf.php
69
     *
70
     * @access protected
71
     *
72
     * @param array $conf: configuration array from TS-Template
0 ignored issues
show
Bug introduced by
There is no parameter named $conf:. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
73
     *
74
     * @return void
75
     */
76
    protected function init(array $conf) {
77
        // Read FlexForm configuration.
78
        $flexFormConf = [];
79
        $this->cObj->readFlexformIntoConf($this->cObj->data['pi_flexform'], $flexFormConf);
80
        if (!empty($flexFormConf)) {
81
            $conf = Helper::mergeRecursiveWithOverrule($flexFormConf, $conf);
82
        }
83
        // Read plugin TS configuration.
84
        $pluginConf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_dlf_'.strtolower(Helper::getUnqualifiedClassName(get_class($this))).'.'];
85
        if (is_array($pluginConf)) {
86
            $conf = Helper::mergeRecursiveWithOverrule($pluginConf, $conf);
87
        }
88
        // Read general TS configuration.
89
        $generalConf = $GLOBALS['TSFE']->tmpl->setup['plugin.'][$this->prefixId.'.'];
90
        if (is_array($generalConf)) {
91
            $conf = Helper::mergeRecursiveWithOverrule($generalConf, $conf);
92
        }
93
        // Read extension configuration.
94
        $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$this->extKey]);
95
        if (is_array($extConf)) {
96
            $conf = Helper::mergeRecursiveWithOverrule($extConf, $conf);
97
        }
98
        // Read TYPO3_CONF_VARS configuration.
99
        $varsConf = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey];
100
        if (is_array($varsConf)) {
101
            $conf = Helper::mergeRecursiveWithOverrule($varsConf, $conf);
102
        }
103
        $this->conf = $conf;
104
        // Set default plugin variables.
105
        $this->pi_setPiVarDefaults();
106
        // Load translation files.
107
        $this->pi_loadLL('EXT:'.$this->extKey.'/Resources/Private/Language/'.Helper::getUnqualifiedClassName(get_class($this)).'.xml');
108
    }
109
110
    /**
111
     * Loads the current document into $this->doc
112
     *
113
     * @access protected
114
     *
115
     * @return void
116
     */
117
    protected function loadDocument() {
118
        // Check for required variable.
119
        if (!empty($this->piVars['id'])
120
            && !empty($this->conf['pages'])) {
121
            // Should we exclude documents from other pages than $this->conf['pages']?
122
            $pid = (!empty($this->conf['excludeOther']) ? intval($this->conf['pages']) : 0);
123
            // Get instance of \Kitodo\Dlf\Common\Document.
124
            $this->doc = Document::getInstance($this->piVars['id'], $pid);
125
            if (!$this->doc->ready) {
0 ignored issues
show
Documentation introduced by
The property $ready is declared protected in Kitodo\Dlf\Common\Document. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
126
                // Destroy the incomplete object.
127
                $this->doc = NULL;
128
                Helper::devLog('Failed to load document with UID '.$this->piVars['id'], DEVLOG_SEVERITY_ERROR);
129
            } else {
130
                // Set configuration PID.
131
                $this->doc->cPid = $this->conf['pages'];
0 ignored issues
show
Documentation introduced by
The property $cPid is declared protected in Kitodo\Dlf\Common\Document. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
132
            }
133 View Code Duplication
        } elseif (!empty($this->piVars['recordId'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
            // Get UID of document with given record identifier.
135
            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
136
                'tx_dlf_documents.uid',
137
                'tx_dlf_documents',
138
                'tx_dlf_documents.record_id='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->piVars['recordId'], 'tx_dlf_documents')
139
                    .Helper::whereClause('tx_dlf_documents'),
140
                '',
141
                '',
142
                '1'
143
            );
144
            if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) == 1) {
145
                list ($this->piVars['id']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
146
                // Set superglobal $_GET array and unset variables to avoid infinite looping.
147
                $_GET[$this->prefixId]['id'] = $this->piVars['id'];
148
                unset ($this->piVars['recordId'], $_GET[$this->prefixId]['recordId']);
149
                // Try to load document.
150
                $this->loadDocument();
151
            } else {
152
                Helper::devLog('Failed to load document with record ID "'.$this->piVars['recordId'].'"', DEVLOG_SEVERITY_ERROR);
153
            }
154
        } else {
155
            Helper::devLog('Invalid UID '.$this->piVars['id'].' or PID '.$this->conf['pages'].' for document loading', DEVLOG_SEVERITY_ERROR);
156
        }
157
    }
158
159
    /**
160
     * The main method of the PlugIn
161
     *
162
     * @access public
163
     *
164
     * @param string $content: The PlugIn content
0 ignored issues
show
Documentation introduced by
There is no parameter named $content:. Did you maybe mean $content?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
165
     * @param array $conf: The PlugIn configuration
0 ignored issues
show
Bug introduced by
There is no parameter named $conf:. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
166
     *
167
     * @abstract
168
     *
169
     * @return string The content that is displayed on the website
170
     */
171
    abstract public function main($content, $conf);
172
173
    /**
174
     * Wraps the input string in a tag with the class attribute set to the class name
175
     *
176
     * @access public
177
     *
178
     * @param string $content: HTML content to wrap in the div-tags with the class of the plugin
0 ignored issues
show
Documentation introduced by
There is no parameter named $content:. Did you maybe mean $content?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
179
     *
180
     * @return string HTML content wrapped, ready to return to the parent object.
181
     */
182
    public function pi_wrapInBaseClass($content) {
183
        if (!$GLOBALS['TSFE']->config['config']['disableWrapInBaseClass']) {
184
            // Use class name instead of $this->prefixId for content wrapping because $this->prefixId is the same for all plugins.
185
            $content = '<div class="tx-dlf-'.Helper::getUnqualifiedClassName(get_class($this)).'">'.$content.'</div>';
186
            if (!$GLOBALS['TSFE']->config['config']['disablePrefixComment']) {
187
                $content = "\n\n<!-- BEGIN: Content of extension '".$this->extKey."', plugin '".Helper::getUnqualifiedClassName(get_class($this))."' -->\n\n".$content."\n\n<!-- END: Content of extension '".$this->extKey."', plugin '".Helper::getUnqualifiedClassName(get_class($this))."' -->\n\n";
188
            }
189
        }
190
        return $content;
191
    }
192
193
    /**
194
     * Parses a string into a Typoscript array
195
     *
196
     * @access protected
197
     *
198
     * @param string $string: The string to parse
0 ignored issues
show
Documentation introduced by
There is no parameter named $string:. Did you maybe mean $string?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
199
     *
200
     * @return array The resulting typoscript array
201
     */
202
    protected function parseTS($string = '') {
203
        $parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::class);
204
        $parser->parse($string);
205
        return $parser->setup;
206
    }
207
208
    /**
209
     * Sets some configuration variables if the plugin is cached.
210
     *
211
     * @access protected
212
     *
213
     * @param boolean $cache: Should the plugin be cached?
0 ignored issues
show
Documentation introduced by
There is no parameter named $cache:. Did you maybe mean $cache?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
214
     *
215
     * @return void
216
     */
217
    protected function setCache($cache = TRUE) {
218
        if ($cache) {
219
            // Set cObject type to "USER" (default).
220
            $this->pi_USER_INT_obj = FALSE;
221
            $this->pi_checkCHash = TRUE;
222
            if (count($this->piVars)) {
223
                // Check cHash or disable caching.
224
                $GLOBALS['TSFE']->reqCHash();
225
            }
226
        } else {
227
            // Set cObject type to "USER_INT".
228
            $this->pi_USER_INT_obj = TRUE;
229
            $this->pi_checkCHash = FALSE;
230
            // Plugins are of type "USER" by default, so convert it to "USER_INT".
231
            $this->cObj->convertToUserIntObject();
232
        }
233
    }
234
}
235