[TASK] Use strict comparison for strings
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Extension / SelectFont.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Extension;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Rtehtmlarea\RteHtmlAreaApi;
19
20 /**
21 * SelectFont extension for htmlArea RTE
22 */
23 class SelectFont extends RteHtmlAreaApi
24 {
25 /**
26 * The name of the plugin registered by the extension
27 *
28 * @var string
29 */
30 protected $pluginName = 'SelectFont';
31
32 /**
33 * The comma-separated list of button names that the registered plugin is adding to the htmlArea RTE toolbar
34 *
35 * @var string
36 */
37 protected $pluginButtons = 'fontstyle,fontsize';
38
39 /**
40 * The name-converting array, converting the button names used in the RTE PageTSConfing to the button id's used by the JS scripts
41 *
42 * @var array
43 */
44 protected $convertToolbarForHtmlAreaArray = [
45 'fontstyle' => 'FontName',
46 'fontsize' => 'FontSize'
47 ];
48
49 /**
50 * List of default fonts
51 *
52 * @var array
53 */
54 protected $defaultFont = [
55 'fontstyle' => [
56 'Arial' => 'Arial,sans-serif',
57 'Arial Black' => '\'Arial Black\',sans-serif',
58 'Verdana' => 'Verdana,Arial,sans-serif',
59 'Times New Roman' => '\'Times New Roman\',Times,serif',
60 'Garamond' => 'Garamond',
61 'Lucida Handwriting' => '\'Lucida Handwriting\'',
62 'Courier' => 'Courier',
63 'Webdings' => 'Webdings',
64 'Wingdings' => 'Wingdings'
65 ],
66 'fontsize' => [
67 'Extra small' => '8px',
68 'Very small' => '9px',
69 'Small' => '10px',
70 'Medium' => '12px',
71 'Large' => '16px',
72 'Very large' => '24px',
73 'Extra large' => '32px'
74 ]
75 ];
76
77 /**
78 * RTE properties
79 *
80 * @var array
81 */
82 protected $RTEProperties;
83
84 /**
85 * Returns TRUE if the plugin is available and correctly initialized
86 *
87 * @param array $configuration Configuration array given from calling object down to the single plugins
88 * @return bool TRUE if this plugin object should be made available in the current environment and is correctly initialized
89 */
90 public function main(array $configuration)
91 {
92 $enabled = parent::main($configuration) && $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rtehtmlarea']['allowStyleAttribute'];
93 $this->RTEProperties = $this->configuration['thisConfig'];
94 return $enabled;
95 }
96
97 /**
98 * Return JS configuration of the htmlArea plugins registered by the extension
99 *
100 * @return string JS configuration for registered plugins
101 */
102 public function buildJavascriptConfiguration()
103 {
104 $jsArray = [];
105 $pluginButtonsArray = GeneralUtility::trimExplode(',', $this->pluginButtons);
106 // Process Page TSConfig configuration for each button
107 foreach ($pluginButtonsArray as $buttonId) {
108 if (in_array($buttonId, $this->toolbar)) {
109 $jsArray[] = $this->buildJSFontItemsConfig($buttonId);
110 }
111 }
112 return implode(LF, $jsArray);
113 }
114
115 /**
116 * Return Javascript configuration of font faces
117 *
118 * @param string $buttonId: button id
119 * @return string Javascript configuration of font faces
120 */
121 protected function buildJSFontItemsConfig($buttonId)
122 {
123 $jsArray = [];
124 $hideItems = '';
125 $addItems = [];
126 // Getting removal and addition configuration
127 if (is_array($this->configuration['thisConfig']['buttons.']) && is_array($this->configuration['thisConfig']['buttons.'][$buttonId . '.'])) {
128 if ($this->configuration['thisConfig']['buttons.'][$buttonId . '.']['removeItems']) {
129 $hideItems = $this->configuration['thisConfig']['buttons.'][$buttonId . '.']['removeItems'];
130 }
131 if ($this->configuration['thisConfig']['buttons.'][$buttonId . '.']['addItems']) {
132 $addItems = GeneralUtility::trimExplode(',', $this->cleanList($this->configuration['thisConfig']['buttons.'][$buttonId . '.']['addItems']), true);
133 }
134 }
135 $languageService = $this->getLanguageService();
136 // Initializing the items array
137 $languageKey = $buttonId === 'fontstyle' ? 'Default font' : 'Default size';
138 $items = [
139 'none' => [
140 $languageService->sL(
141 'LLL:EXT:rtehtmlarea/Resources/Private/Language/Plugins/SelectFont/locallang.xlf:' . $languageKey
142 ),
143 'none'
144 ],
145 ];
146 // Inserting and localizing default items
147 if ($hideItems !== '*') {
148 $index = 0;
149 foreach ($this->defaultFont[$buttonId] as $name => $value) {
150 if (!GeneralUtility::inList($hideItems, strval(($index + 1)))) {
151 $label = $languageService->sL('LLL:EXT:rtehtmlarea/Resources/Private/Language/Plugins/SelectFont/locallang.xlf:' . $name) ?: $name;
152 $items[$name] = [$label, $this->cleanList($value)];
153 }
154 $index++;
155 }
156 }
157 // Adding configured items
158 if (is_array($this->RTEProperties[$buttonId === 'fontstyle' ? 'fonts.' : 'fontSizes.'])) {
159 foreach ($this->RTEProperties[$buttonId === 'fontstyle' ? 'fonts.' : 'fontSizes.'] as $name => $conf) {
160 $name = substr($name, 0, -1);
161 if (in_array($name, $addItems)) {
162 $label = $this->getPageConfigLabel($conf['name']);
163 $items[$name] = [$label, $this->cleanList($conf['value'])];
164 }
165 }
166 }
167 // Seting default item
168 if ($this->configuration['thisConfig']['buttons.'][$buttonId . '.']['defaultItem'] && $items[$this->configuration['thisConfig']['buttons.'][$buttonId . '.']['defaultItem']]) {
169 $items['none'] = [$items[$this->configuration['thisConfig']['buttons.'][$buttonId . '.']['defaultItem']][0], 'none'];
170 unset($items[$this->configuration['thisConfig']['buttons.'][$buttonId . '.']['defaultItem']]);
171 }
172 // Setting the JS list of options
173 $itemsJSArray = [];
174 foreach ($items as $name => $option) {
175 $itemsJSArray[] = ['text' => $option[0], 'value' => $option[1]];
176 }
177 $itemsJSArray = json_encode(['options' => $itemsJSArray]);
178 // Adding to button JS configuration
179 if (!is_array($this->configuration['thisConfig']['buttons.']) || !is_array($this->configuration['thisConfig']['buttons.'][$buttonId . '.'])) {
180 $jsArray[] = 'RTEarea[editornumber].buttons.' . $buttonId . ' = new Object();';
181 }
182 $jsArray[] = 'RTEarea[editornumber].buttons.' . $buttonId . '.dataUrl = "' . $this->writeTemporaryFile($buttonId . '_' . $this->configuration['contentLanguageUid'], 'js', $itemsJSArray) . '";';
183 return implode(LF, $jsArray);
184 }
185 }