1 <?php
2 3 4 5
6
7 8 9 10 11 12
13 class CKEditor extends CInputWidget{
14 const COLS = 40;
15 const ROWS = 10;
16
17 private $language='en';
18 private $allowedLanguages=array(
19 'af','ar','bg','bn','bs','ca','cs','da','de','el','en','en-au','en-ca',
20 'en-uk','eo','es','et','eu','fa','fi','fo','fr','fr-ca','gl','gu','he',
21 'hi','hr','hu','is','it','ja','km','ko','lt','lv','mn','ms','nb','nl',
22 'no','pl','pt','pt-br','ro','ru','sk','sl','sr','sr-latn','sv','th','tr',
23 'uk','vi','zh','zh-cn'
24 );
25
26 private $options=array();
27
28 private $allowedEditorTemplates=array('full','basic','advanced');
29 private $editorTemplate='full';
30
31 private $plugins=array();
32
33 private $contentCSS = '';
34
35
36 private $width = '100%';
37 private $height = '400px';
38
39 private $fontFamilies=array(
40 'Arial'=>'Arial, Helvetica, sans-serif',
41 'Comic Sans MS'=>'Comic Sans MS, cursive',
42 'Courier New'=>'Courier New, Courier, monospace',
43 'Georgia'=>'Georgia, serif',
44 'Lucida Sans Unicode'=>'Lucida Sans Unicode, Lucida Grande, sans-serif',
45 'Tahoma'=>'Tahoma, Geneva, sans-serif',
46 'Times New Roman'=>'Times New Roman, Times, serif',
47 'Trebuchet MS'=>'Trebuchet MS, Helvetica, sans-serif',
48 'Verdana'=>'Verdana, Geneva, sans-serif',
49 );
50
51 private $fontSizes = array(
52 '8'=>'8px',
53 '9'=>'9px',
54 '10'=>'10px',
55 '11'=>'11px',
56 '12'=>'12px',
57 '14'=>'14px',
58 '16'=>'16px',
59 '18'=>'18px',
60 '20'=>'20px',
61 '22'=>'22px',
62 '24'=>'24px',
63 '26'=>'26px',
64 '28'=>'28px',
65 '36'=>'36px',
66 '48'=>'48px',
67 '72'=>'72px'
68 );
69
70 private $toolbar=array();
71
72 public $skin='kama';
73 private $theme='default';
74
75
76 public function __construct($owner=null) {
77 parent::__construct($owner);
78 $this->setLanguage(Yii::app()->language);
79 }
80
81 public function setLanguage($value){
82 $lang = (($p = strpos($value, '_')) !== false) ? str_replace('_', '-', $value) : $value;
83 if (in_array($lang, $this->allowedLanguages)) {
84 $this->language = $lang;
85 }
86 else {
87 $suffix = empty($lang) ? 'en' : ($p !== false) ? strtolower(substr($lang, 0, $p)) : strtolower($lang);
88 if (in_array($suffix, $this->allowedLanguages)) $this->language = $suffix;
89 }
90 if(isset($this->allowedLanguages[$lang]))
91 $this->language=$lang;
92 }
93
94 public function getLanguage(){
95 return $this->language;
96 }
97
98 public function setOptions($value){
99 if (!is_array($value))
100 throw new CException(Yii::t(__CLASS__, 'options must be an array'));
101
102 $this->options=$value;
103 }
104
105 public function getOptions(){
106 return $this->options;
107 }
108
109 public function setHeight($value)
110 {
111 if (!preg_match("/[\d]+[px|\%]/", $value))
112 throw new CException(Yii::t(__CLASS__, 'height must be a string of digits terminated by "%" or "px"'));
113 $this->height = $value;
114 }
115
116 public function getHeight()
117 {
118 return $this->height;
119 }
120
121 public function setWidth($value)
122 {
123 if (!preg_match("/[\d]+[px|\%]/", $value))
124 throw new CException(Yii::t('ETinyMce', 'width must be a string of digits terminated by "%" or "px"'));
125 $this->width = $value;
126 }
127
128 public function getWidth()
129 {
130 return $this->width;
131 }
132
133 public function setFontFamilies($value)
134 {
135 if (!is_array($value))
136 throw new CException(Yii::t(__CLASS__, 'fontFamilies must be an array of strings'));
137 $this->fontFamilies = $value;
138 }
139
140 public function getFontFamilies()
141 {
142 return $this->fontFamilies;
143 }
144
145 public function setFontSizes($value)
146 {
147 if (!is_array($value))
148 throw new CException(Yii::t(__CLASS__, 'fontSizes must be an array of integers'));
149 $this->fontSizes = $value;
150 }
151
152 public function getFontSizes()
153 {
154 return $this->fontSizes;
155 }
156
157 public function setEditorTemplate($value)
158 {
159 if (!in_array($value, $this->allowedEditorTemplates))
160 throw new CException(Yii::t(__CLASS__, 'editorTemplate must be one of {temp}', array('{temp}'=>implode(',', $this->validEditorTemplates))));
161 $this->editorTemplate = $value;
162 }
163
164 public function getEditorTemplate()
165 {
166 return $this->editorTemplate;
167 }
168
169 public function setPlugins($value)
170 {
171 if (!is_array($value))
172 throw new CException(Yii::t(__CLASS__, 'plugins must be an array of strings'));
173 $this->plugins = $value;
174 }
175
176 public function getPlugins()
177 {
178 return $this->plugins;
179 }
180
181 public function setContentCSS($value)
182 {
183 if (!is_string($value))
184 throw new CException(Yii::t(__CLASS__, 'contentCSS must be an URL'));
185 $this->contentCSS = $value;
186 }
187
188 public function getContentCSS()
189 {
190 return $this->contentCSS;
191 }
192
193 public function setToolbar($value){
194 if(is_array($value)||is_string($value)){
195 $this->toolbar=$value;
196 }else throw new CException(Yii::t(__CLASS__, 'toolbar must be an array or string'));
197 }
198
199 public function getToolbar()
200 {
201 return $this->toolbar;
202 }
203
204 public function setSkin($value)
205 {
206 if (!is_string($value))
207 throw new CException(Yii::t(__CLASS__, 'Skin must be a string'));
208 $this->skin = $value;
209 }
210
211 public function getSkin()
212 {
213 return $this->skin;
214 }
215
216 public function setTheme($value)
217 {
218 if (!is_string($value))
219 throw new CException(Yii::t(__CLASS__, 'Theme must be a string'));
220 $this->theme = $value;
221 }
222
223 public function getTheme()
224 {
225 return $this->theme;
226 }
227
228 protected function makeOptions()
229 {
230 list($name,$id) = $this->resolveNameID();
231
232 $options['language'] = $this->language;
233
234
235 if ($this->contentCSS !== '') {
236 $options['contentsCss'] = $this->contentCSS;
237 }
238
239 switch ($this->editorTemplate) {
240 case 'full':
241 $options['toolbar']='Full';
242 break;
243 case 'basic':
244 $options['toolbar']='Basic';
245 break;
246 default:
247 $options['toolbar']=$this->toolbar;
248 }
249
250 $fontFamilies='';
251 foreach($this->fontFamilies as $k=>$v){
252 $fontFamilies.=$k.'/'.$v.';';
253 }
254 $options['font_names']=$fontFamilies;
255
256 $fontSizes='';
257 foreach($this->fontSizes as $k=>$v){
258 $fontSizes.=$k.'/'.$v.';';
259 }
260 $options['fontSize_sizes']=$fontSizes;
261
262 $options['extraPlugins'] = implode(',', $this->plugins);
263
264 $options['skin']=$this->skin;
265 $options['theme']=$this->theme;
266
267 if (is_array($this->options)) {
268 $options = array_merge($options, $this->options);
269 }
270
271
272 $createUrls = array('filebrowserBrowseCreateUrl','filebrowserImageBrowseCreateUrl','filebrowserFlashBrowseCreateUrl','filebrowserUploadCreateUrl');
273 foreach($createUrls AS $name) {
274 if (isset($options[$name]) && $options[$name]) {
275 $route = $options[$name][0];
276 unset($options[$name][0]);
277 $options[str_replace('CreateUrl', 'Url', $name)] = $this->controller->createUrl($route, $options[$name]);
278 }
279 }
280
281
282 return CJavaScript::encode($options);
283 }
284
285 public function run(){
286 parent::run();
287
288 list($name, $id) = $this->resolveNameID();
289
290 $baseDir = dirname(__FILE__);
291 $assets = Yii::app()->getAssetManager()->publish($baseDir.DIRECTORY_SEPARATOR.'assets');
292
293 $options = $this->makeOptions();
294
295 $cs = Yii::app()->getClientScript();
296
297 $cs->registerScriptFile($assets.'/ckeditor.js');
298
299 $this->htmlOptions['id'] = $id;
300 if (!array_key_exists('style', $this->htmlOptions)) {
301 $this->htmlOptions['style'] = "width:{$this->width};height:{$this->height};";
302 }
303 if (!array_key_exists('cols', $this->htmlOptions)) {
304 $this->htmlOptions['cols'] = self::COLS;
305 }
306 if (!array_key_exists('rows', $this->htmlOptions)) {
307 $this->htmlOptions['rows'] = self::ROWS;
308 }
309
310 $js =<<<EOP
311 CKEDITOR.replace('{$name}',{$options});
312 EOP;
313 $cs->registerScript('Yii.'.get_class($this).'#'.$id, $js, CClientScript::POS_LOAD);
314
315 if($this->hasModel()) {
316 $html = CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
317 }
318 else {
319 $html = CHtml::textArea($name, $this->value, $this->htmlOptions);
320 }
321
322 echo $html;
323
324 }
325 }
326 ?>
327