1 <?php
2 3 4 5 6 7 8 9 10
11 Yii::import('bootstrap.helpers.TbArray');
12
13 class WhRedactor extends CInputWidget
14 {
15 16 17 18
19 public $pluginOptions = array();
20
21 22 23 24
25 public $debugMode = false;
26
27 28 29
30 public function init()
31 {
32
33 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
34
35 if (!$style = TbArray::popValue('style', $this->htmlOptions, '')) {
36 $this->htmlOptions['style'] = $style;
37 }
38
39 $width = TbArray::getValue('width', $this->htmlOptions, '100%');
40 $height = TbArray::popValue('height', $this->htmlOptions, '450px');
41 $this->htmlOptions['style'] = "width:{$width};height:{$height};" . $this->htmlOptions['style'];
42 }
43
44 45 46
47 public function run()
48 {
49 $this->renderField();
50 $this->registerClientScript();
51 }
52
53 54 55
56 public function renderField()
57 {
58 list($name, $id) = $this->resolveNameID();
59
60 TbArray::defaultValue('id', $id, $this->htmlOptions);
61 TbArray::defaultValue('name', $name, $this->htmlOptions);
62
63 if ($this->hasModel()) {
64 echo CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
65 } else {
66 echo CHtml::textArea($name, $this->value, $this->htmlOptions);
67 }
68
69 }
70
71 72 73 74
75 public function registerClientScript()
76 {
77
78 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
79 $assetsUrl = $this->getAssetsUrl($path);
80
81
82 $cs = Yii::app()->getClientScript();
83
84 $script = $this->debugMode
85 ? 'redactor.js'
86 : 'redactor.min.js';
87
88 $cs->registerCssFile($assetsUrl . '/css/redactor.css');
89 $cs->registerScriptFile($assetsUrl . '/js/' . $script, CClientScript::POS_END);
90
91
92 $language = TbArray::getValue('lang', $this->pluginOptions);
93 if (!empty($language) && $language != 'en') {
94 $cs->registerScriptFile($assetsUrl . '/js/langs/' . $language . '.js', CClientScript::POS_END);
95 }
96
97
98 $this->registerPlugins($assetsUrl);
99
100
101 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
102
103 $this->getApi()->registerPlugin('redactor', $selector, $this->pluginOptions);
104 }
105
106 107 108
109 protected function registerPlugins($assetsUrl)
110 {
111 if (isset($this->pluginOptions['plugins'])) {
112 $ds = DIRECTORY_SEPARATOR;
113 $pluginsPath = __DIR__ . $ds . 'assets' . $ds . 'js' . $ds . 'plugins' . $ds;
114 $pluginsUrl = $assetsUrl . '/js/plugins/';
115 $scriptTypes = array('css', 'js');
116
117 foreach ($this->pluginOptions['plugins'] as $pluginName) {
118 foreach ($scriptTypes as $type) {
119 if (@file_exists($pluginsPath . $pluginName . $ds . $pluginName . '.' . $type)) {
120 Yii::app()->clientScript->registerScriptFile(
121 $pluginsUrl . '/' .
122 $pluginName . '/' .
123 $pluginName . '.' .
124 $type
125 );
126 }
127 }
128 }
129 }
130 }
131 }
132