774787038f18de47cb3e350f01f4bce15d440958
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / classes / class.tx_t3editor_hooks_tstemplateinfo.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-2010 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 *
34 * @var tx_t3editor
35 */
36 protected $t3editor = NULL;
37
38 /**
39 *
40 * @return tx_t3editor
41 */
42 protected function getT3editor() {
43 if ($this->t3editor == NULL) {
44 $this->t3editor = t3lib_div::makeInstance('tx_t3editor')
45 ->setMode(tx_t3editor::MODE_TYPOSCRIPT);
46 }
47 return $this->t3editor;
48 }
49
50 /**
51 * Hook-function: inject t3editor JavaScript code before the page is compiled
52 * called in typo3/template.php:startPage
53 *
54 * @param array $parameters
55 * @param template $pObj
56 */
57 public function preStartPageHook($parameters, $pObj) {
58 // enable editor in Template-Modul
59 if (preg_match('/sysext\/tstemplate\/ts\/index\.php/', $_SERVER['SCRIPT_NAME'])) {
60
61 $t3editor = $this->getT3editor();
62
63 // insert javascript code in document header
64 $pObj->JScode .= $t3editor->getJavascriptCode($pObj);
65 $pObj->loadJavascriptLib(t3lib_extmgm::extRelPath('t3editor') . 'res/jslib/tx_tstemplateinfo/tx_tstemplateinfo.js');
66 }
67 }
68
69
70 /**
71 * Hook-function:
72 * called in typo3/sysext/tstemplate_info/class.tx_tstemplateinfo.php
73 *
74 * @param array $parameters
75 * @param tx_tstemplateinfo $pObj
76 */
77 public function postOutputProcessingHook($parameters, $pObj) {
78 $t3editor = $this->getT3editor();
79
80 if (!$t3editor->isEnabled()) {
81 return;
82 }
83
84 foreach (array('constants', 'config') as $type) {
85 if ($parameters['e'][$type]) {
86 $attributes = 'rows="' . $parameters['numberOfRows'] . '" ' .
87 'wrap="off" ' .
88 $pObj->pObj->doc->formWidthText(48, 'width:98%;height:60%', 'off');
89
90 $title = $GLOBALS['LANG']->getLL('template') . ' ' .
91 htmlspecialchars($parameters['tplRow']['title']) .
92 $GLOBALS['LANG']->getLL('delimiter') . ' ' .
93 $GLOBALS['LANG']->getLL($type);
94
95 $outCode = $t3editor->getCodeEditor(
96 'data[' . $type . ']',
97 'fixed-font enable-tab',
98 t3lib_div::formatForTextarea($parameters['tplRow'][$type]),
99 $attributes,
100 $title,
101 array(
102 'pageId' => intval($pObj->pObj->id)
103 )
104 );
105 $parameters['theOutput'] = preg_replace(
106 '/\<textarea name="data\[' . $type . '\]".*\>([^\<]*)\<\/textarea\>/mi',
107 $outCode,
108 $parameters['theOutput']
109 );
110 }
111 }
112 }
113
114
115 /**
116 * Process saving request like in class.tstemplateinfo.php (TCE processing)
117 *
118 * @return boolean true if successful
119 */
120 public function save($parameters, $pObj) {
121 $savingsuccess = false;
122 if ($parameters['type'] == 'tx_tstemplateinfo') {
123
124 $pageId = t3lib_div::_GP('pageId');
125 if (!is_numeric($pageId) || $pageId < 1) {
126 return false;
127 }
128
129 // if given use the requested template_uid
130 // if not, use the first template-record on the page (in this case there should only be one record!)
131 $set = t3lib_div::_GP('SET');
132 $template_uid = $set['templatesOnPage'] ? $set['templatesOnPage'] : 0;
133
134 $tmpl = t3lib_div::makeInstance('t3lib_tsparser_ext'); // Defined global here!
135 $tmpl->tt_track = 0; // Do not log time-performance information
136 $tmpl->init();
137
138 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
139 $tplRow = $tmpl->ext_getFirstTemplate($pageId, $template_uid);
140 $existTemplate = (is_array($tplRow) ? true : false);
141
142 if ($existTemplate) {
143 $saveId = ($tplRow['_ORIG_uid'] ? $tplRow['_ORIG_uid'] : $tplRow['uid']);
144
145 // Update template ?
146 $POST = t3lib_div::_POST();
147
148 if ($POST['submit']) {
149 require_once(PATH_t3lib . 'class.t3lib_tcemain.php');
150
151 // Set the data to be saved
152 $recData = array();
153
154 if (is_array($POST['data'])) {
155 foreach ($POST['data'] as $field => $val) {
156 switch ($field) {
157 case 'constants':
158 case 'config':
159 case 'title':
160 case 'sitetitle':
161 case 'description':
162 $recData['sys_template'][$saveId][$field] = $val;
163 break;
164 }
165 }
166 }
167 if (count($recData)) {
168 // Create new tce-object
169 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
170 $tce->stripslashes_values = 0;
171
172 // Initialize
173 $tce->start($recData, array());
174
175 // Saved the stuff
176 $tce->process_datamap();
177
178 // Clear the cache (note: currently only admin-users can clear the
179 // cache in tce_main.php)
180 $tce->clear_cacheCmd('all');
181
182 $savingsuccess = true;
183 }
184 }
185 }
186 }
187 return $savingsuccess;
188 }
189 }
190
191
192 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/t3editor/classes/class.tx_t3editor_hooks_tstemplateinfo.php']) {
193 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/t3editor/classes/class.tx_t3editor_hooks_tstemplateinfo.php']);
194 }
195
196 ?>