Follow-up for feature #10416: htmlArea RTE: Add configuration options for the acronym...
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / extensions / Acronym / class.tx_rtehtmlarea_acronym.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008-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 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24 /**
25 * Acronym extension for htmlArea RTE
26 *
27 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
28 *
29 * TYPO3 SVN ID: $Id$
30 *
31 */
32
33 require_once(t3lib_extMgm::extPath('rtehtmlarea').'class.tx_rtehtmlareaapi.php');
34 require_once(PATH_t3lib.'class.t3lib_querygenerator.php');
35
36 class tx_rtehtmlarea_acronym extends tx_rtehtmlareaapi {
37
38 protected $extensionKey = 'rtehtmlarea'; // The key of the extension that is extending htmlArea RTE
39 protected $pluginName = 'Acronym'; // The name of the plugin registered by the extension
40 protected $relativePathToLocallangFile = ''; // Path to this main locallang file of the extension relative to the extension dir.
41 protected $relativePathToSkin = 'extensions/Acronym/skin/htmlarea.css'; // Path to the skin (css) file relative to the extension dir
42 protected $htmlAreaRTE; // Reference to the invoking object
43 protected $thisConfig; // Reference to RTE PageTSConfig
44 protected $toolbar; // Reference to RTE toolbar array
45 protected $LOCAL_LANG; // Frontend language array
46
47 protected $pluginButtons = 'acronym';
48 protected $convertToolbarForHtmlAreaArray = array (
49 'acronym' => 'Acronym',
50 );
51 protected $acronymIndex = 0;
52 protected $abbreviationIndex = 0;
53
54 public function main($parentObject) {
55
56 return parent::main($parentObject) && t3lib_extMgm::isLoaded('static_info_tables');
57 }
58
59 /**
60 * Return tranformed content
61 *
62 * @param string $content: The content that is about to be sent to the RTE
63 *
64 * @return string the transformed content
65 */
66 public function transformContent($content) {
67
68 // <abbr> was not supported by IE before verison 7
69 if ($this->htmlAreaRTE->client['BROWSER'] == 'msie' && $this->htmlAreaRTE->client['VERSION'] < 7) {
70 // change <abbr> to <acronym>
71 $content = preg_replace('/<(\/?)abbr/i', "<$1acronym", $content);
72 }
73
74 return $content;
75 }
76
77 /**
78 * Return JS configuration of the htmlArea plugins registered by the extension
79 *
80 * @param integer Relative id of the RTE editing area in the form
81 *
82 * @return string JS configuration for registered plugins, in this case, JS configuration of block elements
83 *
84 * The returned string will be a set of JS instructions defining the configuration that will be provided to the plugin(s)
85 * Each of the instructions should be of the form:
86 * RTEarea['.$RTEcounter.']["buttons"]["button-id"]["property"] = "value";
87 */
88 public function buildJavascriptConfiguration($RTEcounter) {
89
90 $registerRTEinJavascriptString = '';
91 $button = 'acronym';
92 if (in_array($button, $this->toolbar)) {
93 if (!is_array($this->thisConfig['buttons.']) || !is_array($this->thisConfig['buttons.'][$button.'.'])) {
94 $registerRTEinJavascriptString .= '
95 RTEarea['.$RTEcounter.']["buttons"]["'. $button .'"] = new Object();';
96 }
97 $registerRTEinJavascriptString .= '
98 RTEarea['.$RTEcounter.'].buttons.'. $button .'.pathAcronymModule = "../../mod2/acronym.php";
99 RTEarea['.$RTEcounter.'].buttons.'. $button .'.acronymUrl = "' . $this->htmlAreaRTE->writeTemporaryFile('', 'acronym_'.$this->htmlAreaRTE->contentLanguageUid, 'js', $this->buildJSAcronymArray($this->htmlAreaRTE->contentLanguageUid)) . '";';
100
101 // <abbr> was not supported by IE before version 7
102 if ($this->htmlAreaRTE->client['BROWSER'] == 'msie' && $this->htmlAreaRTE->client['VERSION'] < 7) {
103 $this->abbreviationIndex = 0;
104 }
105 $registerRTEinJavascriptString .= '
106 RTEarea['.$RTEcounter.'].buttons.'. $button .'.noAcronym = ' . ($this->acronymIndex ? 'false' : 'true') . ';
107 RTEarea['.$RTEcounter.'].buttons.'. $button .'.noAbbr = ' . ($this->abbreviationIndex ? 'false' : 'true') . ';';
108 }
109
110 return $registerRTEinJavascriptString;
111 }
112
113 /**
114 * Return an acronym array for the Acronym plugin
115 *
116 * @return string acronym Javascript array
117 */
118 function buildJSAcronymArray($languageUid) {
119
120 $charset = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] ? $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] : 'iso-8859-1';
121 $button = 'acronym';
122
123 $linebreak = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->htmlAreaRTE->ID]['enableCompressedScripts'] ? '' : chr(10);
124 $JSAcronymArray .= 'acronyms = { ' . $linebreak;
125 $JSAcronymLanguageArray .= 'acronymLanguage = { ' . $linebreak;
126 $JSAbbreviationArray .= 'abbreviations = { ' . $linebreak;
127 $JSAbbreviationLanguageArray .= 'abbreviationLanguage = { ' . $linebreak;
128
129 $tableA = 'tx_rtehtmlarea_acronym';
130 $tableB = 'static_languages';
131 $fields = $tableA.'.type,' . $tableA . '.term,' . $tableA . '.acronym,' . $tableB . '.lg_iso_2';
132 $tableAB = $tableA . ' LEFT JOIN ' . $tableB . ' ON ' . $tableA . '.static_lang_isocode=' . $tableB . '.uid';
133 $whereClause = '1=1';
134 // Get all acronyms on pages to which the user has access
135 $lockBeUserToDBmounts = isset($this->thisConfig['buttons.'][$button.'.']['lockBeUserToDBmounts']) ? isset($this->thisConfig['buttons.'][$button.'.']['lockBeUserToDBmounts']) : $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts'];
136 if (!$GLOBALS['BE_USER']->isAdmin() && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts'] && $lockBeUserToDBmounts) {
137 // Temporarily setting alternative web browsing mounts
138 $altMountPoints = trim($GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.altElementBrowserMountPoints'));
139 if ($altMountPoints) {
140 $savedGroupDataWebmounts = $GLOBALS['BE_USER']->groupData['webmounts'];
141 $GLOBALS['BE_USER']->groupData['webmounts'] = implode(',', array_unique(t3lib_div::intExplode(',', $altMountPoints)));
142 $GLOBALS['WEBMOUNTS'] = $GLOBALS['BE_USER']->returnWebmounts();
143 }
144 $webMounts = $GLOBALS['BE_USER']->returnWebmounts();
145 $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
146 $recursive = isset($this->thisConfig['buttons.'][$button.'.']['recursive']) ? intval($this->thisConfig['buttons.'][$button.'.']['recursive']) : 0 ;
147 if (trim($this->thisConfig['buttons.'][$button.'.']['pages'])) {
148 $pids = t3li_div::trimExplode(',', $this->thisConfig['buttons.'][$button.'.']['pages'], 1);
149 foreach ($pids as $key => $val) {
150 if (!$GLOBALS['BE_USER']->isInWebMount($val, $perms_clause)) {
151 unset($pids[$key]);
152 }
153 }
154 } else {
155 $pids = $webMounts;
156 }
157 // Restoring webmounts
158 if ($altMountPoints) {
159 $GLOBALS['BE_USER']->groupData['webmounts'] = $savedGroupDataWebmounts;
160 $GLOBALS['WEBMOUNTS'] = $GLOBALS['BE_USER']->returnWebmounts();
161 }
162 $queryGenerator = t3lib_div::makeInstance('t3lib_queryGenerator');
163 foreach ($pids as $key => $val) {
164 if ($pageTree) {
165 $pageTreePrefix = ',';
166 }
167 $pageTree .= $pageTreePrefix . $queryGenerator->getTreeList($val, $recursive, $begin = 0, $perms_clause);
168 }
169 $whereClause .= ' AND '. $tableA . '.pid IN (' . $GLOBALS['TYPO3_DB']->fullQuoteStr(($pageTree ? $pageTree : ''), $tableA) . ')';
170 }
171 // Restrict to acronyms applicable to the language of current content element
172 if ($this->htmlAreaRTE->contentLanguageUid > -1) {
173 $whereClause .= ' AND (' . $tableA . '.sys_language_uid=' . $this->htmlAreaRTE->contentLanguageUid . ' OR ' . $tableA . '.sys_language_uid=-1) ';
174 }
175 // Restrict to acronyms in certain languages
176 if (is_array($this->thisConfig['buttons.']) && is_array($this->thisConfig['buttons.']['language.']) && isset($this->thisConfig['buttons.']['language.']['restrictToItems'])) {
177 $languageList = implode("','", t3lib_div::trimExplode(',', $GLOBALS['TYPO3_DB']->fullQuoteStr(strtoupper($this->thisConfig['buttons.']['language.']['restrictToItems']), $tableB)));
178 $whereClause .= ' AND '. $tableB . '.lg_iso_2 IN (' . $languageList . ') ';
179 }
180 $whereClause .= t3lib_BEfunc::BEenableFields($tableA);
181 $whereClause .= t3lib_BEfunc::deleteClause($tableA);
182 $whereClause .= t3lib_BEfunc::BEenableFields($tableB);
183 $whereClause .= t3lib_BEfunc::deleteClause($tableB);
184 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $tableAB, $whereClause);
185 while ($acronymRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
186 if( $acronymRow['type'] == 1) {
187 $JSAcronymArray .= (($this->acronymIndex++)?',':'') . '"' . $GLOBALS['LANG']->csConvObj->utf8_encode($acronymRow['term'], $charset) . '":"' . $GLOBALS['LANG']->csConvObj->utf8_encode($acronymRow['acronym'], $charset) . '"' . $linebreak;
188 $JSAcronymLanguageArray .= (($this->acronymIndex-1)?',':'') . '"' . $GLOBALS['LANG']->csConvObj->utf8_encode($acronymRow['term'], $charset) . '":"' . $GLOBALS['LANG']->csConvObj->utf8_encode(strtolower($acronymRow['lg_iso_2']), $charset) . '"' . $linebreak;
189 }
190 if ($acronymRow['type'] == 2) {
191 $JSAbbreviationArray .= (($this->abbreviationIndex++)?',':'') . '"' . $GLOBALS['LANG']->csConvObj->utf8_encode($acronymRow['term'], $charset) . '":"' . $GLOBALS['LANG']->csConvObj->utf8_encode($acronymRow['acronym'], $charset) . '"' . $linebreak;
192 $JSAbbreviationLanguageArray .= (($this->abbreviationIndex-1)?',':'') . '"' . $GLOBALS['LANG']->csConvObj->utf8_encode($acronymRow['term'], $charset) . '":"' . $GLOBALS['LANG']->csConvObj->utf8_encode(strtolower($acronymRow['lg_iso_2']), $charset) . '"' . $linebreak;
193 }
194 }
195 $JSAcronymArray .= '};' . $linebreak;
196 $JSAcronymLanguageArray .= '};' . $linebreak;
197 $JSAbbreviationArray .= '};' . $linebreak;
198 $JSAbbreviationLanguageArray .= '};' . $linebreak;
199
200 return $JSAcronymArray . $JSAcronymLanguageArray . $JSAbbreviationArray . $JSAbbreviationLanguageArray;
201 }
202 }
203
204 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/extensions/Acronym/class.tx_rtehtmlarea_acronym.php']) {
205 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/extensions/Acronym/class.tx_rtehtmlarea_acronym.php']);
206 }
207 ?>