|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Nip_Form_Element_Dateinput extends Nip_Form_Element_Input |
|
4
|
|
|
{ |
|
5
|
|
|
protected $_type = 'dateinput'; |
|
6
|
|
|
protected $_locale = 'ct_EN'; |
|
7
|
|
|
protected $_format = 'Y-m-d'; |
|
8
|
|
|
protected $_hasTime = false; |
|
9
|
|
|
|
|
10
|
|
|
public function init() |
|
11
|
|
|
{ |
|
12
|
|
|
parent::init(); |
|
13
|
|
|
$this->setAttrib('type', 'date'); |
|
14
|
|
|
|
|
15
|
|
|
if (app()->has('locale') == false) { |
|
|
|
|
|
|
16
|
|
|
return; |
|
17
|
|
|
} |
|
18
|
|
|
$localeObj = app('locale'); |
|
19
|
|
|
if (!($localeObj instanceof \Nip\Locale\Locale)) { |
|
20
|
|
|
return; |
|
21
|
|
|
} |
|
22
|
|
|
$this->setLocale($localeObj->getCurrent()); |
|
23
|
|
|
$this->setFormat($localeObj->getOption(['time', 'dateFormat'])); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getLocale() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->_locale; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function setLocale($format) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->_locale = $format; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getFormat() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->_format; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function setFormat($format) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->_format = $format; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setTime($time) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->_hasTime = (bool)$time; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function hasTime() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->_hasTime; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getData($data, $source = 'abstract') |
|
57
|
|
|
{ |
|
58
|
|
|
if ($source != 'model') { |
|
59
|
|
|
return parent::getData($data, $source); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($data instanceof DateTime) { |
|
63
|
|
|
$this->setValue($data->format('Y-m-d')); |
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($data && $data != '0000-00-00' && $data != '0000-00-00 00:00:00') { |
|
68
|
|
|
$dateUnix = strtotime($data); |
|
69
|
|
|
if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) { |
|
70
|
|
|
$this->setValue(date('Y-m-d', $dateUnix)); |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
$this->setValue(''); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function validate() |
|
81
|
|
|
{ |
|
82
|
|
|
parent::validate(); |
|
83
|
|
|
if (!$this->isError() && $this->getValue()) { |
|
|
|
|
|
|
84
|
|
|
$this->validateFormat(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $requester |
|
90
|
|
|
* @return null |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getValue($requester = 'abstract') |
|
93
|
|
|
{ |
|
94
|
|
|
$value = parent::getValue($requester); |
|
95
|
|
|
if ($requester == 'model') { |
|
96
|
|
|
if ($value) { |
|
97
|
|
|
$unixTime = $this->getUnix(); |
|
98
|
|
|
$value = date('Y-m-d', $unixTime); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $value; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param false $format |
|
107
|
|
|
* @return false|int |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getUnix($format = false) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$format = 'Y-m-d'; |
|
112
|
|
|
$value = $this->getValue(); |
|
|
|
|
|
|
113
|
|
|
$date = ($value) ? date_create_from_format($format, $this->getValue()) :false; |
|
|
|
|
|
|
114
|
|
|
if ($date instanceof DateTime) { |
|
|
|
|
|
|
115
|
|
|
return $date->getTimestamp(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function validateFormat($format = false) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
$format = 'Y-m-d'; |
|
124
|
|
|
$value = $this->getValue(); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
if ($value) { |
|
|
|
|
|
|
127
|
|
|
$unixTime = $this->getUnix('Y-m-d'); |
|
128
|
|
|
if ($unixTime) { |
|
129
|
|
|
$this->setValue(date($format, $unixTime)); |
|
130
|
|
|
|
|
131
|
|
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
$message = $this->getForm()->getMessageTemplate('bad-' . $this->getName()); |
|
134
|
|
|
$message = $message ? $message : 'I couldn\'t parse the ' . strtolower($this->getLabel()) . ' you entered'; |
|
135
|
|
|
$this->addError($message); |
|
136
|
|
|
} |
|
137
|
|
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.