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 = 'M d Y'; |
8
|
|
|
protected $_hasTime = false; |
9
|
|
|
|
10
|
|
|
public function init() |
11
|
|
|
{ |
12
|
|
|
parent::init(); |
13
|
|
|
$localeObj = app('locale'); |
|
|
|
|
14
|
|
|
$this->setLocale($localeObj->getCurrent()); |
15
|
|
|
$this->setFormat($localeObj->getOption(['time', 'dateFormat'])); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function getLocale() |
19
|
|
|
{ |
20
|
|
|
return $this->_locale; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setLocale($format) |
24
|
|
|
{ |
25
|
|
|
$this->_locale = $format; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getFormat() |
29
|
|
|
{ |
30
|
|
|
return $this->_format; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setFormat($format) |
34
|
|
|
{ |
35
|
|
|
$this->_format = $format; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setTime($time) |
39
|
|
|
{ |
40
|
|
|
$this->_hasTime = (bool)$time; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function hasTime() |
44
|
|
|
{ |
45
|
|
|
return $this->_hasTime; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getData($data, $source = 'abstract') |
49
|
|
|
{ |
50
|
|
|
if ($source == 'model') { |
51
|
|
|
if ($data && $data != '0000-00-00' && $data != '0000-00-00 00:00:00') { |
52
|
|
|
$dateUnix = strtotime($data); |
53
|
|
|
if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) { |
54
|
|
|
$this->setValue(date($this->_format, $dateUnix)); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
$this->setValue(''); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return parent::getData($data, $source); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function validate() |
68
|
|
|
{ |
69
|
|
|
parent::validate(); |
70
|
|
|
if (!$this->isError() && $this->getValue()) { |
|
|
|
|
71
|
|
|
$this->validateFormat(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getValue($requester = 'abstract') |
76
|
|
|
{ |
77
|
|
|
$value = parent::getValue($requester); |
|
|
|
|
78
|
|
|
if ($requester == 'model') { |
79
|
|
|
if ($value) { |
|
|
|
|
80
|
|
|
$unixTime = $this->getUnix(); |
81
|
|
|
$value = date('Y-m-d', $unixTime); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getUnix($format = false) |
89
|
|
|
{ |
90
|
|
|
$format = $format ? $format : $this->_format; |
91
|
|
|
$value = $this->getValue(); |
|
|
|
|
92
|
|
|
if ($value) { |
|
|
|
|
93
|
|
|
$date = date_create_from_format($format, $this->getValue()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $date ? $date->getTimestamp() : false; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function validateFormat($format = false) |
100
|
|
|
{ |
101
|
|
|
$format = $format ? $format : $this->_format; |
102
|
|
|
$value = $this->getValue(); |
|
|
|
|
103
|
|
|
|
104
|
|
|
if ($value) { |
|
|
|
|
105
|
|
|
$unixTime = $this->getUnix($format); |
106
|
|
|
if ($unixTime) { |
107
|
|
|
$this->setValue(date($format, $unixTime)); |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
$message = $this->getForm()->getMessageTemplate('bad-'.$this->getName()); |
112
|
|
|
$message = $message ? $message : 'I couldn\'t parse the '.strtolower($this->getLabel()).' you entered'; |
113
|
|
|
$this->addError($message); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.