Project

General

Profile

Paste
Download (21.5 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / webform / components / date.inc @ 389fb945

1
<?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
    'required' => 0,
19
    'extra' => array(
20
      'timezone' => 'user',
21
      'exclude' => array(),
22
      'start_date' => '-2 years',
23
      'end_date' => '+2 years',
24
      'year_textfield' => 0,
25
      'datepicker' => 1,
26
      'title_display' => 0,
27
      'description' => '',
28
      'description_above' => FALSE,
29
      'private' => FALSE,
30
      'analysis' => FALSE,
31
    ),
32
  );
33
}
34

    
35
/**
36
 * Implements _webform_theme_component().
37
 */
38
function _webform_theme_date() {
39
  return array(
40
    'webform_date' => array(
41
      'render element' => 'element',
42
      'file' => 'components/date.inc',
43
    ),
44
    'webform_display_date' => array(
45
      'render element' => 'element',
46
      'file' => 'components/date.inc',
47
    ),
48
    'webform_calendar' => array(
49
      'variables' => array('component' => NULL, 'calendar_classes' => NULL),
50
      'template' => 'templates/webform-calendar',
51
    ),
52
  );
53
}
54

    
55
/**
56
 * Implements _webform_edit_component().
57
 */
58
function _webform_edit_date($component) {
59
  $form = array();
60
  $form['value'] = array(
61
    '#type' => 'textfield',
62
    '#title' => t('Default value'),
63
    '#default_value' => $component['value'],
64
    '#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.'),
65
    '#size' => 60,
66
    '#maxlength' => 127,
67
    '#weight' => 0,
68
  );
69
  $form['extra']['timezone'] = array(
70
    '#type' => 'radios',
71
    '#title' => t('Default value timezone'),
72
    '#default_value' => empty($component['extra']['timezone']) ? 'user' : $component['extra']['timezone'],
73
    '#description' => t('If using relative dates for a default value (for example, "today") base the current day on this timezone.'),
74
    '#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')),
75
    '#weight' => 2,
76
    '#access' => variable_get('configurable_timezones', 1),
77
  );
78

    
79
  $form['extra']['exclude'] = array(
80
    '#type' => 'checkboxes',
81
    '#title' => t('Hide'),
82
    '#default_value' => $component['extra']['exclude'],
83
    '#options' => array(
84
      'day' => t('Day'),
85
      'month' => t('Month'),
86
      'year' => t('Year'),
87
    ),
88
    '#description' => t('A hidden day or month will be set to 1. A hidden year will be set to the year of the default value.'),
89
    '#weight' => 3,
90
  );
91

    
92
  $form['display']['datepicker'] = array(
93
    '#type' => 'checkbox',
94
    '#title' => t('Enable popup calendar'),
95
    '#default_value' => $component['extra']['datepicker'],
96
    '#description' => t('Enable a JavaScript date picker next to the date field.'),
97
    '#weight' => 2,
98
    '#parents' => array('extra', 'datepicker'),
99
  );
100

    
101
  $form['display']['year_textfield'] = array(
102
    '#type' => 'checkbox',
103
    '#title' => t('Use a textfield for year'),
104
    '#default_value' => $component['extra']['year_textfield'],
105
    '#description' => t('If checked, the generated date field will use a textfield for the year. Otherwise it will use a select list.'),
106
    '#weight' => 5,
107
    '#parents' => array('extra', 'year_textfield'),
108
  );
109

    
110
  $form['validation']['start_date'] = array(
111
    '#type' => 'textfield',
112
    '#title' => t('Start date'),
113
    '#default_value' => $component['extra']['start_date'],
114
    '#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>.'),
115
    '#size' => 30,
116
    '#weight' => 3,
117
    '#parents' => array('extra', 'start_date'),
118
  );
119
  $form['validation']['end_date'] = array(
120
    '#type' => 'textfield',
121
    '#title' => t('End date'),
122
    '#default_value' => $component['extra']['end_date'],
123
    '#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>.'),
124
    '#size' => 30,
125
    '#weight' => 4,
126
    '#parents' => array('extra', 'end_date'),
127
  );
128

    
129
  $form['#validate'] = array('_webform_edit_date_validate');
130
  return $form;
131
}
132

    
133
/**
134
 * Implements hook_form_id_validate().
135
 *
136
 * Warns user about hiding all the date fields and not using the date picker.
137
 */
138
function _webform_edit_date_validate($form, &$form_state) {
139
  // Reduce checkbox values to simple non-associative array of values.
140
  form_set_value($form['extra']['exclude'], array_filter(array_values($form_state['values']['extra']['exclude'])), $form_state);
141

    
142
  // Note that Drupal 7 doesn't support setting errors no checkboxes due to
143
  // browser issues. See: https://www.drupal.org/node/222380
144
  if (count($form_state['values']['extra']['exclude']) == 3) {
145
    form_set_error('extra][exclude', 'The day, month and year can\'t all be hidden.');
146
  }
147
  if ($form_state['values']['extra']['exclude'] == array('month')) {
148
    form_set_error('extra][exclude', 'You cannot hide just the month.');
149
  }
150

    
151
  // Validate that the start and end dates are valid. Don't validate the default
152
  // date because with token substitution, it might not be valid at component
153
  // definition time. Also note that validation was introduced in 7.x-4.8 and
154
  // previously-defined webform may have invalid start and end dates.
155
  foreach (array('start_date', 'end_date') as $field) {
156
    if (trim($form_state['values']['extra'][$field]) && !($date[$field] = webform_strtodate('c', $form_state['values']['extra'][$field]))) {
157
      form_set_error("extra][$field", t('The @field could not be interpreted in <a href="http://www.gnu.org/software/tar/manual/html_chapter/Date-input-formats.html">GNU Date Input Format</a>.',
158
      array('@field' => $form['validation'][$field]['#title'])));
159
    }
160
  }
161
  if (!empty($date['start_date']) && !empty($date['end_date']) && $date['end_date'] < $date['start_date']) {
162
    form_set_error('extra][end_date', t('The End date must be on or after the Start date.'));
163
  }
164
}
165

    
166
/**
167
 * Implements _webform_render_component().
168
 */
169
function _webform_render_date($component, $value = NULL, $filter = TRUE, $submission = NULL) {
170
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
171

    
172
  $element = array(
173
    '#type' => 'date',
174
    '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
175
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
176
    '#weight' => $component['weight'],
177
    '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
178
    '#required' => $component['required'],
179
    '#start_date' => trim($component['extra']['start_date']),
180
    '#end_date' => trim($component['extra']['end_date']),
181
    '#reference_timestamp' => $submission && $submission->completed ? $submission->completed : NULL,
182
    '#year_textfield' => $component['extra']['year_textfield'],
183
    '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
184
    '#timezone' => $component['extra']['timezone'],
185
    '#exclude' => $component['extra']['exclude'],
186
    '#process' => array('webform_expand_date'),
187
    '#theme' => 'webform_date',
188
    '#theme_wrappers' => array('webform_element'),
189
    '#element_validate' => array('webform_validate_date'),
190
    '#translatable' => array('title', 'description'),
191
  );
192

    
193
  if ($component['extra']['datepicker']) {
194
    $element['#datepicker'] = TRUE;
195
    $element['#attached'] = array(
196
      'library' => array(
197
        array('system', 'ui.datepicker'),
198
      ),
199
    );
200
  }
201

    
202
  // Set the value from Webform.
203
  if (isset($value[0]) && $value[0] !== '') {
204
    $value = webform_date_array($value[0], 'date');
205
    $element['#default_value'] = $value;
206
  }
207

    
208
  return $element;
209
}
210

    
211
/**
212
 * Form API #process function for Webform date fields.
213
 */
214
function webform_expand_date($element) {
215
  $timezone = $element['#timezone'] != 'user' ? NULL : 'user';
216

    
217
  // Accept a string or array value for #default_value.
218
  if (!empty($element['#default_value']) && is_string($element['#default_value'])) {
219
    $timestring = webform_strtodate('c', $element['#default_value'], $timezone);
220
    $element['#default_value'] = webform_date_array($timestring, 'date');
221
  }
222
  // Prevent an error in PHP 5.4 caused by core's treatment of the #value.
223
  if (isset($element['#value'])) {
224
    unset($element['#value']);
225
  }
226

    
227
  // Set defaults according to existing #default_value (set by Form API).
228
  if (isset($element['#default_value']['month']) || isset($element['#default_value']['day']) || isset($element['#default_value']['year'])) {
229
    $default_values = array(
230
      'month' => $element['#default_value']['month'],
231
      'day' => $element['#default_value']['day'],
232
      'year' => $element['#default_value']['year'],
233
    );
234
  }
235
  else {
236
    $default_values = array(
237
      'day' => NULL,
238
      'month' => NULL,
239
      'year' => NULL,
240
    );
241
  }
242

    
243
  // Let Drupal do it's normal expansion.
244
  $element = form_process_date($element);
245

    
246
  // Convert relative dates to absolute and calculate the year, month and day.
247
  $timezone = $element['#timezone'] != 'user' ? NULL : 'user';
248
  foreach (array('start', 'end') as $start_end) {
249
    $element_field = &$element["#{$start_end}_date"];
250
    $element_field = $element_field ? webform_strtodate('Y-m-d', $element_field, $timezone, $element['#reference_timestamp']) : '';
251
    if ($element_field) {
252
      $parts = explode('-', $element_field);
253
    }
254
    else {
255
      $parts = $start_end == 'start' ? array(webform_strtodate('Y', '-2 years'), 1, 1) : array(webform_strtodate('Y', '+2 years'), 12, 31);
256
      $element_field = '';
257
    }
258
    // Drop PHP reference.
259
    unset($element_field);
260
    $parts[3] = $parts[0] . '-' . $parts[1] . '-' . $parts[2];
261
    $range[$start_end] = array_combine(array('year', 'month', 'day', 'date'), $parts);
262
  }
263

    
264
  // The start date is not guaranteed to be early than the end date for
265
  // historical reasons.
266
  if ($range['start']['date'] > $range['end']['date']) {
267
    $temp = $range['start'];
268
    $range['start'] = $range['end'];
269
    $range['end'] = $temp;
270
  }
271

    
272
  // Restrict the months and days when not all options are valid choices.
273
  if ($element['#start_date'] && $element['#end_date']) {
274
    $delta_months = ($range['end']['year'] * 12 + $range['end']['month']) - ($range['start']['year'] * 12 + $range['start']['month']);
275
    if ($delta_months < 11) {
276
      // There are 10 or fewer months between the start and end date. If there
277
      // were 11, then every month would be possible, and the menu select
278
      // should not be pruned.
279
      $month_options = &$element['month']['#options'];
280
      if ($range['start']['month'] <= $range['end']['month']) {
281
        $month_options = array_intersect_key($month_options, array_flip(range($range['start']['month'], $range['end']['month'])));
282
      }
283
      else {
284
        $month_options = array_intersect_key($month_options, array_flip(range($range['start']['month'], 12))) +
285
          array_intersect_key($month_options, array_flip(range(1, $range['end']['month'])));
286
      }
287
      // Drop PHP reference.
288
      unset($month_options);
289
      if ($delta_months <= 1) {
290
        // The start and end date are either on the same month or consecutive
291
        // months. See if the days should be pruned.
292
        $day_options = &$element['day']['#options'];
293
        if ($range['start']['month'] == $range['end']['month']) {
294
          // Range is within the same month. The days are a simple range from
295
          // start day to end day.
296
          $day_options = array_intersect_key($day_options, array_flip(range($range['start']['day'], $range['end']['day'])));
297
        }
298
        elseif ($range['start']['day'] > $range['end']['day'] + 1) {
299
          // Range spans two months and at least one day would be omitted.
300
          $days_in_month = date('t', mktime(0, 0, 0, $range['start']['month'], 1, $range['start']['year']));
301
          $day_options = array_intersect_key($day_options, array_flip(range($range['start']['day'], $days_in_month))) +
302
            array_intersect_key($day_options, array_flip(range(1, $range['end']['day'])));
303
        }
304
        // Drop PHP reference.
305
        unset($day_options);
306
      }
307
    }
308
  }
309

    
310
  // Set default values.
311
  foreach ($default_values as $type => $value) {
312
    switch ($type) {
313
      case 'month':
314
        $none = t('Month');
315
        $hidden_default = 1;
316
        break;
317

    
318
      case 'day':
319
        $none = t('Day');
320
        $hidden_default = 1;
321
        break;
322

    
323
      case 'year':
324
        $none = t('Year');
325
        $hidden_default = !empty($element['#default_value']['year']) ? $element['#default_value']['year'] : webform_strtodate('Y', 'today', $timezone);
326
        break;
327
    }
328
    unset($element[$type]['#value']);
329
    $element[$type]['#title'] = $none;
330
    $element[$type]['#title_display'] = 'invisible';
331
    $element[$type]['#default_value'] = $default_values[$type];
332
    $element[$type]['#options'] = array('' => $none) + $element[$type]['#options'];
333
    if (in_array($type, $element['#exclude'])) {
334
      $element[$type] += array(
335
        '#prefix' => '<div class="webform-date-field-wrapper element-invisible">',
336
        '#suffix' => '</div>',
337
      );
338
      $element[$type]['#default_value'] = $hidden_default;
339
    }
340
  }
341

    
342
  // Tweak the year field.
343
  if ($element['#year_textfield']) {
344
    $element['year']['#type'] = 'textfield';
345
    $element['year']['#size'] = 5;
346
    $element['year']['#maxlength'] = 4;
347
    unset($element['year']['#options']);
348
  }
349
  elseif ($element['#start_date'] || $element['#end_date']) {
350
    $element['year']['#options'] = array('' => t('Year')) + drupal_map_assoc(range($range['start']['year'], $range['end']['year']));
351
  }
352

    
353
  return $element;
354
}
355

    
356
/**
357
 * Theme a webform date element.
358
 */
359
function theme_webform_date($variables) {
360
  $element = $variables['element'];
361

    
362
  $element['year']['#attributes']['class'][] = 'year';
363
  $element['month']['#attributes']['class'][] = 'month';
364
  $element['day']['#attributes']['class'][] = 'day';
365

    
366
  // Add error classes to all items within the element.
367
  if (form_get_error($element)) {
368
    $element['year']['#attributes']['class'][] = 'error';
369
    $element['month']['#attributes']['class'][] = 'error';
370
    $element['day']['#attributes']['class'][] = 'error';
371
  }
372

    
373
  // Add HTML5 required attribute, if needed.
374
  if ($element['#required']) {
375
    $element['year']['#attributes']['required'] = 'required';
376
    $element['month']['#attributes']['required'] = 'required';
377
    $element['day']['#attributes']['required'] = 'required';
378
  }
379

    
380
  $class = array('webform-container-inline');
381

    
382
  // Add the JavaScript calendar if available (provided by Date module package).
383
  if (!empty($element['#datepicker'])) {
384
    $class[] = 'webform-datepicker';
385
    $calendar_class = array('webform-calendar');
386
    if ($element['#start_date']) {
387
      $calendar_class[] = 'webform-calendar-start-' . $element['#start_date'];
388
    }
389
    if ($element['#end_date']) {
390
      $calendar_class[] = 'webform-calendar-end-' . $element['#end_date'];
391
    }
392
    $calendar_class[] = 'webform-calendar-day-' . variable_get('date_first_day', 0);
393

    
394
    $calendar = theme('webform_calendar', array('component' => $element['#webform_component'], 'calendar_classes' => $calendar_class));
395
  }
396

    
397
  $output = '';
398
  $output .= '<div class="' . implode(' ', $class) . '">';
399
  $output .= drupal_render_children($element);
400
  $output .= isset($calendar) ? $calendar : '';
401
  $output .= '</div>';
402

    
403
  return $output;
404
}
405

    
406
/**
407
 * Element validation for Webform date fields.
408
 */
409
function webform_validate_date($element, $form_state) {
410
  $date_parts = array('day', 'month', 'year');
411

    
412
  // Determine if the user has specified a date. Hidden parts of the date will
413
  // be submitted automatically.
414
  foreach ($date_parts as $date_part) {
415
    if (!in_array($date_part, $element['#exclude']) && $element[$date_part]['#value'] !== '') {
416
      $field_found = TRUE;
417
    }
418
  }
419

    
420
  if (isset($field_found)) {
421
    // Check that each part of the date has been filled in.
422
    foreach ($date_parts as $date_part) {
423
      if (empty($element[$date_part]['#value'])) {
424
        form_error($element[$date_part], t('!part in !name is missing.', array('!name' => $element['#title'], '!part' => $element[$date_part]['#title'])));
425
        $missing_fields = TRUE;
426
      }
427
    }
428
    if (isset($missing_fields)) {
429
      return;
430
    }
431

    
432
    // Ensure date is made up of integers.
433
    foreach ($date_parts as $date_part) {
434
      $element[$date_part]['#value'] = (int) $element[$date_part]['#value'];
435
    }
436

    
437
    // Check for a valid date.
438
    if (!checkdate($element['month']['#value'], $element['day']['#value'], $element['year']['#value'])) {
439
      form_error($element, t('Entered !name is not a valid date.', array('!name' => $element['#title'])));
440
      return;
441
    }
442

    
443
    // Create a timestamp of the entered value for comparison.
444
    $timestamp = strtotime($element['year']['#value'] . '-' . $element['month']['#value'] . '-' . $element['day']['#value']);
445
    $format = webform_date_format('short');
446

    
447
    // Flip start and end if needed. Prior to 7.x-4.8, it was possible to save
448
    // a date component with the end date earlier than the start date.
449
    $date1 = strtotime($element['#start_date']);
450
    $date2 = strtotime($element['#end_date']);
451
    if ($date1 !== FALSE && $date2 !== FALSE) {
452
      $start_date = $date1 < $date2 ? $date1 : $date2;
453
      $end_date = $date1 > $date2 ? $date1 : $date2;
454
    }
455
    else {
456
      $start_date = $date1;
457
      $end_date = $date2;
458
    }
459

    
460
    // Check that the date is after the start date.
461
    if ($start_date !== FALSE && $timestamp < $start_date) {
462
      form_error($element, t('The entered date must be @start_date or later.', array('@start_date' => date($format, $start_date))));
463
    }
464

    
465
    // Check that the date is before the end date.
466
    if ($end_date !== FALSE && $timestamp > $end_date) {
467
      form_error($element, t('The entered date must be @end_date or earlier.', array('@end_date' => date($format, $end_date))));
468
    }
469
  }
470
  elseif ($element['#required']) {
471
    form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
472
  }
473
}
474

    
475
/**
476
 * Implements _webform_submit_component().
477
 */
478
function _webform_submit_date($component, $value) {
479
  // Convert the date to an ISO 8601 format.
480
  return ($value['year'] && $value['month'] && $value['day']) ? webform_date_string($value, 'date') : '';
481
}
482

    
483
/**
484
 * Implements _webform_display_component().
485
 */
486
function _webform_display_date($component, $value, $format = 'html', $submission = array()) {
487
  $value = webform_date_array(isset($value[0]) ? $value[0] : '', 'date');
488

    
489
  return array(
490
    '#title' => $component['name'],
491
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
492
    '#weight' => $component['weight'],
493
    '#theme' => 'webform_display_date',
494
    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
495
    '#format' => $format,
496
    '#exclude' => $component['extra']['exclude'],
497

    
498
    '#value' => $value,
499
    '#translatable' => array('title'),
500
  );
501
}
502

    
503
/**
504
 * Format the text output for this component.
505
 */
506
function theme_webform_display_date($variables) {
507
  $element = $variables['element'];
508
  $output = ' ';
509
  if ($element['#value']['year'] && $element['#value']['month'] && $element['#value']['day']) {
510
    $timestamp = webform_strtotime($element['#value']['month'] . '/' . $element['#value']['day'] . '/' . $element['#value']['year']);
511
    $format = webform_date_format(NULL, $element['#exclude']);
512
    $output = format_date($timestamp, 'custom', $format, 'UTC');
513
  }
514

    
515
  return $output;
516
}
517

    
518
/**
519
 * Implements _webform_analysis_component().
520
 */
521
function _webform_analysis_date($component, $sids = array(), $single = FALSE, $join = NULL) {
522
  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
523
    ->fields('wsd', array('no', 'data'))
524
    ->condition('wsd.nid', $component['nid'])
525
    ->condition('wsd.cid', $component['cid'])
526
    ->orderBy('wsd.sid');
527

    
528
  if (count($sids)) {
529
    $query->condition('wsd.sid', $sids, 'IN');
530
  }
531

    
532
  if ($join) {
533
    $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
534
  }
535

    
536
  $result = $query->execute();
537

    
538
  $dates = array();
539
  $submissions = 0;
540
  foreach ($result as $row) {
541
    $submissions++;
542
    if ($row['data']) {
543
      $dates[] = webform_date_array($row['data']);
544
    }
545
  }
546

    
547
  // Display stats.
548
  $nonblanks = count($dates);
549
  $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
550
  $rows[1] = array(t('User entered value'), $nonblanks);
551

    
552
  return array(
553
    'table_rows' => $rows,
554
  );
555
}
556

    
557
/**
558
 * Implements _webform_table_component().
559
 */
560
function _webform_table_date($component, $value) {
561
  if ($value[0]) {
562
    $timestamp = webform_strtotime($value[0]);
563
    $format = webform_date_format('short', $component['extra']['exclude']);
564
    return format_date($timestamp, 'custom', $format, 'UTC');
565
  }
566
  else {
567
    return '';
568
  }
569
}
570

    
571
/**
572
 * Implements _webform_csv_headers_component().
573
 */
574
function _webform_csv_headers_date($component, $export_options) {
575
  $header = array();
576
  $header[0] = '';
577
  $header[1] = '';
578
  $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
579
  return $header;
580
}
581

    
582
/**
583
 * Implements _webform_csv_data_component().
584
 */
585
function _webform_csv_data_date($component, $export_options, $value) {
586
  if ($value[0]) {
587
    $timestamp = webform_strtotime($value[0]);
588
    if (!empty($export_options['iso8601_date'])) {
589
      // ISO 8601 date: 2004-02-12.
590
      $format = 'Y-m-d';
591
    }
592
    else {
593
      $format = webform_date_format('short');
594
    }
595
    return format_date($timestamp, 'custom', $format, 'UTC');
596
  }
597
  else {
598
    return '';
599
  }
600
}