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