You are here

function _webform_render_time in Webform 5

Same name and namespace in other branches
  1. 5.2 components/time.inc \_webform_render_time()
  2. 6.3 components/time.inc \_webform_render_time()
  3. 6.2 components/time.inc \_webform_render_time()
  4. 7.4 components/time.inc \_webform_render_time()
  5. 7.3 components/time.inc \_webform_render_time()

Build a form item array containing all the properties of this component

Parameters

$component An array of information describing the component, directly correlating to the webform_component database schema:

Return value

An array of a form item to be displayed on the client-side webform

1 call to _webform_render_time()
_webform_submission_display_time in components/time.inc
Display the result of a textfield submission. The output of this function will be displayed under the "results" tab then "submissions".

File

components/time.inc, line 53

Code

function _webform_render_time($component) {
  if (strlen($component['value']) > 0) {

    // Calculate the timestamp in GMT.
    $timestamp = strtotime($component['value']);
    if ($component['extra']['timezone'] == "user") {

      // Use the users timezone.
      global $user;
      $timestamp += (int) $user->timezone;
    }
    elseif ($component['extra']['timezone'] == "gmt") {

      // Use GMT.
      $timestamp += 0;
    }
    else {

      // Use the Drupal site time.
      $timestamp += (int) variable_get('date_default_timezone', 0);
    }

    // Check for daylight savings time.
    if ($component['extra']['check_daylight_savings'] && date("I")) {
      $timestamp += 3600;
    }
  }
  if ($component['extra']['hourformat'] == '12-hour') {
    $first_hour = 1;
    $last_hour = 12;
    $hour_format = 'g';
  }
  else {
    $first_hour = 0;
    $last_hour = 23;
    $hour_format = 'G';
  }
  if (strlen($component['value']) > 0) {
    $hour = gmdate($hour_format, $timestamp);
    $minute = gmdate('i', $timestamp);
    $am_pm = gmdate('a', $timestamp);
  }

  // Generate the choices for drop-down selects.
  $hours[""] = t("hour");
  $minutes[""] = t("minute");
  for ($i = $first_hour; $i <= $last_hour; $i++) {
    $hours[$i] = $i;
  }
  for ($i = 0; $i <= 59; $i++) {
    $minutes[$i < 10 ? "0{$i}" : $i] = $i < 10 ? "0{$i}" : $i;
  }
  $am_pms = array(
    'am' => t('am'),
    'pm' => t('pm'),
  );
  $form_item = array(
    '#title' => $component['name'],
    '#required' => $component['mandatory'],
    '#weight' => $component['weight'],
    '#description' => _webform_filtervalues($component['extra']['description']),
    '#prefix' => '<div class="webform-component-' . $component['type'] . '" id="webform-component-' . $component['form_key'] . '">',
    '#suffix' => '</div>',
    '#theme' => 'webform_time',
  );
  $form_item['hour'] = array(
    '#prefix' => '',
    '#type' => 'select',
    '#default_value' => $hour,
    '#options' => $hours,
    '#validate' => array(
      'webform_validate_time' => array(
        'hour',
        $component['name'],
        $component['mandatory'],
      ),
    ),
  );
  $form_item['minute'] = array(
    '#prefix' => ':',
    '#type' => 'select',
    '#default_value' => $minute,
    '#options' => $minutes,
    '#validate' => array(
      'webform_validate_time' => array(
        'minute',
        $component['name'],
        $component['mandatory'],
      ),
    ),
  );
  if ($component['extra']['hourformat'] == '12-hour') {
    $form_item['ampm'] = array(
      '#type' => 'radios',
      '#default_value' => $am_pm,
      '#options' => $am_pms,
    );
  }
  return $form_item;
}