TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
TypoScriptTemplateModuleController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Tstemplate\Controller;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
33 
38 {
42  public $perms_clause;
43 
47  public $e;
48 
52  public $sObj;
53 
57  public $edit;
58 
62  public $textExtensions = 'html,htm,txt,css,tmpl,inc,js';
63 
67  public $modMenu_type = '';
68 
73 
78 
82  public $pageinfo = [];
83 
87  public $access = false;
88 
94  protected $moduleName = 'web_ts';
95 
101  protected $moduleTemplate;
102 
106  protected $templateService;
107 
111  public function __construct()
112  {
113  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
114  $this->getLanguageService()->includeLLFile('EXT:tstemplate/Resources/Private/Language/locallang.xlf');
115 
116  $this->MCONF = [
117  'name' => $this->moduleName
118  ];
119  $this->moduleTemplate->addJavaScriptCode(
120  'jumpToUrl',
121  '
122  function jumpToUrl(URL) {
123  window.location.href = URL;
124  return false;
125  }
126  '
127  );
128  }
129 
135  public function init()
136  {
137  parent::init();
138  $this->id = (int)GeneralUtility::_GP('id');
139  $this->e = GeneralUtility::_GP('e');
140  $this->sObj = GeneralUtility::_GP('sObj');
141  $this->edit = GeneralUtility::_GP('edit');
142  $this->perms_clause = $this->getBackendUser()->getPagePermsClause(1);
143  }
144 
150  public function clearCache()
151  {
152  if (GeneralUtility::_GP('clear_all_cache')) {
154  $tce = GeneralUtility::makeInstance(DataHandler::class);
155  $tce->start([], []);
156  $tce->clear_cacheCmd('all');
157  }
158  }
159 
165  public function main()
166  {
167  // Access check...
168  // The page will show only if there is a valid page and if this page may be viewed by the user
169  $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->perms_clause);
170  $this->access = is_array($this->pageinfo);
171  $view = $this->getFluidTemplateObject('tstemplate');
172 
173  if ($this->id && $this->access) {
174  $urlParameters = [
175  'id' => $this->id,
176  'template' => 'all'
177  ];
178  $aHref = BackendUtility::getModuleUrl('web_ts', $urlParameters);
179 
180  // JavaScript
181  $this->moduleTemplate->addJavaScriptCode(
182  'TSTemplateInlineJS',
183  'function uFormUrl(aname) {
184  document.forms[0].action = ' . GeneralUtility::quoteJSvalue($aHref . '#') . '+aname;
185  }
186  function brPoint(lnumber,t) {
187  window.location.href = '
189  $aHref . '&SET[function]=TYPO3\\CMS\\Tstemplate\\Controller\\'
190  . 'TypoScriptTemplateObjectBrowserModuleFunctionController&SET[ts_browser_type]='
191  ) . '+(t?"setup":"const")+"&breakPointLN="+lnumber;
192  return false;
193  }
194  if (top.fsMod) top.fsMod.recentIds["web"] = ' . $this->id . ';'
195  );
196  $this->moduleTemplate->getPageRenderer()->addCssInlineBlock(
197  'TSTemplateInlineStyle',
198  'TABLE#typo3-objectBrowser { width: 100%; margin-bottom: 24px; }
199  TABLE#typo3-objectBrowser A { text-decoration: none; }
200  TABLE#typo3-objectBrowser .comment { color: maroon; font-weight: bold; }
201  .ts-typoscript { width: 100%; }
202  .tsob-search-submit {margin-left: 3px; margin-right: 3px;}
203  .tst-analyzer-options { margin:5px 0; }'
204  );
205  // Setting up the context sensitive menu:
206  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClickMenu');
207  // Build the module content
208  $view->assign('actionName', $aHref);
209  $view->assign('typoscriptTemplateModuleContent', $this->getExtObjContent());
210  // Setting up the buttons and markers for docheader
211  $this->getButtons();
212  $this->generateMenu();
213  } else {
214  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
215  $queryBuilder->getRestrictions()
216  ->removeAll()
217  ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
218  ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
219 
220  $result = $queryBuilder->select('pages.uid')
221  ->addSelectLiteral(
222  $queryBuilder->expr()->count('*', 'count'),
223  $queryBuilder->expr()->max('sys_template.root', 'root_max_val'),
224  $queryBuilder->expr()->min('sys_template.root', 'root_min_val')
225  )
226  ->from('pages')
227  ->from('sys_template')
228  ->where($queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('sys_template.pid')))
229  ->groupBy('pages.uid')
230  ->orderBy('pages.pid')
231  ->addOrderBy('pages.sorting')
232  ->execute();
233 
234  $pArray = [];
235  while ($record = $result->fetch()) {
236  $this->setInPageArray($pArray, BackendUtility::BEgetRootLine($record['uid'], 'AND 1=1'), $record);
237  }
238 
239  $view->getRenderingContext()->setControllerAction('PageZero');
240  $view->assign('templateList', $this->renderList($pArray));
241 
242  // RENDER LIST of pages with templates, END
243  // Setting up the buttons and markers for docheader
244  $this->getButtons();
245  }
246  $this->content = $view->render();
247  }
248 
255  protected function generateMenu()
256  {
257  $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
258  $menu->setIdentifier('WebFuncJumpMenu');
259  foreach ($this->MOD_MENU['function'] as $controller => $title) {
260  $item = $menu
261  ->makeMenuItem()
262  ->setHref(
263  BackendUtility::getModuleUrl(
264  $this->moduleName,
265  [
266  'id' => $this->id,
267  'SET' => [
268  'function' => $controller
269  ]
270  ]
271  )
272  )
273  ->setTitle($title);
274  if ($controller === $this->MOD_SETTINGS['function']) {
275  $item->setActive(true);
276  }
277  $menu->addMenuItem($item);
278  }
279  $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
280  }
281 
290  public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
291  {
292  $GLOBALS['SOBE'] = $this;
293  $this->init();
294 
295  // Checking for first level external objects
296  $this->checkExtObj();
297 
298  $this->clearCache();
299  $this->main();
300 
301  $this->moduleTemplate->setContent($this->content);
302  $response->getBody()->write($this->moduleTemplate->renderContent());
303  return $response;
304  }
305 
311  protected function getButtons()
312  {
313  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
314  $lang = $this->getLanguageService();
315 
316  if ($this->id && $this->access) {
317  // View page
318  $viewButton = $buttonBar->makeLinkButton()
319  ->setHref('#')
320  ->setOnClick(BackendUtility::viewOnClick(
321  $this->pageinfo['uid'],
322  '',
323  BackendUtility::BEgetRootLine($this->pageinfo['uid'])
324  ))
325  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
326  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-view', Icon::SIZE_SMALL));
327  $buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, 99);
328  if ($this->extClassConf['name'] === TypoScriptTemplateInformationModuleFunctionController::class) {
329  // NEW button
330  $urlParameters = [
331  'id' => $this->id,
332  'template' => 'all',
333  'createExtension' => 'new'
334  ];
335 
336  if (!empty($this->e) && !GeneralUtility::_POST('_saveandclosedok')) {
337  $saveButton = $buttonBar->makeInputButton()
338  ->setName('_savedok')
339  ->setValue('1')
340  ->setForm('TypoScriptTemplateModuleController')
341  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
342  'actions-document-save',
344  ))
345  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveDoc'));
346 
347  $saveAndCloseButton = $buttonBar->makeInputButton()
348  ->setName('_saveandclosedok')
349  ->setValue('1')
350  ->setForm('TypoScriptTemplateModuleController')
351  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc'))
352  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
353  'actions-document-save-close',
355  ));
356 
357  $splitButtonElement = $buttonBar->makeSplitButton()
358  ->addItem($saveButton)
359  ->addItem($saveAndCloseButton);
360 
361  $buttonBar->addButton($splitButtonElement, ButtonBar::BUTTON_POSITION_LEFT, 3);
362 
363  // CLOSE button
364  $closeButton = $buttonBar->makeLinkButton()
365  ->setHref(BackendUtility::getModuleUrl('web_ts', ['id' => $this->id]))
366  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.closeDoc'))
367  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
368  'actions-document-close',
370  ));
371  $buttonBar->addButton($closeButton);
372  } else {
373  $newButton = $buttonBar->makeLinkButton()
374  ->setHref(BackendUtility::getModuleUrl('web_ts', $urlParameters))
375  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:db_new.php.pagetitle'))
376  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
377  'actions-document-new',
379  ));
380  $buttonBar->addButton($newButton);
381  }
382  } elseif ($this->extClassConf['name'] === TypoScriptTemplateConstantEditorModuleFunctionController::class
383  && !empty($this->MOD_MENU['constant_editor_cat'])) {
384  // SAVE button
385  $saveButton = $buttonBar->makeInputButton()
386  ->setName('_savedok')
387  ->setValue('1')
388  ->setForm('TypoScriptTemplateModuleController')
389  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveDoc'))
390  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
391  'actions-document-save',
393  ))
394  ->setShowLabelText(true);
395  $buttonBar->addButton($saveButton);
396  } elseif ($this->extClassConf['name'] === TypoScriptTemplateObjectBrowserModuleFunctionController::class) {
397  if (!empty($this->sObj)) {
398  // BACK
399  $urlParameters = [
400  'id' => $this->id
401  ];
402  $backButton = $buttonBar->makeLinkButton()
403  ->setHref(BackendUtility::getModuleUrl('web_ts', $urlParameters))
404  ->setClasses('typo3-goBack')
405  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
406  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
407  'actions-view-go-back',
409  ));
410  $buttonBar->addButton($backButton);
411  }
412  }
413  }
414  // Shortcut
415  $shortcutButton = $buttonBar->makeShortcutButton()
416  ->setModuleName($this->MCONF['name'])
417  ->setGetVariables(['id', 'M']);
418  $buttonBar->addButton($shortcutButton);
419  }
420 
421  // OTHER FUNCTIONS:
429  public function linkWrapTemplateTitle($title, $onlyKey = '')
430  {
431  $urlParameters = [
432  'id' => $this->id
433  ];
434  $aHref = BackendUtility::getModuleUrl('web_ts', $urlParameters);
435  if ($onlyKey) {
436  $title = '<a href="' . htmlspecialchars(($aHref . '&e[' . $onlyKey . ']=1&SET[function]=TYPO3\\CMS\\Tstemplate\\Controller\\TypoScriptTemplateInformationModuleFunctionController')) . '">' . htmlspecialchars($title) . '</a>';
437  } else {
438  $title = '<a href="' . htmlspecialchars(($aHref . '&e[constants]=1&e[config]=1&SET[function]=TYPO3\\CMS\\Tstemplate\\Controller\\TypoScriptTemplateInformationModuleFunctionController')) . '">' . htmlspecialchars($title) . '</a>';
439  }
440  return $title;
441  }
442 
449  public function noTemplate($newStandardTemplate = 0)
450  {
451  $this->templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
452  $this->templateService->init();
453 
454  $moduleContent['state'] = InfoboxViewHelper::STATE_INFO;
455 
456  // New standard?
457  if ($newStandardTemplate) {
458  $selector = '';
459  $staticsText = '';
460  // Hook to change output, implemented for statictemplates
461  $hookObject = $this->getHookObjectForAction('newStandardTemplateView');
462  if (!empty($hookObject)) {
463  $reference = [
464  'selectorHtml' => &$selector,
465  'staticsText' => &$staticsText
466  ];
468  $hookObject,
469  $reference,
470  $this
471  );
472  $selector = $reference['selectorHtml'];
473  $staticsText = $reference['staticsText'];
474  }
475  // Extension?
476  $moduleContent['staticsText'] = $staticsText;
477  $moduleContent['selector'] = $selector;
478  }
479  // Go to previous Page with Template...
480  $previousPage = $this->templateService->ext_prevPageWithTemplate($this->id, $this->perms_clause);
481  if ($previousPage) {
482  $urlParameters = [
483  'id' => $previousPage['uid']
484  ];
485  $previousPage['aHref'] = BackendUtility::getModuleUrl('web_ts', $urlParameters);
486  $moduleContent['previousPage'] = $previousPage;
487  }
488  $view = $this->getFluidTemplateObject('tstemplate', 'NoTemplate');
489  $view->assign('content', $moduleContent);
490  return $view->render();
491  }
492 
498  public function templateMenu()
499  {
500  $this->templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
501  $this->templateService->init();
502 
503  $all = $this->templateService->ext_getAllTemplates($this->id);
504  if (count($all) > 1) {
505  $this->MOD_MENU['templatesOnPage'] = [];
506  foreach ($all as $d) {
507  $this->MOD_MENU['templatesOnPage'][$d['uid']] = $d['title'];
508  }
509  }
510  $this->MOD_SETTINGS = BackendUtility::getModuleData(
511  $this->MOD_MENU,
512  GeneralUtility::_GP('SET'),
513  $this->MCONF['name'],
514  $this->modMenu_type,
515  $this->modMenu_dontValidateList,
516  $this->modMenu_setDefaultList
517  );
519  $this->id,
520  'SET[templatesOnPage]',
521  $this->MOD_SETTINGS['templatesOnPage'],
522  $this->MOD_MENU['templatesOnPage']
523  );
524  }
525 
533  public function createTemplate($id, $actTemplateId = 0)
534  {
535  $recData = [];
537  $tce = GeneralUtility::makeInstance(DataHandler::class);
538 
539  if (GeneralUtility::_GP('createExtension')) {
540  $recData['sys_template']['NEW'] = [
541  'pid' => $actTemplateId ? -1 * $actTemplateId : $id,
542  'title' => '+ext'
543  ];
544  $tce->start($recData, []);
545  $tce->process_datamap();
546  } elseif (GeneralUtility::_GP('newWebsite')) {
547  // Hook to handle row data, implemented for statictemplates
548  $hookObject = $this->getHookObjectForAction('newStandardTemplateHandler');
549  if (!empty($hookObject)) {
550  $reference = [
551  'recData' => &$recData,
552  'id' => $id,
553  ];
555  $hookObject,
556  $reference,
557  $this
558  );
559  $recData = $reference['recData'];
560  } else {
561  $recData['sys_template']['NEW'] = [
562  'pid' => $id,
563  'title' => $this->getLanguageService()->getLL('titleNewSite'),
564  'sorting' => 0,
565  'root' => 1,
566  'clear' => 3,
567  'config' => '
568 # Default PAGE object:
569 page = PAGE
570 page.10 = TEXT
571 page.10.value = HELLO WORLD!
572 '
573  ];
574  }
575  $tce->start($recData, []);
576  $tce->process_datamap();
577  $tce->clear_cacheCmd('all');
578  }
579  return $tce->substNEWwithIDs['NEW'];
580  }
581 
582  // RENDER LIST of pages with templates, BEGIN
591  public function setInPageArray(&$pArray, $rlArr, $row)
592  {
593  ksort($rlArr);
594  reset($rlArr);
595  if (!$rlArr[0]['uid']) {
596  array_shift($rlArr);
597  }
598  $cEl = current($rlArr);
599  $pArray[$cEl['uid']] = htmlspecialchars($cEl['title']);
600  array_shift($rlArr);
601  if (!empty($rlArr)) {
602  $key = $cEl['uid'] . '.';
603  if (empty($pArray[$key])) {
604  $pArray[$key] = [];
605  }
606  $this->setInPageArray($pArray[$key], $rlArr, $row);
607  } else {
608  $key = $cEl['uid'] . '_';
609  $pArray[$key] = $row;
610  }
611  }
612 
621  public function renderList($pArray, $lines = [], $c = 0)
622  {
623  if (!is_array($pArray)) {
624  return $lines;
625  }
626 
627  $statusCheckedIcon = $this->moduleTemplate->getIconFactory()
628  ->getIcon('status-status-checked', Icon::SIZE_SMALL)->render();
629  $i = 0;
630  foreach ($pArray as $k => $v) {
632  $line = [];
633  $key = $k . '_';
634  $line['marginLeft'] = $c * 20;
635  $line['class'] = ($i++ % 2 === 0 ? 'bgColor4' : 'bgColor6');
636  $line['pageTitle'] = GeneralUtility::fixed_lgd_cs($pArray[$k], 30);
637  $line['icon'] = $this->moduleTemplate->getIconFactory()
638  ->getIconForRecord(
639  'pages',
640  BackendUtility::getRecordWSOL('pages', $k),
642  )->render();
643  if (!empty($pArray[$key])) {
644  $line['href'] = GeneralUtility::linkThisScript(['id' => (int)$k]);
645  $line['title'] = 'ID: ' . (int)$k;
646  $line['count'] = $pArray[$k . '_']['count'];
647  $line['root_max_val'] = ($pArray[$key]['root_max_val'] > 0 ? $statusCheckedIcon : '&nbsp;');
648  $line['root_min_val'] = ($pArray[$key]['root_min_val'] === 0 ? $statusCheckedIcon : '&nbsp;');
649  } else {
650  $line['href'] = '';
651  $line['title'] = '';
652  $line['count'] = '';
653  $line['root_max_val'] = '';
654  $line['root_min_val'] = '';
655  }
656  $lines[] = $line;
657  $lines = $this->renderList($pArray[$k . '.'], $lines, $c + 1);
658  }
659  }
660  return $lines;
661  }
662 
670  protected function getFluidTemplateObject($extensionName, $templateName = 'Main')
671  {
673  $view = GeneralUtility::makeInstance(StandaloneView::class);
674  $view->getRenderingContext()->getTemplatePaths()->fillDefaultsByPackageName($extensionName);
675  $view->getRenderingContext()->setControllerAction($templateName);
676  $view->getRequest()->setControllerExtensionName('tstemplate');
677  return $view;
678  }
679 
684  protected function getHookObjectForAction($action)
685  {
686  if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class][$action])) {
687  return $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class][$action];
688  }
689  return null;
690  }
691 }
static getRecordWSOL($table, $uid, $fields= '*', $where= '', $useDeleteClause=true, $unsetMovePointers=false)
mainAction(ServerRequestInterface $request, ResponseInterface $response)
static getModuleData($MOD_MENU, $CHANGED_SETTINGS, $modName, $type= '', $dontValidateList= '', $setDefaultList= '')
static viewOnClick($pageUid, $backPath= '', $rootLine=null, $anchorSection= '', $alternativeUrl= '', $additionalGetVars= '', $switchFocus=true)
static getFuncMenu($mainParams, $elementName, $currentValue, $menuItems, $script= '', $addParams= '')
static BEgetRootLine($uid, $clause= '', $workspaceOL=false)
static linkThisScript(array $getParams=[])
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
static callUserFunction($funcName, &$params, &$ref, $_= '', $errorMode=0)