You are here

function date_views_filter_handler_simple::date_parts_form in Date 8

Same name and namespace in other branches
  1. 7.3 date_views/includes/date_views_filter_handler_simple.inc \date_views_filter_handler_simple::date_parts_form()
  2. 7.2 date_views/includes/date_views_filter_handler_simple.inc \date_views_filter_handler_simple::date_parts_form()

A form element to select date part values.

Parameters

string $prefix: A prefix for the date values, 'value', 'min', or 'max' .

string $source: The operator for this element.

string $which: Which element to provide, 'all', 'value', or 'minmax' .

array $operator_values: An array of the allowed operators for this element.

array $identifier: Identifier of the exposed element.

array $relative_id: Form element id to use for the relative date field.

Return value

The form date part element for this instance.

1 call to date_views_filter_handler_simple::date_parts_form()
date_views_filter_handler_simple::value_form in date_views/includes/date_views_filter_handler_simple.inc
Add the selectors to the value form using the date handler.

File

date_views/includes/date_views_filter_handler_simple.inc, line 299
A standard Views filter for a single date field, using Date API form selectors and sql handling.

Class

date_views_filter_handler_simple

Code

function date_parts_form($form_state, $prefix, $source, $which, $operator_values, $identifier, $relative_id) {
  module_load_include('inc', 'date_api', 'date_api_elements');
  switch ($prefix) {
    case 'min':
      $label = t('Start date');
      $relative_label = t('Relative start date');
      break;
    case 'max':
      $label = t('End date');
      $relative_label = t('Relative end date');
      break;
    default:
      $label = '';
      $relative_label = t('Relative date');
      break;
  }
  $type = $this->options['form_type'];
  $format = $this->date_handler
    ->views_formats($this->options['granularity'], 'sql');
  $granularity = array_keys($this->date_handler
    ->date_parts($this->options['granularity']));
  $relative_value = $prefix == 'max' ? $this->options['default_to_date'] : $this->options['default_date'];
  if (!empty($form_state['exposed'])) {

    // UI when the date selector is exposed.
    $default_date = $this
      ->date_default_value($prefix);
    $id = 'edit-' . str_replace('_', '-', $this->field) . '-' . $prefix;
    $form[$prefix] = array(
      '#title' => check_plain($label),
      '#type' => $type,
      '#size' => 20,
      '#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
      '#date_date_format' => DateGranularity::limitFormat($format, $granularity),
      '#date_label_position' => 'within',
      '#date_year_range' => $this->options['year_range'],
      '#process' => array(
        $type . '_element_process',
      ),
      '#prefix' => '<div id="' . $id . '-wrapper"><div id="' . $id . '">',
      '#suffix' => '</div></div>',
    );
    if ($which == 'all') {
      $form[$prefix]['#pre_render'][] = 'ctools_dependent_pre_render';
      $form[$prefix]['#dependency'] = array(
        $source => $operator_values,
      );
    }
    if (!isset($form_state['input'][$identifier][$prefix])) {
      $form_state['input'][$identifier][$prefix] = $this->value[$prefix];
    }
  }
  else {

    // UI when the date selector is on the views configuration screen.
    $default_date = '';
    $id = 'edit-options-value-' . $prefix;
    $form[$prefix . '_group'] = array(
      '#type' => 'fieldset',
      '#attributes' => array(
        'class' => array(
          'date-views-filter-fieldset',
        ),
      ),
    );
    $form[$prefix . '_group'][$prefix . '_choose_input_type'] = array(
      '#title' => check_plain($label),
      '#type' => 'select',
      '#options' => array(
        'date' => t('Select a date'),
        'relative' => 'Enter a relative date',
      ),
      '#attributes' => array(
        'class' => array(
          $prefix . '-choose-input-type',
        ),
      ),
      '#default_value' => !empty($relative_value) ? 'relative' : 'date',
    );
    $form[$prefix . '_group'][$prefix] = array(
      '#title' => t('Select a date'),
      '#type' => $type,
      '#size' => 20,
      '#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
      '#date_format' => DateGranularity::limitFormat($format, $granularity),
      '#date_label_position' => 'within',
      '#date_year_range' => $this->options['year_range'],
      '#process' => array(
        $type . '_element_process',
      ),
      '#prefix' => '<div id="' . $id . '-wrapper"><div id="' . $id . '">',
      '#suffix' => '</div></div>',
      '#states' => array(
        'visible' => array(
          ":input.{$prefix}-choose-input-type" => array(
            'value' => 'date',
          ),
        ),
      ),
    );
    $form[$prefix . '_group'][$relative_id] = array(
      '#type' => 'textfield',
      '#title' => check_plain($relative_label),
      '#default_value' => $relative_value,
      '#description' => t("Relative dates are computed when the view is displayed. Examples: now, now +1 day, 12AM today, Monday next week. <a href=\"@relative_format\">More examples of relative date formats in the PHP documentation</a>.", array(
        '@relative_format' => 'http://www.php.net/manual/en/datetime.formats.relative.php',
      )),
      '#states' => array(
        'visible' => array(
          ":input.{$prefix}-choose-input-type" => array(
            'value' => 'relative',
          ),
        ),
      ),
    );
    if ($which == 'all') {
      $form[$prefix . '_group']['#pre_render'][] = 'ctools_dependent_pre_render';
      $form[$prefix . '_group']['#dependency'] = array(
        $source => $operator_values,
      );
    }
  }
  return $form;
}