|
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
|
|
|
// TODO: use localeconv() ?? |
|
41
|
|
|
$strFormat = strtoupper($this->oFG->getConfig()->getString('Date.Format', 'YMD')); |
|
42
|
|
|
$strSep = $this->oFG->getConfig()->getString('Date.Separator', '-'); |
|
43
|
|
|
$aFormat = ['YMD' => '%Y-%m-%d', 'DMY' => '%d-%m-%Y', 'MDY' => '%m-%d-%Y']; |
|
44
|
|
|
$this->strDateFormat = $aFormat[$strFormat] ?? '%Y-%m-%d'; |
|
45
|
|
|
if ($strSep !== '-') { |
|
46
|
|
|
$this->strDateFormat = str_replace('-', $strSep, $this->strDateFormat); |
|
47
|
|
|
} |
|
48
|
|
|
$this->addAttribute('data-validation', 'date:' . $strFormat . $strSep); |
|
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>'FormDate.Format'</i> parameter |
|
59
|
|
|
* as strftime-compatible format string (default settings: '%Y-%m-%d') |
|
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->strDateFormat, $date->getTimestamp()); |
|
72
|
|
|
} else if (is_numeric($date)) { |
|
73
|
|
|
$strValue = strftime($this->strDateFormat, $date); |
|
74
|
|
|
} else { |
|
75
|
|
|
if ($date != '0000-00-00 00:00:00' && $date != '0000-00-00' && $date != '00:00:00') { |
|
76
|
|
|
$unixtime = strtotime($date); |
|
77
|
|
|
if ($unixtime !== false) { |
|
78
|
|
|
$strValue = strftime($this->strDateFormat, $unixtime); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$strHTML = ''; |
|
84
|
|
|
if (!$this->oFlags->isSet(FormFlags::NO_ZERO) || ($strValue != 0 && $strValue != '0')) { |
|
85
|
|
|
$strHTML = ' value="' . str_replace('"', '"', $strValue) . '"'; |
|
86
|
|
|
} |
|
87
|
|
|
return $strHTML; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|