[BUGFIX] Streamline deprecation messages
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / View / AdminPanelView.php
1 <?php
2
3 namespace TYPO3\CMS\Adminpanel\View;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Adminpanel\Modules\AdminPanelModuleInterface;
19 use TYPO3\CMS\Adminpanel\Service\EditToolbarService;
20 use TYPO3\CMS\Core\Cache\CacheManager;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\PathUtility;
23
24 /**
25 * View class for the admin panel in frontend editing.
26 *
27 * @internal
28 */
29 class AdminPanelView
30 {
31 /**
32 * Determines whether the update button should be shown.
33 *
34 * @var bool
35 */
36 protected $extNeedUpdate = false;
37
38 /**
39 * Force preview
40 *
41 * @var bool
42 */
43 protected $ext_forcePreview = false;
44
45 /**
46 * @var string
47 */
48 protected $extJSCODE = '';
49
50 /**
51 * Array of adminPanel modules
52 *
53 * @var AdminPanelModuleInterface[]
54 */
55 protected $modules = [];
56
57 /**
58 * @var array
59 */
60 protected $configuration;
61
62 /**
63 * Setter for injecting new-style modules
64 *
65 * @see \TYPO3\CMS\Adminpanel\Controller\MainController::render()
66 * @param array $modules
67 * @internal
68 */
69 public function setModules(array $modules): void
70 {
71 $this->modules = $modules;
72 }
73
74 /**
75 * Returns true if admin panel was activated
76 * (switched "on" via GUI)
77 *
78 * @return bool
79 */
80 protected function isAdminPanelActivated(): bool
81 {
82 return $this->getBackendUser()->uc['TSFE_adminConfig']['display_top'] ?? false;
83 }
84
85 /**
86 * Returns LanguageService
87 *
88 * @return \TYPO3\CMS\Core\Localization\LanguageService
89 */
90 protected function getLanguageService()
91 {
92 return $GLOBALS['LANG'];
93 }
94
95 /**
96 * Returns the current BE user.
97 *
98 * @return \TYPO3\CMS\Backend\FrontendBackendUserAuthentication
99 */
100 protected function getBackendUser()
101 {
102 return $GLOBALS['BE_USER'];
103 }
104
105 /**
106 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
107 */
108 protected function getTypoScriptFrontendController()
109 {
110 return $GLOBALS['TSFE'];
111 }
112
113 /*****************************************************
114 * Admin Panel: Deprecated API
115 ****************************************************/
116
117 /**
118 * Backwards compatibility method ensuring hook still gets the same content as before
119 *
120 * @deprecated since TYPO3 v9 - remove when hook can be removed
121 * @internal
122 * @return string
123 * @throws \UnexpectedValueException
124 */
125 public function callDeprecatedHookObject(): string
126 {
127 $moduleContent = '';
128 if ($this->isAdminPanelActivated()) {
129 foreach ($this->modules as $module) {
130 $moduleContent .= $this->getModule($module);
131 }
132
133 foreach (
134 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_adminpanel.php']['extendAdminPanel']
135 ??
136 [] as $className
137 ) {
138 trigger_error(
139 'The hook $GLOBALS[\'TYPO3_CONF_VARS\'][\'SC_OPTIONS\'][\'tslib/class.tslib_adminpanel.php\'][\'extendAdminPanel\'] is deprecated, register an AdminPanelModule instead.',
140 E_USER_DEPRECATED
141 );
142 $hookObject = GeneralUtility::makeInstance($className);
143 if (!$hookObject instanceof AdminPanelViewHookInterface) {
144 throw new \UnexpectedValueException(
145 $className . ' must implement interface ' . AdminPanelViewHookInterface::class,
146 1311942539
147 );
148 }
149 $content = $hookObject->extendAdminPanel($moduleContent, $this);
150 if ($content) {
151 $moduleContent .= '<div class="typo3-adminPanel-section typo3-adminPanel-section-open">';
152 $moduleContent .= ' <div class="typo3-adminPanel-section-body">';
153 $moduleContent .= ' ' . $content;
154 $moduleContent .= ' </div>';
155 $moduleContent .= '</div>';
156 }
157 }
158 }
159 return $moduleContent;
160 }
161
162 /**
163 * Render a single module with header panel
164 *
165 * @deprecated Since TYPO3 v9 - only used in deprecated hook call (which triggers the corresponding deprecation error)
166 * @param AdminPanelModuleInterface $module
167 * @return string
168 */
169 protected function getModule(AdminPanelModuleInterface $module): string
170 {
171 $output = [];
172
173 if ($module->isEnabled()) {
174 $output[] = '<div class="typo3-adminPanel-section typo3-adminPanel-section-open">';
175 $output[] = ' <div class="typo3-adminPanel-section-title">';
176 $output[] = ' ' . $this->getSectionOpenerLink($module);
177 $output[] = ' </div>';
178 $output[] = '<div class="typo3-adminPanel-section-body">';
179 $output[] = ' ' . $module->getContent();
180 $output[] = '</div>';
181 $output[] = '</div>';
182 }
183
184 foreach ($module->getJavaScriptFiles() as $javaScriptFile) {
185 $output[] =
186 '<script src="' .
187 PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($javaScriptFile)) .
188 '"></script>';
189 }
190
191 return implode('', $output);
192 }
193
194 /*****************************************************
195 * Admin Panel Layout Helper functions
196 ****************************************************/
197
198 /**
199 * Wraps a string in a link which will open/close a certain part of the Admin Panel
200 *
201 * @deprecated Since TYPO3 v9 - only used in deprecated hook call (which triggers the corresponding deprecation error)
202 * @param AdminPanelModuleInterface $module
203 * @return string
204 */
205 protected function getSectionOpenerLink(AdminPanelModuleInterface $module): string
206 {
207 $identifier = $module->getIdentifier();
208 $onclick = 'document.TSFE_ADMIN_PANEL_FORM[' .
209 GeneralUtility::quoteJSvalue('TSFE_ADMIN_PANEL[display_' . $identifier . ']') .
210 '].value=' .
211 ($this->getBackendUser()->uc['TSFE_adminConfig']['display_' . $identifier] ? '0' : '1') .
212 ';document.TSFE_ADMIN_PANEL_FORM.submit();return false;';
213
214 $output = [];
215 $output[] = '<span class="typo3-adminPanel-section-title-identifier"></span>';
216 $output[] = '<a href="javascript:void(0)" onclick="' . htmlspecialchars($onclick) . '">';
217 $output[] = ' ' . htmlspecialchars($module->getLabel());
218 $output[] = '</a>';
219 $output[] = '<input type="hidden" name="TSFE_ADMIN_PANEL[display_' .
220 $identifier .
221 ']" value="' .
222 1 .
223 '" />';
224
225 return implode('', $output);
226 }
227
228 /**
229 * Creates the tool bar links for the "edit" section of the Admin Panel.
230 *
231 * @deprecated Since TYPO3 v9 - use EditToolbarService instead or create buttons via fluid
232 * @deprecated
233 * @return string A string containing images wrapped in <a>-tags linking them to proper functions.
234 */
235 public function ext_makeToolBar(): string
236 {
237 trigger_error(
238 'Deprecated since TYPO3 v9, use fluid and backend uri builder to create a toolbar',
239 E_USER_DEPRECATED
240 );
241 $editToolbarService = GeneralUtility::makeInstance(EditToolbarService::class);
242 return $editToolbarService->createToolbar();
243 }
244
245 /**
246 * Translate given key
247 *
248 * @param string $key Key for a label in the $LOCAL_LANG array of "sysext/lang/Resources/Private/Language/locallang_tsfe.xlf
249 * @param bool $convertWithHtmlspecialchars If TRUE the language-label will be sent through htmlspecialchars
250 * @deprecated Since TYPO3 v9 - only used in deprecated methods
251 * @return string The value for the $key
252 */
253 protected function extGetLL($key, $convertWithHtmlspecialchars = true)
254 {
255 $labelStr = $this->getLanguageService()->getLL($key);
256 if ($convertWithHtmlspecialchars) {
257 $labelStr = htmlspecialchars($labelStr);
258 }
259 return $labelStr;
260 }
261
262 /**
263 * Add an additional stylesheet
264 *
265 * @return string
266 * @deprecated since TYPO3 v9 - implement AdminPanelModules via the new API (see AdminPanelModuleInterface)
267 */
268 public function getAdminPanelHeaderData()
269 {
270 trigger_error(
271 'Deprecated since TYPO3 v9 - implement AdminPanelModules via the new API (see AdminPanelModuleInterface)',
272 E_USER_DEPRECATED
273 );
274 $result = '';
275 if (!empty($GLOBALS['TBE_STYLES']['stylesheets']['admPanel'])) {
276 $stylesheet = GeneralUtility::locationHeaderUrl($GLOBALS['TBE_STYLES']['stylesheets']['admPanel']);
277 $result = '<link rel="stylesheet" type="text/css" href="' .
278 htmlspecialchars($stylesheet, ENT_QUOTES | ENT_HTML5) . '" />';
279 }
280 return $result;
281 }
282
283 /**
284 * Checks if an Admin Panel section ("module") is available for the user. If so, TRUE is returned.
285 *
286 * @param string $key The module key, eg. "edit", "preview", "info" etc.
287 * @deprecated
288 * @return bool
289 */
290 public function isAdminModuleEnabled($key)
291 {
292 trigger_error(
293 'Deprecated since TYPO3 v9 - implement AdminPanelModules via the new API (see AdminPanelModuleInterface)',
294 E_USER_DEPRECATED
295 );
296 $result = false;
297 // Returns TRUE if the module checked is "preview" and the forcePreview flag is set.
298 if ($key === 'preview' && $this->ext_forcePreview) {
299 $result = true;
300 } elseif (!empty($this->configuration['enable.']['all'])) {
301 $result = true;
302 } elseif (!empty($this->configuration['enable.'][$key])) {
303 $result = true;
304 }
305 return $result;
306 }
307
308 /**
309 * Saves any change in settings made in the Admin Panel.
310 *
311 * @deprecated since TYPO3 v9 - implement AdminPanelModules via the new API (see AdminPanelModuleInterface)
312 */
313 public function saveConfigOptions()
314 {
315 trigger_error(
316 'Deprecated since TYPO3 v9 - implement AdminPanelModules via the new API (see AdminPanelModuleInterface)',
317 E_USER_DEPRECATED
318 );
319 $input = GeneralUtility::_GP('TSFE_ADMIN_PANEL');
320 $beUser = $this->getBackendUser();
321 if (is_array($input)) {
322 // Setting
323 $beUser->uc['TSFE_adminConfig'] = array_merge(
324 !is_array($beUser->uc['TSFE_adminConfig']) ? [] : $beUser->uc['TSFE_adminConfig'],
325 $input
326 );
327 unset($beUser->uc['TSFE_adminConfig']['action']);
328
329 foreach ($this->modules as $module) {
330 if ($module->isEnabled()) {
331 // We use TYPO3_REQUEST for compatibility reasons. The object and this method are deprecated anyway, this should be fine.
332 $module->onSubmit($input, $GLOBALS['TYPO3_REQUEST']);
333 }
334 }
335 // Saving
336 $beUser->writeUC();
337 // Flush fluid template cache
338 $cacheManager = new CacheManager();
339 $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
340 $cacheManager->getCache('fluid_template')->flush();
341 }
342 }
343
344 /**
345 * Returns the value for an Admin Panel setting.
346 *
347 * @param string $sectionName Module key
348 * @param string $val Setting key
349 * @return mixed The setting value
350 * @deprecated Since TYPO3 v9 - implement AdminPanelModules via the new API (see AdminPanelModuleInterface)
351 */
352 public function extGetFeAdminValue($sectionName, $val = '')
353 {
354 trigger_error(
355 'Deprecated since TYPO3 v9 - implement AdminPanelModules via the new API (see AdminPanelModuleInterface)',
356 E_USER_DEPRECATED
357 );
358 if (!$this->isAdminModuleEnabled($sectionName)) {
359 return null;
360 }
361
362 $beUser = $this->getBackendUser();
363 // Exceptions where the values can be overridden (forced) from backend:
364 // deprecated
365 if (
366 $sectionName === 'edit' && (
367 $val === 'displayIcons' && $this->configuration['module.']['edit.']['forceDisplayIcons'] ||
368 $val === 'displayFieldIcons' && $this->configuration['module.']['edit.']['forceDisplayFieldIcons'] ||
369 $val === 'editNoPopup' && $this->configuration['module.']['edit.']['forceNoPopup']
370 )
371 ) {
372 return true;
373 }
374
375 // Override all settings with user TSconfig
376 if ($val && isset($this->configuration['override.'][$sectionName . '.'][$val])) {
377 return $this->configuration['override.'][$sectionName . '.'][$val];
378 }
379 if (!$val && isset($this->configuration['override.'][$sectionName])) {
380 return $this->configuration['override.'][$sectionName];
381 }
382
383 $returnValue = $val ? $beUser->uc['TSFE_adminConfig'][$sectionName . '_' . $val] : 1;
384
385 // Exception for preview
386 if ($sectionName === 'preview' && $this->ext_forcePreview) {
387 return !$val ? true : $returnValue;
388 }
389
390 // See if the menu is expanded!
391 return $this->isAdminModuleOpen($sectionName) ? $returnValue : null;
392 }
393
394 /**
395 * Enables the force preview option.
396 *
397 * @deprecated since TYPO3 v9 - see AdminPanelModule: Preview
398 */
399 public function forcePreview()
400 {
401 trigger_error('Deprecated since TYPO3 v9, see AdminPanelModule: Preview', E_USER_DEPRECATED);
402 $this->ext_forcePreview = true;
403 }
404
405 /**
406 * Returns TRUE if admin panel module is open
407 *
408 * @param string $key Module key
409 * @return bool TRUE, if the admin panel is open for the specified admin panel module key.
410 * @deprecated Since TYPO3 v9 - implement AdminPanelModules via the new API
411 */
412 public function isAdminModuleOpen($key)
413 {
414 trigger_error('since TYPO3 v9 - use new AdminPanel API instead', E_USER_DEPRECATED);
415 return $this->getBackendUser()->uc['TSFE_adminConfig']['display_top'] &&
416 $this->getBackendUser()->uc['TSFE_adminConfig']['display_' . $key];
417 }
418
419 /**
420 * Returns a row (with 4 columns) for content in a section of the Admin Panel.
421 * It will take $pre as a key to a label to display and $element as the content to put into the forth cell.
422 *
423 * @param string $title Key to label
424 * @param string $content The HTML content for the forth table cell.
425 * @param string $checkbox The HTML for a checkbox or hidden fields.
426 * @param string $innerDivClass The Class attribute for the td element.
427 * @param string $outerDivClass The Class attribute for the tr element.
428 * @return string HTML table row.
429 * @see extGetHead()
430 * @deprecated since TYPO3 v9 - use new AdminPanel API instead
431 */
432 public function extGetItem($title, $content = '', $checkbox = '', $outerDivClass = null, $innerDivClass = null)
433 {
434 trigger_error('since TYPO3 v9 - use new AdminPanel API instead', E_USER_DEPRECATED);
435 $title = $title ? '<label for="' . htmlspecialchars($title) . '">' . $this->extGetLL($title) . '</label>' : '';
436 $out = '';
437 $out .= (string)$outerDivClass ? '<div class="' . htmlspecialchars($outerDivClass) . '">' : '<div>';
438 $out .= (string)$innerDivClass ? '<div class="' . htmlspecialchars($innerDivClass) . '">' : '<div>';
439 $out .= $checkbox . $title . $content . '</div></div>';
440 return $out;
441 }
442
443 /**
444 * Returns a row (with colspan=4) which is a header for a section in the Admin Panel.
445 * It will have a plus/minus icon and a label which is linked so that it submits the form which surrounds the whole Admin Panel when clicked, alterting the TSFE_ADMIN_PANEL[display_' . $pre . '] value
446 * See the functions get*Module
447 *
448 * @param string $sectionSuffix The suffix to the display_ label. Also selects the label from the LOCAL_LANG array.
449 * @return string HTML table row.
450 * @see extGetItem()
451 * @deprecated since TYPO3 v9 - use new AdminPanel API instead
452 */
453 public function extGetHead($sectionSuffix)
454 {
455 trigger_error('since TYPO3 v9 - use new AdminPanel API instead', E_USER_DEPRECATED);
456 return $this->linkSectionHeader($sectionSuffix, $this->extGetLL($sectionSuffix));
457 }
458
459 /**
460 * Wraps a string in a link which will open/close a certain part of the Admin Panel
461 *
462 * @param string $sectionSuffix The code for the display_ label/key
463 * @param string $sectionTitle Title (HTML-escaped)
464 * @param string $className The classname for the <a> tag
465 * @return string $className Linked input string
466 * @see extGetHead()
467 * @deprecated since TYPO3 v9 - use new AdminPanel API instead
468 */
469 public function linkSectionHeader($sectionSuffix, $sectionTitle, $className = '')
470 {
471 trigger_error('since TYPO3 v9 - use new AdminPanel API instead', E_USER_DEPRECATED);
472 $onclick = 'document.TSFE_ADMIN_PANEL_FORM[' .
473 GeneralUtility::quoteJSvalue('TSFE_ADMIN_PANEL[display_' . $sectionSuffix . ']') .
474 '].value=' .
475 ($this->getBackendUser()->uc['TSFE_adminConfig']['display_' . $sectionSuffix] ? '0' : '1') .
476 ';document.TSFE_ADMIN_PANEL_FORM.submit();return false;';
477
478 $output = [];
479 $output[] = '<span class="typo3-adminPanel-section-title-identifier"></span>';
480 $output[] = '<a href="javascript:void(0)" onclick="' . htmlspecialchars($onclick) . '">';
481 $output[] = ' ' . $sectionTitle;
482 $output[] = '</a>';
483 $output[] = '<input type="hidden" name="TSFE_ADMIN_PANEL[display_' .
484 $sectionSuffix .
485 ']" value="' .
486 (int)$this->isAdminModuleOpen($sectionSuffix) .
487 '" />';
488
489 return implode('', $output);
490 }
491 }