-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswitcher.php
More file actions
326 lines (289 loc) · 7.12 KB
/
switcher.php
File metadata and controls
326 lines (289 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* @package Editor Switcher
* @copyright © 2021
* @license http://www.gnu.org/licenses/gpl.html
* @author Thomas Hunziker (www.bakual.net), Yoshiki Kozaki (www.joomler.net)
* @link https://www.bakual.net/
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;
/**
* Editor Switcher Plugin
*
* @since 1.0
*/
class plgEditorSwitcher extends CMSPlugin
{
/**
* A Registry object holding the parameters for the plugin
*
* @var Registry
* @since 2.0
*/
public $params = null;
/**
* Application object
*
* @var JApplicationCms
* @since 2.0
*/
protected $app;
/**
* The selected Editor class
*
* @var CMSPlugin
* @since 2.0
*/
protected $switcherEditor;
/**
* List of available editors
*
* @var array
* @since 2.0
*/
protected $editors;
/**
* The name of the cookie
*
* @var string
* @since 1.0
*/
protected $cookieName = 'editorswitchercurrent';
/**
* Constructor
*
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
*
* @since 2.0
*/
public function __construct(&$subject, $config)
{
parent::__construct($subject, $config);
$editor = $this->app->input->cookie->get($this->cookieName, 'switcher');
if ($editor == 'switcher')
{
$editor = $this->params->get('default_editor', 'tinymce');
}
if (file_exists(JPATH_PLUGINS . '/editors/' . $editor . '/' . $editor . '.php')
&& PluginHelper::isEnabled('editors', $editor))
{
$plugin = PluginHelper::getPlugin('editors', $editor);
$this->setSwitcherEditor($subject, $plugin);
}
else
{
// Get first editor found
$plugins = PluginHelper::getPlugin('editors');
$plugin = null;
foreach ($plugins as $v)
{
if ($v->name != 'switcher')
{
$plugin = $v;
break;
}
}
if ($plugin)
{
$this->setSwitcherEditor($subject, $plugin);
}
else
{
$this->app->enqueueMessage(Text::_('PLG_EDITOR_SWITCHER_EDITORWASNOTFOUND'), 'warning');
}
}
}
/**
* Create the selected editor
*
* @param object $subject
* @param object $plugin
*
* @since 1.0
*/
private function setSwitcherEditor($subject, $plugin)
{
$editor = $plugin->name;
require_once JPATH_PLUGINS . '/editors/' . $editor . '/' . $editor . '.php';
$classname = 'plgEditor' . ucfirst($editor);
$lang = Factory::getLanguage();
// Load language if not already done.
if (!$lang->getPaths('plg_editors_' . $editor))
{
$lang->load('plg_editors_' . $editor, JPATH_ADMINISTRATOR)
|| $lang->load('plg_editors_' . $editor, JPATH_PLUGINS . '/editors/' . $editor);
}
$this->switcherEditor = new $classname($subject, (array) $plugin);
}
/**
* Initialises the Editor.
*
* @return void
*
* @since 1.0
*/
public function onInit()
{
if (is_callable(array($this->switcherEditor, 'onInit')))
{
$this->switcherEditor->onInit();
}
}
/**
* Get the editor content
*
* @param string $id The name of the editor
*
* @return string
*
* @since 1.0
*
* @deprecated 4.0 Use directly the returned code
*/
public function onGetContent($id)
{
if (is_callable(array($this->switcherEditor, 'onGetContent')))
{
return $this->switcherEditor->onGetContent($id);
}
return '';
}
/**
* Set the editor content
*
* @param string $id The name of the editor
* @param string $html The html to place in the editor
*
* @return string
*
* @since 1.0
*
* @deprecated 4.0 Use directly the returned code
*/
public function onSetContent($id, $html)
{
if (is_callable(array($this->switcherEditor, 'onSetContent')))
{
return $this->switcherEditor->onSetContent($id, $html);
}
return '';
}
/**
* Copy editor content to form field
*
* @param string $id The name of the editor
*
* @return string
*
* @since 1.0
*
* @deprecated 4.0 Use directly the returned code
*/
public function onSave($id)
{
if (is_callable(array($this->switcherEditor, 'onSave')))
{
return $this->switcherEditor->onSave($id);
}
return '';
}
/**
* Display the editor area.
*
* @param string $name The name of the editor area.
* @param string $content The content of the field.
* @param string $width The width of the editor area.
* @param string $height The height of the editor area.
* @param int $col The number of columns for the editor area.
* @param int $row The number of rows for the editor area.
* @param boolean $buttons True and the editor buttons will be displayed.
* @param string $id An optional ID for the textarea. If not supplied the name is used.
* @param string $asset The object asset
* @param object $author The author.
* @param array $params Associative array of editor parameters.
*
* @return string
* @since 1.0
*
*/
public function onDisplay(
$name, $content, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null, $params = array())
{
if (is_callable(array($this->switcherEditor, 'onDisplay')))
{
$return = $this->switcherEditor->onDisplay($name, $content, $width, $height, $col, $row, $buttons, $id, $asset, $author, $params);
$return .= $this->setEditorSelector();
return $return;
}
return '';
}
/**
* Create the selector of editors
*
* @staticvar null $selector
*
* @return string
*
* @since 2.0
*/
private function setEditorSelector()
{
static $selector = null;
if (is_null($selector))
{
$db = JFactory::getDbo();
$authGroups = JFactory::getUser()->getAuthorisedGroups();
ArrayHelper::toInteger($authGroups);
$query = $db->getQuery(true);
$query->select($db->qn('element') . ' AS value');
$query->select('CONCAT(UCASE(SUBSTRING(' . $db->qn('element')
. ', 1, 1)), SUBSTRING(' . $db->qn('element') . ', 2)) AS text');
$query->from($db->qn('#__extensions'));
$query->where($db->qn('folder') . ' = ' . $db->q('editors'));
$query->where($db->qn('type') . ' = ' . $db->q('plugin'));
$query->where($db->qn('enabled') . ' = 1');
$query->where($db->qn('element') . ' <> ' . $db->q('switcher'));
$query->where($db->qn('access') . ' IN (' . implode(',', $authGroups) . ')');
$query->order($db->qn('ordering'));
$query->order($db->qn('name'));
$db->setQuery($query);
$this->editors = $db->loadObjectList();
if (count($this->editors) < 2)
{
$selector = '';
return $selector;
}
// Render the select field
ob_start();
include PluginHelper::getLayoutPath('editors', 'switcher');
$selector = ob_get_clean();
}
return $selector;
}
/**
* Inserts html code into the editor
*
* @param string $name The name of the editor
*
* @return string
*
* @since 1.0
*
* @deprecated 4.0 Code is loaded in the init script
*/
public function onGetInsertMethod($name)
{
if (is_callable(array($this->switcherEditor, 'onGetInsertMethod')))
{
return $this->switcherEditor->onGetInsertMethod($name);
}
return '';
}
}