-
Notifications
You must be signed in to change notification settings - Fork 10
/
TinyMce.php
242 lines (217 loc) · 6.47 KB
/
TinyMce.php
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
<?php
namespace zxbodya\yii2\tinymce;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\JqueryAsset;
use yii\web\View;
use yii\widgets\InputWidget;
/**
* This is just an example.
*/
class TinyMce extends InputWidget
{
/** @var bool|string Route to compressor action */
public $compressorRoute = false;
/**
* For example here could be url to yandex spellchecker service.
* http://speller.yandex.net/services/tinyspell
* More info about it here: http://api.yandex.ru/speller/doc/dg/tasks/how-to-spellcheck-tinymce.xml
*
* Or you can build own spellcheking service using code provided by moxicode:
* http://www.tinymce.com/download/download.php
*
* @var bool|string|array URL or an action route that can be used to create a URL or false if no url
*/
public $spellcheckerUrl = false;
/** @var bool|string Must be set to force widget language */
public $language = false; // editor language, if false app language is used
/**
* @var bool|array FileManager configuration.
* For example:
* 'fileManager' => array(
* 'class' => 'TinyMceElFinder',
* 'connectorRoute'=>'admin/elfinder/connector',
* )
*/
public $fileManager = false;
/** @var array Supported languages */
private static $languages = array(
'ar',
'ar_SA',
'bg_BG',
'bn_BD',
'bs',
'ca',
'cs',
'cs_CZ',
'cy',
'da',
'de',
'de_AT',
'el',
'eo',
'es',
'es_MX',
'et',
'eu',
'fa',
'fa_IR',
'fi',
'fo',
'fr_FR',
'ga',
'gl',
'he_IL',
'hi_IN',
'hr',
'hu_HU',
'hy',
'id',
'it',
'ja',
'ka_GE',
'kab',
'ko',
'ko_KR',
'ku',
'ku_IQ',
'lb',
'lt',
'lv',
'ml',
'mn_MN',
'nb_NO',
'nl',
'pl',
'pt_BR',
'pt_PT',
'ro',
'ru',
'si_LK',
'sk',
'sl_SI',
'sr',
'sv_SE',
'ta',
'ta_IN',
'th_TH',
'tr_TR',
'tt',
'ug',
'uk',
'uk_UA',
'vi',
'vi_VN',
'zh_CN',
'zh_TW',
'en_GB',
'km_KH',
'tg',
'tr',
'az',
'en_CA',
'is_IS',
'be',
'dv',
'kk',
'ml_IN',
'gd',
); // widget supported languages
private static $defaultSettings = array(
'language' => 'ru',
'plugins' => array(
"advlist autolink lists link image charmap print preview hr anchor pagebreak",
"searchreplace visualblocks visualchars code fullscreen",
"insertdatetime media nonbreaking save table contextmenu directionality",
"template paste textcolor"
),
'toolbar' => "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media | forecolor backcolor",
'toolbar_items_size' => 'small',
'image_advtab' => true,
'relative_urls' => false,
'spellchecker_languages' => "+Русский=ru",
);
/** @var array Widget settings will override defaultSettings */
public $settings = array();
public function init()
{
$this->settings = array_merge(self::$defaultSettings, $this->settings);
if ($this->language === false) {
$this->settings['language'] = Yii::$app->language;
} else {
$this->settings['language'] = $this->language;
}
if (!in_array($this->settings['language'], self::$languages)) {
$lang = false;
foreach (self::$languages as $i) {
if (strpos($this->settings['language'], $i)) {
$lang = $i;
}
}
if ($lang !== false) {
$this->settings['language'] = $lang;
} else {
$this->settings['language'] = 'en';
}
}
$assetsDir = $this->getView()->getAssetManager()->getBundle(TinyMceAsset::className())->baseUrl;
$this->settings['script_url'] = "{$assetsDir}/tiny_mce.js";
if ($this->spellcheckerUrl !== false) {
$this->settings['plugins'][] = 'spellchecker';
if (is_array($this->spellcheckerUrl)) {
$this->settings['spellchecker_rpc_url'] = Url::toRoute($this->spellcheckerUrl);
} else {
$this->settings['spellchecker_rpc_url'] = $this->spellcheckerUrl;
}
}
}
public function run()
{
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
if (isset($this->model)) {
echo Html::activeTextarea($this->model, $this->attribute, $this->options);
} else {
echo Html::textArea($this->name, $this->value, $this->options);
}
$this->registerScripts();
}
private function registerScripts()
{
$id = $this->options['id'];
$view = $this->getView();
if ($this->compressorRoute === false) {
TinyMceAsset::register($view);
} else {
$opts = array(
'files' => 'jquery.tinymce',
'source' => defined('YII_DEBUG') && YII_DEBUG,
);
$opts["plugins"] = strtr(implode(',', $this->settings['plugins']), array(' ' => ','));
if (isset($this->settings['theme'])) {
$opts["themes"] = $this->settings['theme'];
}
$opts["languages"] = $this->settings['language'];
$view->registerJsFile(
TinyMceCompressorAction::scripUrl($this->compressorRoute, $opts),
[
'depends' => [
'yii\web\JqueryAsset'
]
]
);
}
if ($this->fileManager !== false) {
/** @var $fm FileManager */
$fm = Yii::createObject($this->fileManager);
$fm->init();
$fm->registerAsset($view);
$this->settings['file_browser_callback'] = $fm->getFileBrowserCallback();
}
$settings = Json::encode($this->settings);
$this->getView()->registerJs("$('#{$id}').tinymce({$settings});");
}
}