1 <?php
2 3 4 5 6 7 8 9 10
11
12 Yii::import('bootstrap.helpers.TbArray');
13
14 class WhMaskInput extends CInputWidget
15 {
16
17 18 19 20
21 public $pluginOptions;
22
23 24 25
26 public $mask = '';
27
28 29 30
31 public function init()
32 {
33 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
34 }
35
36 37 38
39 public function run()
40 {
41 $this->renderField();
42 $this->registerClientScript();
43 }
44
45 46 47
48 public function renderField()
49 {
50 list($name, $id) = $this->resolveNameID();
51
52 TbArray::defaultValue('id', $id, $this->htmlOptions);
53 TbArray::defaultValue('name', $name, $this->htmlOptions);
54
55 if ($this->hasModel()) {
56 echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
57 } else {
58 echo CHtml::textField($this->name, $this->value, $this->htmlOptions);
59 }
60 }
61
62 63 64
65 public function registerClientScript()
66 {
67
68 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
69 $assetsUrl = $this->getAssetsUrl($path);
70
71
72 $cs = Yii::app()->getClientScript();
73
74 $cs->registerScriptFile($assetsUrl . '/js/jquery.mask.js');
75
76
77 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
78
79 $options = !empty($this->pluginOptions) ? CJavaScript::encode($this->pluginOptions) : '{}';
80 $script = "jQuery('{$selector}').mask('{$this->mask}',{$options});";
81 Yii::app()->clientScript->registerScript((uniqid(__CLASS__ . '#', true)), $script, CClientScript::POS_END);
82 }
83 }