1 <?php
2 3 4 5 6 7 8 9 10 11
12 Yii::import('bootstrap.helpers.TbArray');
13 Yii::import('bootstrap.helpers.TbHtml');
14
15 class WhLanguages extends CInputWidget
16 {
17 18 19 20 21 22 23 24
25 public $pluginOptions = array();
26
27 28 29
30 public $readOnly = false;
31
32 33 34
35 public $useHelperSelectBox = false;
36
37 38 39
40 public $helperOptions = array();
41
42 43 44 45
46 public function init()
47 {
48
49 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
50
51 TbHtml::addCssClass('bfh-languages', $this->htmlOptions);
52 }
53
54 55 56
57 public function run()
58 {
59 $this->renderField();
60 $this->registerClientScript();
61 }
62
63 64 65
66 public function renderField()
67 {
68 list($name, $id) = $this->resolveNameID();
69
70 TbArray::defaultValue('id', $id, $this->htmlOptions);
71 TbArray::defaultValue('name', $name, $this->htmlOptions);
72
73 if ($this->useHelperSelectBox) {
74 $select = Yii::createComponent(CMap::mergeArray($this->helperOptions, array(
75 'class' => 'yiiwheels.widgets.formhelpers.WhSelectBox',
76 'htmlOptions' => $this->htmlOptions,
77 'model' => $this->model,
78 'attribute' => $this->attribute,
79 'name' => $this->name,
80 'value' => $this->value,
81 'wrapperOptions' => array(
82 'class' => 'bfh-languages',
83 'data-language' => $this->hasModel() ? $this->model->{$this->attribute} : $this->value,
84 'data-flags' => isset($this->pluginOptions['flags']) && $this->pluginOptions['flags']
85 ? 'true'
86 : 'false',
87 'data-available' => isset($this->pluginOptions['available'])
88 ? $this->pluginOptions['available']
89 : null
90 )
91 )));
92 $select->init();
93 $select->run();
94 } else {
95 $this->htmlOptions['data-language'] = $this->hasModel()
96 ? $this->model->{$this->attribute}
97 : $this->value;
98 if (!$this->readOnly) {
99 if ($this->hasModel()) {
100 echo CHtml::activeDropDownList($this->model, $this->attribute, array(), $this->htmlOptions);
101 } else {
102 echo CHtml::dropDownList($name, $this->value, array(), $this->htmlOptions);
103 }
104 } else {
105 echo CHtml::tag('span', $this->htmlOptions);
106 }
107 }
108 }
109
110 111 112
113 public function registerClientScript()
114 {
115
116 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
117 $assetsUrl = $this->getAssetsUrl($path);
118
119
120 $cs = Yii::app()->getClientScript();
121
122 $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers.css');
123 if (isset($this->pluginOptions['flags']) && $this->pluginOptions['flags'] == true) {
124 $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers-countries.flags.css');
125 }
126 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-countries.en_US.js');
127 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-languages.codes.js');
128 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-languages.js');
129
130
131 if (!$this->useHelperSelectBox) {
132 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
133 $this->getApi()->registerPlugin('bfhlanguages', $selector, $this->pluginOptions);
134 }
135 }
136 }