You are here

function date_api_filter_handler::value_form in Date 6.2

Same name and namespace in other branches
  1. 6 date_api.views.inc \date_api_filter_handler::value_form()

Add the selectors to the value form using the date handler.

File

includes/date_api_filter_handler.inc, line 195
Date Views filter handler.

Class

date_api_filter_handler
A flexible, configurable date filter.

Code

function value_form(&$form, &$form_state) {

  // We use different values than the parent form, so we must
  // construct our own form element.
  $form['value'] = array();
  $form['value']['#tree'] = TRUE;

  // We have to make some choices when creating this as an exposed
  // filter form. For example, if the operator is locked and thus
  // not rendered, we can't render dependencies; instead we only
  // render the form items we need.
  $which = 'all';
  $source = '';
  if (!empty($form['operator'])) {
    $source = $form['operator']['#type'] == 'radios' ? 'radio:options[operator]' : 'edit-options-operator';
  }
  if (!empty($form_state['exposed'])) {
    if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator'])) {

      // exposed and locked.
      $which = in_array($this->operator, $this
        ->operator_values(2)) ? 'minmax' : 'value';
    }
    else {
      $source = 'edit-' . form_clean_id($this->options['expose']['operator']);
    }
  }
  $handler = $this->date_handler;
  if ($which == 'all' || $which == 'value') {
    $form['value'] += $this
      ->date_parts_form($form_state, 'value', $source, $which, $this
      ->operator_values(1));
    if ($this->force_value) {
      $form['value']['value']['#force_value'] = TRUE;
    }
  }
  if ($which == 'all' || $which == 'minmax') {
    $form['value'] += $this
      ->date_parts_form($form_state, 'min', $source, $which, $this
      ->operator_values(2));
    $form['value'] += $this
      ->date_parts_form($form_state, 'max', $source, $which, $this
      ->operator_values(2));
    if ($this->force_value) {
      $form['value']['min']['#force_value'] = TRUE;
      $form['value']['max']['#force_value'] = TRUE;
    }
  }

  // Add some text to make it clear when additional options are available.
  $extra = '';
  if (version_compare(PHP_VERSION, '5.2', '>=')) {
    $extra = t(" You can use any values PHP's date_create() can understand, like between '12AM today' and '12AM tomorrow.");
  }
  $form['value']['value']['#prefix'] = '<div class="form-item"><label>' . t('Absolute value') . '</label></div>';
  $form['value']['default_date'] = array(
    '#type' => 'textfield',
    '#title' => t('Date default'),
    '#default_value' => $this->options['default_date'],
    '#prefix' => '<div class="form-item"><label>' . t('Relative value') . '</label><p>' . t("Relative values will be used if no date is set above. Use 'now' to default to the current date at runtime or add modifiers like 'now +1 day'. The To date default value is used when the operator is set to 'between' or 'not between'.") . $extra . '</p><p>' . t('If the filter is exposed, these values will be used to set the inital value of the exposed filter. Leave both date and default values blank to start with no value in the exposed filter.') . '</p></div>',
  );
  $form['value']['default_to_date'] = array(
    '#type' => 'textfield',
    '#title' => t('To date default'),
    '#default_value' => $this->options['default_to_date'],
  );

  // Test if this value is in the UI or exposed, only show these elements in the UI.
  // We'll save it as an option rather than a value to store it for use
  // in the exposed filter.
  if (!empty($form_state['exposed'])) {
    $form['value']['default_date']['#type'] = 'value';
    $form['value']['default_date']['#value'] = $form['value']['default_date']['#default_value'];
    $form['value']['default_to_date']['#type'] = 'value';
    $form['value']['default_to_date']['#value'] = $form['value']['default_to_date']['#default_value'];
    unset($form['value']['default_date']['#prefix']);
    unset($form['value']['value']['#prefix']);
  }
  $form['value']['#theme'] = 'date_views_filter_form';
}