1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\FormElements; |
4
|
|
|
|
5
|
|
|
use SilverStripe\i18n\i18n; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Format date field using ISO value |
9
|
|
|
* |
10
|
|
|
* Serves as a base field for all date field since we need datetime alias |
11
|
|
|
* |
12
|
|
|
* Locale conversion cannot be done by InputMask and should be provided by a third party service |
13
|
|
|
* |
14
|
|
|
* @link https://robinherbots.github.io/Inputmask/#/documentation/datetime |
15
|
|
|
*/ |
16
|
|
|
class InputMaskDateTimeField extends InputMaskField |
17
|
|
|
{ |
18
|
|
|
use HasDateTimeFormat; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Disable description |
22
|
|
|
* |
23
|
|
|
* @var boolean |
24
|
|
|
*/ |
25
|
|
|
protected $disableDescription = false; |
26
|
|
|
|
27
|
|
|
public function __construct($name, $title = null, $value = null) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($name, $title, $value); |
30
|
|
|
|
31
|
|
|
$this->setAlias(self::ALIAS_DATETIME); |
32
|
|
|
$format = self::getDefaultDateFormat() . " HH:MM:ss"; |
33
|
|
|
$this->setInputFormat($format); |
34
|
|
|
$this->setPlaceholder($format); |
35
|
|
|
// use ISO date format when unmasking to ensure proper data storage in the db |
36
|
|
|
$this->setOutputFormat('yyyy-mm-dd HH:MM:ss'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setValue($value, $data = null) |
40
|
|
|
{ |
41
|
|
|
// Normalize input value according to our format |
42
|
|
|
if ($value) { |
43
|
|
|
$value = date((self::convertDateFormatToPhp(self::getDefaultDateFormat())) . ' H:i:s', strtotime($value)); |
44
|
|
|
} |
45
|
|
|
$this->value = $value; |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the input format for inputmask |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public static function getDefaultDateFormat() |
54
|
|
|
{ |
55
|
|
|
$config = self::config()->get('default_input_format'); |
56
|
|
|
if (!$config || $config == 'auto') { |
57
|
|
|
$locale = strtolower(substr(i18n::get_locale(), 0, 2)); |
58
|
|
|
switch ($locale) { |
59
|
|
|
case 'fr': |
60
|
|
|
case 'nl': |
61
|
|
|
return 'dd/mm/yyyy'; |
62
|
|
|
break; |
|
|
|
|
63
|
|
|
default: |
64
|
|
|
return 'yyyy-mm-dd'; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return $config; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $format |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public static function convertDateFormatToPhp($format) |
75
|
|
|
{ |
76
|
|
|
$format = str_replace('yyyy', 'Y', $format); |
77
|
|
|
$format = str_replace('mm', 'm', $format); |
78
|
|
|
$format = str_replace('dd', 'd', $format); |
79
|
|
|
return $format; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Format used to input the date |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getInputFormat() |
88
|
|
|
{ |
89
|
|
|
return $this->getConfig('inputFormat'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function setInputFormat($value) |
93
|
|
|
{ |
94
|
|
|
return $this->setConfig('inputFormat', $value); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Unmasking format |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getOutputFormat() |
103
|
|
|
{ |
104
|
|
|
return $this->getConfig('outputFormat'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
public function setOutputFormat($value) |
109
|
|
|
{ |
110
|
|
|
return $this->setConfig('outputFormat', $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Visual format when the input looses focus |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getDisplayFormat() |
119
|
|
|
{ |
120
|
|
|
return $this->getConfig('displayFormat'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function setDisplayFormat($value) |
124
|
|
|
{ |
125
|
|
|
return $this->setConfig('displayFormat', $value); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param int $seconds |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public static function secondsToTime($seconds) |
133
|
|
|
{ |
134
|
|
|
$t = round($seconds); |
135
|
|
|
return sprintf('%02d:%02d:%02d', ($t / 3600), ($t / 60 % 60), $t % 60); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $time |
140
|
|
|
* @return int |
141
|
|
|
*/ |
142
|
|
|
public static function timeToSeconds($time) |
143
|
|
|
{ |
144
|
|
|
sscanf($time, "%d:%d:%d", $hours, $minutes, $seconds); |
|
|
|
|
145
|
|
|
$result = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes; |
|
|
|
|
146
|
|
|
return (int)$result; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function setDescription($description) |
150
|
|
|
{ |
151
|
|
|
// Allows blocking scaffolded UI desc that has no uses |
152
|
|
|
if ($this->disableDescription) { |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
return parent::setDescription($description); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get disable description |
160
|
|
|
* |
161
|
|
|
* @return boolean |
162
|
|
|
*/ |
163
|
|
|
public function getDisableDescription() |
164
|
|
|
{ |
165
|
|
|
return $this->disableDescription; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set disable description |
170
|
|
|
* |
171
|
|
|
* @param boolean $disableDescription |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function setDisableDescription($disableDescription) |
176
|
|
|
{ |
177
|
|
|
$this->disableDescription = $disableDescription; |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.