7e9121b8eac991e1dae2ee843f19792875551903
[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 (preg_match('/sysext\/tstemplate\/ts\/index\.php/', $_SERVER['SCRIPT_NAME'])) {
64
65 $t3editor = $this->getT3editor();
66
67 // insert javascript code in document header
68 $pObj->JScode .= $t3editor->getJavascriptCode($pObj);
69 }
70 }
71
72
73 /**
74 * Hook-function:
75 * called in typo3/sysext/tstemplate_info/class.tx_tstemplateinfo.php
76 *
77 * @param array $parameters
78 * @param tx_tstemplateinfo $pObj
79 */
80 public function postOutputProcessingHook($parameters, $pObj) {
81 $t3editor = $this->getT3editor();
82
83 if (!$t3editor->isEnabled()) {
84 return;
85 }
86
87 foreach (array('constants', 'config') as $type) {
88 if ($parameters['e'][$type]) {
89 $attributes = 'rows="' . $parameters['numberOfRows'] . '" ' .
90 'wrap="off" ' .
91 $pObj->pObj->doc->formWidthText(48, 'width:98%;height:60%', 'off');
92
93 $title = $GLOBALS['LANG']->getLL('template') . ' ' .
94 htmlspecialchars($parameters['tplRow']['title']) .
95 $GLOBALS['LANG']->getLL('delimiter') . ' ' .
96 $GLOBALS['LANG']->getLL($type);
97
98 $outCode = $t3editor->getCodeEditor(
99 'data[' . $type . ']',
100 'fixed-font enable-tab',
101 '$1', // will be replaced with the actual code later, see preg_replace below
102 $attributes,
103 $title,
104 array(
105 'pageId' => intval($pObj->pObj->id)
106 )
107 );
108 $parameters['theOutput'] = preg_replace(
109 '/\<textarea name="data\[' . $type . '\]".*\>([^\<]*)\<\/textarea\>/mi',
110 $outCode,
111 $parameters['theOutput']
112 );
113 }
114 }
115 }
116
117
118 /**
119 * Process saving request like in class.tstemplateinfo.php (TCE processing)
120 *
121 * @return boolean TRUE if successful
122 */
123 public function save($parameters, $pObj) {
124 $savingsuccess = FALSE;
125 if ($parameters['type'] == $this->ajaxSaveType) {
126
127 $pageId = t3lib_div::_GP('pageId');
128 if (!is_numeric($pageId) || $pageId < 1) {
129 return FALSE;
130 }
131
132 // if given use the requested template_uid
133 // if not, use the first template-record on the page (in this case there should only be one record!)
134 $set = t3lib_div::_GP('SET');
135 $template_uid = $set['templatesOnPage'] ? $set['templatesOnPage'] : 0;
136
137 $tmpl = t3lib_div::makeInstance('t3lib_tsparser_ext'); // Defined global here!
138 $tmpl->tt_track = 0; // Do not log time-performance information
139 $tmpl->init();
140
141 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
142 $tplRow = $tmpl->ext_getFirstTemplate($pageId, $template_uid);
143 $existTemplate = (is_array($tplRow) ? TRUE : FALSE);
144
145 if ($existTemplate) {
146 $saveId = ($tplRow['_ORIG_uid'] ? $tplRow['_ORIG_uid'] : $tplRow['uid']);
147
148 // Update template ?
149 $POST = t3lib_div::_POST();
150
151 if ($POST['submit']) {
152 require_once(PATH_t3lib . 'class.t3lib_tcemain.php');
153
154 // Set the data to be saved
155 $recData = array();
156
157 if (is_array($POST['data'])) {
158 foreach ($POST['data'] as $field => $val) {
159 switch ($field) {
160 case 'constants':
161 case 'config':
162 case 'title':
163 case 'sitetitle':
164 case 'description':
165 $recData['sys_template'][$saveId][$field] = $val;
166 break;
167 }
168 }
169 }
170 if (count($recData)) {
171
172 // process template row before saving
173 require_once t3lib_extMgm::extPath('tstemplate_info').'class.tx_tstemplateinfo.php';
174 $tstemplateinfo = t3lib_div::makeInstance('tx_tstemplateinfo'); /* @var $tstemplateinfo tx_tstemplateinfo */
175 // load the MOD_SETTINGS in order to check if the includeTypoScriptFileContent is set
176 $tstemplateinfo->pObj->MOD_SETTINGS = t3lib_BEfunc::getModuleData(
177 array('includeTypoScriptFileContent' => TRUE),
178 array(),
179 'web_ts'
180 );
181 $recData['sys_template'][$saveId] = $tstemplateinfo->processTemplateRowBeforeSaving($recData['sys_template'][$saveId]);
182
183 // Create new tce-object
184 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
185 $tce->stripslashes_values = 0;
186
187 // Initialize
188 $tce->start($recData, array());
189
190 // Saved the stuff
191 $tce->process_datamap();
192
193 // Clear the cache (note: currently only admin-users can clear the
194 // cache in tce_main.php)
195 $tce->clear_cacheCmd('all');
196
197 $savingsuccess = TRUE;
198 }
199 }
200 }
201 }
202 return $savingsuccess;
203 }
204 }
205 ?>