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