1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15
16 class DatetimeField extends FormField {
17
18 19 20
21 protected $dateField = null;
22
23 24 25
26 protected $timeField = null;
27
28 29 30
31 protected $config = array(
32 'datavalueformat' => 'YYYY-MM-dd HH:mm:ss'
33 );
34
35 function __construct($name, $title = null, $value = ""){
36 $this->dateField = new DateField($name . '[date]', false);
37 $this->timeField = new TimeField($name . '[time]', false);
38
39 parent::__construct($name, $title, $value);
40 }
41
42 function setForm($form) {
43 parent::setForm($form);
44
45 $this->dateField->setForm($form);
46 $this->timeField->setForm($form);
47 }
48
49 function allowHTML5($val) {
50 $this->dateField->allowHTML5($val);
51 $this->timeField->allowHTML5($val);
52 }
53
54 function Field() {
55 return $this->dateField->Field() . $this->timeField->Field();
56 }
57
58 59 60 61 62 63 64
65 function setValue($val) {
66 if(empty($val)) {
67 $this->dateField->setValue(null);
68 $this->timeField->setValue(null);
69 } else {
70
71 if(is_string($val) && Zend_Date::isDate($val, $this->getConfig('datavalueformat'), $this->locale)) {
72
73 $valueObj = new Zend_Date($val, $this->getConfig('datavalueformat'), $this->locale);
74
75 if($this->dateField->getConfig('dmyfields')) {
76 $this->dateField->setValue($valueObj->toArray());
77 } else {
78 $this->dateField->setValue($valueObj->get($this->dateField->getConfig('dateformat')));
79 }
80
81 $this->timeField->setValue($valueObj->get($this->timeField->getConfig('timeformat')));
82 }
83
84 elseif(is_array($val) && array_key_exists('date', $val) && array_key_exists('time', $val)) {
85 $this->dateField->setValue($val['date']);
86 $this->timeField->setValue($val['time']);
87 } else {
88 $this->dateField->setValue($val);
89 $this->timeField->setValue($val);
90 }
91 }
92 }
93
94 function dataValue() {
95 $valDate = $this->dateField->dataValue();
96 $valTime = $this->timeField->dataValue();
97
98 if($valDate && $valTime) {
99 return $valDate . ' ' . $valTime;
100 } else {
101
102 return null;
103 }
104 }
105
106 107 108
109 function getDateField() {
110 return $this->dateField;
111 }
112
113 114 115
116 function getTimeField() {
117 return $this->timeField;
118 }
119
120 function setLocale($locale) {
121 $this->dateField->setLocale($locale);
122 $this->timeField->setLocale($locale);
123 }
124
125 function getLocale() {
126 return $this->dateField->getLocale();
127 }
128
129 130 131 132 133 134 135
136 function setConfig($name, $val) {
137 $this->config[$name] = $val;
138 }
139
140 141 142 143 144 145 146
147 function getConfig($name) {
148 return $this->config[$name];
149 }
150
151 function validate($validator) {
152 $dateValid = $this->dateField->validate($validator);
153 $timeValid = $this->timeField->validate($validator);
154
155 return ($dateValid && $timeValid);
156 }
157
158 function jsValidation() {
159 return $this->dateField->jsValidation() . $this->timeField->jsValidation();
160 }
161
162 function performReadonlyTransformation() {
163 $field = new DatetimeField_Readonly($this->name, $this->title, $this->dataValue());
164 $field->setForm($this->form);
165
166 return $field;
167 }
168 }
169
170 171 172 173 174 175
176 class DatetimeField_Readonly extends DatetimeField {
177
178 protected $readonly = true;
179
180 function Field() {
181 $valDate = $this->dateField->dataValue();
182 $valTime = $this->timeField->dataValue();
183 if($valDate && $valTime) {
184 $format = $this->dateField->getConfig('dateformat') . ' ' . $this->timeField->getConfig('timeformat');
185 $valueObj = new Zend_Date(
186 $valDate . ' ' . $valTime,
187 $this->getConfig('datavalueformat'),
188 $this->dateField->getLocale()
189 );
190 $val = $valueObj->toString($format);
191 } else {
192
193 $val = '<i>(not set)</i>';
194 }
195
196 return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>";
197 }
198
199 function jsValidation() {
200 return null;
201 }
202
203 function validate($validator) {
204 return true;
205 }
206 }
207 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-