1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Input field for date 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
|
|
|
* The value that is submitted is always a string in the specified date format! |
14
|
|
|
* |
15
|
|
|
* @SKienImage FormDate.png |
16
|
|
|
* |
17
|
|
|
* @package Formgenerator |
18
|
|
|
* @author Stefanius <[email protected]> |
19
|
|
|
* @copyright MIT License - see the LICENSE file for details |
20
|
|
|
*/ |
21
|
|
|
class FormDate extends FormInput |
22
|
|
|
{ |
23
|
|
|
/** @var string strftime-compatible format for the date */ |
24
|
|
|
protected string $strDateFormat = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Creates input field for date values. |
28
|
|
|
* @param string $strName Name (if no ID specified, name is used also as ID) |
29
|
|
|
* @param int $wFlags any combination of FormFlag constants |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $strName, int $wFlags = 0) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($strName, '10', $wFlags); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
* @see \SKien\Formgenerator\FormElement::fromXML() |
39
|
|
|
* @internal |
40
|
|
|
*/ |
41
|
|
|
static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement |
42
|
|
|
{ |
43
|
|
|
$strName = self::getAttribString($oXMLElement, 'name'); |
44
|
|
|
$wFlags = self::getAttribFlags($oXMLElement); |
45
|
|
|
$oFormElement = new self($strName, $wFlags); |
46
|
|
|
$oFormParent->add($oFormElement); |
47
|
|
|
$oFormElement->readAdditionalXML($oXMLElement); |
48
|
|
|
return $oFormElement; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* get date format from configuration (default: '%Y-%m-%d'). |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
* @see \SKien\Formgenerator\FormInput::onParentSet() |
55
|
|
|
*/ |
56
|
|
|
protected function onParentSet() : void |
57
|
|
|
{ |
58
|
|
|
$this->setPlaceholder($this->oFG->getConfig()->getString('Date.Placeholder')); |
59
|
|
|
if ($this->oFG->getConfig()->getBool('Date.UseHTML5Type')) { |
60
|
|
|
// if the HTML5 date input used |
61
|
|
|
// - value must be in format YYYY-MM-DD |
62
|
|
|
// - ui-formating and validation is the job of the browser ;-) |
63
|
|
|
// TODO: - adjust width in % since type="date" input 'ignores' the size attrib! |
64
|
|
|
// and the size differs from browser to browser... |
65
|
|
|
// - may check browser => safari didn't support input type="date" |
66
|
|
|
$this->strType = 'date'; |
67
|
|
|
$this->strDateFormat = '%Y-%m-%d'; |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
$strFormat = strtoupper($this->oFG->getConfig()->getString('Date.Format', 'YMD')); |
71
|
|
|
$strSep = $this->oFG->getConfig()->getString('Date.Separator', '-'); |
72
|
|
|
$aFormat = ['YMD' => '%Y-%m-%d', 'DMY' => '%d-%m-%Y', 'MDY' => '%m-%d-%Y']; |
73
|
|
|
$this->strDateFormat = $aFormat[$strFormat] ?? '%Y-%m-%d'; |
74
|
|
|
if ($strSep !== '-') { |
75
|
|
|
$this->strDateFormat = str_replace('-', $strSep, $this->strDateFormat); |
76
|
|
|
} |
77
|
|
|
$this->addAttribute('data-validation', 'date:' . $strSep . $strFormat); |
78
|
|
|
$this->addPicker($strSep, $strFormat); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Accept date value from FormData as <ul> |
83
|
|
|
* <li> DateTime - object </li> |
84
|
|
|
* <li> unix timestamp (int) </li> |
85
|
|
|
* <li> English textual datetime description readable by <b>strtotime</b> <br/> |
86
|
|
|
* can be a DATE, DATETIME or TIMESTAMP value from a DB query |
87
|
|
|
* </li></ul> |
88
|
|
|
* The displayed format can be configured with the <i>'FormDate.Format'</i> parameter |
89
|
|
|
* as strftime-compatible format string (default settings: '%Y-%m-%d') |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
* @see \SKien\Formgenerator\FormElement::buildValue() |
92
|
|
|
* @link https://www.php.net/manual/en/function.strftime.php |
93
|
|
|
*/ |
94
|
|
|
protected function buildValue() : string |
95
|
|
|
{ |
96
|
|
|
$date = $this->oFG->getData()->getValue($this->strName); |
97
|
|
|
|
98
|
|
|
$strValue = ''; |
99
|
|
|
if (is_object($date) && get_class($date) == 'DateTime') { |
100
|
|
|
// DateTime-object |
101
|
|
|
$strValue = strftime($this->strDateFormat, $date->getTimestamp()); |
102
|
|
|
} else if (is_numeric($date)) { |
103
|
|
|
$strValue = strftime($this->strDateFormat, intval($date)); |
104
|
|
|
} else if ($date != '0000-00-00 00:00:00' && $date != '0000-00-00' && $date != '00:00:00') { |
105
|
|
|
$unixtime = strtotime($date); |
106
|
|
|
if ($unixtime !== false) { |
107
|
|
|
$strValue = strftime($this->strDateFormat, $unixtime); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$strHTML = ''; |
112
|
|
|
if (!$this->oFlags->isSet(FormFlags::NO_ZERO) || ($strValue != 0 && $strValue != '0')) { |
113
|
|
|
$strHTML = ' value="' . str_replace('"', '"', $strValue) . '"'; |
114
|
|
|
} |
115
|
|
|
return $strHTML; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Add attributes for the date picker. |
120
|
|
|
* @param string $strSep |
121
|
|
|
* @param string $strFormat |
122
|
|
|
*/ |
123
|
|
|
protected function addPicker(string $strSep, string $strFormat) : void |
124
|
|
|
{ |
125
|
|
|
if ($this->oFlags->isSet(FormFlags::ADD_DATE_PICKER)) { |
126
|
|
|
$this->addAttribute('autocomplete', 'off'); |
127
|
|
|
$aFormat = ['YMD' => 'yyyy-mm-dd', 'DMY' => 'dd-mm-yyyy', 'MDY' => 'mm-dd-yyyy']; |
128
|
|
|
$strDatePickerFormat = $aFormat[$strFormat] ?? 'yyyy-mm-dd'; |
129
|
|
|
if ($strSep !== '-') { |
130
|
|
|
$strDatePickerFormat = str_replace('-', $strSep, $strDatePickerFormat); |
131
|
|
|
} |
132
|
|
|
$this->addAttribute('data-picker', 'date:' . $strDatePickerFormat); |
133
|
|
|
$aDTSel = $this->oFG->getConfig()->getArray('DTSel'); |
134
|
|
|
if (count($aDTSel) > 0) { |
135
|
|
|
$this->oFG->addConfigForJS('DTSel', $aDTSel); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|