-
Notifications
You must be signed in to change notification settings - Fork 4
/
InputWidget.php
187 lines (168 loc) · 6.58 KB
/
InputWidget.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* @copyright Copyright © 2015 Andrew Blake
* @package andrewblake1\yii2-credit-card
* @license https://github.com/andrewblake1/yii2-credit-card/blob/master/LICENSE.md MIT License
* @link https://github.com/andrewblake1/yii2-credit-card
* @version 1.1.1
*/
namespace andrewblake1\creditcard;
use kartik\base\Config;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\BaseHtml;
use yii\helpers\Html;
use yii\validators\StringValidator;
/**
* Extends \kartik\base\InputWidget in order to add the ability to not add a name attribute to an input. This is
* significant when you don't want the input to be submitted for security reasons. Stripe (and I think Braintree if I
* recall correctly) provide javascript API the will take accept the values and return a token to be submitted instead.
*
* This helps for PCI compliance.
*
* @author Andrew Blake <[email protected]>
*/
class InputWidget extends \kartik\base\InputWidget
{
/**
* @var string/boolean the addon content
*/
public $addon;
/**
* @var array HTML attributes for the addon container
* the following special options are identified
* - asButton: boolean if the addon is to be displayed as a button.
* - buttonOptions: array HTML attributes if the addon is to be
* displayed like a button. If [[asButton]] is true, this will
* default to ['class' => 'btn btn-default']
*/
public $addonOptions = [];
/**
* @var string use [[right]] or [[left]] type of addon
* position near to input
*/
public $addonPosition = 'right';
/**
* @var array HTML attributes for the input group container
*/
public $containerOptions = [];
/**
* @var string The value for the type attribute for the input
*/
public $type = 'tel';
/**
* @var string The value of the auto complete attribute
*/
public $autocomplete;
/**
* @var bool If true then name attribute will be added into the input. If false then it won't. This is significant
* when you don't want the input to be submitted for security reasons. Stripe (and I think Braintree if I recall
* correctly) provide javascript API the will take accept the values and return a token to be submitted instead.
* This helps for PCI compliance.
*/
public $submit = true;
public function init()
{
parent::init();
$this->_msgCat = 'creditcard';
$this->initI18N(__DIR__);
$this->options['type'] = $this->type;
$this->options['autocomplete'] = $this->autocomplete;
$this->registerAssets();
}
public function run()
{
return Html::tag('div', $this->renderInput(), $this->containerOptions);
}
/**
* This is largely copied from kartik\time\TimePicker
* @return string
*/
protected function renderInput()
{
Html::addCssClass($this->options, 'form-control');
if (!empty($this->options['disabled'])) {
Html::addCssClass($this->addonOptions, 'disabled-addon');
}
if (ArrayHelper::getValue($this->pluginOptions, 'template', true) === false) {
if (isset($this->size)) {
Html::addCssClass($this->options, 'input-' . $this->size);
Html::addCssClass($this->addonOptions, 'inline-addon inline-addon-' . $this->size);
} else {
Html::addCssClass($this->addonOptions, 'inline-addon');
}
return $this->getInput('textInput') . Html::tag('span', $this->addon, $this->addonOptions);
}
Html::addCssClass($this->containerOptions, 'input-group');
$asButton = ArrayHelper::remove($this->addonOptions, 'asButton', false);
$buttonOptions = ArrayHelper::remove($this->addonOptions, 'buttonOptions', []);
if ($asButton) {
Html::addCssClass($this->addonOptions, 'input-group-btn picker');
$buttonOptions['type'] = 'button';
if (empty($buttonOptions['class'])) {
Html::addCssClass($buttonOptions, 'btn btn-default');
}
$content = Html::button($this->addon, $buttonOptions);
} else {
Html::addCssClass($this->addonOptions, 'input-group-addon picker');
$content = $this->addon;
}
$addon = $this->addon
? Html::tag('span', $content, $this->addonOptions)
: null;
if (isset($this->size)) {
Html::addCssClass($this->containerOptions, 'input-group-' . $this->size);
}
switch($this->addonPosition) {
default:
case 'left':
return $addon . $this->getInput('textInput');
break;
case 'right':
return $this->getInput('textInput') . $addon;
break;
}
}
/**
* {@inheritDoc} Extends the parent to leave out code duplicated in getInput whereby name and value get set again.
*/
protected function initInputWidget()
{
$this->initI18N(__DIR__, 'kvbase');
if (!isset($this->language)) {
$this->language = Yii::$app->language;
}
$this->_lang = Config::getLang($this->language);
if ($this->pluginLoading) {
$this->_loadIndicator = self::LOAD_PROGRESS;
}
$this->initDisability($this->options);
}
protected function getInput($type, $list = false)
{
if ($this->hasModel()) {
if (isset($this->options['maxlength']) && $this->options['maxlength'] === true) {
unset($this->options['maxlength']);
foreach ($this->model->getActiveValidators($this->attribute) as $validator) {
if ($validator instanceof StringValidator && $validator->max !== null) {
$this->options['maxlength'] = $validator->max;
break;
}
}
}
if ($this->submit) {
$this->name = isset($this->options['name']) ? $this->options['name'] : BaseHtml::getInputName($this->model, $this->attribute);
}
$this->value = isset($this->options['value']) ? $this->options['value'] : BaseHtml::getAttributeValue($this->model, $this->attribute);
if (!array_key_exists('id', $this->options)) {
$this->options['id'] = BaseHtml::getInputId($this->model, $this->attribute);
}
}
return Html::textInput($this->name, $this->value, $this->options);
}
public function registerAssets()
{
$view = $this->getView();
CreditCardAsset::register($view);
}
}