-
Notifications
You must be signed in to change notification settings - Fork 1
/
Money.php
executable file
·121 lines (97 loc) · 2.73 KB
/
Money.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace amilna\yap;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
use amilna\yap\MoneyAsset;
/**
* This is just an example.
*/
class Money extends InputWidget
{
public $name;
public $value = 0;
public $pluginOptions = [
"radixPoint"=>".",
"groupSeparator"=> ",",
"digits"=> 2,
"autoGroup"=> true,
"prefix"=> ''
];
public $pluginEvents = [];
private $_displayOptions = [];
public function init()
{
parent::init();
Html::addCssClass($this->options, 'form-control');
Html::addCssClass($this->options, 'yap-money');
$this->_displayOptions = $this->options;
$this->_displayOptions['id'] .= '-disp';
if (isset($this->_displayOptions['name'])) {
unset($this->_displayOptions['name']);
}
$this->registerAssets();
if ($this->hasModel())
{
$model = $this->model;
$atr = $this->attribute;
if (empty($model->$atr))
{
$model->$atr = 0;
}
$this->value = $model->$atr;
}
$input = Html::textInput($this->name, $this->value, $this->_displayOptions);
$input .= $this->hasModel() ?
Html::activeHiddenInput($this->model, $this->attribute) :
Html::hiddenInput($this->name, $this->value);
echo $input;
}
public function run()
{
$view = $this->getView();
$model = $this->model;
$idModel = strtolower($model->formName());
$inputId = $this->attribute;
$options = json_encode($this->pluginOptions);
$modelId = strtolower($idModel."-".$inputId);
$ts = $this->pluginOptions["groupSeparator"];
$ds = $this->pluginOptions["radixPoint"];
$ps = $this->pluginOptions["prefix"];
$js2= "";
$evs = "";
foreach ($this->pluginEvents as $en=>$ev)
{
$js2 .= '
$("#'.$modelId.'").'.$en.'('.$ev.');
';
$evs .= '$("#'.$modelId.'").trigger("'.$en.'");';
}
$js = <<<SCRIPT
$("#$modelId-disp").inputmask("decimal",$options);
$("#$modelId-disp").change(function(){
var val = parseFloat($("#$modelId-disp").val().replace("$ps","").replace(/\\$ts/g,"").replace(/\\$ds/g,"."));
val = (isNaN(val)?0:val);
$("#$modelId").val(val);
$evs
});
SCRIPT;
$view->registerJs($js2);
$view->registerJs($js);
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$view = $this->getView();
MoneyAsset::register($view);
//
// $attr = $this->attribute;
// $js = <<<SCRIPT
//
//SCRIPT;
//
// $view->registerJs($js);
}
}