1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Nip_Form_Element_Dateselect extends Nip_Form_Element_MultiElement |
4
|
|
|
{ |
5
|
|
|
protected $_type = 'dateselect'; |
6
|
|
|
protected $_locale = 'ct_EN'; |
7
|
|
|
protected $format = 'M d Y'; |
8
|
|
|
|
9
|
|
|
public function init() |
10
|
|
|
{ |
11
|
|
|
parent::init(); |
12
|
|
|
|
13
|
|
|
$localeObj = Nip_Locale::instance(); |
14
|
|
|
$this->setLocale($localeObj->getCurrent()); |
15
|
|
|
$this->setFormat($localeObj->getOption(['time', 'dateFormat'])); |
16
|
|
|
|
17
|
|
|
$this->initSelects(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function initSelects() |
21
|
|
|
{ |
22
|
|
|
$inputName = $this->getName(); |
|
|
|
|
23
|
|
|
|
24
|
|
|
if (!$this->hasElement('day')) { |
25
|
|
|
$dayElement = $this->getForm()->getNewElement('select'); |
26
|
|
|
$dayElement->addOption('', '--'); |
|
|
|
|
27
|
|
|
|
28
|
|
|
for ($i = 1; $i <= 31; $i++) { |
29
|
|
|
$dayElement->addOption($i, $i); |
30
|
|
|
} |
31
|
|
|
// $dayElement->setValue(date('d')); |
32
|
|
|
$this->elements['day'] = $dayElement; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
if (!$this->hasElement('month')) { |
37
|
|
|
$monthElement = $this->getForm()->getNewElement('select'); |
38
|
|
|
$monthElement->addOption('', '--'); |
39
|
|
|
|
40
|
|
|
for ($i = 1; $i <= 12; $i++) { |
41
|
|
|
$monthElement->addOption($i, date('M', mktime(0, 0, 0, $i, 1, 2014))); |
42
|
|
|
} |
43
|
|
|
// $monthElement->setValue(date('m')); |
44
|
|
|
$this->elements['month'] = $monthElement; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (!$this->hasElement('year')) { |
48
|
|
|
$yearElement = $this->getForm()->getNewElement('select'); |
49
|
|
|
$yearElement->addOption('', '--'); |
50
|
|
|
|
51
|
|
|
$years = $this->getOption('years', range((int)date('Y') - 100, date('Y') + 5)); |
|
|
|
|
52
|
|
|
|
53
|
|
|
foreach ($years as $year) { |
|
|
|
|
54
|
|
|
$yearElement->addOption($year, $year); |
55
|
|
|
} |
56
|
|
|
// $yearElement->setValue(date('Y')); |
57
|
|
|
$this->elements['year'] = $yearElement; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function setName($name) |
65
|
|
|
{ |
66
|
|
|
$return = parent::setName($name); |
67
|
|
|
$this->updateNameSelects(); |
68
|
|
|
return $return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function updateNameSelects() |
72
|
|
|
{ |
73
|
|
|
$inputName = $this->getName(); |
|
|
|
|
74
|
|
|
$this->getElement('day')->setName($inputName . '[day]'); |
75
|
|
|
$this->getElement('month')->setName($inputName . '[month]'); |
76
|
|
|
$this->getElement('year')->setName($inputName . '[year]'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getLocale() |
83
|
|
|
{ |
84
|
|
|
return $this->_locale; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $format |
89
|
|
|
*/ |
90
|
|
|
public function setLocale($format) |
91
|
|
|
{ |
92
|
|
|
$this->_locale = $format; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getFormat() |
99
|
|
|
{ |
100
|
|
|
return $this->format; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $format |
105
|
|
|
*/ |
106
|
|
|
public function setFormat($format) |
107
|
|
|
{ |
108
|
|
|
$this->format = $format; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $data |
113
|
|
|
* @param string $source |
114
|
|
|
* @return Nip\Form\Elements\AbstractElement |
115
|
|
|
*/ |
116
|
|
|
public function getData($data, $source = 'abstract') |
117
|
|
|
{ |
118
|
|
|
if ($source == 'model') { |
119
|
|
|
if ($data && $data != '0000-00-00' && $data != '0000-00-00 00:00:00') { |
120
|
|
|
$dateUnix = strtotime($data); |
121
|
|
|
if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) { |
122
|
|
|
$this->getElement('day')->setValue(date('d', $dateUnix)); |
123
|
|
|
$this->getElement('month')->setValue(date('m', $dateUnix)); |
124
|
|
|
$this->getElement('year')->setValue(date('Y', $dateUnix)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
return parent::getData($data, $source); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
133
|
|
|
* @param $request |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function getDataFromRequest($request) |
137
|
|
|
{ |
138
|
|
|
if (is_array($request)) { |
139
|
|
|
$elements = $this->getElements(); |
140
|
|
|
foreach ($elements as $key => $element) { |
141
|
|
|
$value = $request[$key]; |
142
|
|
|
if ($value > 0) { |
143
|
|
|
$element->setValue($value); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function validate() |
151
|
|
|
{ |
152
|
|
|
parent::validate(); |
153
|
|
|
if (!$this->isError()) { |
154
|
|
|
$value = $this->getValue(); |
155
|
|
|
if ($value) { |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
161
|
|
|
* @param string $requester |
162
|
|
|
* @return string|null |
163
|
|
|
*/ |
164
|
|
|
public function getValue($requester = 'abstract') |
165
|
|
|
{ |
166
|
|
|
$unixTime = $this->getUnix(); |
167
|
|
|
$format = $requester == 'model' ? 'Y-m-d' : $this->format; |
168
|
|
|
if ($unixTime) { |
169
|
|
|
return date($format, $unixTime); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param bool $format |
177
|
|
|
* @return false|int |
178
|
|
|
*/ |
179
|
|
|
public function getUnix($format = false) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$day = $this->elements['day']->getValue(); |
182
|
|
|
$month = $this->elements['month']->getValue(); |
183
|
|
|
$year = $this->elements['year']->getValue(); |
184
|
|
|
|
185
|
|
|
if ($day < 1 || $month < 1 || $year < 1) { |
186
|
|
|
return 0; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return mktime(0, 0, 0, $month, $day, $year); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|