TYPO3 API  SVNRelease
class.tx_rtehtmlarea_acronym.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2008-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 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 /**
00025  * Acronym extension for htmlArea RTE
00026  *
00027  * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
00028  *
00029  * TYPO3 SVN ID: $Id: class.tx_rtehtmlarea_acronym.php 10120 2011-01-18 20:03:36Z ohader $
00030  *
00031  */
00032 class tx_rtehtmlarea_acronym extends tx_rtehtmlarea_api {
00033 
00034     protected $extensionKey = 'rtehtmlarea';        // The key of the extension that is extending htmlArea RTE
00035     protected $pluginName = 'Acronym';          // The name of the plugin registered by the extension
00036     protected $relativePathToLocallangFile = '';        // Path to this main locallang file of the extension relative to the extension dir.
00037     protected $relativePathToSkin = 'extensions/Acronym/skin/htmlarea.css';     // Path to the skin (css) file relative to the extension dir
00038     protected $htmlAreaRTE;                 // Reference to the invoking object
00039     protected $thisConfig;                  // Reference to RTE PageTSConfig
00040     protected $toolbar;                 // Reference to RTE toolbar array
00041     protected $LOCAL_LANG;                  // Frontend language array
00042 
00043     protected $pluginButtons = 'acronym';
00044     protected $convertToolbarForHtmlAreaArray = array (
00045         'acronym'   => 'Acronym',
00046         );
00047     protected $acronymIndex = 0;
00048     protected $abbreviationIndex = 0;
00049 
00050     public function main($parentObject) {
00051 
00052         return parent::main($parentObject) && t3lib_extMgm::isLoaded('static_info_tables');
00053     }
00054 
00055     /**
00056      * Return tranformed content
00057      *
00058      * @param   string      $content: The content that is about to be sent to the RTE
00059      *
00060      * @return  string      the transformed content
00061      */
00062     public function transformContent($content) {
00063 
00064             // <abbr> was not supported by IE before verison 7
00065         if ($this->htmlAreaRTE->client['browser'] == 'msie' && $this->htmlAreaRTE->client['version'] < 7) {
00066                 // change <abbr> to <acronym>
00067             $content = preg_replace('/<(\/?)abbr/i', "<$1acronym", $content);
00068         }
00069 
00070         return $content;
00071     }
00072 
00073     /**
00074      * Return JS configuration of the htmlArea plugins registered by the extension
00075      *
00076      * @param   integer     Relative id of the RTE editing area in the form
00077      *
00078      * @return  string      JS configuration for registered plugins, in this case, JS configuration of block elements
00079      *
00080      * The returned string will be a set of JS instructions defining the configuration that will be provided to the plugin(s)
00081      * Each of the instructions should be of the form:
00082      *  RTEarea['.$RTEcounter.']["buttons"]["button-id"]["property"] = "value";
00083      */
00084     public function buildJavascriptConfiguration($RTEcounter) {
00085 
00086         $registerRTEinJavascriptString = '';
00087         $button = 'acronym';
00088         if (in_array($button, $this->toolbar)) {
00089             if (!is_array($this->thisConfig['buttons.']) || !is_array($this->thisConfig['buttons.'][$button.'.'])) {
00090                     $registerRTEinJavascriptString .= '
00091             RTEarea['.$RTEcounter.']["buttons"]["'. $button .'"] = new Object();';
00092             }
00093             $registerRTEinJavascriptString .= '
00094             RTEarea['.$RTEcounter.'].buttons.'. $button .'.acronymUrl = "' . $this->htmlAreaRTE->writeTemporaryFile('', 'acronym_'.$this->htmlAreaRTE->contentLanguageUid, 'js', $this->buildJSAcronymArray($this->htmlAreaRTE->contentLanguageUid)) . '";';
00095 
00096                 // <abbr> was not supported by IE before version 7
00097             if ($this->htmlAreaRTE->client['browser'] == 'msie' && $this->htmlAreaRTE->client['version'] < 7) {
00098                 $this->abbreviationIndex = 0;
00099             }
00100             $registerRTEinJavascriptString .= '
00101             RTEarea['.$RTEcounter.'].buttons.'. $button .'.noAcronym = ' . ($this->acronymIndex ? 'false' : 'true') . ';
00102             RTEarea['.$RTEcounter.'].buttons.'. $button .'.noAbbr =  ' . ($this->abbreviationIndex ? 'false' : 'true') . ';';
00103         }
00104 
00105         return $registerRTEinJavascriptString;
00106     }
00107 
00108     /**
00109      * Return an acronym array for the Acronym plugin
00110      *
00111      * @return  string      acronym Javascript array
00112      */
00113     function buildJSAcronymArray($languageUid) {
00114 
00115         $charset = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] ? $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] : 'iso-8859-1';
00116         $button = 'acronym';
00117         $acronymArray = array();
00118         $abbrArray = array();
00119 
00120         $tableA = 'tx_rtehtmlarea_acronym';
00121         $tableB = 'static_languages';
00122         $fields = $tableA.'.type,' . $tableA . '.term,' . $tableA . '.acronym,' . $tableB . '.lg_iso_2,' . $tableB . '.lg_country_iso_2';
00123         $tableAB = $tableA . ' LEFT JOIN ' . $tableB . ' ON ' . $tableA . '.static_lang_isocode=' . $tableB . '.uid';
00124         $whereClause = '1=1';
00125             // Get all acronyms on pages to which the user has access
00126         $lockBeUserToDBmounts = isset($this->thisConfig['buttons.'][$button.'.']['lockBeUserToDBmounts']) ? $this->thisConfig['buttons.'][$button.'.']['lockBeUserToDBmounts'] : $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts'];
00127         if (!$GLOBALS['BE_USER']->isAdmin() && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts'] && $lockBeUserToDBmounts) {
00128                 // Temporarily setting alternative web browsing mounts
00129             $altMountPoints = trim($GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.altElementBrowserMountPoints'));
00130             if ($altMountPoints) {
00131                 $savedGroupDataWebmounts = $GLOBALS['BE_USER']->groupData['webmounts'];
00132                 $GLOBALS['BE_USER']->groupData['webmounts'] = implode(',', array_unique(t3lib_div::intExplode(',', $altMountPoints)));
00133                 $GLOBALS['WEBMOUNTS'] = $GLOBALS['BE_USER']->returnWebmounts();
00134             }
00135             $webMounts = $GLOBALS['BE_USER']->returnWebmounts();
00136             $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
00137             $recursive = isset($this->thisConfig['buttons.'][$button.'.']['recursive']) ? intval($this->thisConfig['buttons.'][$button.'.']['recursive']) : 0 ;
00138             if (trim($this->thisConfig['buttons.'][$button.'.']['pages'])) {
00139                 $pids = t3lib_div::trimExplode(',', $this->thisConfig['buttons.'][$button.'.']['pages'], 1);
00140                 foreach ($pids as $key => $val) {
00141                     if (!$GLOBALS['BE_USER']->isInWebMount($val, $perms_clause)) {
00142                         unset($pids[$key]);
00143                     }
00144                 }
00145             } else {
00146                 $pids = $webMounts;
00147             }
00148                 // Restoring webmounts
00149             if ($altMountPoints) {
00150                 $GLOBALS['BE_USER']->groupData['webmounts'] = $savedGroupDataWebmounts;
00151                 $GLOBALS['WEBMOUNTS'] = $GLOBALS['BE_USER']->returnWebmounts();
00152             }
00153             $queryGenerator = t3lib_div::makeInstance('t3lib_queryGenerator');
00154             foreach ($pids as $key => $val) {
00155                 if ($pageTree) {
00156                     $pageTreePrefix = ',';
00157                     }
00158                 $pageTree .= $pageTreePrefix . $queryGenerator->getTreeList($val, $recursive, $begin = 0, $perms_clause);
00159             }
00160             $whereClause .= ' AND '. $tableA . '.pid IN (' . $GLOBALS['TYPO3_DB']->fullQuoteStr(($pageTree ? $pageTree : ''), $tableA) . ')';
00161         }
00162             // Restrict to acronyms applicable to the language of current content element
00163                 if ($this->htmlAreaRTE->contentLanguageUid > -1) {
00164                         $whereClause .= ' AND (' . $tableA . '.sys_language_uid=' . $this->htmlAreaRTE->contentLanguageUid . ' OR ' . $tableA . '.sys_language_uid=-1) ';
00165                 }
00166             // Restrict to acronyms in certain languages
00167         if (is_array($this->thisConfig['buttons.']) && is_array($this->thisConfig['buttons.']['language.']) && isset($this->thisConfig['buttons.']['language.']['restrictToItems'])) {
00168             $languageList = implode("','", t3lib_div::trimExplode(',', $GLOBALS['TYPO3_DB']->fullQuoteStr(strtoupper($this->thisConfig['buttons.']['language.']['restrictToItems']), $tableB)));
00169             $whereClause .= ' AND '. $tableB . '.lg_iso_2 IN (' . $languageList . ') ';
00170         }
00171         $whereClause .= t3lib_BEfunc::BEenableFields($tableA);
00172         $whereClause .= t3lib_BEfunc::deleteClause($tableA);
00173         $whereClause .= t3lib_BEfunc::BEenableFields($tableB);
00174         $whereClause .= t3lib_BEfunc::deleteClause($tableB);
00175         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $tableAB, $whereClause);
00176         while ($acronymRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00177             $item = array('term' => $GLOBALS['LANG']->csConvObj->utf8_encode($acronymRow['term'], $charset), 'abbr' => $GLOBALS['LANG']->csConvObj->utf8_encode($acronymRow['acronym'], $charset), 'language' => $GLOBALS['LANG']->csConvObj->utf8_encode(strtolower($acronymRow['lg_iso_2']), $charset) . ($acronymRow['lg_country_iso_2'] ? ('-' . $acronymRow['lg_country_iso_2']) : ''));
00178             if ($acronymRow['type'] == 1) {
00179                 $acronymArray[] = $item;
00180             } else if ($acronymRow['type'] == 2) {
00181                 $abbrArray[] = $item;
00182             }
00183         }
00184         $this->acronymIndex = count($acronymArray);
00185         $this->abbreviationIndex = count($abbrArray);
00186         return json_encode(array('abbr' => $abbrArray, 'acronym' => $acronymArray));
00187     }
00188 }
00189 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/extensions/Acronym/class.tx_rtehtmlarea_acronym.php'])) {
00190     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/extensions/Acronym/class.tx_rtehtmlarea_acronym.php']);
00191 }
00192 ?>