1 <?php
2 require_once 'Zend/Date.php';
3
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 class TimeField extends TextField {
30
31 protected $config = array(
32 'showdropdown' => false,
33 'timeformat' => 'HH:mm:ss',
34 'use_strtotime' => true,
35 'datavalueformat' => 'HH:mm:ss'
36 );
37
38 39 40
41 protected $locale = null;
42
43 44 45 46 47
48 protected $valueObj = null;
49
50 function __construct($name, $title = null, $value = ""){
51 if(!$this->locale) {
52 $this->locale = i18n::get_locale();
53 }
54
55 if(!$this->getConfig('timeformat')) {
56 $this->setConfig('timeformat', Zend_Locale_Format::getDateFormat($this->locale));
57 }
58 $this->setHTML5Attribute('type', 'time');
59
60 parent::__construct($name,$title,$value);
61 }
62 63 64 65 66 67 68 69 70 71 72 73
74
75 76 77 78 79
80 protected function FieldDriver($html) {
81 if($this->getConfig('showdropdown')) {
82 Requirements::javascript(SAPPHIRE_DIR . '/javascript/TimeField_dropdown.js');
83 Requirements::css(SAPPHIRE_DIR . '/css/TimeField_dropdown.css');
84
85 $html .= sprintf('<img src="sapphire/images/clock-icon.gif" id="%s-icon"/>', $this->id());
86 $html .= sprintf('<div class="dropdownpopup" id="%s-dropdowntime"></div>', $this->id());
87 $html = '<div class="dropdowntime">' . $html . '</div>';
88 }
89
90 return $html;
91 }
92
93 94 95 96 97
98 function setValue($val) {
99
100
101
102 if($this->getConfig('use_strtotime') && !empty($val)) {
103 if($parsedTimestamp = strtotime($val)) {
104 $parsedObj = new Zend_Date($parsedTimestamp, Zend_Date::TIMESTAMP);
105 $val = $parsedObj->get($this->getConfig('timeformat'));
106 unset($parsedObj);
107 }
108 }
109
110 if(empty($val)) {
111 $this->value = null;
112 $this->valueObj = null;
113 }
114
115 else if(Zend_Date::isDate($val, $this->getConfig('datavalueformat'))) {
116 $this->valueObj = new Zend_Date($val, $this->getConfig('datavalueformat'));
117 $this->value = $this->valueObj->get($this->getConfig('timeformat'));
118 }
119
120 else if(Zend_Date::isDate($val, $this->getConfig('timeformat'), $this->locale)) {
121 $this->valueObj = new Zend_Date($val, $this->getConfig('timeformat'), $this->locale);
122 $this->value = $this->valueObj->get($this->getConfig('timeformat'));
123 }
124
125 else {
126 $this->value = $val;
127 $this->valueObj = null;
128 }
129 }
130
131 132 133
134 function dataValue() {
135 if($this->valueObj) {
136 return $this->valueObj->toString($this->getConfig('datavalueformat'));
137 } else {
138 return null;
139 }
140 }
141
142 143 144
145 function validate($validator) {
146 $valid = true;
147
148
149 if(empty($this->value)) return true;
150
151 if(!Zend_Date::isDate($this->value, $this->getConfig('timeformat'), $this->locale)) {
152 $validator->validationError(
153 $this->name,
154 _t('DateField.VALIDDATEFORMAT', "Please enter a valid time format."),
155 "validation",
156 false
157 );
158 return false;
159 }
160 return true;
161 }
162
163 function ShowDropdown() {
164 if($this->getConfig('showdropdown')) {
165 return true;
166 }
167 return false;
168 }
169
170 171 172
173 function getLocale() {
174 return $this->locale;
175 }
176
177 178 179
180 function setLocale($locale) {
181 $this->locale = $locale;
182 }
183
184 185 186 187
188 function setConfig($name, $val) {
189 $this->config[$name] = $val;
190 }
191
192 193 194 195
196 function getConfig($name) {
197 return $this->config[$name];
198 }
199
200 201 202
203 function performReadonlyTransformation() {
204 return new TimeField_Readonly($this->name, $this->title, $this->dataValue(), $this->getConfig('timeformat'));
205 }
206
207 }
208
209 210 211 212 213 214
215 class TimeField_Readonly extends TimeField {
216
217 protected $readonly = true;
218
219 function Field() {
220 if($this->valueObj) {
221 $val = Convert::raw2xml($this->valueObj->toString($this->getConfig('timeformat')));
222 } else {
223
224 $val = '<i>(not set)</i>';
225 }
226
227 return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>";
228 }
229
230 function jsValidation() {
231 return null;
232 }
233
234 function validate($validator) {
235 return true;
236 }
237 }
238 ?>
[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.
-