-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInputfieldTime.module
More file actions
187 lines (148 loc) · 5.35 KB
/
InputfieldTime.module
File metadata and controls
187 lines (148 loc) · 5.35 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
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
/**
* An Inputfield for handling time inputs.
*/
class InputfieldTime extends Inputfield
{
public static function getModuleInfo()
{
return array(
'title' => __('Time', __FILE__),
'summary' => __('Allows entry of times in various formats', __FILE__),
'version' => '0.2.2',
'permanent' => false,
'autoload' => false,
'singular' => false,
'requires' => array('FieldtypeTime'),
'installs' => array('FieldtypeTime'),
'author' => 'netcarver',
'href' => 'https://processwire.com/talk/topic/7857-module-fieldtypetime-inputfieldtime/',
);
}
public function __construct()
{
parent::__construct();
$this->setAttribute('inhibitClockPicker', 0); // Set to zero if clock picker not wanted
$this->setAttribute('htmlInput', 0);
}
public function init()
{
$this->attr('type', 'text');
$this->attr('size', 10);
$this->attr('maxlength', 10);
$this->attr('placeholder', '');
$this->attr('pattern', '');
parent::init();
}
public function ___renderValue()
{
return htmlspecialchars($this->value, ENT_QUOTES, "UTF-8");
}
public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
$c = $this->config;
$c->scripts->add($c->urls->InputfieldTime . 'InputfieldTime.trigger.js');
$r = parent::renderReady($parent, $renderValueMode);
return $r;
}
public function ___render()
{
$out = '';
$format = $this->hasField->format;
$htmlCompatibleFormat = in_array($format, ['HH24MM', 'HH24MMSS']);
if ($this->htmlInput && $htmlCompatibleFormat) {
$this->setAttribute('type', 'time');
if($format == "HH24MMSS"){
$this->setAttribute('step', 1);
}
if($this->wire()->adminTheme->className() == "AdminThemeUikit") {
$this->class('uk-input');
}
$out .= "\n<input " . $this->getAttributesString() . "/>";
} else {
if (!$this->getAttribute('inhibitClockPicker') && 'HH24MM' == $this->format) {
$id = $this->id;
$out .= "\n<div class=\"input-group clockpicker\" data-autoclose=\"true\">";
$out .= "\n<input " . $this->getAttributesString() . " onclick=\"InputFieldTimePickerTrigger('#$id')\"/>";
$out .= "\n<span class=\"input-group-addon\">\n\t<span class=\"fa fa-clock-o\"></span>\n</span>";
$out .= "\n</div>";
} else {
$out .= "\n<input " . $this->getAttributesString() . " />";
}
}
return $out;
}
public function getAttributes()
{
$attrs = parent::getAttributes();
if (empty($attrs['size'])) {
unset($attrs['size']);
$attrs['class'] = (empty($attrs['class']) ? '' : $attrs['class'] . ' ') . 'InputfieldMaxWidth';
}
$attrs['placeholder'] = "00:00";
return $attrs;
}
public function setAttribute($key, $value)
{
if ($key == 'value') {
$value = $this->sanitizeValue($value);
}
return parent::setAttribute($key, $value);
}
protected function sanitizeValue($value)
{
$value = trim((string)$value);
$format = $this->format;
$colons = $this->useColons;
if (!isset($value) || !strlen("$value")) {
return $value;
}
$negative = substr($value, 0, 1) === '-';
if($negative) {
$value = substr($value, 1);
}
$neg_ok = '-' === substr($format, -1);
if (!$neg_ok) {
$negative = false;
} else {
$format = substr($format, 0, -1);
}
$value = str_replace('-', ':', $value);
$value = preg_replace('/[^\d:]/', '', $value);
if ($negative) {
$value = '-' . $value;
}
return $value;
}
public function ___processInput(WireInputData $input)
{
parent::___processInput($input);
$value = (string) $this->attr('value');
if (strlen($value)) {
/**
* General time string regex - only allows '-', ':' or digits.
*/
$regex = '#^[\d:-]+$#';
if (!preg_match($regex, $value)) {
$this->error($this->_('Only digits and colons (with or without a leading -) are allowed.'));
}
}
return $this;
}
public function ___getConfigInputfields()
{
$inputfields = parent::___getConfigInputfields();
$field = $this->modules->get('InputfieldCheckbox');
$field->label = $this->_('Inhibit display of clock-picker for HH24MM time fields?');
$field->attr('name', 'inhibitClockPicker');
$field->attr('value', 1);
$field->attr('checked', $this->getAttribute('inhibitClockPicker') ? 'checked' : '');
$inputfields->append($field);
$field = $this->modules->get('InputfieldCheckbox');
$field->label = $this->_('Use browser native input?');
$field->attr('name', 'htmlInput');
$field->attr('value', 1);
$field->attr('checked', $this->getAttribute('htmlInput') ? 'checked' : '');
$inputfields->append($field);
return $inputfields;
}
}