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