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 WhStates extends CInputWidget
16 {
17 18 19 20 21 22 23
24 public $pluginOptions = array();
25
26 27 28
29 public $readOnly = false;
30
31 32 33
34 public $useHelperSelectBox = false;
35
36 37 38
39 public $helperOptions = array();
40
41
42 43 44 45
46 public function init()
47 {
48 if (!isset($this->pluginOptions['country'])) {
49 throw new CException(Yii::t('zii', '$pluginOptions["country"] cannot be blank.'));
50 }
51 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
52
53 TbHtml::addCssClass('bfh-states', $this->htmlOptions);
54 }
55
56 57 58
59 public function run()
60 {
61 $this->renderField();
62 $this->registerClientScript();
63 }
64
65 66 67
68 public function renderField()
69 {
70 list($name, $id) = $this->resolveNameID();
71
72 TbArray::defaultValue('id', $id, $this->htmlOptions);
73 TbArray::defaultValue('name', $name, $this->htmlOptions);
74
75 $this->htmlOptions['data-country'] = $this->pluginOptions['country'];
76 $this->pluginOptions['state'] = $this->htmlOptions['data-state'] = $this->hasModel()
77 ? $this->model->{$this->attribute}
78 : $this->value;
79
80 if ($this->useHelperSelectBox) {
81 $select = Yii::createComponent(CMap::mergeArray($this->helperOptions, array(
82 'class' => 'yiiwheels.widgets.formhelpers.WhSelectBox',
83 'htmlOptions' => $this->htmlOptions,
84 'model' => $this->model,
85 'attribute' => $this->attribute,
86 'name' => $this->name,
87 'value' => $this->value,
88 'wrapperOptions' => array(
89 'class' => 'bfh-countries',
90 'data-country' => $this->hasModel() ? $this->model->{$this->attribute} : $this->value,
91 'data-flags' => isset($this->pluginOptions['flags']) ? 'true' : 'false'
92 )
93 )));
94 $select->init();
95 $select->run();
96 } else {
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
123 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-states.en_US.js');
124 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-states.js');
125
126
127 if (!$this->useHelperSelectBox) {
128 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
129 $this->getApi()->registerPlugin('bfhstates', $selector, $this->pluginOptions);
130 }
131 }
132 }