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