|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Input field for date 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 FormDate extends FormInput |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string strftime-compatible format for the date */ |
|
23
|
|
|
protected string $strDateFormat = ''; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Creates input field for date 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 date format from configuration (default: '%Y-%m-%d'). |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function onParentSet() : void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->setPlaceholder($this->oFG->getConfig()->getString('Date.Placeholder')); |
|
41
|
|
|
if ($this->oFG->getConfig()->getBool('Date.UseHTML5Type')) { |
|
42
|
|
|
// if the HTML5 date input used |
|
43
|
|
|
// - value must be in format YYYY-MM-DD |
|
44
|
|
|
// - ui-formating and validation is the job of the browser ;-) |
|
45
|
|
|
// TODO: - adjust width since date input 'ignores' the size attrib! |
|
46
|
|
|
// - may check browser => safari didn't support input type="date" |
|
47
|
|
|
$this->strType = 'date'; |
|
48
|
|
|
$this->strDateFormat = '%Y-%m-%d'; |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
$strFormat = strtoupper($this->oFG->getConfig()->getString('Date.Format', 'YMD')); |
|
52
|
|
|
$strSep = $this->oFG->getConfig()->getString('Date.Separator', '-'); |
|
53
|
|
|
$aFormat = ['YMD' => '%Y-%m-%d', 'DMY' => '%d-%m-%Y', 'MDY' => '%m-%d-%Y']; |
|
54
|
|
|
$this->strDateFormat = $aFormat[$strFormat] ?? '%Y-%m-%d'; |
|
55
|
|
|
if ($strSep !== '-') { |
|
56
|
|
|
$this->strDateFormat = str_replace('-', $strSep, $this->strDateFormat); |
|
57
|
|
|
} |
|
58
|
|
|
$this->addAttribute('data-validation', 'date:' . $strSep . $strFormat); |
|
59
|
|
|
$this->addPicker($strSep, $strFormat); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Accept date value from Formgenerator-data as <ul> |
|
64
|
|
|
* <li> DateTime - object </li> |
|
65
|
|
|
* <li> unix timestamp (int) </li> |
|
66
|
|
|
* <li> English textual datetime description readable by <b>strtotime</b> <br/> |
|
67
|
|
|
* can be a DATE, DATETIME or TIMESTAMP value from a DB query |
|
68
|
|
|
* </li></ul> |
|
69
|
|
|
* The displayed format can be configured with the <i>'FormDate.Format'</i> parameter |
|
70
|
|
|
* as strftime-compatible format string (default settings: '%Y-%m-%d') |
|
71
|
|
|
* {@inheritDoc} |
|
72
|
|
|
* @see \SKien\Formgenerator\FormElement::buildValue() |
|
73
|
|
|
* @link https://www.php.net/manual/en/function.strftime.php |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function buildValue() : string |
|
76
|
|
|
{ |
|
77
|
|
|
$date = $this->oFG->getData()->getValue($this->strName); |
|
78
|
|
|
|
|
79
|
|
|
$strValue = ''; |
|
80
|
|
|
if (is_object($date) && get_class($date) == 'DateTime') { |
|
81
|
|
|
// DateTime-object |
|
82
|
|
|
$strValue = strftime($this->strDateFormat, $date->getTimestamp()); |
|
83
|
|
|
} else if (is_numeric($date)) { |
|
84
|
|
|
$strValue = strftime($this->strDateFormat, $date); |
|
85
|
|
|
} else { |
|
86
|
|
|
if ($date != '0000-00-00 00:00:00' && $date != '0000-00-00' && $date != '00:00:00') { |
|
87
|
|
|
$unixtime = strtotime($date); |
|
88
|
|
|
if ($unixtime !== false) { |
|
89
|
|
|
$strValue = strftime($this->strDateFormat, $unixtime); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$strHTML = ''; |
|
95
|
|
|
if (!$this->oFlags->isSet(FormFlags::NO_ZERO) || ($strValue != 0 && $strValue != '0')) { |
|
96
|
|
|
$strHTML = ' value="' . str_replace('"', '"', $strValue) . '"'; |
|
97
|
|
|
} |
|
98
|
|
|
return $strHTML; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Add attributes for the date picker. |
|
103
|
|
|
* @param string $strSep |
|
104
|
|
|
* @param string $strFormat |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function addPicker(string $strSep, string $strFormat) : void |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->oFlags->isSet(FormFlags::ADD_DATE_PICKER)) { |
|
109
|
|
|
$this->addAttribute('autocomplete', 'off'); |
|
110
|
|
|
$aFormat = ['YMD' => 'yyyy-mm-dd', 'DMY' => 'dd-mm-yyyy', 'MDY' => 'mm-dd-yyyy']; |
|
111
|
|
|
$strDatePickerFormat = $aFormat[$strFormat] ?? 'yyyy-mm-dd'; |
|
112
|
|
|
if ($strSep !== '-') { |
|
113
|
|
|
$strDatePickerFormat = str_replace('-', $strSep, $strDatePickerFormat); |
|
114
|
|
|
} |
|
115
|
|
|
$this->addAttribute('data-picker', 'date:' . $strDatePickerFormat); |
|
116
|
|
|
$aDTSel = $this->oFG->getConfig()->getArray('DTSel'); |
|
117
|
|
|
if (count($aDTSel) > 0) { |
|
118
|
|
|
$this->oFG->addConfigForJS('DTSel', $aDTSel); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|