1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Webform module date component.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements _webform_defaults_component().
|
10 |
|
|
*/
|
11 |
|
|
function _webform_defaults_date() {
|
12 |
|
|
return array(
|
13 |
|
|
'name' => '',
|
14 |
|
|
'form_key' => NULL,
|
15 |
|
|
'pid' => 0,
|
16 |
|
|
'weight' => 0,
|
17 |
|
|
'value' => '',
|
18 |
|
|
'mandatory' => 0,
|
19 |
|
|
'extra' => array(
|
20 |
|
|
'timezone' => 'user',
|
21 |
|
|
'start_date' => '-2 years',
|
22 |
|
|
'end_date' => '+2 years',
|
23 |
|
|
'year_textfield' => 0,
|
24 |
|
|
'datepicker' => 1,
|
25 |
|
|
'title_display' => 0,
|
26 |
|
|
'description' => '',
|
27 |
|
|
'private' => FALSE,
|
28 |
|
|
),
|
29 |
|
|
);
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
/**
|
33 |
|
|
* Implements _webform_theme_component().
|
34 |
|
|
*/
|
35 |
|
|
function _webform_theme_date() {
|
36 |
|
|
return array(
|
37 |
|
|
'webform_date' => array(
|
38 |
|
|
'render element' => 'element',
|
39 |
|
|
'file' => 'components/date.inc',
|
40 |
|
|
),
|
41 |
|
|
'webform_display_date' => array(
|
42 |
|
|
'render element' => 'element',
|
43 |
|
|
'file' => 'components/date.inc',
|
44 |
|
|
),
|
45 |
|
|
'webform_calendar' => array(
|
46 |
|
|
'variables' => array('component' => NULL, 'calendar_classes' => NULL),
|
47 |
|
|
'template' => 'templates/webform-calendar',
|
48 |
|
|
),
|
49 |
|
|
);
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* Implements _webform_edit_component().
|
54 |
|
|
*/
|
55 |
|
|
function _webform_edit_date($component) {
|
56 |
|
|
$form = array();
|
57 |
|
|
$form['value'] = array(
|
58 |
|
|
'#type' => 'textfield',
|
59 |
|
|
'#title' => t('Default value'),
|
60 |
|
|
'#default_value' => $component['value'],
|
61 |
|
|
'#description' => t('The default value of the field.') . '<br />' . t('Accepts any date 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 today, +2 months, and Dec 9 2004 are all valid.'),
|
62 |
|
|
'#size' => 60,
|
63 |
|
|
'#maxlength' => 127,
|
64 |
|
|
'#weight' => 0,
|
65 |
|
|
);
|
66 |
|
|
$form['extra']['timezone'] = array(
|
67 |
|
|
'#type' => 'radios',
|
68 |
|
|
'#title' => t('Default value timezone'),
|
69 |
|
|
'#default_value' => empty($component['extra']['timezone']) ? 'user' : $component['extra']['timezone'],
|
70 |
|
|
'#description' => t('If using relative dates for a default value (e.g. "today") base the current day on this timezone.'),
|
71 |
|
|
'#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')),
|
72 |
|
|
'#weight' => 2,
|
73 |
|
|
'#access' => variable_get('configurable_timezones', 1),
|
74 |
|
|
);
|
75 |
|
|
|
76 |
|
|
$form['display']['datepicker'] = array(
|
77 |
|
|
'#type' => 'checkbox',
|
78 |
|
|
'#title' => t('Enable popup calendar'),
|
79 |
|
|
'#default_value' => $component['extra']['datepicker'],
|
80 |
|
|
'#description' => t('Enable a JavaScript date picker next to the date field.'),
|
81 |
|
|
'#weight' => 2,
|
82 |
|
|
'#parents' => array('extra', 'datepicker'),
|
83 |
|
|
);
|
84 |
|
|
|
85 |
|
|
$form['display']['year_textfield'] = array(
|
86 |
|
|
'#type' => 'checkbox',
|
87 |
|
|
'#title' => t('Use a textfield for year'),
|
88 |
|
|
'#default_value' => $component['extra']['year_textfield'],
|
89 |
|
|
'#description' => t('If checked, the generated date field will use a textfield for the year. Otherwise it will use a select list.'),
|
90 |
|
|
'#weight' => 5,
|
91 |
|
|
'#parents' => array('extra', 'year_textfield'),
|
92 |
|
|
);
|
93 |
|
|
|
94 |
|
|
$form['validation']['start_date'] = array(
|
95 |
|
|
'#type' => 'textfield',
|
96 |
|
|
'#title' => t('Start date'),
|
97 |
|
|
'#default_value' => $component['extra']['start_date'],
|
98 |
|
|
'#description' => t('The earliest date that may be entered into the field.') . ' ' . t('Accepts any date in any <a href="http://www.gnu.org/software/tar/manual/html_chapter/Date-input-formats.html">GNU Date Input Format</a>.'),
|
99 |
|
|
'#size' => 30,
|
100 |
|
|
'#weight' => 3,
|
101 |
|
|
'#parents' => array('extra', 'start_date'),
|
102 |
|
|
);
|
103 |
|
|
$form['validation']['end_date'] = array(
|
104 |
|
|
'#type' => 'textfield',
|
105 |
|
|
'#title' => t('End date'),
|
106 |
|
|
'#default_value' => $component['extra']['end_date'],
|
107 |
|
|
'#description' => t('The latest date that may be entered into the field.') . ' ' . t('Accepts any date in any <a href="http://www.gnu.org/software/tar/manual/html_chapter/Date-input-formats.html">GNU Date Input Format</a>.'),
|
108 |
|
|
'#size' => 30,
|
109 |
|
|
'#weight' => 4,
|
110 |
|
|
'#parents' => array('extra', 'end_date'),
|
111 |
|
|
);
|
112 |
|
|
|
113 |
|
|
return $form;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
/**
|
117 |
|
|
* Implements _webform_render_component().
|
118 |
|
|
*/
|
119 |
|
|
function _webform_render_date($component, $value = NULL, $filter = TRUE) {
|
120 |
|
|
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
|
121 |
|
|
|
122 |
|
|
$element = array(
|
123 |
|
|
'#type' => 'date',
|
124 |
|
|
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
|
125 |
|
|
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
|
126 |
|
|
'#weight' => $component['weight'],
|
127 |
|
|
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
|
128 |
|
|
'#required' => $component['mandatory'],
|
129 |
|
|
'#start_date' => trim($component['extra']['start_date']),
|
130 |
|
|
'#end_date' => trim($component['extra']['end_date']),
|
131 |
|
|
'#year_textfield' => $component['extra']['year_textfield'],
|
132 |
|
|
'#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
|
133 |
|
|
'#timezone' => $component['extra']['timezone'],
|
134 |
|
|
'#process' => array('webform_expand_date'),
|
135 |
|
|
'#theme' => 'webform_date',
|
136 |
|
|
'#theme_wrappers' => array('webform_element'),
|
137 |
|
|
'#element_validate' => array('webform_validate_date'),
|
138 |
|
|
'#translatable' => array('title', 'description'),
|
139 |
|
|
);
|
140 |
|
|
|
141 |
|
|
if ($component['extra']['datepicker']) {
|
142 |
|
|
$element['#datepicker'] = TRUE;
|
143 |
|
|
$element['#attached'] = array(
|
144 |
|
|
'library' => array(
|
145 |
|
|
array('system', 'ui.datepicker'),
|
146 |
|
|
),
|
147 |
|
|
);
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
// Set the value from Webform.
|
151 |
|
|
if (isset($value[0]) && $value[0] !== '') {
|
152 |
|
|
$value = webform_date_array($value[0], 'date');
|
153 |
|
|
$element['#default_value'] = $value;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
return $element;
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
/**
|
160 |
|
|
* Form API #process function for Webform date fields.
|
161 |
|
|
*/
|
162 |
|
|
function webform_expand_date($element) {
|
163 |
|
|
// Accept a string or array value for #default_value.
|
164 |
|
|
if (!empty($element['#default_value']) && is_string($element['#default_value'])) {
|
165 |
|
|
$timezone = $element['#timezone'] != 'user' ? NULL : 'user';
|
166 |
|
|
$timestring = webform_strtodate('c', $element['#default_value'], $timezone);
|
167 |
|
|
$element['#default_value'] = webform_date_array($timestring, 'date');
|
168 |
|
|
}
|
169 |
ca0757b9
|
Assos Assos
|
// Prevent an error in PHP 5.4 caused by core's treatment of the #value.
|
170 |
|
|
if (isset($element['#value'])) {
|
171 |
|
|
unset($element['#value']);
|
172 |
|
|
}
|
173 |
85ad3d82
|
Assos Assos
|
|
174 |
|
|
// Set defaults according to existing #default_value (set by Form API)
|
175 |
|
|
if (isset($element['#default_value']['month']) || isset($element['#default_value']['day']) || isset($element['#default_value']['year'])) {
|
176 |
|
|
$default_values = array(
|
177 |
|
|
'month' => $element['#default_value']['month'],
|
178 |
|
|
'day' => $element['#default_value']['day'],
|
179 |
|
|
'year' => $element['#default_value']['year'],
|
180 |
|
|
);
|
181 |
|
|
}
|
182 |
|
|
else {
|
183 |
|
|
$default_values = array(
|
184 |
|
|
'day' => NULL,
|
185 |
|
|
'month' => NULL,
|
186 |
|
|
'year' => NULL,
|
187 |
|
|
);
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
// Let Drupal do it's normal expansion.
|
191 |
|
|
$element = form_process_date($element);
|
192 |
|
|
|
193 |
|
|
// Set default values.
|
194 |
|
|
foreach ($default_values as $type => $value) {
|
195 |
|
|
switch ($type) {
|
196 |
|
|
case 'month':
|
197 |
|
|
$none = t('Month');
|
198 |
|
|
break;
|
199 |
|
|
case 'day':
|
200 |
|
|
$none = t('Day');
|
201 |
|
|
break;
|
202 |
|
|
case 'year':
|
203 |
|
|
$none = t('Year');
|
204 |
|
|
break;
|
205 |
|
|
}
|
206 |
|
|
unset($element[$type]['#value']);
|
207 |
3753f249
|
Assos Assos
|
$element[$type]['#title'] = $none;
|
208 |
|
|
$element[$type]['#title_display'] = 'invisible';
|
209 |
85ad3d82
|
Assos Assos
|
$element[$type]['#default_value'] = isset($default_values[$type]) ? $default_values[$type] : NULL;
|
210 |
|
|
$element[$type]['#options'] = array('' => $none) + $element[$type]['#options'];
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
// Convert relative dates to absolute ones.
|
214 |
|
|
foreach (array('start_date', 'end_date') as $start_end) {
|
215 |
|
|
$timezone = $element['#timezone'] != 'user' ? NULL : 'user';
|
216 |
|
|
$date = webform_strtodate('Y-m-d', $element['#' . $start_end], $timezone);
|
217 |
|
|
$element['#' . $start_end] = $date ? $date : '';
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
// Tweak the year field.
|
221 |
|
|
if ($element['#year_textfield']) {
|
222 |
|
|
$element['year']['#type'] = 'textfield';
|
223 |
|
|
$element['year']['#size'] = 5;
|
224 |
|
|
$element['year']['#maxlength'] = 4;
|
225 |
|
|
unset($element['year']['#options']);
|
226 |
|
|
}
|
227 |
|
|
elseif ($element['#start_date'] || $element['#end_date']) {
|
228 |
|
|
$start_year = $element['#start_date'] ? webform_strtodate('Y', $element['#start_date']) : webform_strtodate('Y', '-2 years');
|
229 |
|
|
$end_year = $element['#end_date'] ? webform_strtodate('Y', $element['#end_date']) : webform_strtodate('Y', '+2 years');
|
230 |
|
|
$element['year']['#options'] = array('' => t('Year')) + drupal_map_assoc(range($start_year, $end_year));
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
return $element;
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
/**
|
237 |
|
|
* Theme a webform date element.
|
238 |
|
|
*/
|
239 |
|
|
function theme_webform_date($variables) {
|
240 |
|
|
$element = $variables['element'];
|
241 |
|
|
|
242 |
3753f249
|
Assos Assos
|
$element['year']['#attributes']['class'][] = 'year';
|
243 |
|
|
$element['month']['#attributes']['class'][] = 'month';
|
244 |
|
|
$element['day']['#attributes']['class'][] = 'day';
|
245 |
85ad3d82
|
Assos Assos
|
|
246 |
|
|
// Add error classes to all items within the element.
|
247 |
|
|
if (form_get_error($element)) {
|
248 |
|
|
$element['year']['#attributes']['class'][] = 'error';
|
249 |
|
|
$element['month']['#attributes']['class'][] = 'error';
|
250 |
|
|
$element['day']['#attributes']['class'][] = 'error';
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
$class = array('webform-container-inline');
|
254 |
|
|
|
255 |
|
|
// Add the JavaScript calendar if available (provided by Date module package).
|
256 |
|
|
if (!empty($element['#datepicker'])) {
|
257 |
|
|
$class[] = 'webform-datepicker';
|
258 |
|
|
$calendar_class = array('webform-calendar');
|
259 |
|
|
if ($element['#start_date']) {
|
260 |
|
|
$calendar_class[] = 'webform-calendar-start-' . $element['#start_date'];
|
261 |
|
|
}
|
262 |
|
|
if ($element['#end_date']) {
|
263 |
|
|
$calendar_class[] = 'webform-calendar-end-' . $element['#end_date'];
|
264 |
|
|
}
|
265 |
|
|
$calendar_class[] ='webform-calendar-day-' . variable_get('date_first_day', 0);
|
266 |
|
|
|
267 |
|
|
$calendar = theme('webform_calendar', array('component' => $element['#webform_component'], 'calendar_classes' => $calendar_class));
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
$output = '';
|
271 |
|
|
$output .= '<div class="' . implode(' ', $class) . '">';
|
272 |
|
|
$output .= drupal_render_children($element);
|
273 |
|
|
$output .= isset($calendar) ? $calendar : '';
|
274 |
|
|
$output .= '</div>';
|
275 |
|
|
|
276 |
|
|
return $output;
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
/**
|
280 |
|
|
* Element validation for Webform date fields.
|
281 |
|
|
*/
|
282 |
|
|
function webform_validate_date($element, $form_state) {
|
283 |
|
|
// Check if the user filled the required fields.
|
284 |
|
|
foreach (array('day', 'month', 'year') as $field_type) {
|
285 |
|
|
if (!is_numeric($element[$field_type]['#value']) && $element['#required']) {
|
286 |
|
|
form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
|
287 |
|
|
return;
|
288 |
|
|
}
|
289 |
|
|
}
|
290 |
|
|
|
291 |
|
|
if ($element['month']['#value'] !== '' || $element['day']['#value'] !== '' || $element['year']['#value'] !== '') {
|
292 |
|
|
// Check for a valid date.
|
293 |
|
|
if (!checkdate((int) $element['month']['#value'], (int) $element['day']['#value'], (int) $element['year']['#value'])) {
|
294 |
|
|
form_error($element, t('Entered !name is not a valid date.', array('!name' => $element['#title'])));
|
295 |
|
|
return;
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
// Create a timestamp of the entered value for comparison.
|
299 |
|
|
$timestamp = strtotime($element['year']['#value'] . '-' . $element['month']['#value'] . '-' . $element['day']['#value']);
|
300 |
|
|
$format = webform_date_format('short');
|
301 |
|
|
|
302 |
|
|
// Flip start and end if needed.
|
303 |
|
|
$date1 = strtotime($element['#start_date']);
|
304 |
|
|
$date2 = strtotime($element['#end_date']);
|
305 |
|
|
if ($date1 !== FALSE && $date2 !== FALSE) {
|
306 |
|
|
$start_date = $date1 < $date2 ? $date1 : $date2;
|
307 |
|
|
$end_date = $date1 > $date2 ? $date1 : $date2;
|
308 |
|
|
}
|
309 |
|
|
else {
|
310 |
|
|
$start_date = $date1;
|
311 |
|
|
$end_date = $date2;
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
// Check that the date is after the start date.
|
315 |
|
|
if ($start_date !== FALSE) {
|
316 |
|
|
if ($timestamp < $start_date) {
|
317 |
|
|
form_error($element, t('The entered date must be @start_date or later.', array('@start_date' => date($format, $start_date))));
|
318 |
|
|
}
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
// Check that the date is before the end date.
|
322 |
|
|
if ($end_date !== FALSE) {
|
323 |
|
|
if ($timestamp > $end_date) {
|
324 |
|
|
form_error($element, t('The entered date must be @end_date or earlier.', array('@end_date' => date($format, $end_date))));
|
325 |
|
|
}
|
326 |
|
|
}
|
327 |
|
|
}
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
/**
|
331 |
|
|
* Implements _webform_submit_component().
|
332 |
|
|
*/
|
333 |
|
|
function _webform_submit_date($component, $value) {
|
334 |
|
|
// Convert the date to an ISO 8601 format.
|
335 |
|
|
return ($value['year'] && $value['month'] && $value['day']) ? webform_date_string($value, 'date') : '';
|
336 |
|
|
}
|
337 |
|
|
|
338 |
|
|
/**
|
339 |
|
|
* Implements _webform_display_component().
|
340 |
|
|
*/
|
341 |
|
|
function _webform_display_date($component, $value, $format = 'html') {
|
342 |
|
|
$value = webform_date_array(isset($value[0]) ? $value[0] : '', 'date');
|
343 |
|
|
|
344 |
|
|
return array(
|
345 |
|
|
'#title' => $component['name'],
|
346 |
|
|
'#weight' => $component['weight'],
|
347 |
|
|
'#theme' => 'webform_display_date',
|
348 |
|
|
'#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
|
349 |
|
|
'#format' => $format,
|
350 |
|
|
'#value' => $value,
|
351 |
|
|
'#translatable' => array('title'),
|
352 |
|
|
);
|
353 |
|
|
}
|
354 |
|
|
|
355 |
|
|
/**
|
356 |
|
|
* Format the text output for this component.
|
357 |
|
|
*/
|
358 |
|
|
function theme_webform_display_date($variables) {
|
359 |
|
|
$element = $variables['element'];
|
360 |
|
|
$output = ' ';
|
361 |
|
|
if ($element['#value']['year'] && $element['#value']['month'] && $element['#value']['day']) {
|
362 |
|
|
$timestamp = webform_strtotime($element['#value']['month'] . '/' . $element['#value']['day'] . '/' . $element['#value']['year']);
|
363 |
|
|
$format = webform_date_format('medium');
|
364 |
|
|
$output = format_date($timestamp, 'custom', $format, 'UTC');
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
return $output;
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
/**
|
371 |
|
|
* Implements _webform_analysis_component().
|
372 |
|
|
*/
|
373 |
|
|
function _webform_analysis_date($component, $sids = array()) {
|
374 |
|
|
$query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
|
375 |
|
|
->fields('wsd', array('no', 'data'))
|
376 |
|
|
->condition('nid', $component['nid'])
|
377 |
|
|
->condition('cid', $component['cid'])
|
378 |
|
|
->orderBy('sid');
|
379 |
|
|
|
380 |
|
|
if (count($sids)) {
|
381 |
|
|
$query->condition('sid', $sids, 'IN');
|
382 |
|
|
}
|
383 |
|
|
|
384 |
|
|
$result = $query->execute();
|
385 |
|
|
|
386 |
|
|
$dates = array();
|
387 |
|
|
$submissions = 0;
|
388 |
|
|
foreach ($result as $row) {
|
389 |
|
|
$submissions++;
|
390 |
|
|
if ($row['data']) {
|
391 |
|
|
$dates[] = webform_date_array($row['data']);
|
392 |
|
|
}
|
393 |
|
|
}
|
394 |
|
|
|
395 |
|
|
// Display stats.
|
396 |
|
|
$nonblanks = count($dates);
|
397 |
|
|
$rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
|
398 |
|
|
$rows[1] = array(t('User entered value'), $nonblanks);
|
399 |
|
|
return $rows;
|
400 |
|
|
}
|
401 |
|
|
|
402 |
|
|
/**
|
403 |
|
|
* Implements _webform_table_component().
|
404 |
|
|
*/
|
405 |
|
|
function _webform_table_date($component, $value) {
|
406 |
|
|
if ($value[0]) {
|
407 |
|
|
$timestamp = webform_strtotime($value[0]);
|
408 |
|
|
$format = webform_date_format('short');
|
409 |
|
|
return format_date($timestamp, 'custom', $format, 'UTC');
|
410 |
|
|
}
|
411 |
|
|
else {
|
412 |
|
|
return '';
|
413 |
|
|
}
|
414 |
|
|
}
|
415 |
|
|
|
416 |
|
|
/**
|
417 |
|
|
* Implements _webform_csv_headers_component().
|
418 |
|
|
*/
|
419 |
|
|
function _webform_csv_headers_date($component, $export_options) {
|
420 |
|
|
$header = array();
|
421 |
|
|
$header[0] = '';
|
422 |
|
|
$header[1] = '';
|
423 |
|
|
$header[2] = $component['name'];
|
424 |
|
|
return $header;
|
425 |
|
|
}
|
426 |
|
|
|
427 |
|
|
/**
|
428 |
|
|
* Implements _webform_csv_data_component().
|
429 |
|
|
*/
|
430 |
|
|
function _webform_csv_data_date($component, $export_options, $value) {
|
431 |
|
|
if ($value[0]) {
|
432 |
|
|
$timestamp = webform_strtotime($value[0]);
|
433 |
|
|
$format = webform_date_format('short');
|
434 |
|
|
return format_date($timestamp, 'custom', $format, 'UTC');
|
435 |
|
|
}
|
436 |
|
|
else {
|
437 |
|
|
return '';
|
438 |
|
|
}
|
439 |
|
|
} |