TYPO3 API  SVNRelease
class.tx_rtehtmlarea_pi2.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2005-2011 Stanislas Rolland <typo3(arobas)sjbr.ca>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * Front end RTE based on htmlArea
00029  *
00030  * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
00031  *
00032  * $Id: class.tx_rtehtmlarea_pi2.php 10502 2011-02-20 04:18:31Z stan $  *
00033  */
00034 class tx_rtehtmlarea_pi2 extends tx_rtehtmlarea_base {
00035 
00036         // External:
00037     public $RTEWrapStyle = '';              // Alternative style for RTE wrapper <div> tag.
00038     public $RTEdivStyle = '';               // Alternative style for RTE <div> tag.
00039 
00040         // For the editor
00041     var $elementId;
00042     var $elementParts;
00043     var $tscPID;
00044     var $typeVal;
00045     var $thePid;
00046     var $RTEsetup = array();
00047     var $thisConfig = array();
00048     var $confValues;
00049     public $language;
00050     public $OutputCharset;
00051     var $specConf;
00052     var $LOCAL_LANG;
00053     /**
00054      * @var t3lib_PageRenderer
00055      */
00056     protected $pageRenderer;
00057 
00058     /**
00059      * Draws the RTE as an iframe
00060      *
00061      * @param   object      Reference to parent object, which is an instance of the TCEforms.
00062      * @param   string      The table name
00063      * @param   string      The field name
00064      * @param   array       The current row from which field is being rendered
00065      * @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.
00066      * @param   array       "special" configuration - what is found at position 4 in the types configuration of a field from record, parsed into an array.
00067      * @param   array       Configuration for RTEs; A mix between TSconfig and otherwise. Contains configuration for display, which buttons are enabled, additional transformation information etc.
00068      * @param   string      Record "type" field value.
00069      * @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!
00070      * @param   integer     PID value of record (true parent page id)
00071      * @return  string      HTML code for RTE!
00072      */
00073     function drawRTE($parentObject,$table,$field,$row,$PA,$specConf,$thisConfig,$RTEtypeVal,$RTErelPath,$thePidValue) {
00074         global $TSFE, $TYPO3_CONF_VARS, $TYPO3_DB;
00075 
00076         $this->TCEform = $parentObject;
00077         $this->client = $this->clientInfo();
00078         $this->typoVersion = t3lib_div::int_from_ver(TYPO3_version);
00079 
00080         /* =======================================
00081          * INIT THE EDITOR-SETTINGS
00082          * =======================================
00083          */
00084             // Get the path to this extension:
00085         $this->extHttpPath = t3lib_extMgm::siteRelPath($this->ID);
00086             // Get the site URL
00087         $this->siteURL = $GLOBALS['TSFE']->absRefPrefix ? $GLOBALS['TSFE']->absRefPrefix : '';
00088             // Get the host URL
00089         $this->hostURL = '';
00090             // Element ID + pid
00091         $this->elementId = $PA['itemFormElName'];
00092         $this->elementParts[0] = $table;
00093         $this->elementParts[1] = $row['uid'];
00094         $this->tscPID = $thePidValue;
00095         $this->thePid = $thePidValue;
00096 
00097             // Record "type" field value:
00098         $this->typeVal = $RTEtypeVal; // TCA "type" value for record
00099 
00100             // RTE configuration
00101         $pageTSConfig = $TSFE->getPagesTSconfig();
00102         if (is_array($pageTSConfig) && is_array($pageTSConfig['RTE.'])) {
00103             $this->RTEsetup = $pageTSConfig['RTE.'];
00104         }
00105 
00106         if (is_array($thisConfig) && !empty($thisConfig)) {
00107             $this->thisConfig = $thisConfig;
00108         } else if (is_array($this->RTEsetup['default.']) && is_array($this->RTEsetup['default.']['FE.'])) {
00109             $this->thisConfig = $this->RTEsetup['default.']['FE.'];
00110         }
00111 
00112             // Special configuration (line) and default extras:
00113         $this->specConf = $specConf;
00114 
00115         if ($this->thisConfig['forceHTTPS']) {
00116             $this->extHttpPath = preg_replace('/^(http|https)/', 'https', $this->extHttpPath);
00117             $this->siteURL = preg_replace('/^(http|https)/', 'https', $this->siteURL);
00118             $this->hostURL = preg_replace('/^(http|https)/', 'https', $this->hostURL);
00119         }
00120             // Register RTE windows:
00121         $this->TCEform->RTEwindows[] = $PA['itemFormElName'];
00122         $textAreaId = preg_replace('/[^a-zA-Z0-9_:.-]/', '_', $PA['itemFormElName']);
00123         $textAreaId = htmlspecialchars(preg_replace('/^[^a-zA-Z]/', 'x', $textAreaId)) . '_' . strval($this->TCEform->RTEcounter);
00124         /* =======================================
00125          * LANGUAGES & CHARACTER SETS
00126          * =======================================
00127          */
00128             // Language
00129         $TSFE->initLLvars();
00130         $this->language = $TSFE->lang;
00131         $this->LOCAL_LANG = t3lib_div::readLLfile('EXT:' . $this->ID . '/locallang.xml', $this->language);
00132         if ($this->language == 'default' || !$this->language)   {
00133             $this->language = 'en';
00134         }
00135         $this->contentLanguageUid = ($row['sys_language_uid'] > 0) ? $row['sys_language_uid'] : 0;
00136         if (t3lib_extMgm::isLoaded('static_info_tables')) {
00137             if ($this->contentLanguageUid) {
00138                 $tableA = 'sys_language';
00139                 $tableB = 'static_languages';
00140                 $languagesUidsList = $this->contentLanguageUid;
00141                 $selectFields = $tableA . '.uid,' . $tableB . '.lg_iso_2,' . $tableB . '.lg_country_iso_2,' . $tableB . '.lg_typo3';
00142                 $tableAB = $tableA . ' LEFT JOIN ' . $tableB . ' ON ' . $tableA . '.static_lang_isocode=' . $tableB . '.uid';
00143                 $whereClause = $tableA . '.uid IN (' . $languagesUidsList . ') ';
00144                 $whereClause .= t3lib_BEfunc::BEenableFields($tableA);
00145                 $whereClause .= t3lib_BEfunc::deleteClause($tableA);
00146                 $res = $TYPO3_DB->exec_SELECTquery($selectFields, $tableAB, $whereClause);
00147                 while($languageRow = $TYPO3_DB->sql_fetch_assoc($res)) {
00148                     $this->contentISOLanguage = strtolower(trim($languageRow['lg_iso_2']).(trim($languageRow['lg_country_iso_2'])?'_'.trim($languageRow['lg_country_iso_2']):''));
00149                     $this->contentTypo3Language = strtolower(trim($languageRow['lg_typo3']));
00150                 }
00151             } else {
00152                 $this->contentISOLanguage = $GLOBALS['TSFE']->sys_language_isocode ? $GLOBALS['TSFE']->sys_language_isocode : 'en';
00153                 $selectFields = 'lg_iso_2, lg_typo3';
00154                 $tableAB = 'static_languages';
00155                 $whereClause = 'lg_iso_2 = ' . $TYPO3_DB->fullQuoteStr(strtoupper($this->contentISOLanguage), $tableAB);
00156                 $res = $TYPO3_DB->exec_SELECTquery($selectFields, $tableAB, $whereClause);
00157                 while($languageRow = $TYPO3_DB->sql_fetch_assoc($res)) {
00158                     $this->contentTypo3Language = strtolower(trim($languageRow['lg_typo3']));
00159                 }
00160             }
00161         }
00162         $this->contentISOLanguage = $this->contentISOLanguage ? $this->contentISOLanguage : ($GLOBALS['TSFE']->sys_language_isocode ? $GLOBALS['TSFE']->sys_language_isocode : 'en');
00163         $this->contentTypo3Language = $this->contentTypo3Language ? $this->contentTypo3Language : $GLOBALS['TSFE']->lang;
00164         if ($this->contentTypo3Language == 'default') {
00165             $this->contentTypo3Language = 'en';
00166         }
00167 
00168             // Character set
00169         $this->charset = $TSFE->renderCharset;
00170         $this->OutputCharset  = $TSFE->metaCharset ? $TSFE->metaCharset : $TSFE->renderCharset;
00171 
00172             // Set the charset of the content
00173         $this->contentCharset = $TSFE->csConvObj->charSetArray[$this->contentTypo3Language];
00174         $this->contentCharset = $this->contentCharset ? $this->contentCharset : 'iso-8859-1';
00175         $this->contentCharset = trim($TSFE->config['config']['metaCharset']) ? trim($TSFE->config['config']['metaCharset']) : $this->contentCharset;
00176 
00177         /* =======================================
00178          * TOOLBAR CONFIGURATION
00179          * =======================================
00180          */
00181         $this->initializeToolbarConfiguration();
00182 
00183         /* =======================================
00184          * SET STYLES
00185          * =======================================
00186          */
00187 
00188         $width = 460+($this->TCEform->docLarge ? 150 : 0);
00189         if (isset($this->thisConfig['RTEWidthOverride'])) {
00190             if (strstr($this->thisConfig['RTEWidthOverride'], '%')) {
00191                 if ($this->client['browser'] != 'msie') {
00192                     $width = (intval($this->thisConfig['RTEWidthOverride']) > 0) ? $this->thisConfig['RTEWidthOverride'] : '100%';
00193                 }
00194             } else {
00195                 $width = (intval($this->thisConfig['RTEWidthOverride']) > 0) ? intval($this->thisConfig['RTEWidthOverride']) : $width;
00196             }
00197         }
00198         $RTEWidth = strstr($width, '%') ? $width : $width . 'px';
00199         $editorWrapWidth = strstr($width, '%') ? $width :  ($width+2) . 'px';
00200         $height = 380;
00201         $RTEHeightOverride = intval($this->thisConfig['RTEHeightOverride']);
00202         $height = ($RTEHeightOverride > 0) ? $RTEHeightOverride : $height;
00203         $RTEHeight = $height . 'px';
00204         $editorWrapHeight = ($height+2) . 'px';
00205         $this->RTEWrapStyle = $this->RTEWrapStyle ? $this->RTEWrapStyle : ($this->RTEdivStyle ? $this->RTEdivStyle : ('height:' . $editorWrapHeight . '; width:'. $editorWrapWidth . ';'));
00206         $this->RTEdivStyle = $this->RTEdivStyle ? $this->RTEdivStyle : 'position:relative; left:0px; top:0px; height:' . $RTEHeight . '; width:'.$RTEWidth.'; border: 1px solid black;';
00207 
00208         /* =======================================
00209          * LOAD JS, CSS and more
00210          * =======================================
00211          */
00212         $pageRenderer = $this->getPageRenderer();
00213             // Preloading the pageStyle and including RTE skin stylesheets
00214         $this->addPageStyle();
00215         $this->addSkin();
00216             // Re-initialize the scripts array so that only the cumulative set of plugins of the last RTE on the page is used
00217         $this->cumulativeScripts[$this->TCEform->RTEcounter] = array();
00218         $this->includeScriptFiles($this->TCEform->RTEcounter);
00219         $this->buildJSMainLangFile($this->TCEform->RTEcounter);
00220             // Register RTE in JS:
00221         $this->TCEform->additionalJS_post[] = $this->wrapCDATA($this->registerRTEinJS($this->TCEform->RTEcounter, '', '', '',$textAreaId));
00222             // Set the save option for the RTE:
00223         $this->TCEform->additionalJS_submit[] = $this->setSaveRTE($this->TCEform->RTEcounter, $this->TCEform->formName, $textAreaId);
00224             // Loading ExtJs JavaScript files and inline code, if not configured in TS setup
00225         if (!$GLOBALS['TSFE']->isINTincScript() || !is_array($GLOBALS['TSFE']->pSetup['javascriptLibs.']['ExtJs.'])) {
00226             $pageRenderer->loadExtJs();
00227             $pageRenderer->enableExtJSQuickTips();
00228             if (!$GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->ID]['enableCompressedScripts']) {
00229                 $pageRenderer->enableExtJsDebug();
00230             }
00231         }
00232         $pageRenderer->addCssFile($this->siteURL . 't3lib/js/extjs/ux/resize.css');
00233         $pageRenderer->addJsFile($this->siteURL . 't3lib/js/extjs/ux/ext.resizable.js');
00234         $pageRenderer->addJsFile($this->siteURL . '/t3lib/js/extjs/notifications.js');
00235         if ($this->TCEform->RTEcounter == 1) {
00236             $this->TCEform->additionalJS_pre['rtehtmlarea-loadJScode'] = $this->wrapCDATA($this->loadJScode($this->TCEform->RTEcounter));
00237         }
00238         $this->TCEform->additionalJS_initial = $this->loadJSfiles($this->TCEform->RTEcounter);
00239         if ($GLOBALS['TSFE']->isINTincScript()) {
00240             $GLOBALS['TSFE']->additionalHeaderData['rtehtmlarea'] = $pageRenderer->render();
00241         }
00242         /* =======================================
00243          * DRAW THE EDITOR
00244          * =======================================
00245          */
00246             // Transform value:
00247         $value = $this->transformContent('rte',$PA['itemFormElValue'],$table,$field,$row,$specConf,$thisConfig,$RTErelPath,$thePidValue);
00248 
00249             // Further content transformation by registered plugins
00250         foreach ($this->registeredPlugins as $pluginId => $plugin) {
00251             if ($this->isPluginEnabled($pluginId) && method_exists($plugin, "transformContent")) {
00252                 $value = $plugin->transformContent($value);
00253             }
00254         }
00255             // draw the textarea
00256         $item = $this->triggerField($PA['itemFormElName']).'
00257             <div id="pleasewait' . $textAreaId . '" class="pleasewait" style="display: block;" >' . $TSFE->csConvObj->conv($TSFE->getLLL('Please wait',$this->LOCAL_LANG), $this->charset, $TSFE->renderCharset) . '</div>
00258             <div id="editorWrap' . $textAreaId . '" class="editorWrap" style="visibility: hidden; '. htmlspecialchars($this->RTEWrapStyle). '">
00259             <textarea id="RTEarea' . $textAreaId . '" name="'.htmlspecialchars($PA['itemFormElName']).'" rows="0" cols="0" style="'.htmlspecialchars($this->RTEdivStyle).'">'.t3lib_div::formatForTextarea($value).'</textarea>
00260             </div>' . LF;
00261         return $item;
00262     }
00263     /**
00264      * Add style sheet file to document header
00265      *
00266      * @param   string      $key: some key identifying the style sheet
00267      * @param   string      $href: uri to the style sheet file
00268      * @param   string      $title: value for the title attribute of the link element
00269      * @return  string      $relation: value for the rel attribute of the link element
00270      * @return  void
00271      */
00272     protected function addStyleSheet($key, $href, $title='', $relation='stylesheet') {
00273         $this->pageRenderer->addCssFile($href, $relation, 'screen', $title);
00274     }
00275     /**
00276      * Return the JS-Code for copy the HTML-Code from the editor in the hidden input field.
00277      * This is for submit function from the form.
00278      *
00279      * @param   integer     $RTEcounter: The index number of the RTE editing area.
00280      * @param   string      $form: the name of the form
00281      * @param   string      $textareaId: the id of the textarea
00282      *
00283      * @return  string      the JS-Code
00284      */
00285     function setSaveRTE($RTEcounter, $form, $textareaId) {
00286         return '
00287         if (RTEarea[\'' . $textareaId . '\'] && !RTEarea[\'' . $textareaId . '\'].deleted) {
00288             var field = document.getElementById(\'RTEarea' . $textareaId . '\');
00289             if (field && field.nodeName.toLowerCase() == \'textarea\') {
00290                 field.value = RTEarea[\'' . $textareaId . '\'][\'editor\'].getHTML();
00291             }
00292         } else {
00293             OK = 0;
00294         }';
00295     }
00296     /**
00297      * Gets instance of PageRenderer
00298      *
00299      * @return  t3lib_PageRenderer
00300      */
00301     public function getPageRenderer() {
00302         if (!isset($this->pageRenderer)) {
00303             if ($GLOBALS['TSFE']->isINTincScript()) {
00304                     // We use an instance of t3lib_PageRenderer to render additional header data
00305                     // because this script is invoked after header has been rendered by $GLOBALS['TSFE']->getPageRenderer()
00306                 $this->pageRenderer = t3lib_div::makeInstance('t3lib_PageRenderer');
00307                 $this->pageRenderer->setTemplateFile($this->extHttpPath . 'templates/rtehtmlarea_pageheader_frontend.html');
00308             } else {
00309                 $this->pageRenderer = $GLOBALS['TSFE']->getPageRenderer();
00310             }
00311             $this->pageRenderer->setBackPath(TYPO3_mainDir);
00312         }
00313         return $this->pageRenderer;
00314     }
00315     /**
00316      * Wrap input string in CDATA enclosure
00317      *
00318      * @param   string      $string: input to be wrapped
00319      *
00320      * @return  string      wrapped string
00321      */
00322     public function wrapCDATA ($string) {
00323         return implode(LF,
00324             array(
00325                 '',
00326                 '/*<![CDATA[*/',
00327                 $string,
00328                 '/*]]>*/'
00329             )
00330         );
00331     }
00332 }
00333 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/pi2/class.tx_rtehtmlarea_pi2.php'])) {
00334     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/pi2/class.tx_rtehtmlarea_pi2.php']);
00335 }
00336 ?>