[!!!][TASK] Remove deprecated code from backend controllers
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / ContentElement / NewContentElementController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\ContentElement;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Module\AbstractModule;
20 use TYPO3\CMS\Backend\Template\DocumentTemplate;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
22 use TYPO3\CMS\Backend\View\BackendLayoutView;
23 use TYPO3\CMS\Backend\Wizard\NewContentElementWizardHookInterface;
24 use TYPO3\CMS\Core\Imaging\Icon;
25 use TYPO3\CMS\Core\Imaging\IconProvider\BitmapIconProvider;
26 use TYPO3\CMS\Core\Imaging\IconRegistry;
27 use TYPO3\CMS\Core\Utility\ArrayUtility;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Core\Utility\StringUtility;
30
31 /**
32 * Script Class for the New Content element wizard
33 */
34 class NewContentElementController extends AbstractModule
35 {
36 /**
37 * Page id
38 *
39 * @var int
40 */
41 public $id;
42
43 /**
44 * Sys language
45 *
46 * @var int
47 */
48 public $sys_language = 0;
49
50 /**
51 * Return URL.
52 *
53 * @var string
54 */
55 public $R_URI = '';
56
57 /**
58 * If set, the content is destined for a specific column.
59 *
60 * @var int|null
61 */
62 public $colPos;
63
64 /**
65 * @var int
66 */
67 public $uid_pid;
68
69 /**
70 * Module TSconfig.
71 *
72 * @var array
73 */
74 public $modTSconfig = array();
75
76 /**
77 * Internal backend template object
78 *
79 * @var DocumentTemplate
80 */
81 public $doc;
82
83 /**
84 * Used to accumulate the content of the module.
85 *
86 * @var string
87 */
88 public $content;
89
90 /**
91 * Access boolean.
92 *
93 * @var bool
94 */
95 public $access;
96
97 /**
98 * config of the wizard
99 *
100 * @var array
101 */
102 public $config;
103
104 /**
105 * @var array
106 */
107 protected $pageInfo;
108
109 /**
110 * @var array
111 */
112 protected $elementWrapper;
113
114 /**
115 * @var string
116 */
117 protected $onClickEvent;
118
119 /**
120 * @var array
121 */
122 protected $MCONF;
123
124 /**
125 * Constructor
126 */
127 public function __construct()
128 {
129 parent::__construct();
130 $GLOBALS['SOBE'] = $this;
131 $this->init();
132 }
133
134 /**
135 * Constructor, initializing internal variables.
136 *
137 * @return void
138 */
139 public function init()
140 {
141 $lang = $this->getLanguageService();
142 $lang->includeLLFile('EXT:lang/locallang_misc.xlf');
143 $LOCAL_LANG_orig = $GLOBALS['LOCAL_LANG'];
144 $lang->includeLLFile('EXT:backend/Resources/Private/Language/locallang_db_new_content_el.xlf');
145 ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG_orig, $GLOBALS['LOCAL_LANG']);
146 $GLOBALS['LOCAL_LANG'] = $LOCAL_LANG_orig;
147
148 // Setting internal vars:
149 $this->id = (int)GeneralUtility::_GP('id');
150 $this->sys_language = (int)GeneralUtility::_GP('sys_language_uid');
151 $this->R_URI = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
152 $this->colPos = GeneralUtility::_GP('colPos') === null ? null : (int)GeneralUtility::_GP('colPos');
153 $this->uid_pid = (int)GeneralUtility::_GP('uid_pid');
154 $this->MCONF['name'] = 'xMOD_db_new_content_el';
155 $this->modTSconfig = BackendUtility::getModTSconfig($this->id, 'mod.wizards.newContentElement');
156 $config = BackendUtility::getPagesTSconfig($this->id);
157 $this->config = $config['mod.']['wizards.']['newContentElement.'];
158 // Starting the document template object:
159 // We keep this here in case somebody relies on it in a hook or alike
160 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
161 // Setting up the context sensitive menu:
162 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClickMenu');
163 // Getting the current page and receiving access information (used in main())
164 $perms_clause = $this->getBackendUser()->getPagePermsClause(1);
165 $this->pageInfo = BackendUtility::readPageAccess($this->id, $perms_clause);
166 $this->access = is_array($this->pageInfo) ? 1 : 0;
167 }
168
169 /**
170 * Injects the request object for the current request or subrequest
171 * As this controller goes only through the main() method, it is rather simple for now
172 *
173 * @param ServerRequestInterface $request the current request
174 * @param ResponseInterface $response
175 * @return ResponseInterface the response with the content
176 */
177 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
178 {
179 $this->main();
180 $this->moduleTemplate->setContent($this->content);
181 $response->getBody()->write($this->moduleTemplate->renderContent());
182 return $response;
183 }
184
185 /**
186 * Creating the module output.
187 *
188 * @throws \UnexpectedValueException
189 * @return void
190 */
191 public function main()
192 {
193 $lang = $this->getLanguageService();
194 $this->content .= '<form action="" name="editForm" id="NewContentElementController"><input type="hidden" name="defValues" value="" />';
195 if ($this->id && $this->access) {
196 // Init position map object:
197 $posMap = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\View\ContentCreationPagePositionMap::class);
198 $posMap->cur_sys_language = $this->sys_language;
199 // If a column is pre-set:
200 if (isset($this->colPos)) {
201 if ($this->uid_pid < 0) {
202 $row = array();
203 $row['uid'] = abs($this->uid_pid);
204 } else {
205 $row = '';
206 }
207 $this->onClickEvent = $posMap->onClickInsertRecord(
208 $row,
209 $this->colPos,
210 '',
211 $this->uid_pid,
212 $this->sys_language
213 );
214 } else {
215 $this->onClickEvent = '';
216 }
217 // ***************************
218 // Creating content
219 // ***************************
220 $this->content .= '<h1>' . $lang->getLL('newContentElement') . '</h1>';
221 // Wizard
222 $wizardItems = $this->wizardArray();
223 // Wrapper for wizards
224 $this->elementWrapper['section'] = array('', '');
225 // Hook for manipulating wizardItems, wrapper, onClickEvent etc.
226 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms']['db_new_content_el']['wizardItemsHook'])) {
227 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms']['db_new_content_el']['wizardItemsHook'] as $classData) {
228 $hookObject = GeneralUtility::getUserObj($classData);
229 if (!$hookObject instanceof NewContentElementWizardHookInterface) {
230 throw new \UnexpectedValueException(
231 '$hookObject must implement interface ' . NewContentElementWizardHookInterface::class,
232 1227834741
233 );
234 }
235 $hookObject->manipulateWizardItems($wizardItems, $this);
236 }
237 }
238 // Add document inline javascript
239 $this->moduleTemplate->addJavaScriptCode(
240 'NewContentElementWizardInlineJavascript',
241 '
242 function goToalt_doc() {
243 ' . $this->onClickEvent . '
244 }'
245 );
246
247 $iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
248
249 // Traverse items for the wizard.
250 // An item is either a header or an item rendered with a radio button and title/description and icon:
251 $cc = ($key = 0);
252 $menuItems = array();
253 foreach ($wizardItems as $k => $wInfo) {
254 if ($wInfo['header']) {
255 $menuItems[] = array(
256 'label' => htmlspecialchars($wInfo['header']),
257 'content' => $this->elementWrapper['section'][0]
258 );
259 $key = count($menuItems) - 1;
260 } else {
261 $content = '';
262
263 if (!$this->onClickEvent) {
264 // Radio button:
265 $oC = 'document.editForm.defValues.value=unescape(' . GeneralUtility::quoteJSvalue(rawurlencode($wInfo['params'])) . ');goToalt_doc();' . (!$this->onClickEvent ? 'window.location.hash=\'#sel2\';' : '');
266 $content .= '<div class="media-left"><input type="radio" name="tempB" value="' . htmlspecialchars($k) . '" onclick="' . htmlspecialchars($oC) . '" /></div>';
267 // Onclick action for icon/title:
268 $aOnClick = 'document.getElementsByName(\'tempB\')[' . $cc . '].checked=1;' . $oC . 'return false;';
269 } else {
270 $aOnClick = "document.editForm.defValues.value=unescape('" . rawurlencode($wInfo['params']) . "');goToalt_doc();" . (!$this->onClickEvent?"window.location.hash='#sel2';":'');
271 }
272
273 $icon = $this->moduleTemplate->getIconFactory()->getIcon($wInfo['iconIdentifier'])->render();
274 $menuItems[$key]['content'] .= '
275 <div class="media">
276 <a href="#" onclick="' . htmlspecialchars($aOnClick) . '">
277 ' . $content . '
278 <div class="media-left">
279 ' . $icon . '
280 </div>
281 <div class="media-body">
282 <strong>' . htmlspecialchars($wInfo['title']) . '</strong>' .
283 '<br />' .
284 nl2br(htmlspecialchars(trim($wInfo['description']))) .
285 '</div>
286 </a>
287 </div>';
288 $cc++;
289 }
290 }
291 // Add closing section-tag
292 foreach ($menuItems as $key => $val) {
293 $menuItems[$key]['content'] .= $this->elementWrapper['section'][1];
294 }
295 // Add the wizard table to the content, wrapped in tabs
296 $code = '<p>' . $lang->getLL('sel1', 1) . '</p>' . $this->moduleTemplate->getDynamicTabMenu(
297 $menuItems,
298 'new-content-element-wizard'
299 );
300
301 $this->content .= !$this->onClickEvent ? '<h2>' . $lang->getLL('1_selectType', true) . '</h2>' : '';
302 $this->content .= '<div>' . $code . '</div>';
303
304 // If the user must also select a column:
305 if (!$this->onClickEvent) {
306 // Add anchor "sel2"
307 $this->content .= '<div><a name="sel2"></a></div>';
308 // Select position
309 $code = '<p>' . $lang->getLL('sel2', 1) . '</p>';
310
311 // Load SHARED page-TSconfig settings and retrieve column list from there, if applicable:
312 $colPosArray = GeneralUtility::callUserFunction(
313 BackendLayoutView::class . '->getColPosListItemsParsed',
314 $this->id,
315 $this
316 );
317 $colPosIds = array_column($colPosArray, 1);
318 // Removing duplicates, if any
319 $colPosList = implode(',', array_unique(array_map('intval', $colPosIds)));
320 // Finally, add the content of the column selector to the content:
321 $code .= $posMap->printContentElementColumns($this->id, 0, $colPosList, 1, $this->R_URI);
322 $this->content .= '<h2>' . $lang->getLL('2_selectPosition', true) . '</h2><div>' . $code . '</div>';
323 }
324 } else {
325 // In case of no access:
326 $this->content = '';
327 $this->content .= '<h1>' . $lang->getLL('newContentElement') . '</h1>';
328 }
329 $this->content .= '</form>';
330 // Setting up the buttons and markers for docheader
331 $this->getButtons();
332 }
333
334 /**
335 * Create the panel of buttons for submitting the form or otherwise perform operations.
336 */
337 protected function getButtons()
338 {
339 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
340 if ($this->R_URI) {
341 $backButton = $buttonBar->makeLinkButton()
342 ->setHref($this->R_URI)
343 ->setTitle($this->getLanguageService()->getLL('goBack'))
344 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
345 'actions-view-go-back',
346 Icon::SIZE_SMALL
347 ));
348 $buttonBar->addButton($backButton);
349 }
350 $cshButton = $buttonBar->makeHelpButton()->setModuleName('xMOD_csh_corebe')->setFieldName('new_ce');
351 $buttonBar->addButton($cshButton);
352 }
353
354 /***************************
355 *
356 * OTHER FUNCTIONS:
357 *
358 ***************************/
359
360 /**
361 * Returns the array of elements in the wizard display.
362 * For the plugin section there is support for adding elements there from a global variable.
363 *
364 * @return array
365 */
366 public function wizardArray()
367 {
368 $wizardItems = array();
369 if (is_array($this->config)) {
370 $wizards = $this->config['wizardItems.'];
371 $appendWizards = $this->wizard_appendWizards($wizards['elements.']);
372 if (is_array($wizards)) {
373 foreach ($wizards as $groupKey => $wizardGroup) {
374 $groupKey = rtrim($groupKey, '.');
375 $showItems = GeneralUtility::trimExplode(',', $wizardGroup['show'], true);
376 $showAll = $wizardGroup['show'] === '*';
377 $groupItems = array();
378 if (is_array($appendWizards[$groupKey . '.']['elements.'])) {
379 $wizardElements = array_merge((array)$wizardGroup['elements.'], $appendWizards[$groupKey . '.']['elements.']);
380 } else {
381 $wizardElements = $wizardGroup['elements.'];
382 }
383 if (is_array($wizardElements)) {
384 foreach ($wizardElements as $itemKey => $itemConf) {
385 $itemKey = rtrim($itemKey, '.');
386 if ($showAll || in_array($itemKey, $showItems)) {
387 $tmpItem = $this->wizard_getItem($groupKey, $itemKey, $itemConf);
388 if ($tmpItem) {
389 $groupItems[$groupKey . '_' . $itemKey] = $tmpItem;
390 }
391 }
392 }
393 }
394 if (!empty($groupItems)) {
395 $wizardItems[$groupKey] = $this->wizard_getGroupHeader($groupKey, $wizardGroup);
396 $wizardItems = array_merge($wizardItems, $groupItems);
397 }
398 }
399 }
400 }
401 // Remove elements where preset values are not allowed:
402 $this->removeInvalidElements($wizardItems);
403 return $wizardItems;
404 }
405
406 /**
407 * @param mixed $wizardElements
408 * @return array
409 */
410 public function wizard_appendWizards($wizardElements)
411 {
412 if (!is_array($wizardElements)) {
413 $wizardElements = array();
414 }
415 if (is_array($GLOBALS['TBE_MODULES_EXT']['xMOD_db_new_content_el']['addElClasses'])) {
416 foreach ($GLOBALS['TBE_MODULES_EXT']['xMOD_db_new_content_el']['addElClasses'] as $class => $path) {
417 require_once $path;
418 $modObj = GeneralUtility::makeInstance($class);
419 $wizardElements = $modObj->proc($wizardElements);
420 }
421 }
422 $returnElements = array();
423 foreach ($wizardElements as $key => $wizardItem) {
424 preg_match('/^[a-zA-Z0-9]+_/', $key, $group);
425 $wizardGroup = $group[0] ? substr($group[0], 0, -1) . '.' : $key;
426 $returnElements[$wizardGroup]['elements.'][substr($key, strlen($wizardGroup)) . '.'] = $wizardItem;
427 }
428 return $returnElements;
429 }
430
431 /**
432 * @param string $groupKey Not used
433 * @param string $itemKey Not used
434 * @param array $itemConf
435 * @return array
436 */
437 public function wizard_getItem($groupKey, $itemKey, $itemConf)
438 {
439 $itemConf['title'] = $this->getLanguageService()->sL($itemConf['title']);
440 $itemConf['description'] = $this->getLanguageService()->sL($itemConf['description']);
441 $itemConf['tt_content_defValues'] = $itemConf['tt_content_defValues.'];
442 unset($itemConf['tt_content_defValues.']);
443 return $itemConf;
444 }
445
446 /**
447 * @param string $groupKey Not used
448 * @param array $wizardGroup
449 * @return array
450 */
451 public function wizard_getGroupHeader($groupKey, $wizardGroup)
452 {
453 return array(
454 'header' => $this->getLanguageService()->sL($wizardGroup['header'])
455 );
456 }
457
458 /**
459 * Checks the array for elements which might contain unallowed default values and will unset them!
460 * Looks for the "tt_content_defValues" key in each element and if found it will traverse that array as fieldname /
461 * value pairs and check.
462 * The values will be added to the "params" key of the array (which should probably be unset or empty by default).
463 *
464 * @param array $wizardItems Wizard items, passed by reference
465 * @return void
466 */
467 public function removeInvalidElements(&$wizardItems)
468 {
469 // Get TCEFORM from TSconfig of current page
470 $row = array('pid' => $this->id);
471 $TCEFORM_TSconfig = BackendUtility::getTCEFORM_TSconfig('tt_content', $row);
472 $headersUsed = array();
473 // Traverse wizard items:
474 foreach ($wizardItems as $key => $cfg) {
475 // Exploding parameter string, if any (old style)
476 if ($wizardItems[$key]['params']) {
477 // Explode GET vars recursively
478 $tempGetVars = GeneralUtility::explodeUrl2Array($wizardItems[$key]['params'], true);
479 // If tt_content values are set, merge them into the tt_content_defValues array,
480 // unset them from $tempGetVars and re-implode $tempGetVars into the param string
481 // (in case remaining parameters are around).
482 if (is_array($tempGetVars['defVals']['tt_content'])) {
483 $wizardItems[$key]['tt_content_defValues'] = array_merge(
484 is_array($wizardItems[$key]['tt_content_defValues']) ? $wizardItems[$key]['tt_content_defValues'] : array(),
485 $tempGetVars['defVals']['tt_content']
486 );
487 unset($tempGetVars['defVals']['tt_content']);
488 $wizardItems[$key]['params'] = GeneralUtility::implodeArrayForUrl('', $tempGetVars);
489 }
490 }
491 // If tt_content_defValues are defined...:
492 if (is_array($wizardItems[$key]['tt_content_defValues'])) {
493 $backendUser = $this->getBackendUser();
494 // Traverse field values:
495 foreach ($wizardItems[$key]['tt_content_defValues'] as $fN => $fV) {
496 if (is_array($GLOBALS['TCA']['tt_content']['columns'][$fN])) {
497 // Get information about if the field value is OK:
498 $config = &$GLOBALS['TCA']['tt_content']['columns'][$fN]['config'];
499 $authModeDeny = $config['type'] == 'select' && $config['authMode']
500 && !$backendUser->checkAuthMode('tt_content', $fN, $fV, $config['authMode']);
501 // explode TSconfig keys only as needed
502 if (!isset($removeItems[$fN])) {
503 $removeItems[$fN] = GeneralUtility::trimExplode(
504 ',',
505 $TCEFORM_TSconfig[$fN]['removeItems'],
506 true
507 );
508 }
509 if (!isset($keepItems[$fN])) {
510 $keepItems[$fN] = GeneralUtility::trimExplode(
511 ',',
512 $TCEFORM_TSconfig[$fN]['keepItems'],
513 true
514 );
515 }
516 $isNotInKeepItems = !empty($keepItems[$fN]) && !in_array($fV, $keepItems[$fN]);
517 if ($authModeDeny || $fN === 'CType' && in_array($fV, $removeItems[$fN]) || $isNotInKeepItems) {
518 // Remove element all together:
519 unset($wizardItems[$key]);
520 break;
521 } else {
522 // Add the parameter:
523 $wizardItems[$key]['params'] .= '&defVals[tt_content][' . $fN . ']=' . rawurlencode($fV);
524 $tmp = explode('_', $key);
525 $headersUsed[$tmp[0]] = $tmp[0];
526 }
527 }
528 }
529 }
530 }
531 // remove headers without elements
532 foreach ($wizardItems as $key => $cfg) {
533 $tmp = explode('_', $key);
534 if ($tmp[0] && !$tmp[1] && !in_array($tmp[0], $headersUsed)) {
535 unset($wizardItems[$key]);
536 }
537 }
538 }
539
540 /**
541 * Returns LanguageService
542 *
543 * @return \TYPO3\CMS\Lang\LanguageService
544 */
545 protected function getLanguageService()
546 {
547 return $GLOBALS['LANG'];
548 }
549
550 /**
551 * Returns the current BE user.
552 *
553 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
554 */
555 protected function getBackendUser()
556 {
557 return $GLOBALS['BE_USER'];
558 }
559 }