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 public function init()
46 {
47
48 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
49
50 if (!$style = TbArray::popValue('style', $this->htmlOptions, '')) {
51 $this->htmlOptions['style'] = $style;
52 }
53
54 $width = TbArray::getValue('width', $this->htmlOptions, '100%');
55 $height = TbArray::popValue('height', $this->htmlOptions, '450px');
56 $this->htmlOptions['style'] = "width:{$width};height:{$height};" . $this->htmlOptions['style'];
57 }
58
59 60 61
62 public function run()
63 {
64
65 list($name, $id) = $this->resolveNameID();
66
67 $this->htmlOptions['id'] = $id;
68
69 $this->registerClientScript();
70
71 if (!array_key_exists('style', $this->htmlOptions))
72 $this->htmlOptions['style'] = "width:{$this->width};height:{$this->height};";
73
74 if ($this->hasModel())
75 echo CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
76 else
77 echo CHtml::textArea($name, $this->value, $this->htmlOptions);
78 }
79
80 81 82
83 public function registerClientScript()
84 {
85
86 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
87 $assetsUrl = $this->getAssetsUrl($path);
88
89
90 $cs = Yii::app()->getClientScript();
91
92 $cs->registerCssFile($assetsUrl . '/css/bootstrap-wysihtml5.css');
93 if (isset($this->pluginOptions['color'])) {
94 $cs->registerCssFile($assetsUrl . '/css/wysiwyg-color.css');
95 }
96
97 $cs->registerScriptFile($assetsUrl . '/js/wysihtml5-0.3.0.js');
98 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-wysihtml5.js');
99
100 if (in_array(@$this->pluginOptions['locale'], array('de-DE', 'es-ES', 'fr', 'fr-NL', 'pt-BR', 'sv-SE'))) {
101 $cs->registerScriptFile(
102 $assetsUrl . '/js/locale/bootstrap-wysihtml5.' . $this->pluginOptions['locale'] . '.js'
103 );
104 } elseif (in_array($this->lang, array('de-DE', 'es-ES', 'fr', 'fr-NL', 'pt-BR', 'sv-SE'))) {
105 $cs->registerScriptFile($assetsUrl . '/js/locale/bootstrap-wysihtml5.' . $this->lang . '.js');
106 $this->pluginOptions['locale'] = $this->lang;
107 }
108
109 $this->normalizeStylesheetsProperty();
110
111
112 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
113
114 $this->getApi()->registerPlugin('wysihtml5', $selector, $this->pluginOptions);
115
116 }
117
118 119 120
121 private function normalizeStylesheetsProperty()
122 {
123 if (empty($this->pluginOptions['stylesheets']))
124 $this->pluginOptions['stylesheets'] = array();
125 else if (is_array($this->pluginOptions['stylesheets']))
126 $this->pluginOptions['stylesheets'] = array_filter($this->pluginOptions['stylesheets'], 'is_string');
127 else if (is_string($this->pluginOptions['stylesheets']))
128 $this->pluginOptions['stylesheets'] = array($this->pluginOptions['stylesheets']);
129 else
130 $this->pluginOptions['stylesheets'] = array();
131 }
132 }
133