98479bcf4ecb2334a2ba11cd4ada5b39999186a7
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / Browser / AbstractElementBrowser.php
1 <?php
2 namespace 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
17 use TYPO3\CMS\Backend\Routing\UriBuilder;
18 use TYPO3\CMS\Backend\Template\DocumentTemplate;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Localization\LanguageService;
22 use TYPO3\CMS\Core\Page\PageRenderer;
23 use 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 */
30 abstract class AbstractElementBrowser
31 {
32 /**
33 * @var DocumentTemplate
34 */
35 protected $doc;
36
37 /**
38 * @var PageRenderer
39 */
40 protected $pageRenderer;
41
42 /**
43 * URL of current request
44 *
45 * @var string
46 */
47 protected $thisScript = '';
48
49 /**
50 * @var IconFactory
51 */
52 protected $iconFactory;
53
54 /**
55 * Active with TYPO3 Element Browser: Contains the name of the form field for which this window
56 * opens - thus allows us to make references back to the main window in which the form is.
57 * Example value: "data[pages][39][bodytext]|||tt_content|"
58 * or "data[tt_content][NEW3fba56fde763d][image]|||gif,jpg,jpeg,tif,bmp,pcx,tga,png,pdf,ai|"
59 *
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[79][tt_address][1][<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->loadJquery();
87 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Recordlist/ElementBrowser');
88
89 $this->initialize();
90 }
91
92 /**
93 * Main initialization
94 */
95 protected function initialize()
96 {
97 $this->determineScriptUrl();
98 $this->initVariables();
99 }
100
101 /**
102 * Sets the script url depending on being a module or script request
103 */
104 protected function determineScriptUrl()
105 {
106 if ($routePath = GeneralUtility::_GP('route')) {
107 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
108 $this->thisScript = (string)$uriBuilder->buildUriFromRoutePath($routePath);
109 } else {
110 $this->thisScript = GeneralUtility::getIndpEnv('SCRIPT_NAME');
111 }
112 }
113
114 /**
115 */
116 protected function initVariables()
117 {
118 $this->bparams = GeneralUtility::_GP('bparams');
119 if ($this->bparams === null) {
120 $this->bparams = '';
121 }
122 }
123
124 /**
125 * Initialize document template object
126 */
127 protected function initDocumentTemplate()
128 {
129 $bodyDataAttributes = array_merge(
130 $this->getBParamDataAttributes(),
131 $this->getBodyTagAttributes()
132 );
133 foreach ($bodyDataAttributes as $attributeName => $value) {
134 $this->doc->bodyTagAdditions .= ' ' . $attributeName . '="' . htmlspecialchars($value) . '"';
135 }
136
137 // unset the default jumpToUrl() function as we ship our own
138 unset($this->doc->JScodeArray['jumpToUrl']);
139 }
140
141 /**
142 * @return string[] Array of body-tag attributes
143 */
144 abstract protected function getBodyTagAttributes();
145
146 /**
147 * Splits parts of $this->bparams and returns needed data attributes for the Javascript
148 *
149 * @return string[] Data attributes for Javascript
150 */
151 protected function getBParamDataAttributes()
152 {
153 list($fieldRef, $rteParams, $rteConfig, , $irreObjectId, $irreCheckUniqueAction, $irreAddAction, $irreInsertAction) = explode('|', $this->bparams);
154
155 return [
156 'data-this-script-url' => strpos($this->thisScript, '?') === false ? $this->thisScript . '?' : $this->thisScript . '&',
157 'data-form-field-name' => 'data[' . $fieldRef . '][' . $rteParams . '][' . $rteConfig . ']',
158 'data-field-reference' => $fieldRef,
159 'data-field-reference-slashed' => addslashes($fieldRef),
160 'data-rte-parameters' => $rteParams,
161 'data-rte-configuration' => $rteConfig,
162 'data-irre-object-id' => $irreObjectId,
163 'data-irre-check-unique-action' => $irreCheckUniqueAction,
164 'data-irre-add-action' => $irreAddAction,
165 'data-irre-insert-action' => $irreInsertAction,
166 ];
167 }
168
169 /**
170 * @return LanguageService
171 */
172 protected function getLanguageService()
173 {
174 return $GLOBALS['LANG'];
175 }
176
177 /**
178 * @return BackendUserAuthentication
179 */
180 protected function getBackendUser()
181 {
182 return $GLOBALS['BE_USER'];
183 }
184 }