Added feature #14057: htmlArea RTE: Extjize the RTE ajax requests.
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / extensions / Language / class.tx_rtehtmlarea_language.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 * Language plugin 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_language extends tx_rtehtmlareaapi {
36
37 protected $extensionKey = 'rtehtmlarea'; // The key of the extension that is extending htmlArea RTE
38 protected $pluginName = 'Language'; // The name of the plugin registered by the extension
39 protected $relativePathToLocallangFile = 'extensions/Language/locallang.xml'; // Path to this main locallang file of the extension relative to the extension dir.
40 protected $relativePathToSkin = 'extensions/Language/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 = 'lefttoright,righttoleft,language,showlanguagemarks';
47 protected $convertToolbarForHtmlAreaArray = array (
48 'lefttoright' => 'LeftToRight',
49 'righttoleft' => 'RightToLeft',
50 'language' => 'Language',
51 'showlanguagemarks' => 'ShowLanguageMarks',
52 );
53
54 public function main($parentObject) {
55 if (!t3lib_extMgm::isLoaded('static_info_tables')) {
56 $this->pluginButtons = t3lib_div::rmFromList('language', $this->pluginButtons);
57 } else {
58 require_once(t3lib_extMgm::extPath('static_info_tables').'class.tx_staticinfotables_div.php');
59 }
60 return parent::main($parentObject);
61 }
62
63 /**
64 * Return JS configuration of the htmlArea plugins registered by the extension
65 *
66 * @param integer Relative id of the RTE editing area in the form
67 *
68 * @return string JS configuration for registered plugins
69 *
70 * The returned string will be a set of JS instructions defining the configuration that will be provided to the plugin(s)
71 * Each of the instructions should be of the form:
72 * RTEarea['.$RTEcounter.'].buttons.button-id.property = "value";
73 */
74 public function buildJavascriptConfiguration($RTEcounter) {
75 $button = 'language';
76 $registerRTEinJavascriptString = '';
77 if (in_array($button, $this->toolbar)) {
78 if (!is_array( $this->thisConfig['buttons.']) || !is_array($this->thisConfig['buttons.'][$button . '.'])) {
79 $registerRTEinJavascriptString .= '
80 RTEarea['.$RTEcounter.'].buttons.'. $button .' = new Object();';
81 }
82 if ($this->htmlAreaRTE->is_FE()) {
83 $first = $GLOBALS['TSFE']->getLLL('No language mark',$this->LOCAL_LANG);
84 } else {
85 $first = $GLOBALS['LANG']->getLL('No language mark');
86 }
87 $languages = array('none' => $first);
88 $languages = array_flip(array_merge($languages, $this->getLanguages()));
89 $languagesJSArray = array();
90 foreach ($languages as $key => $value) {
91 $languagesJSArray[] = array('text' => $key, 'value' => $value);
92 }
93 $languagesJSArray = json_encode(array('options' => $languagesJSArray));
94 $registerRTEinJavascriptString .= '
95 RTEarea['.$RTEcounter.'].buttons.'. $button .'.dataUrl = "' . $this->htmlAreaRTE->writeTemporaryFile('', $button . '_' . $this->htmlAreaRTE->contentLanguageUid, 'js', $languagesJSArray) . '";';
96 }
97 return $registerRTEinJavascriptString;
98 }
99 /**
100 * Getting all languages into an array
101 * where the key is the ISO alpha-2 code of the language
102 * and where the value are the name of the language in the current language
103 * Note: we exclude sacred and constructed languages
104 *
105 * @return array An array of names of languages
106 */
107 function getLanguages() {
108 $where = '1=1';
109 $table = 'static_languages';
110 $lang = tx_staticinfotables_div::getCurrentLanguage();
111 $nameArray = array();
112 $titleFields = tx_staticinfotables_div::getTCAlabelField($table, TRUE, $lang);
113 $prefixedTitleFields = array();
114 foreach ($titleFields as $titleField) {
115 $prefixedTitleFields[] = $table.'.'.$titleField;
116 }
117 $labelFields = implode(',', $prefixedTitleFields);
118 // Restrict to certain languages
119 if (is_array($this->thisConfig['buttons.']) && is_array($this->thisConfig['buttons.']['language.']) && isset($this->thisConfig['buttons.']['language.']['restrictToItems'])) {
120 $languageList = implode("','", t3lib_div::trimExplode(',', $GLOBALS['TYPO3_DB']->fullQuoteStr(strtoupper($this->thisConfig['buttons.']['language.']['restrictToItems']), $table)));
121 $where .= ' AND '. $table . '.lg_iso_2 IN (' . $languageList . ')';
122 }
123 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
124 $table.'.lg_iso_2,'.$table.'.lg_country_iso_2,'.$labelFields,
125 $table,
126 $where.' AND lg_constructed = 0 '.
127 ($this->htmlAreaRTE->is_FE() ? $GLOBALS['TSFE']->sys_page->enableFields($table) : t3lib_BEfunc::BEenableFields($table) . t3lib_BEfunc::deleteClause($table))
128 );
129 $prefixLabelWithCode = !$this->thisConfig['buttons.']['language.']['prefixLabelWithCode'] ? false : true;
130 $postfixLabelWithCode = !$this->thisConfig['buttons.']['language.']['postfixLabelWithCode'] ? false : true;
131 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
132 $code = strtolower($row['lg_iso_2']).($row['lg_country_iso_2']?'-'.strtoupper($row['lg_country_iso_2']):'');
133 foreach ($titleFields as $titleField) {
134 if ($row[$titleField]) {
135 $nameArray[$code] = $this->htmlAreaRTE->is_FE() ? $GLOBALS['TSFE']->csConv($row[$titleField], $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['static_info_tables']['charset']) : ($this->htmlAreaRTE->TCEform->inline->isAjaxCall ? $GLOBALS['LANG']->csConvObj->utf8_encode($row[$titleField], $GLOBALS['LANG']->charSet) : $row[$titleField]);
136 $nameArray[$code] = $prefixLabelWithCode ? ($code . ' - ' . $nameArray[$code]) : ($postfixLabelWithCode ? ($nameArray[$code] . ' - ' . $code) : $nameArray[$code]);
137 break;
138 }
139 }
140 }
141 $GLOBALS['TYPO3_DB']->sql_free_result($res);
142 uasort($nameArray, 'strcoll');
143 return $nameArray;
144 }
145
146 /**
147 * Return an updated array of toolbar enabled buttons
148 *
149 * @param array $show: array of toolbar elements that will be enabled, unless modified here
150 *
151 * @return array toolbar button array, possibly updated
152 */
153 public function applyToolbarConstraints($show) {
154 if (!t3lib_extMgm::isLoaded('static_info_tables')) {
155 return array_diff($show, array('language'));
156 } else {
157 return $show;
158 }
159 }
160 }
161 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/extensions/Language/class.tx_rtehtmlarea_language.php']) {
162 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/extensions/Language/class.tx_rtehtmlarea_language.php']);
163 }
164 ?>