Fixed bug #10532: htmlArea RTE: language code of lang attribute should be lower case
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / extensions / Language / class.tx_rtehtmlarea_language.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 * 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_merge($languages, $this->getLanguages());
89 $languagesJSArray = 'HTMLArea.languageOptions = ' . json_encode(array_flip($languages));
90 $registerRTEinJavascriptString .= '
91 RTEarea['.$RTEcounter.'].buttons.'. $button .'.languagesUrl = "' . $this->htmlAreaRTE->writeTemporaryFile('', 'languages_'.$this->htmlAreaRTE->contentLanguageUid, 'js', $languagesJSArray) . '";';
92 }
93 return $registerRTEinJavascriptString;
94 }
95
96 /**
97 * Getting all languages into an array
98 * where the key is the ISO alpha-2 code of the language
99 * and where the value are the name of the language in the current language
100 * Note: we exclude sacred and constructed languages
101 *
102 * @return array An array of names of languages
103 */
104 function getLanguages() {
105 $where = '1=1';
106 $table = 'static_languages';
107 $lang = tx_staticinfotables_div::getCurrentLanguage();
108 $nameArray = array();
109 $titleFields = tx_staticinfotables_div::getTCAlabelField($table, TRUE, $lang);
110 $prefixedTitleFields = array();
111 foreach ($titleFields as $titleField) {
112 $prefixedTitleFields[] = $table.'.'.$titleField;
113 }
114 $labelFields = implode(',', $prefixedTitleFields);
115 // Restrict to certain languages
116 if (is_array($this->thisConfig['buttons.']) && is_array($this->thisConfig['buttons.']['language.']) && isset($this->thisConfig['buttons.']['language.']['restrictToItems'])) {
117 $languageList = implode("','", t3lib_div::trimExplode(',', $GLOBALS['TYPO3_DB']->fullQuoteStr(strtoupper($this->thisConfig['buttons.']['language.']['restrictToItems']), $table)));
118 $where .= ' AND '. $table . '.lg_iso_2 IN (' . $languageList . ')';
119 }
120 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
121 $table.'.lg_iso_2,'.$table.'.lg_country_iso_2,'.$labelFields,
122 $table,
123 $where.' AND lg_constructed = 0 '.
124 ($this->htmlAreaRTE->is_FE() ? $GLOBALS['TSFE']->sys_page->enableFields($table) : t3lib_BEfunc::BEenableFields($table) . t3lib_BEfunc::deleteClause($table))
125 );
126 $prefixLabelWithCode = !$this->thisConfig['buttons.']['language.']['prefixLabelWithCode'] ? false : true;
127 $postfixLabelWithCode = !$this->thisConfig['buttons.']['language.']['postfixLabelWithCode'] ? false : true;
128 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
129 $code = strtolower($row['lg_iso_2']).($row['lg_country_iso_2']?'-'.strtoupper($row['lg_country_iso_2']):'');
130 foreach ($titleFields as $titleField) {
131 if ($row[$titleField]) {
132 $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]);
133 $nameArray[$code] = $prefixLabelWithCode ? ($code . ' - ' . $nameArray[$code]) : ($postfixLabelWithCode ? ($nameArray[$code] . ' - ' . $code) : $nameArray[$code]);
134 break;
135 }
136 }
137 }
138 $GLOBALS['TYPO3_DB']->sql_free_result($res);
139 uasort($nameArray, 'strcoll');
140 return $nameArray;
141 }
142
143 /**
144 * Return an updated array of toolbar enabled buttons
145 *
146 * @param array $show: array of toolbar elements that will be enabled, unless modified here
147 *
148 * @return array toolbar button array, possibly updated
149 */
150 public function applyToolbarConstraints($show) {
151 if (!t3lib_extMgm::isLoaded('static_info_tables')) {
152 return array_diff($show, array('language'));
153 } else {
154 return $show;
155 }
156 }
157 }
158
159 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/extensions/Language/class.tx_rtehtmlarea_language.php']) {
160 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/extensions/Language/class.tx_rtehtmlarea_language.php']);
161 }
162
163 ?>