[BUGFIX] t3editor broken after tstemplate refactoring
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / classes / class.tx_t3editor_hooks_tstemplateinfo.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-2011 Tobias Liebig <mail_typo3@etobi.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 require_once(t3lib_extMgm::extPath('t3editor', 'classes/class.tx_t3editor.php'));
29
30 class tx_t3editor_hooks_tstemplateinfo {
31
32 /**
33 * @var tx_t3editor
34 */
35 protected $t3editor = NULL;
36
37 /**
38 * @var string
39 */
40 protected $ajaxSaveType = 'tx_tstemplateinfo';
41
42 /**
43 * @return tx_t3editor
44 */
45 protected function getT3editor() {
46 if ($this->t3editor == NULL) {
47 $this->t3editor = t3lib_div::makeInstance('tx_t3editor')
48 ->setMode(tx_t3editor::MODE_TYPOSCRIPT)
49 ->setAjaxSaveType($this->ajaxSaveType);
50 }
51 return $this->t3editor;
52 }
53
54 /**
55 * Hook-function: inject t3editor JavaScript code before the page is compiled
56 * called in typo3/template.php:startPage
57 *
58 * @param array $parameters
59 * @param template $pObj
60 */
61 public function preStartPageHook($parameters, $pObj) {
62 // enable editor in Template-Modul
63 if (t3lib_div::_GET('M') === 'web_ts') {
64 $t3editor = $this->getT3editor();
65 // insert javascript code in document header
66 $pObj->JScode .= $t3editor->getJavascriptCode($pObj);
67 }
68 }
69
70
71 /**
72 * Hook-function:
73 * called in typo3/sysext/tstemplate_info/class.tx_tstemplateinfo.php
74 *
75 * @param array $parameters
76 * @param tx_tstemplateinfo $pObj
77 */
78 public function postOutputProcessingHook($parameters, $pObj) {
79 $t3editor = $this->getT3editor();
80
81 if (!$t3editor->isEnabled()) {
82 return;
83 }
84
85 foreach (array('constants', 'config') as $type) {
86 if ($parameters['e'][$type]) {
87 $attributes = 'rows="' . $parameters['numberOfRows'] . '" ' .
88 'wrap="off" ' .
89 $pObj->pObj->doc->formWidthText(48, 'width:98%;height:60%', 'off');
90
91 $title = $GLOBALS['LANG']->getLL('template') . ' ' .
92 htmlspecialchars($parameters['tplRow']['title']) .
93 $GLOBALS['LANG']->getLL('delimiter') . ' ' .
94 $GLOBALS['LANG']->getLL($type);
95
96 $outCode = $t3editor->getCodeEditor(
97 'data[' . $type . ']',
98 'fixed-font enable-tab',
99 '$1', // will be replaced with the actual code later, see preg_replace below
100 $attributes,
101 $title,
102 array(
103 'pageId' => intval($pObj->pObj->id)
104 )
105 );
106 $parameters['theOutput'] = preg_replace(
107 '/\<textarea name="data\[' . $type . '\]".*\>([^\<]*)\<\/textarea\>/mi',
108 $outCode,
109 $parameters['theOutput']
110 );
111 }
112 }
113 }
114
115
116 /**
117 * Process saving request like in class.tstemplateinfo.php (TCE processing)
118 *
119 * @return boolean TRUE if successful
120 */
121 public function save($parameters, $pObj) {
122 $savingsuccess = FALSE;
123 if ($parameters['type'] == $this->ajaxSaveType) {
124
125 $pageId = t3lib_div::_GP('pageId');
126 if (!is_numeric($pageId) || $pageId < 1) {
127 return FALSE;
128 }
129
130 // if given use the requested template_uid
131 // if not, use the first template-record on the page (in this case there should only be one record!)
132 $set = t3lib_div::_GP('SET');
133 $template_uid = $set['templatesOnPage'] ? $set['templatesOnPage'] : 0;
134
135 $tmpl = t3lib_div::makeInstance('t3lib_tsparser_ext'); // Defined global here!
136 $tmpl->tt_track = 0; // Do not log time-performance information
137 $tmpl->init();
138
139 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
140 $tplRow = $tmpl->ext_getFirstTemplate($pageId, $template_uid);
141 $existTemplate = (is_array($tplRow) ? TRUE : FALSE);
142
143 if ($existTemplate) {
144 $saveId = ($tplRow['_ORIG_uid'] ? $tplRow['_ORIG_uid'] : $tplRow['uid']);
145
146 // Update template ?
147 $POST = t3lib_div::_POST();
148
149 if ($POST['submit']) {
150 require_once(PATH_t3lib . 'class.t3lib_tcemain.php');
151
152 // Set the data to be saved
153 $recData = array();
154
155 if (is_array($POST['data'])) {
156 foreach ($POST['data'] as $field => $val) {
157 switch ($field) {
158 case 'constants':
159 case 'config':
160 case 'title':
161 case 'sitetitle':
162 case 'description':
163 $recData['sys_template'][$saveId][$field] = $val;
164 break;
165 }
166 }
167 }
168 if (count($recData)) {
169
170 // process template row before saving
171 require_once t3lib_extMgm::extPath('tstemplate_info').'class.tx_tstemplateinfo.php';
172 $tstemplateinfo = t3lib_div::makeInstance('tx_tstemplateinfo'); /* @var $tstemplateinfo tx_tstemplateinfo */
173 // load the MOD_SETTINGS in order to check if the includeTypoScriptFileContent is set
174 $tstemplateinfo->pObj->MOD_SETTINGS = t3lib_BEfunc::getModuleData(
175 array('includeTypoScriptFileContent' => TRUE),
176 array(),
177 'web_ts'
178 );
179 $recData['sys_template'][$saveId] = $tstemplateinfo->processTemplateRowBeforeSaving($recData['sys_template'][$saveId]);
180
181 // Create new tce-object
182 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
183 $tce->stripslashes_values = 0;
184
185 // Initialize
186 $tce->start($recData, array());
187
188 // Saved the stuff
189 $tce->process_datamap();
190
191 // Clear the cache (note: currently only admin-users can clear the
192 // cache in tce_main.php)
193 $tce->clear_cacheCmd('all');
194
195 $savingsuccess = TRUE;
196 }
197 }
198 }
199 }
200 return $savingsuccess;
201 }
202 }
203 ?>