1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15 Yii::import('bootstrap.helpers.TbArray');
16
17 class WhHtml5Editor extends CInputWidget
18 {
19 20 21 22
23 public $lang = 'en';
24
25 26 27
28 public $htmlOptions = array();
29
30 31 32
33 public $pluginOptions = array();
34
35 36 37
38 public $width = '100%';
39
40 41 42
43 public $height = '400px';
44
45 46 47
48 public function run()
49 {
50
51 list($name, $id) = $this->resolveNameID();
52
53 $this->registerClientScript($id);
54
55 $this->htmlOptions['id'] = $id;
56
57 if (!array_key_exists('style', $this->htmlOptions))
58 $this->htmlOptions['style'] = "width:{$this->width};height:{$this->height};";
59
60 if ($this->hasModel())
61 echo CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
62 else
63 echo CHtml::textArea($name, $this->value, $this->htmlOptions);
64 }
65
66 67 68 69
70 public function registerClientScript($id)
71 {
72
73 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
74 $assetsUrl = $this->getAssetsUrl($path);
75
76
77 $cs = Yii::app()->getClientScript();
78
79 $cs->registerCssFile($assetsUrl . '/css/bootstrap-wysihtml5.css');
80 if (isset($this->pluginOptions['color'])) {
81 $cs->registerCssFile($assetsUrl . '/css/wysiwyg-color.css');
82 }
83
84 $cs->registerScriptFile($assetsUrl . '/js/wysihtml5-0.3.0.js');
85 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-wysihtml5.js');
86
87 if (in_array(@$this->pluginOptions['locale'], array('de-DE', 'es-ES', 'fr', 'fr-NL', 'pt-BR', 'sv-SE'))) {
88 $cs->registerScriptFile(
89 $assetsUrl . '/js/locale/bootstrap-wysihtml5.' . $this->pluginOptions['locale'] . '.js'
90 );
91 } elseif (in_array($this->lang, array('de-DE', 'es-ES', 'fr', 'fr-NL', 'pt-BR', 'sv-SE'))) {
92 $cs->registerScriptFile($assetsUrl . '/js/locale/bootstrap-wysihtml5.' . $this->lang . '.js');
93 $this->pluginOptions['locale'] = $this->lang;
94 }
95
96 $this->normalizeStylesheetsProperty();
97
98
99 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
100
101 $this->getApi()->registerPlugin('wysihtml5', $selector, $this->pluginOptions);
102
103 }
104
105 106 107
108 private function normalizeStylesheetsProperty()
109 {
110 if (empty($this->pluginOptions['stylesheets']))
111 $this->pluginOptions['stylesheets'] = array();
112 else if (is_array($this->pluginOptions['stylesheets']))
113 $this->pluginOptions['stylesheets'] = array_filter($this->pluginOptions['stylesheets'], 'is_string');
114 else if (is_string($this->pluginOptions['stylesheets']))
115 $this->pluginOptions['stylesheets'] = array($this->pluginOptions['stylesheets']);
116 else
117 $this->pluginOptions['stylesheets'] = array();
118 }
119 }
120