TYPO3  7.6
TypoScriptTemplateInfoHook.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\T3editor\Hook;
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 
50  public function preStartPageHook()
51  {
52  // Enable editor in Template-Modul
53  if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('M') === 'web_ts') {
54  $t3editor = $this->getT3editor();
55  // Insert javascript code
56  $t3editor->getJavascriptCode();
57  }
58  }
59 
68  public function postOutputProcessingHook($parameters, $pObj)
69  {
70  $t3editor = $this->getT3editor();
71  $t3editor->getJavascriptCode();
72  foreach (array('constants', 'config') as $type) {
73  if ($parameters['e'][$type]) {
74  $attributes = 'rows="' . $parameters['numberOfRows'] . '" ' . 'wrap="off" ' . $pObj->pObj->doc->formWidth(48, true, 'width:98%;height:60%');
75  $title = $GLOBALS['LANG']->getLL('template') . ' ' . htmlspecialchars($parameters['tplRow']['title']) . $GLOBALS['LANG']->getLL('delimiter') . ' ' . $GLOBALS['LANG']->getLL($type);
76  $outCode = $t3editor->getCodeEditor('data[' . $type . ']', 'text-monospace enable-tab', '$1', $attributes, $title, array(
77  'pageId' => (int)$pObj->pObj->id
78  ));
79  $parameters['theOutput'] = preg_replace('/\\<textarea name="data\\[' . $type . '\\]".*\\>([^\\<]*)\\<\\/textarea\\>/mi', $outCode, $parameters['theOutput']);
80  }
81  }
82  }
83 
89  public function save($parameters, $pObj)
90  {
91  $savingsuccess = false;
92  if ($parameters['type'] == $this->ajaxSaveType) {
93  $pageId = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('pageId');
94  if (!is_numeric($pageId) || $pageId < 1) {
95  return false;
96  }
97  // If given use the requested template_uid
98  // if not, use the first template-record on the page (in this case there should only be one record!)
99  $set = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SET');
100  $template_uid = $set['templatesOnPage'] ?: 0;
101  // Defined global here!
102  $tmpl = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\ExtendedTemplateService::class);
103  // Do not log time-performance information
104  $tmpl->tt_track = 0;
105  $tmpl->init();
106  // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
107  $tplRow = $tmpl->ext_getFirstTemplate($pageId, $template_uid);
108  $existTemplate = is_array($tplRow);
109  if ($existTemplate) {
110  $saveId = $tplRow['_ORIG_uid'] ?: $tplRow['uid'];
111  // Update template ?
112  $POST = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST();
113  if ($POST['submit']) {
114  // Set the data to be saved
115  $recData = array();
116  if (is_array($POST['data'])) {
117  foreach ($POST['data'] as $field => $val) {
118  switch ($field) {
119  case 'constants':
120  case 'config':
121  // Replace Windows- and Mac linebreaks
122  $val = str_replace([CRLF, CR], LF, $val);
123  $recData['sys_template'][$saveId][$field] = $val;
124  break;
125  }
126  }
127  }
128  if (!empty($recData)) {
129  // process template row before saving
130  $tstemplateinfo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Tstemplate\Controller\TypoScriptTemplateInformationModuleFunctionController::class);
131  /* @var $tstemplateinfo \TYPO3\CMS\Tstemplate\Controller\TypoScriptTemplateInformationModuleFunctionController */
132  // load the MOD_SETTINGS in order to check if the includeTypoScriptFileContent is set
133  $tstemplateinfo->pObj = $pObj;
134  $tstemplateinfo->pObj->MOD_SETTINGS = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData(array('includeTypoScriptFileContent' => true), array(), 'web_ts');
135  $recData['sys_template'][$saveId] = $tstemplateinfo->processTemplateRowBeforeSaving($recData['sys_template'][$saveId]);
136  // Create new tce-object
137  $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
138  $tce->stripslashes_values = 0;
139  // Initialize
140  $tce->start($recData, array());
141  // Saved the stuff
142  $tce->process_datamap();
143  // Clear the cache (note: currently only admin-users can clear the
144  // cache in tce_main.php)
145  $tce->clear_cacheCmd('all');
146  $savingsuccess = true;
147  }
148  }
149  }
150  }
151  return $savingsuccess;
152  }
153 }