1 <?php
2 3 4 5 6 7 8 9 10
11 Yii::import('bootstrap.helpers.TbArray');
12
13 class WhPhone extends CInputWidget
14 {
15
16 17 18
19 public $format = false;
20
21 22 23
24 public $readOnly = false;
25
26 27 28
29 public $pluginOptions = array();
30
31
32 33 34 35
36 public function init()
37 {
38
39 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
40
41 TbHtml::addCssClass('bfh-phone', $this->htmlOptions);
42 $this->htmlOptions['data-format'] = $this->format;
43 if ($this->readOnly) {
44 $this->htmlOptions['data-number'] = $this->hasModel()
45 ? $this->model->{$this->attribute}
46 : $this->value;
47 } else {
48 $this->pluginOptions['format'] = $this->format;
49 $this->pluginOptions['value'] = $this->hasModel()
50 ? $this->model->{$this->attribute}
51 : $this->value;
52 }
53
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
76 if (!$this->readOnly) {
77 if ($this->hasModel()) {
78 echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
79 } else {
80 echo CHtml::textField($name, $this->value, $this->htmlOptions);
81 }
82 } else {
83 echo CHtml::tag('span', $this->htmlOptions);
84 }
85 }
86
87 88 89
90 public function registerClientScript()
91 {
92
93 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
94 $assetsUrl = $this->getAssetsUrl($path);
95
96
97 $cs = Yii::app()->getClientScript();
98
99 $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers.css');
100 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-phone.js');
101
102
103 if(!$this->readOnly)
104 {
105 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
106 $this->getApi()->registerPlugin('bfhphone', $selector, $this->pluginOptions);
107 }
108 }
109 }