TYPO3  7.6
NewContentElementController.php
Go to the documentation of this file.
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 
30 
35 {
41  public $id;
42 
48  public $sys_language = 0;
49 
55  public $R_URI = '';
56 
62  public $colPos;
63 
67  public $uid_pid;
68 
74  public $modTSconfig = array();
75 
81  public $doc;
82 
88  public $content;
89 
95  public $access;
96 
102  public $config;
103 
107  protected $pageInfo;
108 
112  protected $elementWrapper;
113 
117  protected $onClickEvent;
118 
122  protected $MCONF;
123 
127  public function __construct()
128  {
129  parent::__construct();
130  $GLOBALS['SOBE'] = $this;
131  $this->init();
132  }
133 
139  public function init()
140  {
141  $lang = $this->getLanguageService();
142  $lang->includeLLFile('EXT:lang/locallang_misc.xlf');
143  $LOCAL_LANG_orig = $GLOBALS['LOCAL_LANG'];
144  $lang->includeLLFile('EXT:backend/Resources/Private/Language/locallang_db_new_content_el.xlf');
145  ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG_orig, $GLOBALS['LOCAL_LANG']);
146  $GLOBALS['LOCAL_LANG'] = $LOCAL_LANG_orig;
147 
148  // Setting internal vars:
149  $this->id = (int)GeneralUtility::_GP('id');
150  $this->sys_language = (int)GeneralUtility::_GP('sys_language_uid');
151  $this->R_URI = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
152  $this->colPos = GeneralUtility::_GP('colPos') === null ? null : (int)GeneralUtility::_GP('colPos');
153  $this->uid_pid = (int)GeneralUtility::_GP('uid_pid');
154  $this->MCONF['name'] = 'xMOD_db_new_content_el';
155  $this->modTSconfig = BackendUtility::getModTSconfig($this->id, 'mod.wizards.newContentElement');
157  $this->config = $config['mod.']['wizards.']['newContentElement.'];
158  // Starting the document template object:
159  // We keep this here in case somebody relies on it in a hook or alike
160  $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
161  // Setting up the context sensitive menu:
162  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClickMenu');
163  // Getting the current page and receiving access information (used in main())
164  $perms_clause = $this->getBackendUser()->getPagePermsClause(1);
165  $this->pageInfo = BackendUtility::readPageAccess($this->id, $perms_clause);
166  $this->access = is_array($this->pageInfo) ? 1 : 0;
167  }
168 
178  {
179  $this->main();
180  $this->moduleTemplate->setContent($this->content);
181  $response->getBody()->write($this->moduleTemplate->renderContent());
182  return $response;
183  }
184 
191  public function main()
192  {
193  $lang = $this->getLanguageService();
194  $this->content .= '<form action="" name="editForm" id="NewContentElementController"><input type="hidden" name="defValues" value="" />';
195  if ($this->id && $this->access) {
196  // Init position map object:
197  $posMap = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\View\ContentCreationPagePositionMap::class);
198  $posMap->cur_sys_language = $this->sys_language;
199  // If a column is pre-set:
200  if (isset($this->colPos)) {
201  if ($this->uid_pid < 0) {
202  $row = array();
203  $row['uid'] = abs($this->uid_pid);
204  } else {
205  $row = '';
206  }
207  $this->onClickEvent = $posMap->onClickInsertRecord(
208  $row,
209  $this->colPos,
210  '',
211  $this->uid_pid,
212  $this->sys_language
213  );
214  } else {
215  $this->onClickEvent = '';
216  }
217  // ***************************
218  // Creating content
219  // ***************************
220  $this->content .= '<h1>' . $lang->getLL('newContentElement') . '</h1>';
221  // Wizard
222  $wizardItems = $this->wizardArray();
223  // Wrapper for wizards
224  $this->elementWrapper['section'] = array('', '');
225  // Hook for manipulating wizardItems, wrapper, onClickEvent etc.
226  if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms']['db_new_content_el']['wizardItemsHook'])) {
227  foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms']['db_new_content_el']['wizardItemsHook'] as $classData) {
228  $hookObject = GeneralUtility::getUserObj($classData);
229  if (!$hookObject instanceof NewContentElementWizardHookInterface) {
230  throw new \UnexpectedValueException(
231  '$hookObject must implement interface ' . NewContentElementWizardHookInterface::class,
232  1227834741
233  );
234  }
235  $hookObject->manipulateWizardItems($wizardItems, $this);
236  }
237  }
238  // Add document inline javascript
239  $this->moduleTemplate->addJavaScriptCode(
240  'NewContentElementWizardInlineJavascript',
241  '
242  function goToalt_doc() { //
243  ' . $this->onClickEvent . '
244  }
245 
246  if(top.refreshMenu) {
247  top.refreshMenu();
248  } else {
249  top.TYPO3ModuleMenu.refreshMenu();
250  }'
251  );
252 
253  $iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
254 
255  // Traverse items for the wizard.
256  // An item is either a header or an item rendered with a radio button and title/description and icon:
257  $cc = ($key = 0);
258  $menuItems = array();
259  foreach ($wizardItems as $k => $wInfo) {
260  if ($wInfo['header']) {
261  $menuItems[] = array(
262  'label' => htmlspecialchars($wInfo['header']),
263  'content' => $this->elementWrapper['section'][0]
264  );
265  $key = count($menuItems) - 1;
266  } else {
267  $content = '';
268 
269  if (!$this->onClickEvent) {
270  // Radio button:
271  $oC = 'document.editForm.defValues.value=unescape(' . GeneralUtility::quoteJSvalue(rawurlencode($wInfo['params'])) . ');goToalt_doc();' . (!$this->onClickEvent ? 'window.location.hash=\'#sel2\';' : '');
272  $content .= '<div class="media-left"><input type="radio" name="tempB" value="' . htmlspecialchars($k) . '" onclick="' . htmlspecialchars($oC) . '" /></div>';
273  // Onclick action for icon/title:
274  $aOnClick = 'document.getElementsByName(\'tempB\')[' . $cc . '].checked=1;' . $oC . 'return false;';
275  } else {
276  $aOnClick = "document.editForm.defValues.value=unescape('" . rawurlencode($wInfo['params']) . "');goToalt_doc();" . (!$this->onClickEvent?"window.location.hash='#sel2';":'');
277  }
278 
279  if (isset($wInfo['icon'])) {
280  GeneralUtility::deprecationLog('The PageTS-Config: mod.wizards.newContentElement.wizardItems.*.elements.*.icon'
281  . ' is deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8.'
282  . ' Register your icon in IconRegistry::registerIcon and use the new setting:'
283  . ' mod.wizards.newContentElement.wizardItems.*.elements.*.iconIdentifier');
284  $wInfo['iconIdentifier'] = 'content-' . $k;
285  $icon = $wInfo['icon'];
286  if (StringUtility::beginsWith($icon, '../typo3conf/ext/')) {
287  $icon = str_replace('../typo3conf/ext/', 'EXT:', $icon);
288  }
289  if (!StringUtility::beginsWith($icon, 'EXT:') && strpos($icon, '/') !== false) {
290  $icon = TYPO3_mainDir . GeneralUtility::resolveBackPath($wInfo['icon']);
291  }
292  $iconRegistry->registerIcon($wInfo['iconIdentifier'], BitmapIconProvider::class, array(
293  'source' => $icon
294  ));
295  }
296  $icon = $this->moduleTemplate->getIconFactory()->getIcon($wInfo['iconIdentifier'])->render();
297  $menuItems[$key]['content'] .= '
298  <div class="media">
299  <a href="#" onclick="' . htmlspecialchars($aOnClick) . '">
300  ' . $content . '
301  <div class="media-left">
302  ' . $icon . '
303  </div>
304  <div class="media-body">
305  <strong>' . htmlspecialchars($wInfo['title']) . '</strong>' .
306  '<br />' .
307  nl2br(htmlspecialchars(trim($wInfo['description']))) .
308  '</div>
309  </a>
310  </div>';
311  $cc++;
312  }
313  }
314  // Add closing section-tag
315  foreach ($menuItems as $key => $val) {
316  $menuItems[$key]['content'] .= $this->elementWrapper['section'][1];
317  }
318  // Add the wizard table to the content, wrapped in tabs
319  $code = '<p>' . $lang->getLL('sel1', 1) . '</p>' . $this->moduleTemplate->getDynamicTabMenu(
320  $menuItems,
321  'new-content-element-wizard'
322  );
323 
324  $this->content .= !$this->onClickEvent ? '<h2>' . $lang->getLL('1_selectType', true) . '</h2>' : '';
325  $this->content .= '<div>' . $code . '</div>';
326 
327  // If the user must also select a column:
328  if (!$this->onClickEvent) {
329  // Add anchor "sel2"
330  $this->content .= '<div><a name="sel2"></a></div>';
331  // Select position
332  $code = '<p>' . $lang->getLL('sel2', 1) . '</p>';
333 
334  // Load SHARED page-TSconfig settings and retrieve column list from there, if applicable:
335  $colPosArray = GeneralUtility::callUserFunction(
336  BackendLayoutView::class . '->getColPosListItemsParsed',
337  $this->id,
338  $this
339  );
340  $colPosIds = array_column($colPosArray, 1);
341  // Removing duplicates, if any
342  $colPosList = implode(',', array_unique(array_map('intval', $colPosIds)));
343  // Finally, add the content of the column selector to the content:
344  $code .= $posMap->printContentElementColumns($this->id, 0, $colPosList, 1, $this->R_URI);
345  $this->content .= '<h2>' . $lang->getLL('2_selectPosition', true) . '</h2><div>' . $code . '</div>';
346  }
347  } else {
348  // In case of no access:
349  $this->content = '';
350  $this->content .= '<h1>' . $lang->getLL('newContentElement') . '</h1>';
351  }
352  $this->content .= '</form>';
353  // Setting up the buttons and markers for docheader
354  $this->getButtons();
355  }
356 
360  protected function getButtons()
361  {
362  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
363  if ($this->R_URI) {
364  $backButton = $buttonBar->makeLinkButton()
365  ->setHref($this->R_URI)
366  ->setTitle($this->getLanguageService()->getLL('goBack', true))
367  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
368  'actions-view-go-back',
370  ));
371  $buttonBar->addButton($backButton);
372  }
373  $cshButton = $buttonBar->makeHelpButton()->setModuleName('xMOD_csh_corebe')->setFieldName('new_ce');
374  $buttonBar->addButton($cshButton);
375  }
376 
377  /***************************
378  *
379  * OTHER FUNCTIONS:
380  *
381  ***************************/
388  public function getWizardItems()
389  {
391  return $this->wizardArray();
392  }
393 
400  public function wizardArray()
401  {
402  $wizardItems = array();
403  if (is_array($this->config)) {
404  $wizards = $this->config['wizardItems.'];
405  $appendWizards = $this->wizard_appendWizards($wizards['elements.']);
406  if (is_array($wizards)) {
407  foreach ($wizards as $groupKey => $wizardGroup) {
408  $groupKey = rtrim($groupKey, '.');
409  $showItems = GeneralUtility::trimExplode(',', $wizardGroup['show'], true);
410  $showAll = $wizardGroup['show'] === '*';
411  $groupItems = array();
412  if (is_array($appendWizards[$groupKey . '.']['elements.'])) {
413  $wizardElements = array_merge((array)$wizardGroup['elements.'], $appendWizards[$groupKey . '.']['elements.']);
414  } else {
415  $wizardElements = $wizardGroup['elements.'];
416  }
417  if (is_array($wizardElements)) {
418  foreach ($wizardElements as $itemKey => $itemConf) {
419  $itemKey = rtrim($itemKey, '.');
420  if ($showAll || in_array($itemKey, $showItems)) {
421  $tmpItem = $this->wizard_getItem($groupKey, $itemKey, $itemConf);
422  if ($tmpItem) {
423  $groupItems[$groupKey . '_' . $itemKey] = $tmpItem;
424  }
425  }
426  }
427  }
428  if (!empty($groupItems)) {
429  $wizardItems[$groupKey] = $this->wizard_getGroupHeader($groupKey, $wizardGroup);
430  $wizardItems = array_merge($wizardItems, $groupItems);
431  }
432  }
433  }
434  }
435  // Remove elements where preset values are not allowed:
436  $this->removeInvalidElements($wizardItems);
437  return $wizardItems;
438  }
439 
444  public function wizard_appendWizards($wizardElements)
445  {
446  if (!is_array($wizardElements)) {
447  $wizardElements = array();
448  }
449  if (is_array($GLOBALS['TBE_MODULES_EXT']['xMOD_db_new_content_el']['addElClasses'])) {
450  foreach ($GLOBALS['TBE_MODULES_EXT']['xMOD_db_new_content_el']['addElClasses'] as $class => $path) {
451  require_once $path;
452  $modObj = GeneralUtility::makeInstance($class);
453  $wizardElements = $modObj->proc($wizardElements);
454  }
455  }
456  $returnElements = array();
457  foreach ($wizardElements as $key => $wizardItem) {
458  preg_match('/^[a-zA-Z0-9]+_/', $key, $group);
459  $wizardGroup = $group[0] ? substr($group[0], 0, -1) . '.' : $key;
460  $returnElements[$wizardGroup]['elements.'][substr($key, strlen($wizardGroup)) . '.'] = $wizardItem;
461  }
462  return $returnElements;
463  }
464 
471  public function wizard_getItem($groupKey, $itemKey, $itemConf)
472  {
473  $itemConf['title'] = $this->getLanguageService()->sL($itemConf['title']);
474  $itemConf['description'] = $this->getLanguageService()->sL($itemConf['description']);
475  $itemConf['tt_content_defValues'] = $itemConf['tt_content_defValues.'];
476  unset($itemConf['tt_content_defValues.']);
477  return $itemConf;
478  }
479 
485  public function wizard_getGroupHeader($groupKey, $wizardGroup)
486  {
487  return array(
488  'header' => $this->getLanguageService()->sL($wizardGroup['header'])
489  );
490  }
491 
501  public function removeInvalidElements(&$wizardItems)
502  {
503  // Get TCEFORM from TSconfig of current page
504  $row = array('pid' => $this->id);
505  $TCEFORM_TSconfig = BackendUtility::getTCEFORM_TSconfig('tt_content', $row);
506  $headersUsed = array();
507  // Traverse wizard items:
508  foreach ($wizardItems as $key => $cfg) {
509  // Exploding parameter string, if any (old style)
510  if ($wizardItems[$key]['params']) {
511  // Explode GET vars recursively
512  $tempGetVars = GeneralUtility::explodeUrl2Array($wizardItems[$key]['params'], true);
513  // If tt_content values are set, merge them into the tt_content_defValues array,
514  // unset them from $tempGetVars and re-implode $tempGetVars into the param string
515  // (in case remaining parameters are around).
516  if (is_array($tempGetVars['defVals']['tt_content'])) {
517  $wizardItems[$key]['tt_content_defValues'] = array_merge(
518  is_array($wizardItems[$key]['tt_content_defValues']) ? $wizardItems[$key]['tt_content_defValues'] : array(),
519  $tempGetVars['defVals']['tt_content']
520  );
521  unset($tempGetVars['defVals']['tt_content']);
522  $wizardItems[$key]['params'] = GeneralUtility::implodeArrayForUrl('', $tempGetVars);
523  }
524  }
525  // If tt_content_defValues are defined...:
526  if (is_array($wizardItems[$key]['tt_content_defValues'])) {
527  $backendUser = $this->getBackendUser();
528  // Traverse field values:
529  foreach ($wizardItems[$key]['tt_content_defValues'] as $fN => $fV) {
530  if (is_array($GLOBALS['TCA']['tt_content']['columns'][$fN])) {
531  // Get information about if the field value is OK:
532  $config = &$GLOBALS['TCA']['tt_content']['columns'][$fN]['config'];
533  $authModeDeny = $config['type'] == 'select' && $config['authMode']
534  && !$backendUser->checkAuthMode('tt_content', $fN, $fV, $config['authMode']);
535  // explode TSconfig keys only as needed
536  if (!isset($removeItems[$fN])) {
537  $removeItems[$fN] = GeneralUtility::trimExplode(
538  ',',
539  $TCEFORM_TSconfig[$fN]['removeItems'],
540  true
541  );
542  }
543  if (!isset($keepItems[$fN])) {
544  $keepItems[$fN] = GeneralUtility::trimExplode(
545  ',',
546  $TCEFORM_TSconfig[$fN]['keepItems'],
547  true
548  );
549  }
550  $isNotInKeepItems = !empty($keepItems[$fN]) && !in_array($fV, $keepItems[$fN]);
551  if ($authModeDeny || $fN === 'CType' && in_array($fV, $removeItems[$fN]) || $isNotInKeepItems) {
552  // Remove element all together:
553  unset($wizardItems[$key]);
554  break;
555  } else {
556  // Add the parameter:
557  $wizardItems[$key]['params'] .= '&defVals[tt_content][' . $fN . ']=' . rawurlencode($fV);
558  $tmp = explode('_', $key);
559  $headersUsed[$tmp[0]] = $tmp[0];
560  }
561  }
562  }
563  }
564  }
565  // remove headers without elements
566  foreach ($wizardItems as $key => $cfg) {
567  $tmp = explode('_', $key);
568  if ($tmp[0] && !$tmp[1] && !in_array($tmp[0], $headersUsed)) {
569  unset($wizardItems[$key]);
570  }
571  }
572  }
573 
579  protected function getLanguageService()
580  {
581  return $GLOBALS['LANG'];
582  }
583 
589  protected function getBackendUser()
590  {
591  return $GLOBALS['BE_USER'];
592  }
593 }