Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / pi2 / class.tx_rtehtmlarea_pi2.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2005-2011 Stanislas Rolland <typo3(arobas)sjbr.ca>
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 * Front end RTE based on htmlArea
29 *
30 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
31 *
32 * $Id$ *
33 */
34 class tx_rtehtmlarea_pi2 extends tx_rtehtmlarea_base {
35
36 // External:
37 public $RTEWrapStyle = ''; // Alternative style for RTE wrapper <div> tag.
38 public $RTEdivStyle = ''; // Alternative style for RTE <div> tag.
39
40 // For the editor
41 var $elementId;
42 var $elementParts;
43 var $tscPID;
44 var $typeVal;
45 var $thePid;
46 var $RTEsetup = array();
47 var $thisConfig = array();
48 var $confValues;
49 public $language;
50 public $OutputCharset;
51 var $specConf;
52 var $LOCAL_LANG;
53 /**
54 * @var t3lib_PageRenderer
55 */
56 protected $pageRenderer;
57
58 /**
59 * Draws the RTE as an iframe
60 *
61 * @param object Reference to parent object, which is an instance of the TCEforms.
62 * @param string The table name
63 * @param string The field name
64 * @param array The current row from which field is being rendered
65 * @param array Array of standard content for rendering form fields from TCEforms. See TCEforms for details on this. Includes for instance the value and the form field name, java script actions and more.
66 * @param array "special" configuration - what is found at position 4 in the types configuration of a field from record, parsed into an array.
67 * @param array Configuration for RTEs; A mix between TSconfig and otherwise. Contains configuration for display, which buttons are enabled, additional transformation information etc.
68 * @param string Record "type" field value.
69 * @param string Relative path for images/links in RTE; this is used when the RTE edits content from static files where the path of such media has to be transformed forth and back!
70 * @param integer PID value of record (true parent page id)
71 * @return string HTML code for RTE!
72 */
73 function drawRTE($parentObject,$table,$field,$row,$PA,$specConf,$thisConfig,$RTEtypeVal,$RTErelPath,$thePidValue) {
74 global $TSFE, $TYPO3_CONF_VARS, $TYPO3_DB;
75
76 $this->TCEform = $parentObject;
77 $this->client = $this->clientInfo();
78 $this->typoVersion = t3lib_div::int_from_ver(TYPO3_version);
79
80 /* =======================================
81 * INIT THE EDITOR-SETTINGS
82 * =======================================
83 */
84 // Get the path to this extension:
85 $this->extHttpPath = t3lib_extMgm::siteRelPath($this->ID);
86 // Get the site URL
87 $this->siteURL = $GLOBALS['TSFE']->absRefPrefix ? $GLOBALS['TSFE']->absRefPrefix : '';
88 // Get the host URL
89 $this->hostURL = '';
90 // Element ID + pid
91 $this->elementId = $PA['itemFormElName'];
92 $this->elementParts[0] = $table;
93 $this->elementParts[1] = $row['uid'];
94 $this->tscPID = $thePidValue;
95 $this->thePid = $thePidValue;
96
97 // Record "type" field value:
98 $this->typeVal = $RTEtypeVal; // TCA "type" value for record
99
100 // RTE configuration
101 $pageTSConfig = $TSFE->getPagesTSconfig();
102 if (is_array($pageTSConfig) && is_array($pageTSConfig['RTE.'])) {
103 $this->RTEsetup = $pageTSConfig['RTE.'];
104 }
105
106 if (is_array($thisConfig) && !empty($thisConfig)) {
107 $this->thisConfig = $thisConfig;
108 } else if (is_array($this->RTEsetup['default.']) && is_array($this->RTEsetup['default.']['FE.'])) {
109 $this->thisConfig = $this->RTEsetup['default.']['FE.'];
110 }
111
112 // Special configuration (line) and default extras:
113 $this->specConf = $specConf;
114
115 if ($this->thisConfig['forceHTTPS']) {
116 $this->extHttpPath = preg_replace('/^(http|https)/', 'https', $this->extHttpPath);
117 $this->siteURL = preg_replace('/^(http|https)/', 'https', $this->siteURL);
118 $this->hostURL = preg_replace('/^(http|https)/', 'https', $this->hostURL);
119 }
120 /* =======================================
121 * LANGUAGES & CHARACTER SETS
122 * =======================================
123 */
124 // Language
125 $TSFE->initLLvars();
126 $this->language = $TSFE->lang;
127 $this->LOCAL_LANG = t3lib_div::readLLfile('EXT:' . $this->ID . '/locallang.xml', $this->language);
128 if ($this->language == 'default' || !$this->language) {
129 $this->language = 'en';
130 }
131 $this->contentLanguageUid = ($row['sys_language_uid'] > 0) ? $row['sys_language_uid'] : 0;
132 if (t3lib_extMgm::isLoaded('static_info_tables')) {
133 if ($this->contentLanguageUid) {
134 $tableA = 'sys_language';
135 $tableB = 'static_languages';
136 $languagesUidsList = $this->contentLanguageUid;
137 $selectFields = $tableA . '.uid,' . $tableB . '.lg_iso_2,' . $tableB . '.lg_country_iso_2,' . $tableB . '.lg_typo3';
138 $tableAB = $tableA . ' LEFT JOIN ' . $tableB . ' ON ' . $tableA . '.static_lang_isocode=' . $tableB . '.uid';
139 $whereClause = $tableA . '.uid IN (' . $languagesUidsList . ') ';
140 $whereClause .= t3lib_BEfunc::BEenableFields($tableA);
141 $whereClause .= t3lib_BEfunc::deleteClause($tableA);
142 $res = $TYPO3_DB->exec_SELECTquery($selectFields, $tableAB, $whereClause);
143 while($languageRow = $TYPO3_DB->sql_fetch_assoc($res)) {
144 $this->contentISOLanguage = strtolower(trim($languageRow['lg_iso_2']).(trim($languageRow['lg_country_iso_2'])?'_'.trim($languageRow['lg_country_iso_2']):''));
145 $this->contentTypo3Language = strtolower(trim($languageRow['lg_typo3']));
146 }
147 } else {
148 $this->contentISOLanguage = $GLOBALS['TSFE']->sys_language_isocode ? $GLOBALS['TSFE']->sys_language_isocode : 'en';
149 $selectFields = 'lg_iso_2, lg_typo3';
150 $tableAB = 'static_languages';
151 $whereClause = 'lg_iso_2 = ' . $TYPO3_DB->fullQuoteStr(strtoupper($this->contentISOLanguage), $tableAB);
152 $res = $TYPO3_DB->exec_SELECTquery($selectFields, $tableAB, $whereClause);
153 while($languageRow = $TYPO3_DB->sql_fetch_assoc($res)) {
154 $this->contentTypo3Language = strtolower(trim($languageRow['lg_typo3']));
155 }
156 }
157 }
158 $this->contentISOLanguage = $this->contentISOLanguage ? $this->contentISOLanguage : ($GLOBALS['TSFE']->sys_language_isocode ? $GLOBALS['TSFE']->sys_language_isocode : 'en');
159 $this->contentTypo3Language = $this->contentTypo3Language ? $this->contentTypo3Language : $GLOBALS['TSFE']->lang;
160 if ($this->contentTypo3Language == 'default') {
161 $this->contentTypo3Language = 'en';
162 }
163
164 // Character set
165 $this->charset = $TSFE->renderCharset;
166 $this->OutputCharset = $TSFE->metaCharset ? $TSFE->metaCharset : $TSFE->renderCharset;
167
168 // Set the charset of the content
169 $this->contentCharset = $TSFE->csConvObj->charSetArray[$this->contentTypo3Language];
170 $this->contentCharset = $this->contentCharset ? $this->contentCharset : 'iso-8859-1';
171 $this->contentCharset = trim($TSFE->config['config']['metaCharset']) ? trim($TSFE->config['config']['metaCharset']) : $this->contentCharset;
172
173 /* =======================================
174 * TOOLBAR CONFIGURATION
175 * =======================================
176 */
177 $this->initializeToolbarConfiguration();
178
179 /* =======================================
180 * SET STYLES
181 * =======================================
182 */
183
184 $width = 460+($this->TCEform->docLarge ? 150 : 0);
185 if (isset($this->thisConfig['RTEWidthOverride'])) {
186 if (strstr($this->thisConfig['RTEWidthOverride'], '%')) {
187 if ($this->client['browser'] != 'msie') {
188 $width = (intval($this->thisConfig['RTEWidthOverride']) > 0) ? $this->thisConfig['RTEWidthOverride'] : '100%';
189 }
190 } else {
191 $width = (intval($this->thisConfig['RTEWidthOverride']) > 0) ? intval($this->thisConfig['RTEWidthOverride']) : $width;
192 }
193 }
194 $RTEWidth = strstr($width, '%') ? $width : $width . 'px';
195 $editorWrapWidth = strstr($width, '%') ? $width : ($width+2) . 'px';
196 $height = 380;
197 $RTEHeightOverride = intval($this->thisConfig['RTEHeightOverride']);
198 $height = ($RTEHeightOverride > 0) ? $RTEHeightOverride : $height;
199 $RTEHeight = $height . 'px';
200 $editorWrapHeight = ($height+2) . 'px';
201 $this->RTEWrapStyle = $this->RTEWrapStyle ? $this->RTEWrapStyle : ($this->RTEdivStyle ? $this->RTEdivStyle : ('height:' . $editorWrapHeight . '; width:'. $editorWrapWidth . ';'));
202 $this->RTEdivStyle = $this->RTEdivStyle ? $this->RTEdivStyle : 'position:relative; left:0px; top:0px; height:' . $RTEHeight . '; width:'.$RTEWidth.'; border: 1px solid black;';
203
204 /* =======================================
205 * LOAD JS, CSS and more
206 * =======================================
207 */
208 $pageRenderer = $this->getPageRenderer();
209 // Preloading the pageStyle and including RTE skin stylesheets
210 $this->addPageStyle();
211 $this->addSkin();
212 // Loading ExtJs JavaScript files and inline code, if not configured in TS setup
213 if (!$GLOBALS['TSFE']->isINTincScript() || !is_array($GLOBALS['TSFE']->pSetup['javascriptLibs.']['ExtJs.'])) {
214 $pageRenderer->loadExtJs();
215 $pageRenderer->enableExtJSQuickTips();
216 if (!$GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->ID]['enableCompressedScripts']) {
217 $pageRenderer->enableExtJsDebug();
218 }
219 }
220 $pageRenderer->addCssFile($this->siteURL . 't3lib/js/extjs/ux/resize.css');
221 $pageRenderer->addJsFile($this->siteURL . 't3lib/js/extjs/ux/ext.resizable.js');
222 $pageRenderer->addJsFile($this->siteURL . '/t3lib/js/extjs/notifications.js');
223 if ($this->TCEform->RTEcounter == 1) {
224 $this->TCEform->additionalJS_pre['rtehtmlarea-loadJScode'] = $this->wrapCDATA($this->loadJScode($this->TCEform->RTEcounter));
225 }
226 $this->TCEform->additionalJS_initial = $this->loadJSfiles($this->TCEform->RTEcounter);
227 if ($GLOBALS['TSFE']->isINTincScript()) {
228 $GLOBALS['TSFE']->additionalHeaderData['rtehtmlarea'] = $pageRenderer->render();
229 }
230 /* =======================================
231 * DRAW THE EDITOR
232 * =======================================
233 */
234 // Transform value:
235 $value = $this->transformContent('rte',$PA['itemFormElValue'],$table,$field,$row,$specConf,$thisConfig,$RTErelPath,$thePidValue);
236
237 // Further content transformation by registered plugins
238 foreach ($this->registeredPlugins as $pluginId => $plugin) {
239 if ($this->isPluginEnabled($pluginId) && method_exists($plugin, "transformContent")) {
240 $value = $plugin->transformContent($value);
241 }
242 }
243
244 // Register RTE windows:
245 $this->TCEform->RTEwindows[] = $PA['itemFormElName'];
246 $textAreaId = preg_replace('/[^a-zA-Z0-9_:.-]/', '_', $PA['itemFormElName']);
247 $textAreaId = htmlspecialchars(preg_replace('/^[^a-zA-Z]/', 'x', $textAreaId)) . '_' . strval($this->TCEform->RTEcounter);
248
249 // Register RTE in JS:
250 $this->TCEform->additionalJS_post[] = $this->wrapCDATA($this->registerRTEinJS($this->TCEform->RTEcounter, '', '', '',$textAreaId));
251
252 // Set the save option for the RTE:
253 $this->TCEform->additionalJS_submit[] = $this->setSaveRTE($this->TCEform->RTEcounter, $this->TCEform->formName, $textAreaId);
254
255 // draw the textarea
256 $item = $this->triggerField($PA['itemFormElName']).'
257 <div id="pleasewait' . $textAreaId . '" class="pleasewait" style="display: block;" >' . $TSFE->csConvObj->conv($TSFE->getLLL('Please wait',$this->LOCAL_LANG), $this->charset, $TSFE->renderCharset) . '</div>
258 <div id="editorWrap' . $textAreaId . '" class="editorWrap" style="visibility: hidden; '. htmlspecialchars($this->RTEWrapStyle). '">
259 <textarea id="RTEarea' . $textAreaId . '" name="'.htmlspecialchars($PA['itemFormElName']).'" rows="0" cols="0" style="'.htmlspecialchars($this->RTEdivStyle).'">'.t3lib_div::formatForTextarea($value).'</textarea>
260 </div>' . ($TYPO3_CONF_VARS['EXTCONF'][$this->ID]['enableDebugMode'] ? '<div id="HTMLAreaLog"></div>' : '') . '
261 ';
262 return $item;
263 }
264 /**
265 * Add style sheet file to document header
266 *
267 * @param string $key: some key identifying the style sheet
268 * @param string $href: uri to the style sheet file
269 * @param string $title: value for the title attribute of the link element
270 * @return string $relation: value for the rel attribute of the link element
271 * @return void
272 */
273 protected function addStyleSheet($key, $href, $title='', $relation='stylesheet') {
274 $this->pageRenderer->addCssFile($href, $relation, 'screen', $title);
275 }
276 /**
277 * Return the JS-Code for copy the HTML-Code from the editor in the hidden input field.
278 * This is for submit function from the form.
279 *
280 * @param integer $RTEcounter: The index number of the RTE editing area.
281 * @param string $form: the name of the form
282 * @param string $textareaId: the id of the textarea
283 *
284 * @return string the JS-Code
285 */
286 function setSaveRTE($RTEcounter, $form, $textareaId) {
287 return '
288 if (RTEarea[\'' . $textareaId . '\'] && !RTEarea[\'' . $textareaId . '\'].deleted) {
289 var field = document.getElementById(\'RTEarea' . $textareaId . '\');
290 if (field && field.nodeName.toLowerCase() == \'textarea\') {
291 field.value = RTEarea[\'' . $textareaId . '\'][\'editor\'].getHTML();
292 }
293 } else {
294 OK = 0;
295 }';
296 }
297 /**
298 * Gets instance of PageRenderer
299 *
300 * @return t3lib_PageRenderer
301 */
302 public function getPageRenderer() {
303 if (!isset($this->pageRenderer)) {
304 if ($GLOBALS['TSFE']->isINTincScript()) {
305 // We use an instance of t3lib_PageRenderer to render additional header data
306 // because this script is invoked after header has been rendered by $GLOBALS['TSFE']->getPageRenderer()
307 $this->pageRenderer = t3lib_div::makeInstance('t3lib_PageRenderer');
308 $this->pageRenderer->setTemplateFile($this->extHttpPath . 'templates/rtehtmlarea_pageheader_frontend.html');
309 } else {
310 $this->pageRenderer = $GLOBALS['TSFE']->getPageRenderer();
311 }
312 $this->pageRenderer->setBackPath(TYPO3_mainDir);
313 }
314 return $this->pageRenderer;
315 }
316 /**
317 * Wrap input string in CDATA enclosure
318 *
319 * @param string $string: input to be wrapped
320 *
321 * @return string wrapped string
322 */
323 public function wrapCDATA ($string) {
324 return implode(LF,
325 array(
326 '',
327 '/*<![CDATA[*/',
328 $string,
329 '/*]]>*/'
330 )
331 );
332 }
333 }
334 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/pi2/class.tx_rtehtmlarea_pi2.php'])) {
335 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/pi2/class.tx_rtehtmlarea_pi2.php']);
336 }
337 ?>