[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/webform/components/ -> date.inc (source)

   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        'arguments' => array('element' => NULL),
  39        'file' => 'components/date.inc',
  40      ),
  41      'webform_display_date' => array(
  42        'arguments' => array('element' => NULL),
  43        'file' => 'components/date.inc',
  44      ),
  45      'webform_calendar' => array(
  46        'arguments' => 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) && module_exists('date_timezone'),
  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      '#access' => function_exists('date_popup_load'),
  83      '#parents' => array('extra', 'datepicker'),
  84    );
  85  
  86    $form['display']['year_textfield'] = array(
  87      '#type' => 'checkbox',
  88      '#title' => t('Use a textfield for year'),
  89      '#default_value' => $component['extra']['year_textfield'],
  90      '#description' => t('If checked, the generated date field will use a textfield for the year. Otherwise it will use a select list.'),
  91      '#weight' => 5,
  92      '#parents' => array('extra', 'year_textfield'),
  93    );
  94  
  95    $form['validation']['start_date'] = array(
  96      '#type' => 'textfield',
  97      '#title' => t('Start date'),
  98      '#default_value' => $component['extra']['start_date'],
  99      '#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>.'),
 100      '#size' => 30,
 101      '#weight' => 3,
 102      '#parents' => array('extra', 'start_date'),
 103    );
 104    $form['validation']['end_date'] = array(
 105      '#type' => 'textfield',
 106      '#title' => t('End date'),
 107      '#default_value' => $component['extra']['end_date'],
 108      '#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>.'),
 109      '#size' => 30,
 110      '#weight' => 4,
 111      '#parents' => array('extra', 'end_date'),
 112    );
 113  
 114    return $form;
 115  }
 116  
 117  /**
 118   * Implements _webform_render_component().
 119   */
 120  function _webform_render_date($component, $value = NULL, $filter = TRUE) {
 121    $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
 122  
 123    $element = array(
 124      '#type' => 'date',
 125      '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
 126      '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
 127      '#weight' => $component['weight'],
 128      '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
 129      '#required' => $component['mandatory'],
 130      '#start_date' => trim($component['extra']['start_date']),
 131      '#end_date' => trim($component['extra']['end_date']),
 132      '#year_textfield' => $component['extra']['year_textfield'],
 133      '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
 134      '#timezone' => $component['extra']['timezone'],
 135      '#process' => array('webform_expand_date'),
 136      '#theme' => 'webform_date',
 137      '#theme_wrappers' => array('webform_element_wrapper'),
 138      '#pre_render' => array('webform_element_title_display'),
 139      '#post_render' => array('webform_element_wrapper'),
 140      '#element_validate' => array('webform_validate_date'),
 141      '#translatable' => array('title', 'description'),
 142    );
 143  
 144    if ($component['extra']['datepicker'] && function_exists('date_popup_load')) {
 145      $element['#datepicker'] = TRUE;
 146    }
 147  
 148    // Set the value from Webform.
 149    if (isset($value[0]) && $value[0] !== '') {
 150      $value = webform_date_array($value[0], 'date');
 151      $element['#default_value'] = $value;
 152    }
 153  
 154    return $element;
 155  }
 156  
 157  /**
 158   * Form API #process function for Webform date fields.
 159   */
 160  function webform_expand_date($element) {
 161    // Accept a string or array value for #default_value.
 162    if (!empty($element['#default_value']) && is_string($element['#default_value'])) {
 163      $timezone = $element['#timezone'] != 'user' ? NULL : 'user';
 164      $timestring = webform_strtodate('c', $element['#default_value'], $timezone);
 165      $element['#default_value'] = webform_date_array($timestring, 'date');
 166    }
 167  
 168    // Set defaults according to existing #default_value (set by Form API)
 169    if (isset($element['#default_value']['month']) || isset($element['#default_value']['day']) || isset($element['#default_value']['year'])) {
 170      $default_values = array(
 171        'month' => $element['#default_value']['month'],
 172        'day' => $element['#default_value']['day'],
 173        'year' => $element['#default_value']['year'],
 174      );
 175    }
 176    else {
 177      $default_values = array(
 178        'day' => NULL,
 179        'month' => NULL,
 180        'year' => NULL,
 181      );
 182    }
 183  
 184    // Let Drupal do it's normal expansion.
 185    $element = expand_date($element);
 186  
 187    // Set default values.
 188    foreach ($default_values as $type => $value) {
 189      switch ($type) {
 190        case 'month':
 191          $none = t('Month');
 192          break;
 193        case 'day':
 194          $none = t('Day');
 195          break;
 196        case 'year':
 197          $none = t('Year');
 198          break;
 199      }
 200      unset($element[$type]['#value']);
 201      $element[$type]['#default_value'] = isset($default_values[$type]) ? $default_values[$type] : NULL;
 202      $element[$type]['#options'] = array('' => $none) + $element[$type]['#options'];
 203    }
 204  
 205    // Convert relative dates to absolute ones.
 206    foreach (array('start_date', 'end_date') as $start_end) {
 207      $timezone = $element['#timezone'] != 'user' ? NULL : 'user';
 208      $date = webform_strtodate('Y-m-d', $element['#' . $start_end], $timezone);
 209      $element['#' . $start_end] = $date ? $date : '';
 210    }
 211  
 212    // Tweak the year field.
 213    if ($element['#year_textfield']) {
 214      $element['year']['#type'] = 'textfield';
 215      $element['year']['#size'] = 5;
 216      $element['year']['#maxlength'] = 4;
 217      unset($element['year']['#options']);
 218    }
 219    elseif ($element['#start_date'] || $element['#end_date']) {
 220      $start_year = $element['#start_date'] ? webform_strtodate('Y', $element['#start_date']) : webform_strtodate('Y', '-2 years');
 221      $end_year = $element['#end_date'] ? webform_strtodate('Y', $element['#end_date']) : webform_strtodate('Y', '+2 years');
 222      $element['year']['#options'] = array('' => t('Year')) + drupal_map_assoc(range($start_year, $end_year));
 223    }
 224  
 225    return $element;
 226  }
 227  
 228  /**
 229   * Theme a webform date element.
 230   */
 231  function theme_webform_date($element) {
 232    $element['year']['#attributes']['class'] = 'year';
 233    $element['month']['#attributes']['class'] = 'month';
 234    $element['day']['#attributes']['class'] = 'day';
 235  
 236    // Add error classes to all items within the element.
 237    if (form_get_error($element)) {
 238      $element['year']['#attributes']['class'] .= ' error';
 239      $element['month']['#attributes']['class'] .= ' error';
 240      $element['day']['#attributes']['class'] .= ' error';
 241    }
 242  
 243    $class = array('webform-container-inline');
 244  
 245    // Add the JavaScript calendar if available (provided by Date module package).
 246    if (!empty($element['#datepicker']) && function_exists('date_popup_load')) {
 247      date_popup_load();
 248  
 249      $class[] = 'webform-datepicker';
 250      $calendar_class = array('webform-calendar');
 251      if ($element['#start_date']) {
 252        $calendar_class[] = 'webform-calendar-start-' . $element['#start_date'];
 253      }
 254      if ($element['#end_date']) {
 255        $calendar_class[] = 'webform-calendar-end-' . $element['#end_date'];
 256      }
 257      $calendar_class[] ='webform-calendar-day-' . variable_get('date_first_day', 0);
 258  
 259      $calendar = theme('webform_calendar', $element['#webform_component'], $calendar_class);
 260    }
 261  
 262    $output = '';
 263    $output .= '<div class="' . implode(' ', $class) . '">';
 264    foreach (element_children($element) as $key) {
 265      $output .= drupal_render($element[$key]);
 266    }
 267    $output .= isset($calendar) ? $calendar : '';
 268    $output .= '</div>';
 269    return $output;
 270  }
 271  
 272  /**
 273   * Element validation for Webform date fields.
 274   */
 275  function webform_validate_date($element, $form_state) {
 276    // Check if the user filled the required fields.
 277    foreach (array('day', 'month', 'year') as $field_type) {
 278      if (!is_numeric($element[$field_type]['#value']) && $element['#required']) {
 279        form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
 280        return;
 281      }
 282    }
 283  
 284    if ($element['month']['#value'] !== '' || $element['day']['#value'] !== '' || $element['year']['#value'] !== '') {
 285      // Check for a valid date.
 286      if (!checkdate((int) $element['month']['#value'], (int) $element['day']['#value'], (int) $element['year']['#value'])) {
 287        form_error($element, t('Entered !name is not a valid date.', array('!name' => $element['#title'])));
 288        return;
 289      }
 290  
 291      // Create a timestamp of the entered value for comparison.
 292      $timestamp = strtotime($element['year']['#value'] . '-' . $element['month']['#value'] . '-' . $element['day']['#value']);
 293      $format = webform_date_format('short');
 294  
 295      // Flip start and end if needed.
 296      $date1 = strtotime($element['#start_date']);
 297      $date2 = strtotime($element['#end_date']);
 298      if ($date1 !== FALSE && $date2 !== FALSE) {
 299        $start_date = $date1 < $date2 ? $date1 : $date2;
 300        $end_date = $date1 > $date2 ? $date1 : $date2;
 301      }
 302      else {
 303        $start_date = $date1;
 304        $end_date = $date2;
 305      }
 306  
 307      // Check that the date is after the start date.
 308      if ($start_date !== FALSE) {
 309        if ($timestamp < $start_date) {
 310          form_error($element, t('The entered date must be @start_date or later.', array('@start_date' => date($format, $start_date))));
 311        }
 312      }
 313  
 314      // Check that the date is before the end date.
 315      if ($end_date !== FALSE) {
 316        if ($timestamp > $end_date) {
 317          form_error($element, t('The entered date must be @end_date or earlier.', array('@end_date' => date($format, $end_date))));
 318        }
 319      }
 320    }
 321  }
 322  
 323  /**
 324   * Implements _webform_submit_component().
 325   */
 326  function _webform_submit_date($component, $value) {
 327    // Convert the date to an ISO 8601 format.
 328    return ($value['year'] && $value['month'] && $value['day']) ? webform_date_string($value, 'date') : '';
 329  }
 330  
 331  /**
 332   * Implements _webform_display_component().
 333   */
 334  function _webform_display_date($component, $value, $format = 'html') {
 335    $value = webform_date_array(isset($value[0]) ? $value[0] : '', 'date');
 336  
 337    return array(
 338      '#title' => $component['name'],
 339      '#weight' => $component['weight'],
 340      '#theme' => 'webform_display_date',
 341      '#theme_wrappers' => $format == 'html' ? array('webform_element', 'webform_element_wrapper') : array('webform_element_text'),
 342      '#post_render' => array('webform_element_wrapper'),
 343      '#format' => $format,
 344      '#value' => $value,
 345      '#translatable' => array('title'),
 346    );
 347  }
 348  
 349  /**
 350   * Format the text output for this component.
 351   */
 352  function theme_webform_display_date($element) {
 353    $output = ' ';
 354    if ($element['#value']['year'] && $element['#value']['month'] && $element['#value']['day']) {
 355      $timestamp = webform_strtotime($element['#value']['month'] . '/' . $element['#value']['day'] . '/' . $element['#value']['year']);
 356      $format = webform_date_format('medium');
 357      $output = format_date($timestamp, 'custom', $format, 0);
 358    }
 359  
 360    return $output;
 361  }
 362  
 363  /**
 364   * Implements _webform_analysis_component().
 365   */
 366  function _webform_analysis_date($component, $sids = array()) {
 367    $placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
 368    $sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : "";
 369    $query = 'SELECT no,data ' .
 370      ' FROM {webform_submitted_data} ' .
 371      ' WHERE nid = %d ' .
 372      ' AND  cid = %d ' . $sidfilter .
 373      ' ORDER BY sid ASC ';
 374  
 375    $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids));
 376  
 377    $dates = array();
 378    $submissions = 0;
 379    while ($row = db_fetch_array($result)) {
 380      $submissions++;
 381      if ($row['data']) {
 382        $dates[] = webform_date_array($row['data']);
 383      }
 384    }
 385  
 386    // Display stats.
 387    $nonblanks = count($dates);
 388    $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
 389    $rows[1] = array(t('User entered value'), $nonblanks);
 390    return $rows;
 391  }
 392  
 393  /**
 394   * Implements _webform_table_component().
 395   */
 396  function _webform_table_date($component, $value) {
 397    if ($value[0]) {
 398      $timestamp = webform_strtotime($value[0]);
 399      $format = webform_date_format('short');
 400      return format_date($timestamp, 'custom', $format, 0);
 401    }
 402    else {
 403      return '';
 404    }
 405  }
 406  
 407  /**
 408   * Implements _webform_csv_headers_component().
 409   */
 410  function _webform_csv_headers_date($component, $export_options) {
 411    $header = array();
 412    $header[0] = '';
 413    $header[1] = '';
 414    $header[2] = $component['name'];
 415    return $header;
 416  }
 417  
 418  /**
 419   * Implements _webform_csv_data_component().
 420   */
 421  function _webform_csv_data_date($component, $export_options, $value) {
 422    if ($value[0]) {
 423      $timestamp = webform_strtotime($value[0]);
 424      $format = webform_date_format('short');
 425      return format_date($timestamp, 'custom', $format, 0);
 426    }
 427    else {
 428      return '';
 429    }
 430  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7