NewContentElementController.php 18.3 KB
Newer Older
1
2
3
<?php
namespace TYPO3\CMS\Backend\Controller\ContentElement;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

Nicole Cordes's avatar
Nicole Cordes committed
17
18
19
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\Utility\IconUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Core\Utility\PathUtility;
Nicole Cordes's avatar
Nicole Cordes committed
21

22
23
24
25
26
27
/**
 * Script Class for the New Content element wizard
 */
class NewContentElementController {

	/**
28
29
	 * Page id
	 *
30
	 * @var int
31
32
33
34
	 */
	public $id;

	/**
35
36
	 * Sys language
	 *
37
	 * @var int
38
39
40
41
	 */
	public $sys_language = 0;

	/**
42
43
	 * Return URL.
	 *
44
	 * @var string
45
46
47
48
	 */
	public $R_URI = '';

	/**
49
50
	 * If set, the content is destined for a specific column.
	 *
51
	 * @var int|null
52
53
54
55
	 */
	public $colPos;

	/**
56
	 * @var int
57
58
59
60
	 */
	public $uid_pid;

	/**
61
62
	 * Module TSconfig.
	 *
63
	 * @var array
64
65
66
67
68
69
	 */
	public $modTSconfig = array();

	/**
	 * Internal backend template object
	 *
70
	 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
71
72
73
74
	 */
	public $doc;

	/**
75
76
	 * Used to accumulate the content of the module.
	 *
77
	 * @var string
78
79
80
81
	 */
	public $content;

	/**
82
83
	 * Access boolean.
	 *
84
	 * @var bool
85
86
87
88
	 */
	public $access;

	/**
89
90
	 * config of the wizard
	 *
91
	 * @var array
92
93
94
	 */
	public $config;

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
	/**
	 * @var array
	 */
	protected $pageInfo;

	/**
	 * @var array
	 */
	protected $elementWrapper;

	/**
	 * @var array
	 */
	protected $elementWrapperForTabs;

	/**
	 * @var string
	 */
	protected $onClickEvent;

	/**
	 * @var array
	 */
	protected $MCONF;

120
121
122
123
124
125
	/**
	 * Constructor, initializing internal variables.
	 *
	 * @return void
	 */
	public function init() {
126
127
		$lang = $this->getLanguageService();
		$lang->includeLLFile('EXT:lang/locallang_misc.xlf');
128
		$LOCAL_LANG_orig = $GLOBALS['LOCAL_LANG'];
Wouter Wolters's avatar
Wouter Wolters committed
129
		$lang->includeLLFile('EXT:backend/Resources/Private/Language/locallang_db_new_content_el.xlf');
130
131
132
		\TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG_orig, $GLOBALS['LOCAL_LANG']);
		$GLOBALS['LOCAL_LANG'] = $LOCAL_LANG_orig;

133
		// Setting internal vars:
134
135
		$this->id = (int)GeneralUtility::_GP('id');
		$this->sys_language = (int)GeneralUtility::_GP('sys_language_uid');
Nicole Cordes's avatar
Nicole Cordes committed
136
		$this->R_URI = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
137
		$this->colPos = GeneralUtility::_GP('colPos') === NULL ? NULL : (int)GeneralUtility::_GP('colPos');
138
		$this->uid_pid = (int)GeneralUtility::_GP('uid_pid');
139
		$this->MCONF['name'] = 'xMOD_db_new_content_el';
Nicole Cordes's avatar
Nicole Cordes committed
140
141
		$this->modTSconfig = BackendUtility::getModTSconfig($this->id, 'mod.wizards.newContentElement');
		$config = BackendUtility::getPagesTSconfig($this->id);
142
143
		$this->config = $config['mod.']['wizards.']['newContentElement.'];
		// Starting the document template object:
144
		$this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
145
		$this->doc->backPath = $GLOBALS['BACK_PATH'];
146
		$this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/db_new_content_el.html');
147
148
149
150
151
		$this->doc->JScode = '';
		$this->doc->form = '<form action="" name="editForm"><input type="hidden" name="defValues" value="" />';
		// Setting up the context sensitive menu:
		$this->doc->getContextMenuCode();
		// Getting the current page and receiving access information (used in main())
152
153
154
		$perms_clause = $this->getBackendUser()->getPagePermsClause(1);
		$this->pageInfo = BackendUtility::readPageAccess($this->id, $perms_clause);
		$this->access = is_array($this->pageInfo) ? 1 : 0;
155
156
157
158
159
160
161
162
	}

	/**
	 * Creating the module output.
	 *
	 * @return void
	 */
	public function main() {
163
		$lang = $this->getLanguageService();
164
165
		if ($this->id && $this->access) {
			// Init position map object:
166
			$posMap = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\View\ContentCreationPagePositionMap::class);
167
168
169
			$posMap->cur_sys_language = $this->sys_language;
			$posMap->backPath = $GLOBALS['BACK_PATH'];
			// If a column is pre-set:
170
			if (isset($this->colPos)) {
171
172
173
174
175
176
177
178
179
180
181
182
183
				if ($this->uid_pid < 0) {
					$row = array();
					$row['uid'] = abs($this->uid_pid);
				} else {
					$row = '';
				}
				$this->onClickEvent = $posMap->onClickInsertRecord($row, $this->colPos, '', $this->uid_pid, $this->sys_language);
			} else {
				$this->onClickEvent = '';
			}
			// ***************************
			// Creating content
			// ***************************
184
			$this->content .= $this->doc->header($lang->getLL('newContentElement'));
185
186
187
			// Wizard
			$wizardItems = $this->getWizardItems();
			// Wrapper for wizards
188
			$this->elementWrapper['section'] = array('', '');
189
190
191
192
193
			// Copy wrapper for tabs
			$this->elementWrapperForTabs = $this->elementWrapper;
			// Hook for manipulating wizardItems, wrapper, onClickEvent etc.
			if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms']['db_new_content_el']['wizardItemsHook'])) {
				foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms']['db_new_content_el']['wizardItemsHook'] as $classData) {
Nicole Cordes's avatar
Nicole Cordes committed
194
					$hookObject = GeneralUtility::getUserObj($classData);
195
					if (!$hookObject instanceof \TYPO3\CMS\Backend\Wizard\NewContentElementWizardHookInterface) {
196
						throw new \UnexpectedValueException('$hookObject must implement interface ' . \TYPO3\CMS\Backend\Wizard\NewContentElementWizardHookInterface::class, 1227834741);
197
198
199
200
201
202
203
204
205
					}
					$hookObject->manipulateWizardItems($wizardItems, $this);
				}
			}
			if ($this->config['renderMode'] == 'tabs' && $this->elementWrapperForTabs != $this->elementWrapper) {
				// Restore wrapper for tabs if they are overwritten in hook
				$this->elementWrapper = $this->elementWrapperForTabs;
			}
			// Add document inline javascript
206
			$this->doc->JScode = $this->doc->wrapScriptTags('
207
				function goToalt_doc() {	//
208
					' . $this->onClickEvent . '
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
				}

				if(top.refreshMenu) {
					top.refreshMenu();
				} else {
					top.TYPO3ModuleMenu.refreshMenu();
				}
			');
			// Traverse items for the wizard.
			// An item is either a header or an item rendered with a radio button and title/description and icon:
			$cc = ($key = 0);
			$menuItems = array();
			foreach ($wizardItems as $k => $wInfo) {
				if ($wInfo['header']) {
					$menuItems[] = array(
						'label' => htmlspecialchars($wInfo['header']),
						'content' => $this->elementWrapper['section'][0]
					);
					$key = count($menuItems) - 1;
				} else {
					$content = '';
230
231
232

					if (!$this->onClickEvent) {
						// Radio button:
233
						$oC = 'document.editForm.defValues.value=unescape(' . GeneralUtility::quoteJSvalue(rawurlencode($wInfo['params'])) . ');goToalt_doc();' . (!$this->onClickEvent ? 'window.location.hash=\'#sel2\';' : '');
234
						$content .= '<div class="media-left"><input type="radio" name="tempB" value="' . htmlspecialchars($k) . '" onclick="' . htmlspecialchars($oC) . '" /></div>';
235
236
237
						// Onclick action for icon/title:
						$aOnClick = 'document.getElementsByName(\'tempB\')[' . $cc . '].checked=1;' . $oC . 'return false;';
					} else {
238
						$aOnClick = "document.editForm.defValues.value=unescape('" . rawurlencode($wInfo['params']) . "');goToalt_doc();" . (!$this->onClickEvent?"window.location.hash='#sel2';":'');
239
240
					}

241
					$icon = $wInfo['icon'];
242
					if (strpos($wInfo['icon'], '..') === FALSE && !GeneralUtility::isAbsPath($icon)) {
243
						$icon = GeneralUtility::getFileAbsFileName($icon, TRUE, TRUE);
244
245
246
						$pathInfo = PathUtility::pathinfo($icon);
						$path = PathUtility::getRelativePathTo($pathInfo['dirname']);
						$icon = $path . $pathInfo['basename'];
247
					}
248
249
250
251
252
					$menuItems[$key]['content'] .= '
						<div class="media">
							' . $content . '
							<div class="media-left">
								<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">
253
									<img' . IconUtility::skinImg($this->doc->backPath, $icon) . ' alt="" />
254
								</a>
255
							</div>
256
257
258
259
260
261
262
							<div class="media-body">
								<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">
									<strong>' . htmlspecialchars($wInfo['title']) . '</strong>
									<br />' . nl2br(htmlspecialchars(trim($wInfo['description']))) .
								'</a>
							</div>
						</div>';
263
264
265
266
267
268
269
270
271
					$cc++;
				}
			}
			// Add closing section-tag
			foreach ($menuItems as $key => $val) {
				$menuItems[$key]['content'] .= $this->elementWrapper['section'][1];
			}
			// Add the wizard table to the content, wrapped in tabs:
			if ($this->config['renderMode'] == 'tabs') {
272
				$code = '<p>' . $lang->getLL('sel1', 1) . '</p>' . $this->doc->getDynamicTabMenu($menuItems, 'new-content-element-wizard');
273
			} else {
274
				$code = '<p>' . $lang->getLL('sel1', 1) . '</p>';
275
				foreach ($menuItems as $section) {
276
					$code .= '<h3 class="divider">' . $section['label'] . '</h3>' . $section['content'];
277
278
				}
			}
279
			$this->content .= $this->doc->section(!$this->onClickEvent ? $lang->getLL('1_selectType') : '', $code, 0, 1);
280
281
282
283
284
			// If the user must also select a column:
			if (!$this->onClickEvent) {
				// Add anchor "sel2"
				$this->content .= $this->doc->section('', '<a name="sel2"></a>');
				// Select position
285
				$code = '<p>' . $lang->getLL('sel2', 1) . '</p>';
286

287
				// Load SHARED page-TSconfig settings and retrieve column list from there, if applicable:
288
				$colPosArray = GeneralUtility::callUserFunction(\TYPO3\CMS\Backend\View\BackendLayoutView::class . '->getColPosListItemsParsed', $this->id, $this);
289
				$colPosIds = array_column($colPosArray, 1);
290
				// Removing duplicates, if any
291
				$colPosList = implode(',', array_unique(array_map('intval', $colPosIds)));
292
293
				// Finally, add the content of the column selector to the content:
				$code .= $posMap->printContentElementColumns($this->id, 0, $colPosList, 1, $this->R_URI);
294
				$this->content .= $this->doc->section($lang->getLL('2_selectPosition'), $code, 0, 1);
295
296
297
298
			}
		} else {
			// In case of no access:
			$this->content = '';
299
			$this->content .= $this->doc->header($lang->getLL('newContentElement'));
300
301
302
303
304
305
306
			$this->content .= $this->doc->spacer(5);
		}
		// Setting up the buttons and markers for docheader
		$docHeaderButtons = $this->getButtons();
		$markers['CSH'] = $docHeaderButtons['csh'];
		$markers['CONTENT'] = $this->content;
		// Build the <body> for the module
307
308
		$this->content = $this->doc->startPage($lang->getLL('newContentElement'));
		$this->content .= $this->doc->moduleBody($this->pageInfo, $docHeaderButtons, $markers);
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
		$this->content .= $this->doc->sectionEnd();
		$this->content .= $this->doc->endPage();
		$this->content = $this->doc->insertStylesAndJS($this->content);
	}

	/**
	 * Print out the accumulated content:
	 *
	 * @return void
	 */
	public function printContent() {
		echo $this->content;
	}

	/**
	 * Create the panel of buttons for submitting the form or otherwise perform operations.
	 *
	 * @return array All available buttons as an assoc. array
	 */
	protected function getButtons() {
		$buttons = array(
			'csh' => '',
			'back' => ''
		);
		if ($this->id && $this->access) {
Benni Mack's avatar
Benni Mack committed
334
			$buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'new_ce');
335
			if ($this->R_URI) {
336
				$buttons['back'] = '<a href="' . htmlspecialchars($this->R_URI) . '" class="typo3-goBack" title="' . $this->getLanguageService()->getLL('goBack', TRUE) . '">' . IconUtility::getSpriteIcon('actions-view-go-back') . '</a>';
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
			}
		}
		return $buttons;
	}

	/***************************
	 *
	 * OTHER FUNCTIONS:
	 *
	 ***************************/
	/**
	 * Returns the content of wizardArray() function...
	 *
	 * @return array Returns the content of wizardArray() function...
	 */
	public function getWizardItems() {
		return $this->wizardArray();
	}

	/**
	 * Returns the array of elements in the wizard display.
	 * For the plugin section there is support for adding elements there from a global variable.
	 *
	 * @return array
	 */
	public function wizardArray() {
363
		$wizardItems = array();
364
365
		if (is_array($this->config)) {
			$wizards = $this->config['wizardItems.'];
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
			$appendWizards = $this->wizard_appendWizards($wizards['elements.']);
			if (is_array($wizards)) {
				foreach ($wizards as $groupKey => $wizardGroup) {
					$groupKey = rtrim($groupKey, '.');
					$showItems = GeneralUtility::trimExplode(',', $wizardGroup['show'], TRUE);
					$showAll = $wizardGroup['show'] === '*';
					$groupItems = array();
					if (is_array($appendWizards[$groupKey . '.']['elements.'])) {
						$wizardElements = array_merge((array)$wizardGroup['elements.'], $appendWizards[$groupKey . '.']['elements.']);
					} else {
						$wizardElements = $wizardGroup['elements.'];
					}
					if (is_array($wizardElements)) {
						foreach ($wizardElements as $itemKey => $itemConf) {
							$itemKey = rtrim($itemKey, '.');
							if ($showAll || in_array($itemKey, $showItems)) {
								$tmpItem = $this->wizard_getItem($groupKey, $itemKey, $itemConf);
								if ($tmpItem) {
									$groupItems[$groupKey . '_' . $itemKey] = $tmpItem;
								}
386
387
388
							}
						}
					}
389
					if (!empty($groupItems)) {
390
391
392
						$wizardItems[$groupKey] = $this->wizard_getGroupHeader($groupKey, $wizardGroup);
						$wizardItems = array_merge($wizardItems, $groupItems);
					}
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
				}
			}
		}
		// Remove elements where preset values are not allowed:
		$this->removeInvalidElements($wizardItems);
		return $wizardItems;
	}

	/**
	 * @param mixed $wizardElements
	 * @return array
	 */
	public function wizard_appendWizards($wizardElements) {
		if (!is_array($wizardElements)) {
			$wizardElements = array();
		}
		if (is_array($GLOBALS['TBE_MODULES_EXT']['xMOD_db_new_content_el']['addElClasses'])) {
			foreach ($GLOBALS['TBE_MODULES_EXT']['xMOD_db_new_content_el']['addElClasses'] as $class => $path) {
				require_once $path;
Nicole Cordes's avatar
Nicole Cordes committed
412
				$modObj = GeneralUtility::makeInstance($class);
413
414
415
416
417
418
419
420
421
422
423
424
425
				$wizardElements = $modObj->proc($wizardElements);
			}
		}
		$returnElements = array();
		foreach ($wizardElements as $key => $wizardItem) {
			preg_match('/^[a-zA-Z0-9]+_/', $key, $group);
			$wizardGroup = $group[0] ? substr($group[0], 0, -1) . '.' : $key;
			$returnElements[$wizardGroup]['elements.'][substr($key, strlen($wizardGroup)) . '.'] = $wizardItem;
		}
		return $returnElements;
	}

	/**
426
427
	 * @param string $groupKey Not used
	 * @param string $itemKey Not used
428
429
430
431
	 * @param array $itemConf
	 * @return array
	 */
	public function wizard_getItem($groupKey, $itemKey, $itemConf) {
432
433
		$itemConf['title'] = $this->getLanguageService()->sL($itemConf['title']);
		$itemConf['description'] = $this->getLanguageService()->sL($itemConf['description']);
434
435
436
437
438
439
		$itemConf['tt_content_defValues'] = $itemConf['tt_content_defValues.'];
		unset($itemConf['tt_content_defValues.']);
		return $itemConf;
	}

	/**
440
	 * @param string $groupKey Not used
441
442
443
444
445
	 * @param array $wizardGroup
	 * @return array
	 */
	public function wizard_getGroupHeader($groupKey, $wizardGroup) {
		return array(
446
			'header' => $this->getLanguageService()->sL($wizardGroup['header'])
447
448
449
450
451
		);
	}

	/**
	 * Checks the array for elements which might contain unallowed default values and will unset them!
452
453
	 * 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).
454
455
456
457
458
459
460
	 *
	 * @param array $wizardItems Wizard items, passed by reference
	 * @return void
	 */
	public function removeInvalidElements(&$wizardItems) {
		// Get TCEFORM from TSconfig of current page
		$row = array('pid' => $this->id);
Nicole Cordes's avatar
Nicole Cordes committed
461
		$TCEFORM_TSconfig = BackendUtility::getTCEFORM_TSconfig('tt_content', $row);
462
463
464
465
466
467
		$headersUsed = array();
		// Traverse wizard items:
		foreach ($wizardItems as $key => $cfg) {
			// Exploding parameter string, if any (old style)
			if ($wizardItems[$key]['params']) {
				// Explode GET vars recursively
Nicole Cordes's avatar
Nicole Cordes committed
468
				$tempGetVars = GeneralUtility::explodeUrl2Array($wizardItems[$key]['params'], TRUE);
469
470
471
				// If tt_content values are set, merge them into the tt_content_defValues array,
				// unset them from $tempGetVars and re-implode $tempGetVars into the param string
				// (in case remaining parameters are around).
472
				if (is_array($tempGetVars['defVals']['tt_content'])) {
473
474
475
476
477
478
					$wizardItems[$key]['tt_content_defValues'] = array_merge(
						is_array($wizardItems[$key]['tt_content_defValues'])
							? $wizardItems[$key]['tt_content_defValues']
							: array(),
						$tempGetVars['defVals']['tt_content']
					);
479
					unset($tempGetVars['defVals']['tt_content']);
Nicole Cordes's avatar
Nicole Cordes committed
480
					$wizardItems[$key]['params'] = GeneralUtility::implodeArrayForUrl('', $tempGetVars);
481
482
483
484
				}
			}
			// If tt_content_defValues are defined...:
			if (is_array($wizardItems[$key]['tt_content_defValues'])) {
485
				$backendUser = $this->getBackendUser();
486
487
488
489
				// Traverse field values:
				foreach ($wizardItems[$key]['tt_content_defValues'] as $fN => $fV) {
					if (is_array($GLOBALS['TCA']['tt_content']['columns'][$fN])) {
						// Get information about if the field value is OK:
490
						$config = &$GLOBALS['TCA']['tt_content']['columns'][$fN]['config'];
491
						$authModeDeny = $config['type'] == 'select' && $config['authMode']
492
							&& !$backendUser->checkAuthMode('tt_content', $fN, $fV, $config['authMode']);
493
494
495
496
497
498
499
						// explode TSconfig keys only as needed
						if (!isset($removeItems[$fN])) {
							$removeItems[$fN] = GeneralUtility::trimExplode(',', $TCEFORM_TSconfig[$fN]['removeItems'], TRUE);
						}
						if (!isset($keepItems[$fN])) {
							$keepItems[$fN] = GeneralUtility::trimExplode(',', $TCEFORM_TSconfig[$fN]['keepItems'], TRUE);
						}
500
						$isNotInKeepItems = !empty($keepItems[$fN]) && !in_array($fV, $keepItems[$fN]);
501
						if ($authModeDeny || $fN === 'CType' && in_array($fV, $removeItems[$fN]) || $isNotInKeepItems) {
502
503
504
505
506
							// Remove element all together:
							unset($wizardItems[$key]);
							break;
						} else {
							// Add the parameter:
507
							$wizardItems[$key]['params'] .= '&defVals[tt_content][' . $fN . ']=' . rawurlencode($fV);
508
509
510
511
512
513
514
515
516
517
							$tmp = explode('_', $key);
							$headersUsed[$tmp[0]] = $tmp[0];
						}
					}
				}
			}
		}
		// remove headers without elements
		foreach ($wizardItems as $key => $cfg) {
			$tmp = explode('_', $key);
518
			if ($tmp[0] && !$tmp[1] && !in_array($tmp[0], $headersUsed)) {
519
520
521
522
523
				unset($wizardItems[$key]);
			}
		}
	}

524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
	/**
	 * Returns LanguageService
	 *
	 * @return \TYPO3\CMS\Lang\LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

	/**
	 * Returns the current BE user.
	 *
	 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
	 */
	protected function getBackendUser() {
		return $GLOBALS['BE_USER'];
	}

542
}