TYPO3 CMS  TYPO3_8-7
TypoScriptTemplateInfoHook.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  protected $t3editor = null;
26 
30  protected $ajaxSaveType = 'TypoScriptTemplateInformationModuleFunctionController';
31 
35  protected function getT3editor()
36  {
37  if ($this->t3editor == null) {
38  $this->t3editor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\T3editor\T3editor::class)->setMode(\TYPO3\CMS\T3editor\T3editor::MODE_TYPOSCRIPT)->setAjaxSaveType($this->ajaxSaveType);
39  }
40  return $this->t3editor;
41  }
42 
49  public function preStartPageHook()
50  {
51  // Enable editor in Template-Modul
52  if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('M') === 'web_ts') {
53  $t3editor = $this->getT3editor();
54  // Insert javascript code
55  $t3editor->getJavascriptCode();
56  }
57  }
58 
66  public function postOutputProcessingHook($parameters, $pObj)
67  {
68  $t3editor = $this->getT3editor();
69  $t3editor->getJavascriptCode();
70  foreach (['constants', 'config'] as $type) {
71  if ($parameters['e'][$type]) {
72  $attributes = 'rows="' . (int)$parameters['numberOfRows'] . '" wrap="off" style="width:98%;height:60%"';
73  $title = $GLOBALS['LANG']->getLL('template') . ' ' . $parameters['tplRow']['title'] . $GLOBALS['LANG']->getLL('delimiter') . ' ' . $GLOBALS['LANG']->getLL($type);
74  $outCode = $t3editor->getCodeEditor('data[' . $type . ']', 'text-monospace enable-tab', '$1', $attributes, $title, [
75  'pageId' => (int)$pObj->pObj->id
76  ]);
77  $parameters['theOutput'] = preg_replace('/\\<textarea name="data\\[' . $type . '\\]".*\\>([^\\<]*)\\<\\/textarea\\>/mi', $outCode, $parameters['theOutput']);
78  }
79  }
80  }
81 
87  public function save($parameters, $pObj)
88  {
89  $savingsuccess = false;
90  if ($parameters['type'] == $this->ajaxSaveType) {
92  if (!is_numeric($pageId) || $pageId < 1) {
93  return false;
94  }
95  // If given use the requested template_uid
96  // if not, use the first template-record on the page (in this case there should only be one record!)
98  $template_uid = $set['templatesOnPage'] ?: 0;
99  // Defined global here!
100  $tmpl = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\ExtendedTemplateService::class);
101  $tmpl->init();
102  // Get the first template record on the page, which might be hidden as well
103  // (for instance the TypoScript constant editor is persisting to the first template)
104  $tplRow = $tmpl->ext_getFirstTemplate($pageId, $template_uid);
105  $existTemplate = is_array($tplRow);
106  if ($existTemplate) {
107  $saveId = $tplRow['_ORIG_uid'] ?: $tplRow['uid'];
108  // Update template ?
110  if ($POST['submit']) {
111  // Set the data to be saved
112  $recData = [];
113  if (is_array($POST['data'])) {
114  foreach ($POST['data'] as $field => $val) {
115  switch ($field) {
116  case 'constants':
117  case 'config':
118  // Replace Windows- and Mac linebreaks
119  $val = str_replace([CRLF, CR], LF, $val);
120  $recData['sys_template'][$saveId][$field] = $val;
121  break;
122  }
123  }
124  }
125  if (!empty($recData)) {
126  // process template row before saving
127  $tstemplateinfo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Tstemplate\Controller\TypoScriptTemplateInformationModuleFunctionController::class);
128  /* @var $tstemplateinfo \TYPO3\CMS\Tstemplate\Controller\TypoScriptTemplateInformationModuleFunctionController */
129  // load the MOD_SETTINGS in order to check if the includeTypoScriptFileContent is set
130  $tstemplateinfo->pObj = $pObj;
131  $tstemplateinfo->pObj->MOD_SETTINGS = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData(['includeTypoScriptFileContent' => true], [], 'web_ts');
132  $recData['sys_template'][$saveId] = $tstemplateinfo->processTemplateRowBeforeSaving($recData['sys_template'][$saveId]);
133  // Create new tce-object
134  $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
135  // Initialize
136  $tce->start($recData, []);
137  // Saved the stuff
138  $tce->process_datamap();
139  // Clear the cache (note: currently only admin-users can clear the
140  // cache in tce_main.php)
141  $tce->clear_cacheCmd('all');
142  $savingsuccess = true;
143  }
144  }
145  }
146  }
147  return $savingsuccess;
148  }
149 }
static getModuleData( $MOD_MENU, $CHANGED_SETTINGS, $modName, $type='', $dontValidateList='', $setDefaultList='')
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']