forked from devkpv/ckeditor-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECKEditor.php
312 lines (253 loc) · 8.47 KB
/
ECKEditor.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
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
<?php
/**
* Description of CKEditor
*
* @author Ekstazi
* v 0.2
*/
class ECKEditor extends CInputWidget {
const COLS = 40;
const ROWS = 10;
private $language='en';
private $allowedLanguages=array(
'af','ar','bg','bn','bs','ca','cs','da','de','el','en','en-au','en-ca',
'en-uk','eo','es','et','eu','fa','fi','fo','fr','fr-ca','gl','gu','he',
'hi','hr','hu','is','it','ja','km','ko','lt','lv','mn','ms','nb','nl',
'no','pl','pt','pt-br','ro','ru','sk','sl','sr','sr-latn','sv','th','tr',
'uk','vi','zh','zh-cn'
);
private $options=array();
//advanced - пользователь сам определяет тулбар
private $allowedEditorTemplates=array('full','basic','advanced');
private $editorTemplate='full';
private $plugins=array();
private $contentCSS = '';
private $width = '100%';
private $height = '400px';
private $fontFamilies=array(
'Arial'=>'Arial, Helvetica, sans-serif',
'Comic Sans MS'=>'Comic Sans MS, cursive',
'Courier New'=>'Courier New, Courier, monospace',
'Georgia'=>'Georgia, serif',
'Lucida Sans Unicode'=>'Lucida Sans Unicode, Lucida Grande, sans-serif',
'Tahoma'=>'Tahoma, Geneva, sans-serif',
'Times New Roman'=>'Times New Roman, Times, serif',
'Trebuchet MS'=>'Trebuchet MS, Helvetica, sans-serif',
'Verdana'=>'Verdana, Geneva, sans-serif',
);
private $fontSizes = array(
'8'=>'8px',
'9'=>'9px',
'10'=>'10px',
'11'=>'11px',
'12'=>'12px',
'14'=>'14px',
'16'=>'16px',
'18'=>'18px',
'20'=>'20px',
'22'=>'22px',
'24'=>'24px',
'26'=>'26px',
'28'=>'28px',
'36'=>'36px',
'48'=>'48px',
'72'=>'72px'
);
private $toolbar=array();
private $skin='kama';
private $theme='default';
public function __construct($owner=null) {
parent::__construct($owner);
$this->setLanguage(Yii::app()->language);
}
public function setLanguage($value){
$lang = (($p = strpos($value, '_')) !== false) ? str_replace('_', '-', $value) : $value;
if (in_array($lang, $this->allowedLanguages)) {
$this->language = $lang;
}
else {
$suffix = empty($lang) ? 'en' : ($p !== false) ? strtolower(substr($lang, 0, $p)) : strtolower($lang);
if (in_array($suffix, $this->allowedLanguages)) $this->language = $suffix;
}
if(isset($this->allowedLanguages[$language]))
$this->language=$language;
}
public function getLanguage(){
return $this->language;
}
public function setOptions($value){
if (!is_array($value))
throw new CException(Yii::t(__CLASS__, 'options must be an array'));
$this->options=$value;
}
public function getOptions(){
return $this->options;
}
public function setHeight($value)
{
if (!preg_match("/[\d]+[px|\%]/", $value))
throw new CException(Yii::t(__CLASS__, 'height must be a string of digits terminated by "%" or "px"'));
$this->height = $value;
}
public function getHeight()
{
return $this->height;
}
public function setWidth($value)
{
if (!preg_match("/[\d]+[px|\%]/", $value))
throw new CException(Yii::t('ETinyMce', 'width must be a string of digits terminated by "%" or "px"'));
$this->width = $value;
}
public function getWidth()
{
return $this->width;
}
public function setFontFamilies($value)
{
if (!is_array($value))
throw new CException(Yii::t(__CLASS__, 'fontFamilies must be an array of strings'));
$this->fontFamilies = $value;
}
public function getFontFamilies()
{
return $this->fontFamilies;
}
public function setFontSizes($value)
{
if (!is_array($value))
throw new CException(Yii::t(__CLASS__, 'fontSizes must be an array of integers'));
$this->fontSizes = $value;
}
public function getFontSizes()
{
return $this->fontSizes;
}
public function setEditorTemplate($value)
{
if (!in_array($value, $this->allowedEditorTemplates))
throw new CException(Yii::t(__CLASS__, 'editorTemplate must be one of {temp}', array('{temp}'=>implode(',', $this->validEditorTemplates))));
$this->editorTemplate = $value;
}
public function getEditorTemplate()
{
return $this->editorTemplate;
}
public function setPlugins($value)
{
if (!is_array($value))
throw new CException(Yii::t(__CLASS__, 'plugins must be an array of strings'));
$this->plugins = $value;
}
public function getPlugins()
{
return $this->plugins;
}
public function setContentCSS($value)
{
if (!is_string($value))
throw new CException(Yii::t(__CLASS__, 'contentCSS must be an URL'));
$this->contentCSS = $value;
}
public function getContentCSS()
{
return $this->contentCSS;
}
public function setToolbar($value){
if(is_array($value)||is_string($value)){
$this->toolbar=$value;
}else throw new CException(Yii::t(__CLASS__, 'toolbar must be an array or string'));
}
public function getToolbar()
{
return $this->toolbar;
}
public function setSkin($value)
{
if (!is_string($value))
throw new CException(Yii::t(__CLASS__, 'Skin must be a string'));
$this->skin = $value;
}
public function getSkin()
{
return $this->skin;
}
public function setTheme($value)
{
if (!is_string($value))
throw new CException(Yii::t(__CLASS__, 'Theme must be a string'));
$this->theme = $value;
}
public function getTheme()
{
return $this->theme;
}
protected function makeOptions()
{
list($name,$id) = $this->resolveNameID();
$options['language'] = $this->language;
// to make the content look like if it were in your target page
if ($this->contentCSS !== '') {
$options['contentsCss'] = $this->contentCSS;
}
switch ($this->editorTemplate) {
case 'full':
$options['toolbar']='Full';
break;
case 'basic':
$options['toolbar']='Basic';
break;
default:
$options['toolbar']=$this->toolbar;
}
$fontFamilies='';
foreach($this->fontFamilies as $k=>$v){
$fontFamilies.=$k.'/'.$v.';';
}
$options['font_names']=$fontFamilies;
$fontSizes='';
foreach($this->fontSizes as $k=>$v){
$fontSizes.=$k.'/'.$v.';';
}
$options['fontSize_sizes']=$fontSizes;
$options['extraPlugins'] = implode(',', $this->plugins);
$options['skin']=$this->skin;
$options['theme']=$this->theme;
// here any option is overriden by user's options
if (is_array($this->options)) {
$options = array_merge($options, $this->options);
}
return CJavaScript::encode($options);
}
public function run(){
parent::run();
list($name, $id) = $this->resolveNameID();
$baseDir = dirname(__FILE__);
$assets = Yii::app()->getAssetManager()->publish($baseDir.DIRECTORY_SEPARATOR.'assets');
$options = $this->makeOptions();
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($assets.'/ckeditor.js');
$this->htmlOptions['id'] = $id;
if (!array_key_exists('style', $this->htmlOptions)) {
$this->htmlOptions['style'] = "width:{$this->width};height:{$this->height};";
}
if (!array_key_exists('cols', $this->htmlOptions)) {
$this->htmlOptions['cols'] = self::COLS;
}
if (!array_key_exists('rows', $this->htmlOptions)) {
$this->htmlOptions['rows'] = self::ROWS;
}
$js =<<<EOP
CKEDITOR.replace('{$name}',{$options});
EOP;
$cs->registerScript('Yii.'.get_class($this).'#'.$id, $js, CClientScript::POS_LOAD);
if($this->hasModel()) {
$html = CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
}
else {
$html = CHtml::textArea($name, $this->value, $this->htmlOptions);
}
echo $html;
}
}
?>