[CLEANUP] Replace count with empty in multiple extensions
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / Hook / TypoScriptTemplateInfoHook.php
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
17 /**
18 * Hook for tstemplate info
19 *
20 * @author Tobias Liebig <mail_typo3@etobi.de>
21 */
22 class TypoScriptTemplateInfoHook {
23
24 /**
25 * @var \TYPO3\CMS\T3editor\T3editor
26 */
27 protected $t3editor = NULL;
28
29 /**
30 * @var string
31 */
32 protected $ajaxSaveType = 'TypoScriptTemplateInformationModuleFunctionController';
33
34 /**
35 * @return \TYPO3\CMS\T3editor\T3editor
36 */
37 protected function getT3editor() {
38 if ($this->t3editor == NULL) {
39 $this->t3editor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\T3editor\T3editor::class)->setMode(\TYPO3\CMS\T3editor\T3editor::MODE_TYPOSCRIPT)->setAjaxSaveType($this->ajaxSaveType);
40 }
41 return $this->t3editor;
42 }
43
44 /**
45 * Hook-function: inject t3editor JavaScript code before the page is compiled
46 * called in \TYPO3\CMS\Backend\Template\DocumentTemplate:startPage
47 *
48 * @param array $parameters
49 * @param \TYPO3\CMS\Backend\Template\DocumentTemplate $pObj
50 * @return void
51 * @see \TYPO3\CMS\Backend\Template\DocumentTemplate::startPage
52 */
53 public function preStartPageHook($parameters, $pObj) {
54 // Enable editor in Template-Modul
55 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('M') === 'web_ts') {
56 $t3editor = $this->getT3editor();
57 // Insert javascript code in document header
58 $pObj->JScode .= $t3editor->getJavascriptCode($pObj);
59 }
60 }
61
62 /**
63 * Hook-function:
64 * called in typo3/sysext/tstemplate_info/Classes/Controller/TypoScriptTemplateInformationModuleFunctionController.php
65 *
66 * @param array $parameters
67 * @param \TYPO3\CMS\Tstemplate\Controller\TypoScriptTemplateInformationModuleFunctionController $pObj
68 * @return void
69 */
70 public function postOutputProcessingHook($parameters, $pObj) {
71 $t3editor = $this->getT3editor();
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
84 /**
85 * Process saving request like in class.tstemplateinfo.php (TCE processing)
86 *
87 * @return bool TRUE if successful
88 */
89 public function save($parameters, $pObj) {
90 $savingsuccess = FALSE;
91 if ($parameters['type'] == $this->ajaxSaveType) {
92 $pageId = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('pageId');
93 if (!is_numeric($pageId) || $pageId < 1) {
94 return FALSE;
95 }
96 // If given use the requested template_uid
97 // if not, use the first template-record on the page (in this case there should only be one record!)
98 $set = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SET');
99 $template_uid = $set['templatesOnPage'] ?: 0;
100 // Defined global here!
101 $tmpl = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\ExtendedTemplateService::class);
102 // Do not log time-performance information
103 $tmpl->tt_track = 0;
104 $tmpl->init();
105 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
106 $tplRow = $tmpl->ext_getFirstTemplate($pageId, $template_uid);
107 $existTemplate = is_array($tplRow) ? TRUE : FALSE;
108 if ($existTemplate) {
109 $saveId = $tplRow['_ORIG_uid'] ?: $tplRow['uid'];
110 // Update template ?
111 $POST = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST();
112 if ($POST['submit']) {
113 // Set the data to be saved
114 $recData = array();
115 if (is_array($POST['data'])) {
116 foreach ($POST['data'] as $field => $val) {
117 switch ($field) {
118 case 'constants':
119 case 'config':
120 // Replace Windows- and Mac linebreaks
121 $val = str_replace([CRLF, CR], LF, $val);
122 $recData['sys_template'][$saveId][$field] = $val;
123 break;
124 }
125 }
126 }
127 if (!empty($recData)) {
128 // process template row before saving
129 $tstemplateinfo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Tstemplate\Controller\TypoScriptTemplateInformationModuleFunctionController::class);
130 /* @var $tstemplateinfo \TYPO3\CMS\Tstemplate\Controller\TypoScriptTemplateInformationModuleFunctionController */
131 // load the MOD_SETTINGS in order to check if the includeTypoScriptFileContent is set
132 $tstemplateinfo->pObj = $pObj;
133 $tstemplateinfo->pObj->MOD_SETTINGS = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData(array('includeTypoScriptFileContent' => TRUE), array(), 'web_ts');
134 $recData['sys_template'][$saveId] = $tstemplateinfo->processTemplateRowBeforeSaving($recData['sys_template'][$saveId]);
135 // Create new tce-object
136 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
137 $tce->stripslashes_values = 0;
138 // Initialize
139 $tce->start($recData, array());
140 // Saved the stuff
141 $tce->process_datamap();
142 // Clear the cache (note: currently only admin-users can clear the
143 // cache in tce_main.php)
144 $tce->clear_cacheCmd('all');
145 $savingsuccess = TRUE;
146 }
147 }
148 }
149 }
150 return $savingsuccess;
151 }
152
153 }