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