-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathSensitive.php
More file actions
139 lines (118 loc) · 3.18 KB
/
Sensitive.php
File metadata and controls
139 lines (118 loc) · 3.18 KB
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
<?php namespace Backend\FormWidgets;
use Backend\Classes\FormWidgetBase;
use Winter\Storm\Html\Helper as HtmlHelper;
/**
* Sensitive widget.
*
* Renders a password field that can be optionally made visible
*
* @package winter\wn-backend-module
*/
class Sensitive extends FormWidgetBase
{
/**
* @var bool If true, the sensitive field cannot be edited, but can be toggled.
*/
public $readOnly = false;
/**
* @var bool If true, the sensitive field is disabled.
*/
public $disabled = false;
/**
* @var bool If true, a button will be available to copy the value.
*/
public $allowCopy = false;
/**
* @var string The string that will be used as a placeholder for an unrevealed sensitive value.
*/
public $hiddenPlaceholder = '__hidden__';
/**
* @var bool If true, the sensitive input will be hidden if the user changes to another tab in their browser.
*/
public $hideOnTabChange = true;
/**
* @inheritDoc
*/
protected $defaultAlias = 'sensitive';
/**
* @inheritDoc
*/
public function init()
{
$this->fillFromConfig([
'readOnly',
'disabled',
'allowCopy',
'hiddenPlaceholder',
'hideOnTabChange',
]);
if ($this->formField->disabled || $this->formField->readOnly) {
$this->previewMode = true;
}
}
/**
* @inheritDoc
*/
public function render()
{
$this->prepareVars();
return $this->makePartial('sensitive');
}
/**
* Prepares the view data for the widget partial.
*/
public function prepareVars()
{
$this->vars['readOnly'] = $this->readOnly;
$this->vars['disabled'] = $this->disabled;
$this->vars['hasValue'] = !empty($this->getLoadValue());
$this->vars['allowCopy'] = $this->allowCopy;
$this->vars['hiddenPlaceholder'] = $this->hiddenPlaceholder;
$this->vars['hideOnTabChange'] = $this->hideOnTabChange;
}
/**
* Reveals the value of a hidden, unmodified sensitive field.
*
* @return array
*/
public function onShowValue()
{
return [
'value' => $this->getLoadValue()
];
}
/**
* @inheritDoc
*/
public function getLoadValue()
{
if ($this->formField->value !== null && $this->formField->value != $this->hiddenPlaceholder) {
return $this->formField->value;
}
$parts = HtmlHelper::nameToArray($this->getFieldName());
array_shift($parts); // remove Model name
$fieldName = array_shift($parts);
$value = $this->model->getAttribute($fieldName);
if (count($parts) && is_array($value)) {
$value = array_get($value, implode('.', $parts));
}
return $value;
}
/**
* @inheritDoc
*/
public function getSaveValue($value)
{
if ($value === $this->hiddenPlaceholder) {
$value = $this->getLoadValue();
}
return $value;
}
/**
* @inheritDoc
*/
protected function loadAssets()
{
$this->addJs('js/dist/sensitive.js', 'core');
}
}