Projet

Général

Profil

Paste
Télécharger (14,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / components / date.inc @ 74f6bef0

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
    '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

    
170
  // Set defaults according to existing #default_value (set by Form API)
171
  if (isset($element['#default_value']['month']) || isset($element['#default_value']['day']) || isset($element['#default_value']['year'])) {
172
    $default_values = array(
173
      'month' => $element['#default_value']['month'],
174
      'day' => $element['#default_value']['day'],
175
      'year' => $element['#default_value']['year'],
176
    );
177
  }
178
  else {
179
    $default_values = array(
180
      'day' => NULL,
181
      'month' => NULL,
182
      'year' => NULL,
183
    );
184
  }
185

    
186
  // Let Drupal do it's normal expansion.
187
  $element = form_process_date($element);
188

    
189
  // Set default values.
190
  foreach ($default_values as $type => $value) {
191
    switch ($type) {
192
      case 'month':
193
        $none = t('Month');
194
        break;
195
      case 'day':
196
        $none = t('Day');
197
        break;
198
      case 'year':
199
        $none = t('Year');
200
        break;
201
    }
202
    unset($element[$type]['#value']);
203
    $element[$type]['#title'] = $none;
204
    $element[$type]['#title_display'] = 'invisible';
205
    $element[$type]['#default_value'] = isset($default_values[$type]) ? $default_values[$type] : NULL;
206
    $element[$type]['#options'] = array('' => $none) + $element[$type]['#options'];
207
  }
208

    
209
  // Convert relative dates to absolute ones.
210
  foreach (array('start_date', 'end_date') as $start_end) {
211
    $timezone = $element['#timezone'] != 'user' ? NULL : 'user';
212
    $date = webform_strtodate('Y-m-d', $element['#' . $start_end], $timezone);
213
    $element['#' . $start_end] = $date ? $date : '';
214
  }
215

    
216
  // Tweak the year field.
217
  if ($element['#year_textfield']) {
218
    $element['year']['#type'] = 'textfield';
219
    $element['year']['#size'] = 5;
220
    $element['year']['#maxlength'] = 4;
221
    unset($element['year']['#options']);
222
  }
223
  elseif ($element['#start_date'] || $element['#end_date']) {
224
    $start_year = $element['#start_date'] ? webform_strtodate('Y', $element['#start_date']) : webform_strtodate('Y', '-2 years');
225
    $end_year = $element['#end_date'] ? webform_strtodate('Y', $element['#end_date']) : webform_strtodate('Y', '+2 years');
226
    $element['year']['#options'] = array('' => t('Year')) + drupal_map_assoc(range($start_year, $end_year));
227
  }
228

    
229
  return $element;
230
}
231

    
232
/**
233
 * Theme a webform date element.
234
 */
235
function theme_webform_date($variables) {
236
  $element = $variables['element'];
237

    
238
  $element['year']['#attributes']['class'][] = 'year';
239
  $element['month']['#attributes']['class'][] = 'month';
240
  $element['day']['#attributes']['class'][] = 'day';
241

    
242
  // Add error classes to all items within the element.
243
  if (form_get_error($element)) {
244
    $element['year']['#attributes']['class'][] = 'error';
245
    $element['month']['#attributes']['class'][] = 'error';
246
    $element['day']['#attributes']['class'][] = 'error';
247
  }
248

    
249
  $class = array('webform-container-inline');
250

    
251
  // Add the JavaScript calendar if available (provided by Date module package).
252
  if (!empty($element['#datepicker'])) {
253
    $class[] = 'webform-datepicker';
254
    $calendar_class = array('webform-calendar');
255
    if ($element['#start_date']) {
256
      $calendar_class[] = 'webform-calendar-start-' . $element['#start_date'];
257
    }
258
    if ($element['#end_date']) {
259
      $calendar_class[] = 'webform-calendar-end-' . $element['#end_date'];
260
    }
261
    $calendar_class[] ='webform-calendar-day-' . variable_get('date_first_day', 0);
262

    
263
    $calendar = theme('webform_calendar', array('component' => $element['#webform_component'], 'calendar_classes' => $calendar_class));
264
  }
265

    
266
  $output = '';
267
  $output .= '<div class="' . implode(' ', $class) . '">';
268
  $output .= drupal_render_children($element);
269
  $output .= isset($calendar) ? $calendar : '';
270
  $output .= '</div>';
271

    
272
  return $output;
273
}
274

    
275
/**
276
 * Element validation for Webform date fields.
277
 */
278
function webform_validate_date($element, $form_state) {
279
  // Check if the user filled the required fields.
280
  foreach (array('day', 'month', 'year') as $field_type) {
281
    if (!is_numeric($element[$field_type]['#value']) && $element['#required']) {
282
      form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
283
      return;
284
    }
285
  }
286

    
287
  if ($element['month']['#value'] !== '' || $element['day']['#value'] !== '' || $element['year']['#value'] !== '') {
288
    // Check for a valid date.
289
    if (!checkdate((int) $element['month']['#value'], (int) $element['day']['#value'], (int) $element['year']['#value'])) {
290
      form_error($element, t('Entered !name is not a valid date.', array('!name' => $element['#title'])));
291
      return;
292
    }
293

    
294
    // Create a timestamp of the entered value for comparison.
295
    $timestamp = strtotime($element['year']['#value'] . '-' . $element['month']['#value'] . '-' . $element['day']['#value']);
296
    $format = webform_date_format('short');
297

    
298
    // Flip start and end if needed.
299
    $date1 = strtotime($element['#start_date']);
300
    $date2 = strtotime($element['#end_date']);
301
    if ($date1 !== FALSE && $date2 !== FALSE) {
302
      $start_date = $date1 < $date2 ? $date1 : $date2;
303
      $end_date = $date1 > $date2 ? $date1 : $date2;
304
    }
305
    else {
306
      $start_date = $date1;
307
      $end_date = $date2;
308
    }
309

    
310
    // Check that the date is after the start date.
311
    if ($start_date !== FALSE) {
312
      if ($timestamp < $start_date) {
313
        form_error($element, t('The entered date must be @start_date or later.', array('@start_date' => date($format, $start_date))));
314
      }
315
    }
316

    
317
    // Check that the date is before the end date.
318
    if ($end_date !== FALSE) {
319
      if ($timestamp > $end_date) {
320
        form_error($element, t('The entered date must be @end_date or earlier.', array('@end_date' => date($format, $end_date))));
321
      }
322
    }
323
  }
324
}
325

    
326
/**
327
 * Implements _webform_submit_component().
328
 */
329
function _webform_submit_date($component, $value) {
330
  // Convert the date to an ISO 8601 format.
331
  return ($value['year'] && $value['month'] && $value['day']) ? webform_date_string($value, 'date') : '';
332
}
333

    
334
/**
335
 * Implements _webform_display_component().
336
 */
337
function _webform_display_date($component, $value, $format = 'html') {
338
  $value = webform_date_array(isset($value[0]) ? $value[0] : '', 'date');
339

    
340
  return array(
341
    '#title' => $component['name'],
342
    '#weight' => $component['weight'],
343
    '#theme' => 'webform_display_date',
344
    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
345
    '#format' => $format,
346
    '#value' => $value,
347
    '#translatable' => array('title'),
348
  );
349
}
350

    
351
/**
352
 * Format the text output for this component.
353
 */
354
function theme_webform_display_date($variables) {
355
  $element = $variables['element'];
356
  $output = ' ';
357
  if ($element['#value']['year'] && $element['#value']['month'] && $element['#value']['day']) {
358
    $timestamp = webform_strtotime($element['#value']['month'] . '/' . $element['#value']['day'] . '/' . $element['#value']['year']);
359
    $format = webform_date_format('medium');
360
    $output = format_date($timestamp, 'custom', $format, 'UTC');
361
  }
362

    
363
  return $output;
364
}
365

    
366
/**
367
 * Implements _webform_analysis_component().
368
 */
369
function _webform_analysis_date($component, $sids = array()) {
370
  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
371
    ->fields('wsd', array('no', 'data'))
372
    ->condition('nid', $component['nid'])
373
    ->condition('cid', $component['cid'])
374
    ->orderBy('sid');
375

    
376
  if (count($sids)) {
377
    $query->condition('sid', $sids, 'IN');
378
  }
379

    
380
  $result = $query->execute();
381

    
382
  $dates = array();
383
  $submissions = 0;
384
  foreach ($result as $row) {
385
    $submissions++;
386
    if ($row['data']) {
387
      $dates[] = webform_date_array($row['data']);
388
    }
389
  }
390

    
391
  // Display stats.
392
  $nonblanks = count($dates);
393
  $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
394
  $rows[1] = array(t('User entered value'), $nonblanks);
395
  return $rows;
396
}
397

    
398
/**
399
 * Implements _webform_table_component().
400
 */
401
function _webform_table_date($component, $value) {
402
  if ($value[0]) {
403
    $timestamp = webform_strtotime($value[0]);
404
    $format = webform_date_format('short');
405
    return format_date($timestamp, 'custom', $format, 'UTC');
406
  }
407
  else {
408
    return '';
409
  }
410
}
411

    
412
/**
413
 * Implements _webform_csv_headers_component().
414
 */
415
function _webform_csv_headers_date($component, $export_options) {
416
  $header = array();
417
  $header[0] = '';
418
  $header[1] = '';
419
  $header[2] = $component['name'];
420
  return $header;
421
}
422

    
423
/**
424
 * Implements _webform_csv_data_component().
425
 */
426
function _webform_csv_data_date($component, $export_options, $value) {
427
  if ($value[0]) {
428
    $timestamp = webform_strtotime($value[0]);
429
    $format = webform_date_format('short');
430
    return format_date($timestamp, 'custom', $format, 'UTC');
431
  }
432
  else {
433
    return '';
434
  }
435
}