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