NewContentElementController.php 17.6 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
20
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\Utility\IconUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

21
22
23
24
25
26
27
28
/**
 * Script Class for the New Content element wizard
 *
 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
 */
class NewContentElementController {

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

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

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

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

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

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

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

	/**
76
77
	 * Includes a list of files to include between init() and main() - see init()
	 *
78
	 * @var array
79
80
81
82
	 */
	public $include_once = array();

	/**
83
84
	 * Used to accumulate the content of the module.
	 *
85
	 * @var string
86
87
88
89
	 */
	public $content;

	/**
90
91
	 * Access boolean.
	 *
92
	 * @var bool
93
94
95
96
	 */
	public $access;

	/**
97
98
	 * config of the wizard
	 *
99
	 * @var array
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	 */
	public $config;

	/**
	 * Constructor, initializing internal variables.
	 *
	 * @return void
	 */
	public function init() {
		// Setting class files to include:
		if (is_array($GLOBALS['TBE_MODULES_EXT']['xMOD_db_new_content_el']['addElClasses'])) {
			$this->include_once = array_merge($this->include_once, $GLOBALS['TBE_MODULES_EXT']['xMOD_db_new_content_el']['addElClasses']);
		}
		// Setting internal vars:
114
115
		$this->id = (int)GeneralUtility::_GP('id');
		$this->sys_language = (int)GeneralUtility::_GP('sys_language_uid');
Nicole Cordes's avatar
Nicole Cordes committed
116
		$this->R_URI = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
117
		$this->colPos = GeneralUtility::_GP('colPos') === NULL ? NULL : (int)GeneralUtility::_GP('colPos');
118
		$this->uid_pid = (int)GeneralUtility::_GP('uid_pid');
119
		$this->MCONF['name'] = 'xMOD_db_new_content_el';
Nicole Cordes's avatar
Nicole Cordes committed
120
121
		$this->modTSconfig = BackendUtility::getModTSconfig($this->id, 'mod.wizards.newContentElement');
		$config = BackendUtility::getPagesTSconfig($this->id);
122
123
		$this->config = $config['mod.']['wizards.']['newContentElement.'];
		// Starting the document template object:
124
		$this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
125
		$this->doc->backPath = $GLOBALS['BACK_PATH'];
126
		$this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/db_new_content_el.html');
127
128
129
130
131
132
		$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())
		$perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
Nicole Cordes's avatar
Nicole Cordes committed
133
		$this->pageinfo = BackendUtility::readPageAccess($this->id, $perms_clause);
134
135
136
137
138
139
140
141
142
143
144
		$this->access = is_array($this->pageinfo) ? 1 : 0;
	}

	/**
	 * Creating the module output.
	 *
	 * @return void
	 */
	public function main() {
		if ($this->id && $this->access) {
			// Init position map object:
145
			$posMap = GeneralUtility::makeInstance(\ext_posMap::class);
146
147
148
			$posMap->cur_sys_language = $this->sys_language;
			$posMap->backPath = $GLOBALS['BACK_PATH'];
			// If a column is pre-set:
149
			if (isset($this->colPos)) {
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
				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
			// ***************************
			// use a wrapper div
			$this->content .= '<div id="user-setup-wrapper">';
			$this->content .= $this->doc->header($GLOBALS['LANG']->getLL('newContentElement'));
			$this->content .= $this->doc->spacer(5);
			// Wizard
			$code = '';
			$wizardItems = $this->getWizardItems();
			// Wrapper for wizards
171
			$this->elementWrapper['section'] = array('<ul class="contentelement-wizard list-unstyled">', '</ul>');
172
173
174
175
176
			// 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
177
					$hookObject = GeneralUtility::getUserObj($classData);
178
					if (!$hookObject instanceof \TYPO3\CMS\Backend\Wizard\NewContentElementWizardHookInterface) {
179
						throw new \UnexpectedValueException('$hookObject must implement interface TYPO3\\CMS\\Backend\\Wizard\\NewContentElementWizardHookInterface', 1227834741);
180
181
182
183
184
185
186
187
188
					}
					$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
189
			$this->doc->JScode = $this->doc->wrapScriptTags('
190
				function goToalt_doc() {	//
191
					' . $this->onClickEvent . '
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
				}

				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 = '';
213
214
215
216

					if (!$this->onClickEvent) {
						// Radio button:
						$oC = 'document.editForm.defValues.value=unescape(\'' . rawurlencode($wInfo['params']) . '\');goToalt_doc();' . (!$this->onClickEvent ? 'window.location.hash=\'#sel2\';' : '');
217
						$content .= '<div class="contentelement-wizard-item-input"><input type="radio" name="tempB" value="' . htmlspecialchars($k) . '" onclick="' . htmlspecialchars($oC) . '" /></div>';
218
219
220
						// Onclick action for icon/title:
						$aOnClick = 'document.getElementsByName(\'tempB\')[' . $cc . '].checked=1;' . $oC . 'return false;';
					} else {
221
						$aOnClick = "document.editForm.defValues.value=unescape('" . rawurlencode($wInfo['params']) . "');goToalt_doc();" . (!$this->onClickEvent?"window.location.hash='#sel2';":'');
222
223
					}

224
225
					$menuItems[$key]['content'] .=
						'<li>
226
227
228
229
230
231
232
233
234
235
236
237
238
							<div class="contentelement-wizard-item">
								' . $content . '
								<div class="contentelement-wizard-item-icon">
									<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">
										<img' . IconUtility::skinImg($this->doc->backPath, $wInfo['icon'], '') . ' alt="" />
									</a>
								</div>
								<div class="contentelement-wizard-item-text">
									<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">
										<strong>' . htmlspecialchars($wInfo['title']) . '</strong>
										<br />' . nl2br(htmlspecialchars(trim($wInfo['description']))) .
									'</a>
								</div>
239
240
							</div>
						</li>';
241
242
243
244
245
246
247
248
249
					$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') {
250
				$code = $GLOBALS['LANG']->getLL('sel1', 1) . '<br /><br />' . $this->doc->getDynTabMenu($menuItems, 'new-content-element-wizard', FALSE, FALSE);
251
252
253
			} else {
				$code = $GLOBALS['LANG']->getLL('sel1', 1) . '<br /><br />';
				foreach ($menuItems as $section) {
254
					$code .= '<h3 class="divider">' . $section['label'] . '</h3>' . $section['content'];
255
256
257
258
259
260
261
262
263
264
				}
			}
			$this->content .= $this->doc->section(!$this->onClickEvent ? $GLOBALS['LANG']->getLL('1_selectType') : '', $code, 0, 1);
			// If the user must also select a column:
			if (!$this->onClickEvent) {
				// Add anchor "sel2"
				$this->content .= $this->doc->section('', '<a name="sel2"></a>');
				$this->content .= $this->doc->spacer(20);
				// Select position
				$code = $GLOBALS['LANG']->getLL('sel2', 1) . '<br /><br />';
265

266
				// Load SHARED page-TSconfig settings and retrieve column list from there, if applicable:
267
268
				$colPosArray = GeneralUtility::callUserFunction('TYPO3\\CMS\\Backend\\View\\BackendLayoutView->getColPosListItemsParsed', $this->id, $this);
				$colPosIds = array_column($colPosArray, 1);
269
				// Removing duplicates, if any
270
				$colPosList = implode(',', array_unique(array_map('intval', $colPosIds)));
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
				// Finally, add the content of the column selector to the content:
				$code .= $posMap->printContentElementColumns($this->id, 0, $colPosList, 1, $this->R_URI);
				$this->content .= $this->doc->section($GLOBALS['LANG']->getLL('2_selectPosition'), $code, 0, 1);
			}
			// Close wrapper div
			$this->content .= '</div>';
		} else {
			// In case of no access:
			$this->content = '';
			$this->content .= $this->doc->header($GLOBALS['LANG']->getLL('newContentElement'));
			$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
		$this->content = $this->doc->startPage($GLOBALS['LANG']->getLL('newContentElement'));
		$this->content .= $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
		$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
315
			$buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'new_ce');
316
			if ($this->R_URI) {
Nicole Cordes's avatar
Nicole Cordes committed
317
				$buttons['back'] = '<a href="' . htmlspecialchars($this->R_URI) . '" class="typo3-goBack" title="' . $GLOBALS['LANG']->getLL('goBack', TRUE) . '">' . IconUtility::getSpriteIcon('actions-view-go-back') . '</a>';
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
			}
		}
		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() {
		if (is_array($this->config)) {
			$wizards = $this->config['wizardItems.'];
		}
		$appendWizards = $this->wizard_appendWizards($wizards['elements.']);
		$wizardItems = array();
		if (is_array($wizards)) {
			foreach ($wizards as $groupKey => $wizardGroup) {
				$groupKey = preg_replace('/\\.$/', '', $groupKey);
Nicole Cordes's avatar
Nicole Cordes committed
352
				$showItems = GeneralUtility::trimExplode(',', $wizardGroup['show'], TRUE);
353
				$showAll = $wizardGroup['show'] === '*';
354
355
				$groupItems = array();
				if (is_array($appendWizards[$groupKey . '.']['elements.'])) {
356
					$wizardElements = array_merge((array)$wizardGroup['elements.'], $appendWizards[$groupKey . '.']['elements.']);
357
358
359
360
361
362
363
364
365
				} else {
					$wizardElements = $wizardGroup['elements.'];
				}
				if (is_array($wizardElements)) {
					foreach ($wizardElements as $itemKey => $itemConf) {
						$itemKey = preg_replace('/\\.$/', '', $itemKey);
						if ($showAll || in_array($itemKey, $showItems)) {
							$tmpItem = $this->wizard_getItem($groupKey, $itemKey, $itemConf);
							if ($tmpItem) {
366
								$groupItems[$groupKey . '_' . $itemKey] = $tmpItem;
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
							}
						}
					}
				}
				if (count($groupItems)) {
					$wizardItems[$groupKey] = $this->wizard_getGroupHeader($groupKey, $wizardGroup);
					$wizardItems = array_merge($wizardItems, $groupItems);
				}
			}
		}
		// 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
393
				$modObj = GeneralUtility::makeInstance($class);
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
				$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;
	}

	/**
	 * @param string Not used
	 * @param string Not used
	 * @param array $itemConf
	 * @return array
	 */
	public function wizard_getItem($groupKey, $itemKey, $itemConf) {
		$itemConf['title'] = $GLOBALS['LANG']->sL($itemConf['title']);
		$itemConf['description'] = $GLOBALS['LANG']->sL($itemConf['description']);
		$itemConf['tt_content_defValues'] = $itemConf['tt_content_defValues.'];
		unset($itemConf['tt_content_defValues.']);
		return $itemConf;
	}

	/**
	 * @param string Not used
	 * @param array $wizardGroup
	 * @return array
	 */
	public function wizard_getGroupHeader($groupKey, $wizardGroup) {
		return array(
			'header' => $GLOBALS['LANG']->sL($wizardGroup['header'])
		);
	}

	/**
	 * Checks the array for elements which might contain unallowed default values and will unset them!
	 * 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).
	 *
	 * @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
441
		$TCEFORM_TSconfig = BackendUtility::getTCEFORM_TSconfig('tt_content', $row);
442
443
444
445
446
447
		$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
448
				$tempGetVars = GeneralUtility::explodeUrl2Array($wizardItems[$key]['params'], TRUE);
449
450
451
				// 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).
452
				if (is_array($tempGetVars['defVals']['tt_content'])) {
453
454
455
456
457
458
					$wizardItems[$key]['tt_content_defValues'] = array_merge(
						is_array($wizardItems[$key]['tt_content_defValues'])
							? $wizardItems[$key]['tt_content_defValues']
							: array(),
						$tempGetVars['defVals']['tt_content']
					);
459
					unset($tempGetVars['defVals']['tt_content']);
Nicole Cordes's avatar
Nicole Cordes committed
460
					$wizardItems[$key]['params'] = GeneralUtility::implodeArrayForUrl('', $tempGetVars);
461
462
463
464
465
466
467
468
				}
			}
			// If tt_content_defValues are defined...:
			if (is_array($wizardItems[$key]['tt_content_defValues'])) {
				// 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:
469
						$config = &$GLOBALS['TCA']['tt_content']['columns'][$fN]['config'];
470
471
472
473
474
475
476
477
478
479
480
						$authModeDeny = $config['type'] == 'select' && $config['authMode']
							&& !$GLOBALS['BE_USER']->checkAuthMode('tt_content', $fN, $fV, $config['authMode']);
						// 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);
						}
						$isNotInKeepItems = count($keepItems[$fN]) && !in_array($fV, $keepItems[$fN]);
						if ($authModeDeny || in_array($fV, $removeItems[$fN]) || $isNotInKeepItems) {
481
482
483
484
485
							// Remove element all together:
							unset($wizardItems[$key]);
							break;
						} else {
							// Add the parameter:
486
							$wizardItems[$key]['params'] .= '&defVals[tt_content][' . $fN . ']=' . rawurlencode($fV);
487
488
489
490
491
492
493
494
495
496
							$tmp = explode('_', $key);
							$headersUsed[$tmp[0]] = $tmp[0];
						}
					}
				}
			}
		}
		// remove headers without elements
		foreach ($wizardItems as $key => $cfg) {
			$tmp = explode('_', $key);
497
			if ($tmp[0] && !$tmp[1] && !in_array($tmp[0], $headersUsed)) {
498
499
500
501
502
503
				unset($wizardItems[$key]);
			}
		}
	}

}