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 WhCountries 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
47 public function init()
48 {
49
50 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
51
52 TbHtml::addCssClass('bfh-countries', $this->htmlOptions);
53 }
54
55 56 57
58 public function run()
59 {
60 $this->renderField();
61 $this->registerClientScript();
62 }
63
64 65 66
67 public function renderField()
68 {
69 list($name, $id) = $this->resolveNameID();
70
71 TbArray::defaultValue('id', $id, $this->htmlOptions);
72 TbArray::defaultValue('name', $name, $this->htmlOptions);
73
74 if ($this->useHelperSelectBox) {
75 $select = Yii::createComponent(CMap::mergeArray($this->helperOptions, array(
76 'class' => 'yiiwheels.widgets.formhelpers.WhSelectBox',
77 'htmlOptions' => $this->htmlOptions,
78 'model' => $this->model,
79 'attribute' => $this->attribute,
80 'name' => $this->name,
81 'value' => $this->value,
82 'wrapperOptions' => array(
83 'class' => 'bfh-countries',
84 'data-country' => $this->hasModel() ? $this->model->{$this->attribute} : $this->value,
85 'data-flags' => isset($this->pluginOptions['flags']) && $this->pluginOptions['flags']
86 ? 'true'
87 : 'false',
88 )
89 )));
90 $select->init();
91 $select->run();
92 } else {
93 $this->htmlOptions['data-country'] = $this->hasModel()
94 ? $this->model->{$this->attribute}
95 : $this->value;
96
97 if (!$this->readOnly) {
98 if ($this->hasModel()) {
99 echo CHtml::activeDropDownList($this->model, $this->attribute, array(), $this->htmlOptions);
100 } else {
101 echo CHtml::dropDownList($name, $this->value, array(), $this->htmlOptions);
102 }
103 } else {
104 echo CHtml::tag('span', $this->htmlOptions);
105 }
106 }
107 }
108
109 110 111
112 public function registerClientScript()
113 {
114
115 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
116 $assetsUrl = $this->getAssetsUrl($path);
117
118
119 $cs = Yii::app()->getClientScript();
120
121 $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers.css');
122 if (isset($this->pluginOptions['flags']) && $this->pluginOptions['flags'] == true) {
123 $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers-countries.flags.css');
124 }
125 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-countries.en_US.js');
126 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-countries.js');
127
128
129 if (!$this->useHelperSelectBox) {
130 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
131 $this->getApi()->registerPlugin('bfhcountries', $selector, $this->pluginOptions);
132 }
133 }
134 }