1 <?php
2 3 4 5 6 7 8 9 10 11
12
13 Yii::import('bootstrap.helpers.TbArray');
14 Yii::import('bootstrap.helpers.TbHtml');
15
16 class WhInputWidget extends CInputWidget {
17 18 19
20 public $pluginOptions = array();
21 22 23
24 public $clientEvents = array();
25 26 27 28
29 public $language;
30 31 32
33 public $languagePath;
34 public $readOnly = false;
35 36 37
38 public function init()
39 {
40 parent::init();
41
42 if (!$this->hasModel() && $this->name === null) {
43 throw new CException("Either 'name', or 'model' and 'attribute' properties must be specified.");
44 }
45 if (!isset($this->htmlOptions['id'])) {
46 $this->htmlOptions['id'] = $this->hasModel()
47 ? CHtml::activeId($this->model, $this->attribute)
48 : $this->getId();
49 }
50
51 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
52
53 $this->htmlOptions = TbArray::merge($this->asDataAttributes($this->pluginOptions), $this->htmlOptions);
54 $this->pluginOptions = false;
55
56 if ($this->hasModel()) {
57 $this->htmlOptions['data-name'] = CHtml::activeId($this->model, $this->attribute);
58 $this->htmlOptions['data-value'] = CHtml::value($this->model, $this->attribute);
59 } else {
60 $this->htmlOptions['data-name'] = $this->name;
61 $this->htmlOptions['data-value'] = $this->value;
62 }
63
64 }
65
66 67 68 69 70
71 protected function asDataAttributes($options)
72 {
73 $data = [];
74 foreach ($options as $key => $value) {
75 $data["data-$key"] = is_bool($value) ? ($value ? 'true' : 'false') : $value;
76 }
77 return $data;
78 }
79
80 81 82 83
84 protected function registerPlugin($name)
85 {
86 $path = __DIR__ . DIRECTORY_SEPARATOR . 'assets';
87 $assetsUrl = $this->getAssetsUrl($path);
88
89
90 $cs = Yii::app()->getClientScript();
91 $cs->registerCssFile($assetsUrl . "/css/bootstrap-formhelpers.min.css");
92 $cs->registerScriptFile($assetsUrl . "/js/bootstrap-formhelpers.min.js", CClientScript::POS_END);
93
94 if($this->language) {
95 $fname = "bootstrap-formhelpers-" . (substr($name, 3)) . ".{$this->language}.js";
96 $languageFile = $this->languagePath ? : $assetsUrl . "/i18n/{$this->language}/{$fname}";
97 $cs->registerScriptFile($languageFile);
98 }
99
100 $id = $this->htmlOptions['id'];
101 $js = array();
102 if ($this->pluginOptions !== false) {
103 $options = empty($this->pluginOptions) ? '' : CJavaScript::encode($this->pluginOptions);
104 $js[] = "jQuery('#$id').{$name}({$options});";
105 }
106
107 if (!empty($this->clientEvents)) {
108 foreach ($this->clientEvents as $event => $handler) {
109 $js[] = "jQuery('#$id').on('$event', $handler);";
110 }
111 }
112 if(count($js))
113 {
114 $cs->registerScript($id, implode("\n", $js));
115 }
116 }
117
118 }