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