1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Input field for time value. |
8
|
|
|
* |
9
|
|
|
* Size is fixed to 10 characters. |
10
|
|
|
* Default the format is tried to get from the local environment, but can be |
11
|
|
|
* specified in the configuration. |
12
|
|
|
* |
13
|
|
|
* It can be configured to display the time value with or without seconds. |
14
|
|
|
* The timepicker also uses this settings. |
15
|
|
|
* |
16
|
|
|
* The value that is posted is always in the specified date format! |
17
|
|
|
* |
18
|
|
|
* @SKienImage FormTime.png |
19
|
|
|
* |
20
|
|
|
* @package Formgenerator |
21
|
|
|
* @author Stefanius <[email protected]> |
22
|
|
|
* @copyright MIT License - see the LICENSE file for details |
23
|
|
|
*/ |
24
|
|
|
class FormTime extends FormInput |
25
|
|
|
{ |
26
|
|
|
/** @var string strftime-compatible format for the time */ |
27
|
|
|
protected string $strTimeFormat = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates input field for time values. |
31
|
|
|
* @param string $strName Name (if no ID specified, name is used also as ID) |
32
|
|
|
* @param int $wFlags any combination of FormFlag constants |
33
|
|
|
*/ |
34
|
|
|
public function __construct(string $strName, int $wFlags = 0) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($strName, 10, $wFlags); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
* @see \SKien\Formgenerator\FormElement::fromXML() |
42
|
|
|
* @internal |
43
|
|
|
*/ |
44
|
|
|
static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement |
45
|
|
|
{ |
46
|
|
|
$strName = self::getAttribString($oXMLElement, 'name'); |
47
|
|
|
$wFlags = self::getAttribFlags($oXMLElement); |
48
|
|
|
$oFormElement = new self($strName, $wFlags); |
49
|
|
|
$oFormParent->add($oFormElement); |
50
|
|
|
$oFormElement->readAdditionalXML($oXMLElement); |
51
|
|
|
return $oFormElement; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* get time format from configuration (default: '%H:%M'). |
56
|
|
|
*/ |
57
|
|
|
protected function onParentSet() : void |
58
|
|
|
{ |
59
|
|
|
$this->setPlaceholder($this->oFG->getConfig()->getString('Time.Placeholder')); |
60
|
|
|
$bSeconds = $this->oFG->getConfig()->getBool('Time.Seconds', false); |
61
|
|
|
$strSep = $this->oFG->getConfig()->getString('Time.Separator', ':'); |
62
|
|
|
$this->strTimeFormat = ($bSeconds ? '%H:%M:%S' : '%H:%M'); |
63
|
|
|
if ($strSep !== ':') { |
64
|
|
|
$this->strTimeFormat = str_replace(':', $strSep, $this->strTimeFormat); |
65
|
|
|
} |
66
|
|
|
$this->addAttribute('data-validation', 'time:' . $strSep . ($bSeconds ? '1' : '0') . 'm'); |
67
|
|
|
$this->addPicker($strSep, $bSeconds); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Accept date/time value from Formgenerator-data as <ul> |
72
|
|
|
* <li> DateTime - object </li> |
73
|
|
|
* <li> unix timestamp (int) </li> |
74
|
|
|
* <li> English textual datetime description readable by <b>strtotime</b> <br/> |
75
|
|
|
* can be a DATE, DATETIME or TIMESTAMP value from a DB query |
76
|
|
|
* </li></ul> |
77
|
|
|
* The displayed format can be set in the configuration |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
* @see \SKien\Formgenerator\FormElement::buildValue() |
80
|
|
|
* @link https://www.php.net/manual/en/function.strftime.php |
81
|
|
|
*/ |
82
|
|
|
protected function buildValue() : string |
83
|
|
|
{ |
84
|
|
|
$date = $this->oFG->getData()->getValue($this->strName); |
85
|
|
|
|
86
|
|
|
$strValue = ''; |
87
|
|
|
if (is_object($date) && get_class($date) == 'DateTime') { |
88
|
|
|
// DateTime-object |
89
|
|
|
$strValue = strftime($this->strTimeFormat, $date->getTimestamp()); |
90
|
|
|
} else if (is_numeric($date)) { |
91
|
|
|
$strValue = strftime($this->strTimeFormat, intval($date)); |
92
|
|
|
} else { |
93
|
|
|
$unixtime = strtotime($date); |
94
|
|
|
if ($unixtime !== false) { |
95
|
|
|
$strValue = strftime($this->strTimeFormat, $unixtime); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$strHTML = ''; |
100
|
|
|
if (!$this->oFlags->isSet(FormFlags::NO_ZERO) || ($strValue != 0 && $strValue != '0')) { |
101
|
|
|
$strHTML = ' value="' . str_replace('"', '"', $strValue) . '"'; |
102
|
|
|
} |
103
|
|
|
return $strHTML; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add attributes for the time picker. |
108
|
|
|
* @param string $strSep |
109
|
|
|
* @param bool $bSeconds |
110
|
|
|
*/ |
111
|
|
|
protected function addPicker(string $strSep, bool $bSeconds) : void |
112
|
|
|
{ |
113
|
|
|
if ($this->oFlags->isSet(FormFlags::ADD_TIME_PICKER)) { |
114
|
|
|
$this->addAttribute('autocomplete', 'off'); |
115
|
|
|
$strTimePickerFormat = ($bSeconds ? 'HH:MM:SS"' : 'HH:MM'); |
116
|
|
|
if ($strSep !== ':') { |
117
|
|
|
$strTimePickerFormat = str_replace(':', $strSep, $strTimePickerFormat); |
118
|
|
|
} |
119
|
|
|
$this->addAttribute('data-picker', 'time:' . $strTimePickerFormat); |
120
|
|
|
$aDTSel = $this->oFG->getConfig()->getArray('DTSel'); |
121
|
|
|
if (count($aDTSel) > 0) { |
122
|
|
|
$this->oFG->addConfigForJS('DTSel', $aDTSel); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|