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 function Field() {
64 $html = parent::Field();
65
66 $html = $this->FieldDriver($html);
67
68 return $html;
69 }
70
71 72 73 74 75
76 protected function FieldDriver($html) {
77 if($this->getConfig('showdropdown')) {
78 Requirements::javascript(SAPPHIRE_DIR . '/javascript/TimeField_dropdown.js');
79 Requirements::css(SAPPHIRE_DIR . '/css/TimeField_dropdown.css');
80
81 $html .= sprintf('<img src="sapphire/images/clock-icon.gif" id="%s-icon"/>', $this->id());
82 $html .= sprintf('<div class="dropdownpopup" id="%s-dropdowntime"></div>', $this->id());
83 $html = '<div class="dropdowntime">' . $html . '</div>';
84 }
85
86 return $html;
87 }
88
89 90 91 92 93
94 function setValue($val) {
95
96
97
98 if($this->getConfig('use_strtotime') && !empty($val)) {
99 if($parsedTimestamp = strtotime($val)) {
100 $parsedObj = new Zend_Date($parsedTimestamp, Zend_Date::TIMESTAMP);
101 $val = $parsedObj->get($this->getConfig('timeformat'));
102 unset($parsedObj);
103 }
104 }
105
106 if(empty($val)) {
107 $this->value = null;
108 $this->valueObj = null;
109 }
110
111 else if(Zend_Date::isDate($val, $this->getConfig('datavalueformat'))) {
112 $this->valueObj = new Zend_Date($val, $this->getConfig('datavalueformat'));
113 $this->value = $this->valueObj->get($this->getConfig('timeformat'));
114 }
115
116 else if(Zend_Date::isDate($val, $this->getConfig('timeformat'), $this->locale)) {
117 $this->valueObj = new Zend_Date($val, $this->getConfig('timeformat'), $this->locale);
118 $this->value = $this->valueObj->get($this->getConfig('timeformat'));
119 }
120
121 else {
122 $this->value = $val;
123 $this->valueObj = null;
124 }
125 }
126
127 128 129
130 function dataValue() {
131 if($this->valueObj) {
132 return $this->valueObj->toString($this->getConfig('datavalueformat'));
133 } else {
134 return null;
135 }
136 }
137
138 139 140
141 function validate($validator) {
142 $valid = true;
143
144
145 if(empty($this->value)) return true;
146
147 if(!Zend_Date::isDate($this->value, $this->getConfig('timeformat'), $this->locale)) {
148 $validator->validationError(
149 $this->name,
150 _t('DateField.VALIDDATEFORMAT', "Please enter a valid time format."),
151 "validation",
152 false
153 );
154 return false;
155 }
156 return true;
157 }
158
159 160 161
162 function getLocale() {
163 return $this->locale;
164 }
165
166 167 168
169 function setLocale($locale) {
170 $this->locale = $locale;
171 }
172
173 174 175 176
177 function setConfig($name, $val) {
178 $this->config[$name] = $val;
179 }
180
181 182 183 184
185 function getConfig($name) {
186 return $this->config[$name];
187 }
188
189 190 191
192 function performReadonlyTransformation() {
193 return new TimeField_Readonly($this->name, $this->title, $this->dataValue(), $this->getConfig('timeformat'));
194 }
195
196 }
197
198 199 200 201 202 203
204 class TimeField_Readonly extends TimeField {
205
206 protected $readonly = true;
207
208 function Field() {
209 if($this->valueObj) {
210 $val = Convert::raw2xml($this->valueObj->toString($this->getConfig('timeformat')));
211 } else {
212
213 $val = '<i>(not set)</i>';
214 }
215
216 return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>";
217 }
218
219 function jsValidation() {
220 return null;
221 }
222
223 function validate($validator) {
224 return true;
225 }
226 }
227 ?>
[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.
-