1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Webform module time component.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
// Time depends on functions provided by date.
|
9 |
|
|
webform_component_include('date');
|
10 |
|
|
|
11 |
|
|
/**
|
12 |
|
|
* Implements _webform_defaults_component().
|
13 |
|
|
*/
|
14 |
|
|
function _webform_defaults_time() {
|
15 |
|
|
return array(
|
16 |
|
|
'name' => '',
|
17 |
|
|
'form_key' => NULL,
|
18 |
|
|
'pid' => 0,
|
19 |
|
|
'weight' => 0,
|
20 |
|
|
'value' => '',
|
21 |
|
|
'mandatory' => 0,
|
22 |
|
|
'extra' => array(
|
23 |
|
|
'timezone' => 'user',
|
24 |
|
|
'hourformat' => '12-hour',
|
25 |
|
|
'minuteincrements' => 1,
|
26 |
|
|
'title_display' => 0,
|
27 |
|
|
'description' => '',
|
28 |
|
|
'private' => FALSE,
|
29 |
|
|
),
|
30 |
|
|
);
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
/**
|
34 |
|
|
* Implements _webform_theme_component().
|
35 |
|
|
*/
|
36 |
|
|
function _webform_theme_time() {
|
37 |
|
|
return array(
|
38 |
|
|
'webform_time' => array(
|
39 |
|
|
'render element' => 'element',
|
40 |
|
|
'file' => 'components/time.inc',
|
41 |
|
|
),
|
42 |
|
|
'webform_display_time' => array(
|
43 |
|
|
'render element' => 'element',
|
44 |
|
|
'file' => 'components/time.inc',
|
45 |
|
|
),
|
46 |
|
|
);
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* Implements _webform_edit_component().
|
51 |
|
|
*/
|
52 |
|
|
function _webform_edit_time($component) {
|
53 |
|
|
$form = array();
|
54 |
|
|
$form['value'] = array(
|
55 |
|
|
'#type' => 'textfield',
|
56 |
|
|
'#title' => t('Default value'),
|
57 |
|
|
'#default_value' => $component['value'],
|
58 |
|
|
'#description' => t('The default value of the field.') . '<br />' . t('Accepts a time in any <a href="http://www.gnu.org/software/tar/manual/html_chapter/Date-input-formats.html">GNU Date Input Format</a>. Strings such as now, +2 hours, and 10:30pm are all valid.'),
|
59 |
|
|
'#size' => 60,
|
60 |
|
|
'#maxlength' => 127,
|
61 |
|
|
'#weight' => 0,
|
62 |
|
|
);
|
63 |
|
|
$form['extra']['timezone'] = array(
|
64 |
|
|
'#type' => 'radios',
|
65 |
|
|
'#title' => t('Default value timezone'),
|
66 |
|
|
'#default_value' => $component['extra']['timezone'],
|
67 |
|
|
'#description' => t('If using relative dates for a default value (e.g. "now") base the current time on this timezone.'),
|
68 |
|
|
'#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')),
|
69 |
|
|
'#weight' => 2,
|
70 |
|
|
'#access' => variable_get('configurable_timezones', 1),
|
71 |
|
|
);
|
72 |
|
|
$form['display']['hourformat'] = array(
|
73 |
|
|
'#type' => 'radios',
|
74 |
|
|
'#title' => t('Time format'),
|
75 |
|
|
'#default_value' => $component['extra']['hourformat'],
|
76 |
|
|
'#options' => array('12-hour' => t('12-hour (am/pm)'), '24-hour' => t('24-hour')),
|
77 |
|
|
'#weight' => 2,
|
78 |
|
|
'#parents' => array('extra', 'hourformat'),
|
79 |
|
|
);
|
80 |
|
|
$form['display']['minuteincrements'] = array(
|
81 |
|
|
'#type' => 'select',
|
82 |
|
|
'#title' => t('Minute increments'),
|
83 |
|
|
'#default_value' => $component['extra']['minuteincrements'],
|
84 |
|
|
'#options' => array(
|
85 |
|
|
1 => t('1 minute'),
|
86 |
|
|
5 => t('5 minute'),
|
87 |
|
|
10 => t('10 minute'),
|
88 |
|
|
15 => t('15 minute'),
|
89 |
|
|
30 => t('30 minute'),
|
90 |
|
|
),
|
91 |
|
|
'#weight' => 3,
|
92 |
|
|
'#parents' => array('extra', 'minuteincrements'),
|
93 |
|
|
);
|
94 |
|
|
return $form;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
/**
|
98 |
|
|
* Implements _webform_render_component().
|
99 |
|
|
*/
|
100 |
|
|
function _webform_render_time($component, $value = NULL, $filter = TRUE) {
|
101 |
|
|
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
|
102 |
|
|
|
103 |
|
|
$element = array(
|
104 |
|
|
'#type' => 'webform_time',
|
105 |
|
|
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
|
106 |
|
|
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
|
107 |
|
|
'#required' => $component['mandatory'],
|
108 |
|
|
'#weight' => $component['weight'],
|
109 |
|
|
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
|
110 |
|
|
'#element_validate' => array('webform_validate_time'),
|
111 |
|
|
'#hourformat' => $component['extra']['hourformat'],
|
112 |
|
|
'#minuteincrements' => $component['extra']['minuteincrements'],
|
113 |
|
|
'#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
|
114 |
|
|
'#timezone' => $component['extra']['timezone'],
|
115 |
|
|
'#process' => array('webform_expand_time'),
|
116 |
|
|
'#theme' => 'webform_time',
|
117 |
|
|
'#theme_wrappers' => array('webform_element'),
|
118 |
|
|
'#translatable' => array('title', 'description'),
|
119 |
|
|
);
|
120 |
|
|
|
121 |
|
|
// Set the value from Webform if available.
|
122 |
|
|
if (!empty($value[0])) {
|
123 |
|
|
$element['#default_value'] = $value[0];
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
return $element;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/**
|
130 |
|
|
* Form API #process function for Webform time fields.
|
131 |
|
|
*/
|
132 |
|
|
function webform_expand_time($element) {
|
133 |
|
|
// Expand the default value from a string into an array.
|
134 |
|
|
if (!empty($element['#default_value'])) {
|
135 |
|
|
// Adjust the time based on the user or site timezone.
|
136 |
|
|
if (variable_get('configurable_timezones', 1) && $element['#timezone'] == 'user') {
|
137 |
|
|
$timezone_name = isset($GLOBALS['user']->timezone) ? $GLOBALS['user']->timezone : 'UTC';
|
138 |
|
|
}
|
139 |
|
|
else {
|
140 |
|
|
$timezone_name = variable_get('date_default_timezone', 'UTC');
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
$default_values = webform_date_array(webform_strtodate('c', $element['#default_value'], $timezone_name), 'time');
|
144 |
|
|
}
|
145 |
|
|
else {
|
146 |
|
|
$default_values = array(
|
147 |
|
|
'hour' => '',
|
148 |
|
|
'minute' => '',
|
149 |
|
|
'second' => '',
|
150 |
|
|
);
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
$first_hour = 0;
|
154 |
|
|
$last_hour = 23;
|
155 |
|
|
if ($element['#hourformat'] == '12-hour') {
|
156 |
|
|
$first_hour = 1;
|
157 |
|
|
$last_hour = 12;
|
158 |
|
|
$default_values = webform_time_convert($default_values, '12-hour');
|
159 |
|
|
$default_values['ampm'] = $default_values['ampm'] ? $default_values['ampm'] : 'am';
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
// Generate the choices for drop-down selects.
|
163 |
|
|
$hours[''] = t('hour');
|
164 |
|
|
$minutes[''] = t('minute');
|
165 |
|
|
for ($i = $first_hour; $i <= $last_hour; $i++) {
|
166 |
|
|
$hours[$i] = $i;
|
167 |
|
|
}
|
168 |
|
|
for ($i = 0; $i <= 59; $i += $element['#minuteincrements']) {
|
169 |
|
|
$minutes[$i] = $i < 10 ? "0$i" : $i;
|
170 |
|
|
}
|
171 |
|
|
$ampms = array('am' => t('am'), 'pm' => t('pm'));
|
172 |
|
|
|
173 |
|
|
// Adjust the default for minutes if needed, rounding up to the closest value.
|
174 |
|
|
if (!isset($minutes[$default_values['minute']])) {
|
175 |
|
|
foreach ($minutes as $minute => $padded_minute) {
|
176 |
|
|
if ($minute > $default_values['minute']) {
|
177 |
|
|
$default_values['minute'] = $minute;
|
178 |
|
|
break;
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
// If the above loop didn't set a value, it's because rounding up would go to
|
184 |
|
|
// the next hour. This gets quite a bit more complicated, since we need to
|
185 |
|
|
// deal with looping around on hours, as well as flipping am/pm.
|
186 |
|
|
if (!isset($minutes[$default_values['minute']])) {
|
187 |
|
|
$default_values['minute'] = 0;
|
188 |
|
|
$default_values['hour']++;
|
189 |
|
|
// If the hour rolls over also, set hour to the first hour in the list.
|
190 |
|
|
if (!isset($hours[$default_values['hour']])) {
|
191 |
|
|
$default_values['hour'] = $element['#hourformat'] == '12-hour' ? 1 : 0;
|
192 |
|
|
}
|
193 |
|
|
// If the hour has been incremented to 12:00 in 12-hour format, flip am/pm.
|
194 |
|
|
// Note that technically midnight and noon are neither am or pm, but common
|
195 |
|
|
// convention (and US standard) is to represent 12:00am as midnight.
|
196 |
|
|
// See http://en.wikipedia.org/wiki/Midnight#Start_and_end_of_day.
|
197 |
|
|
if ($element['#hourformat'] == '12-hour' && $default_values['hour'] == 12) {
|
198 |
|
|
$default_values['ampm'] = $default_values['ampm'] == 'am' ? 'pm' : 'am';
|
199 |
|
|
}
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
$element['hour'] = array(
|
203 |
|
|
'#prefix' => '',
|
204 |
|
|
'#type' => 'select',
|
205 |
3753f249
|
Assos Assos
|
'#title' => t('Hour'),
|
206 |
|
|
'#title_display' => 'invisible',
|
207 |
85ad3d82
|
Assos Assos
|
'#default_value' => $default_values['hour'],
|
208 |
|
|
'#options' => $hours,
|
209 |
|
|
);
|
210 |
|
|
$element['minute'] = array(
|
211 |
|
|
'#prefix' => ':',
|
212 |
|
|
'#type' => 'select',
|
213 |
3753f249
|
Assos Assos
|
'#title' => t('Minute'),
|
214 |
|
|
'#title_display' => 'invisible',
|
215 |
85ad3d82
|
Assos Assos
|
'#default_value' => $default_values['minute'],
|
216 |
|
|
'#options' => $minutes,
|
217 |
|
|
);
|
218 |
|
|
if (strcmp($element['#hourformat'], '12-hour') == 0) {
|
219 |
|
|
$element['ampm'] = array(
|
220 |
|
|
'#type' => 'radios',
|
221 |
|
|
'#default_value' => $default_values['ampm'],
|
222 |
|
|
'#options' => $ampms,
|
223 |
|
|
);
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
// Set the overall default value.
|
227 |
|
|
if ($default_values['hour'] !== '') {
|
228 |
|
|
$element['#default_value'] = webform_date_string($default_values);
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
return $element;
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
/**
|
235 |
|
|
* Theme a webform time element.
|
236 |
|
|
*/
|
237 |
|
|
function theme_webform_time($variables) {
|
238 |
|
|
$element = $variables['element'];
|
239 |
|
|
|
240 |
3753f249
|
Assos Assos
|
$element['hour']['#attributes']['class'][] = 'hour';
|
241 |
|
|
$element['minute']['#attributes']['class'][] = 'minute';
|
242 |
85ad3d82
|
Assos Assos
|
|
243 |
|
|
// Add error classes to all items within the element.
|
244 |
|
|
if (form_get_error($element)) {
|
245 |
|
|
$element['hour']['#attributes']['class'][] = 'error';
|
246 |
|
|
$element['minute']['#attributes']['class'][] = 'error';
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
$output = '<div class="webform-container-inline">' . drupal_render($element['hour']) . drupal_render($element['minute']) . drupal_render($element['ampm']) . '</div>';
|
250 |
|
|
|
251 |
|
|
return $output;
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
function webform_validate_time($element, $form_state) {
|
255 |
|
|
$form_key = $element['#webform_component']['form_key'];
|
256 |
|
|
$name = $element['#webform_component']['name'];
|
257 |
|
|
|
258 |
|
|
// Check if the user filled the required fields.
|
259 |
|
|
foreach ($element['#hourformat'] == '12-hour' ? array('hour', 'minute', 'ampm') : array('hour', 'minute') as $field_type) {
|
260 |
|
|
if ($element[$field_type]['#value'] === '' && $element['#required']) {
|
261 |
|
|
form_error($element, t('%field field is required.', array('%field' => $name)));
|
262 |
|
|
return;
|
263 |
|
|
}
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
// Check for a valid time.
|
267 |
|
|
if ($element['hour']['#value'] !== '' || $element['minute']['#value'] !== '') {
|
268 |
|
|
if (!is_numeric($element['hour']['#value']) || !is_numeric($element['minute']['#value']) || (isset($element['ampm']) && $element['ampm']['#value'] === '')) {
|
269 |
|
|
form_error($element, t('Entered %name is not a valid time.', array('%name' => $name)));
|
270 |
|
|
return;
|
271 |
|
|
}
|
272 |
|
|
}
|
273 |
|
|
}
|
274 |
|
|
|
275 |
|
|
/**
|
276 |
|
|
* Implements _webform_submit_component().
|
277 |
|
|
*/
|
278 |
|
|
function _webform_submit_time($component, $value) {
|
279 |
|
|
// Convert to 24-hour time before string conversion.
|
280 |
|
|
if ($component['extra']['hourformat'] == '12-hour') {
|
281 |
|
|
$value = webform_time_convert($value, '24-hour');
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
// Convert the value into a ISO 8601 string.
|
285 |
|
|
return $value['hour'] !== '' ? webform_date_string($value, 'time') : '';
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
/**
|
289 |
|
|
* Implements _webform_display_component().
|
290 |
|
|
*/
|
291 |
|
|
function _webform_display_time($component, $value, $format = 'html') {
|
292 |
|
|
$value = webform_date_array(isset($value[0]) ? $value[0] : '', 'time');
|
293 |
|
|
if ($component['extra']['hourformat'] == '12-hour') {
|
294 |
|
|
$value = webform_time_convert($value, '12-hour');
|
295 |
|
|
}
|
296 |
|
|
|
297 |
|
|
return array(
|
298 |
|
|
'#title' => $component['name'],
|
299 |
|
|
'#weight' => $component['weight'],
|
300 |
|
|
'#theme' => 'webform_display_time',
|
301 |
|
|
'#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
|
302 |
|
|
'#format' => $format,
|
303 |
|
|
'#hourformat' => $component['extra']['hourformat'],
|
304 |
|
|
'#value' => $value,
|
305 |
|
|
'#translatable' => array('title'),
|
306 |
|
|
);
|
307 |
|
|
}
|
308 |
|
|
|
309 |
|
|
/**
|
310 |
|
|
* Format the output of data for this component.
|
311 |
|
|
*/
|
312 |
|
|
function theme_webform_display_time($variables) {
|
313 |
|
|
$element = $variables['element'];
|
314 |
|
|
$output = ' ';
|
315 |
|
|
if (isset($element['#value']['hour']) && $element['#value']['hour'] !== '' && isset($element['#value']['minute']) && $element['#value']['minute'] !== '') {
|
316 |
|
|
if ($element['#hourformat'] == '24-hour') {
|
317 |
|
|
$output = sprintf('%02d', $element['#value']['hour']) . ':' . sprintf('%02d', $element['#value']['minute']);
|
318 |
|
|
}
|
319 |
|
|
else {
|
320 |
|
|
$output = $element['#value']['hour'] . ':' . sprintf('%02d', $element['#value']['minute']) . ' ' . $element['#value']['ampm'];
|
321 |
|
|
}
|
322 |
|
|
}
|
323 |
|
|
return $output;
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
/**
|
327 |
|
|
* Implements _webform_analysis_component().
|
328 |
|
|
*/
|
329 |
|
|
function _webform_analysis_time($component, $sids = array()) {
|
330 |
|
|
$query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
|
331 |
|
|
->fields('wsd', array('no', 'data'))
|
332 |
|
|
->condition('nid', $component['nid'])
|
333 |
|
|
->condition('cid', $component['cid'])
|
334 |
|
|
->orderBy('sid');
|
335 |
|
|
|
336 |
|
|
if (count($sids)) {
|
337 |
|
|
$query->condition('sid', $sids, 'IN');
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
$result = $query->execute();
|
341 |
|
|
|
342 |
|
|
$times = array();
|
343 |
|
|
$submissions = 0;
|
344 |
|
|
foreach ($result as $row) {
|
345 |
|
|
$submissions++;
|
346 |
|
|
if ($row['data']) {
|
347 |
|
|
$times[] = webform_date_array($row['data']);
|
348 |
|
|
}
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
// Display stats.
|
352 |
|
|
$nonblanks = count($times);
|
353 |
|
|
$rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
|
354 |
|
|
$rows[1] = array(t('User entered value'), $nonblanks);
|
355 |
|
|
return $rows;
|
356 |
|
|
}
|
357 |
|
|
|
358 |
|
|
/**
|
359 |
|
|
* Implements _webform_table_component().
|
360 |
|
|
*/
|
361 |
|
|
function _webform_table_time($component, $value) {
|
362 |
|
|
if ($value[0]) {
|
363 |
|
|
$time = webform_date_array($value[0], 'time');
|
364 |
|
|
if ($component['extra']['hourformat'] == '24-hour') {
|
365 |
|
|
return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']);
|
366 |
|
|
}
|
367 |
|
|
else {
|
368 |
|
|
$time = webform_time_convert($time, '12-hour');
|
369 |
|
|
return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm'];
|
370 |
|
|
}
|
371 |
|
|
}
|
372 |
|
|
else {
|
373 |
|
|
return '';
|
374 |
|
|
}
|
375 |
|
|
}
|
376 |
|
|
|
377 |
|
|
/**
|
378 |
|
|
* Implements _webform_csv_headers_component().
|
379 |
|
|
*/
|
380 |
|
|
function _webform_csv_headers_time($component, $export_options) {
|
381 |
|
|
$header = array();
|
382 |
|
|
$header[0] = '';
|
383 |
|
|
$header[1] = '';
|
384 |
|
|
$header[2] = $component['name'];
|
385 |
|
|
return $header;
|
386 |
|
|
}
|
387 |
|
|
|
388 |
|
|
/**
|
389 |
|
|
* Implements _webform_csv_data_component().
|
390 |
|
|
*/
|
391 |
|
|
function _webform_csv_data_time($component, $export_options, $value) {
|
392 |
|
|
if ($value[0]) {
|
393 |
|
|
$time = webform_date_array($value[0], 'time');
|
394 |
|
|
if ($component['extra']['hourformat'] == '24-hour') {
|
395 |
|
|
return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']);
|
396 |
|
|
}
|
397 |
|
|
else {
|
398 |
|
|
$time = webform_time_convert($time, '12-hour');
|
399 |
|
|
return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm'];
|
400 |
|
|
}
|
401 |
|
|
}
|
402 |
|
|
else {
|
403 |
|
|
return '';
|
404 |
|
|
}
|
405 |
|
|
}
|
406 |
|
|
|
407 |
|
|
/**
|
408 |
|
|
* Convert a time between a 24-hour and a 12-hour value.
|
409 |
|
|
*
|
410 |
|
|
* @param $array
|
411 |
|
|
* An array of hour, minute, second, and optionally ampm.
|
412 |
|
|
* @param $format
|
413 |
|
|
* Either 12-hour or 24-hour.
|
414 |
|
|
* @return
|
415 |
|
|
* An array with hour, minute, second, and ampm (if using "12-hour").
|
416 |
|
|
*/
|
417 |
|
|
function webform_time_convert($array, $format) {
|
418 |
|
|
if ($array['hour'] !== '') {
|
419 |
|
|
if ($format == '12-hour') {
|
420 |
|
|
$array['ampm'] = ($array['hour'] >= 12 && $array['hour'] < 24) ? 'pm' : 'am';
|
421 |
|
|
$array['hour'] = ($array['hour'] > 12 || $array['hour'] == 0) ? abs($array['hour'] - 12) : (int) $array['hour'];
|
422 |
|
|
}
|
423 |
|
|
elseif ($format == '24-hour' && isset($array['ampm'])) {
|
424 |
|
|
$array['hour'] = ($array['hour'] < 12 && $array['ampm'] == 'pm') ? $array['hour'] + 12 : (int) $array['hour'];
|
425 |
|
|
$array['hour'] = ($array['hour'] == 12 && $array['ampm'] == 'am') ? 0 : $array['hour'];
|
426 |
|
|
}
|
427 |
|
|
}
|
428 |
|
|
|
429 |
|
|
if ($format == '12-hour' && !isset($array['ampm'])) {
|
430 |
|
|
$array['ampm'] = '';
|
431 |
|
|
}
|
432 |
|
|
elseif ($format == '24-hour' && isset($array['ampm'])) {
|
433 |
|
|
unset($array['ampm']);
|
434 |
|
|
}
|
435 |
|
|
|
436 |
|
|
return $array;
|
437 |
|
|
} |