1<?php
2namespace TYPO3\CMS\Recordlist\Browser;
3
4/*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17use TYPO3\CMS\Backend\Routing\UriBuilder;
18use TYPO3\CMS\Backend\Template\DocumentTemplate;
19use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20use TYPO3\CMS\Core\Imaging\IconFactory;
21use TYPO3\CMS\Core\Localization\LanguageService;
22use TYPO3\CMS\Core\Page\PageRenderer;
23use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25/**
26 * Base class for element browsers
27 *
28 * NOTE: This class should only be used internally. Extensions must implement the ElementBrowserInterface.
29 * @internal This class is a specific LinkBrowser implementation and is not part of the TYPO3's Core API.
30 */
31abstract class AbstractElementBrowser
32{
33    /**
34     * @var DocumentTemplate
35     */
36    protected $doc;
37
38    /**
39     * @var PageRenderer
40     */
41    protected $pageRenderer;
42
43    /**
44     * URL of current request
45     *
46     * @var string
47     */
48    protected $thisScript = '';
49
50    /**
51     * @var IconFactory
52     */
53    protected $iconFactory;
54
55    /**
56     * Active with TYPO3 Element Browser: Contains the name of the form field for which this window
57     * opens - thus allows us to make references back to the main window in which the form is.
58     * Example value: "data[pages][39][bodytext]|||tt_content|"
59     * or "data[tt_content][NEW3fba56fde763d][image]|||gif,jpg,jpeg,tif,bmp,pcx,tga,png,pdf,ai|"
60     * Values:
61     * 0: form field name reference, eg. "data[tt_content][123][image]"
62     * 1: htmlArea RTE parameters: editorNo:contentTypo3Language
63     * 2: RTE config parameters: RTEtsConfigParams
64     * 3: allowed types. Eg. "tt_content" or "gif,jpg,jpeg,tif,bmp,pcx,tga,png,pdf,ai"
65     * 4: IRRE uniqueness: target level object-id to perform actions/checks on, eg. "data-4-pages-4-nav_icon-sys_file_reference" ("data-<uid>-<table>-<pid>-<field>-<foreign_table>")
66     * 5: IRRE uniqueness: name of function in opener window that checks if element is already used, eg. "inline.checkUniqueElement"
67     * 6: IRRE uniqueness: name of function in opener window that performs some additional(!) action, eg. "inline.setUniqueElement"
68     * 7: IRRE uniqueness: name of function in opener window that performs action instead of using addElement/insertElement, eg. "inline.importElement"
69     *
70     * $pArr = explode('|', $this->bparams);
71     * $formFieldName = $pArr[0];
72     * $allowedTablesOrFileTypes = $pArr[3];
73     *
74     * @var string
75     */
76    protected $bparams;
77
78    /**
79     * Construct
80     */
81    public function __construct()
82    {
83        $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
84        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
85        $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
86        $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Recordlist/ElementBrowser');
87
88        $this->initialize();
89    }
90
91    /**
92     * Main initialization
93     */
94    protected function initialize()
95    {
96        $this->determineScriptUrl();
97        $this->initVariables();
98    }
99
100    /**
101     * Sets the script url depending on being a module or script request
102     */
103    protected function determineScriptUrl()
104    {
105        if ($routePath = GeneralUtility::_GP('route')) {
106            $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
107            $this->thisScript = (string)$uriBuilder->buildUriFromRoutePath($routePath);
108        } else {
109            $this->thisScript = GeneralUtility::getIndpEnv('SCRIPT_NAME');
110        }
111    }
112
113    /**
114     */
115    protected function initVariables()
116    {
117        $this->bparams = GeneralUtility::_GP('bparams');
118        if ($this->bparams === null) {
119            $this->bparams = '';
120        }
121    }
122
123    /**
124     * Initialize document template object
125     */
126    protected function initDocumentTemplate()
127    {
128        $bodyDataAttributes = array_merge(
129            $this->getBParamDataAttributes(),
130            $this->getBodyTagAttributes()
131        );
132        foreach ($bodyDataAttributes as $attributeName => $value) {
133            $this->doc->bodyTagAdditions .= ' ' . $attributeName . '="' . htmlspecialchars($value) . '"';
134        }
135
136        // unset the default jumpToUrl() function as we ship our own
137        unset($this->doc->JScodeArray['jumpToUrl']);
138    }
139
140    /**
141     * @return string[] Array of body-tag attributes
142     */
143    abstract protected function getBodyTagAttributes();
144
145    /**
146     * Splits parts of $this->bparams and returns needed data attributes for the Javascript
147     *
148     * @return string[] Data attributes for Javascript
149     */
150    protected function getBParamDataAttributes()
151    {
152        list($fieldRef, $rteParams, $rteConfig, , $irreObjectId, $irreCheckUniqueAction, $irreAddAction, $irreInsertAction) = explode('|', $this->bparams);
153
154        return [
155            'data-this-script-url' => strpos($this->thisScript, '?') === false ? $this->thisScript . '?' : $this->thisScript . '&',
156            'data-form-field-name' => 'data[' . $fieldRef . '][' . $rteParams . '][' . $rteConfig . ']',
157            'data-field-reference' => $fieldRef,
158            'data-field-reference-slashed' => addslashes($fieldRef),
159            'data-rte-parameters' => $rteParams,
160            'data-rte-configuration' => $rteConfig,
161            'data-irre-object-id' => $irreObjectId,
162            'data-irre-check-unique-action' => $irreCheckUniqueAction,
163            'data-irre-add-action' => $irreAddAction,
164            'data-irre-insert-action' => $irreInsertAction,
165        ];
166    }
167
168    /**
169     * @return LanguageService
170     */
171    protected function getLanguageService()
172    {
173        return $GLOBALS['LANG'];
174    }
175
176    /**
177     * @return BackendUserAuthentication
178     */
179    protected function getBackendUser()
180    {
181        return $GLOBALS['BE_USER'];
182    }
183}
184