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