|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Input field for time value. |
|
8
|
|
|
* - size always 10 |
|
9
|
|
|
* - field will be added to JS form validation |
|
10
|
|
|
* |
|
11
|
|
|
* #### History |
|
12
|
|
|
* - *2020-05-12* initial version |
|
13
|
|
|
* - *2021-01-07* PHP 7.4 |
|
14
|
|
|
* |
|
15
|
|
|
* @package Formgenerator |
|
16
|
|
|
* @version 1.1.0 |
|
17
|
|
|
* @author Stefanius <[email protected]> |
|
18
|
|
|
* @copyright MIT License - see the LICENSE file for details |
|
19
|
|
|
*/ |
|
20
|
|
|
class FormTime extends FormInput |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string strftime-compatible format for the time */ |
|
23
|
|
|
protected string $strTimeFormat = ''; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Creates input field for time values. |
|
27
|
|
|
* @param string $strName |
|
28
|
|
|
* @param int $wFlags |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(string $strName, int $wFlags = 0) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct($strName, 10, $wFlags); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* get time format from configuration (default: '%H:%M'). |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function onParentSet() : void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->setPlaceholder($this->oFG->getConfig()->getString('Time.Placeholder')); |
|
41
|
|
|
$bSeconds = $this->oFG->getConfig()->getBool('Time.Seconds', false); |
|
42
|
|
|
$strSep = $this->oFG->getConfig()->getString('Time.Separator', ':'); |
|
43
|
|
|
$this->strTimeFormat = ($bSeconds ? '%H:%M:%S' : '%H:%M'); |
|
44
|
|
|
if ($strSep !== ':') { |
|
45
|
|
|
$this->strTimeFormat = str_replace(':', $strSep, $this->strTimeFormat); |
|
46
|
|
|
} |
|
47
|
|
|
$this->addAttribute('data-validation', 'time:' . $strSep . ($bSeconds ? '1' : '0') . 'm'); |
|
48
|
|
|
$this->addPicker($strSep, $bSeconds); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Accept date value from Formgenerator-data as <ul> |
|
53
|
|
|
* <li> DateTime - object </li> |
|
54
|
|
|
* <li> unix timestamp (int) </li> |
|
55
|
|
|
* <li> English textual datetime description readable by <b>strtotime</b> <br/> |
|
56
|
|
|
* can be a DATE, DATETIME or TIMESTAMP value from a DB query |
|
57
|
|
|
* </li></ul> |
|
58
|
|
|
* The displayed format can be configured with the <i>'FormTime.Format'</i> parameter |
|
59
|
|
|
* as strftime-compatible format string (default settings: '%H:%M) |
|
60
|
|
|
* {@inheritDoc} |
|
61
|
|
|
* @see \SKien\Formgenerator\FormElement::buildValue() |
|
62
|
|
|
* @link https://www.php.net/manual/en/function.strftime.php |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function buildValue() : string |
|
65
|
|
|
{ |
|
66
|
|
|
$date = $this->oFG->getData()->getValue($this->strName); |
|
67
|
|
|
|
|
68
|
|
|
$strValue = ''; |
|
69
|
|
|
if (is_object($date) && get_class($date) == 'DateTime') { |
|
70
|
|
|
// DateTime-object |
|
71
|
|
|
$strValue = strftime($this->strTimeFormat, $date->getTimestamp()); |
|
72
|
|
|
} else if (is_numeric($date)) { |
|
73
|
|
|
$strValue = strftime($this->strTimeFormat, $date); |
|
74
|
|
|
} else { |
|
75
|
|
|
$unixtime = strtotime($date); |
|
76
|
|
|
if ($unixtime !== false) { |
|
77
|
|
|
$strValue = strftime($this->strTimeFormat, $unixtime); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$strHTML = ''; |
|
82
|
|
|
if (!$this->oFlags->isSet(FormFlags::NO_ZERO) || ($strValue != 0 && $strValue != '0')) { |
|
83
|
|
|
$strHTML = ' value="' . str_replace('"', '"', $strValue) . '"'; |
|
84
|
|
|
} |
|
85
|
|
|
return $strHTML; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Add attributes for the time picker. |
|
90
|
|
|
* @param string $strSep |
|
91
|
|
|
* @param bool $bSeconds |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function addPicker(string $strSep, bool $bSeconds) : void |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->oFlags->isSet(FormFlags::ADD_TIME_PICKER)) { |
|
96
|
|
|
$this->addAttribute('autocomplete', 'off'); |
|
97
|
|
|
$strTimePickerFormat = ($bSeconds ? 'HH:MM:SS"' : 'HH:MM'); |
|
98
|
|
|
if ($strSep !== ':') { |
|
99
|
|
|
$strTimePickerFormat = str_replace(':', $strSep, $strTimePickerFormat); |
|
100
|
|
|
} |
|
101
|
|
|
$this->addAttribute('data-picker', 'time:' . $strTimePickerFormat); |
|
102
|
|
|
$aDTSel = $this->oFG->getConfig()->getArray('DTSel'); |
|
103
|
|
|
if (count($aDTSel) > 0) { |
|
104
|
|
|
$this->oFG->addConfigForJS('DTSel', $aDTSel); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|