diff --git a/date_views/includes/date_views_filter_handler_simple.inc b/date_views/includes/date_views_filter_handler_simple.inc index 4fa4c40..18d6838 100644 --- a/date_views/includes/date_views_filter_handler_simple.inc +++ b/date_views/includes/date_views_filter_handler_simple.inc @@ -9,6 +9,7 @@ class date_views_filter_handler_simple extends views_handler_filter_date { var $date_handler = NULL; var $offset = NULL; + var $element_number = 1; function init(&$view, &$options) { parent::init($view, $options); @@ -61,7 +62,15 @@ function operators() { function date_default_value($prefix, $options = NULL) { $default_date = ''; if (empty($options)) { - $options = $this->options; + if ($this->options['is_grouped']) { + if ('All' == $this->options['group_info']['default_group']) { + return ''; + } + $options = $this->options['group_info']['group_items'][$this->options['group_info']['default_group']]['value'][$prefix . '_group']; + } + else { + $options = $this->options; + } } // If this is a remembered value, use the value from the SESSION. if (!empty($this->options['expose']['remember'])) { @@ -71,10 +80,11 @@ function date_default_value($prefix, $options = NULL) { } } - // This is a date that needs to be constructed from options like 'now' . + // This is a relative date. $default_option = $prefix == 'max' ? $options['default_to_date'] : $options['default_date']; - if (!empty($default_option)) { - str_replace('now', 'today', $default_option); + $is_relative = ($this->options['is_grouped'] && isset($options[$prefix . '_choose_input_type']) && $options[$prefix . '_choose_input_type'] == 'relative') || (!empty($options['default_date'])); + if ($is_relative && !empty($default_option)) { + $default_option = str_replace('now', 'today', $default_option); $date = date_create($default_option, date_default_timezone_object()); $default_date = !empty($date) ? $date->format($this->format) : ''; @@ -83,7 +93,12 @@ function date_default_value($prefix, $options = NULL) { } // This a fixed date. else { - $default_date = $options['value'][$prefix]; + if ($this->options['is_grouped']) { + $default_date = empty($options[$prefix]) ? '' : $options[$prefix]; + } + else { + $default_date = empty($options['value'][$prefix]) ? '' : $options['value'][$prefix]; + } } return $default_date; } @@ -103,7 +118,12 @@ function get_filter_value($prefix, $input) { if (empty($this->options['exposed'])) { return str_replace(' ', 'T', $this->date_default_value($prefix)); } - elseif (isset($this->options['expose']['identifier']) && !isset($_GET[$this->options['expose']['identifier']])) { + elseif ($this->options['is_grouped'] && isset($this->view->exposed_raw_input[$this->options['group_info']['identifier']])) { + $identifier = $this->view->exposed_raw_input[$this->options['group_info']['identifier']]; + $options = $this->options['group_info']['group_items'][$identifier]['value'][$prefix . '_group']; + return str_replace(' ', 'T', $this->date_default_value($prefix, $options)); + } + elseif (isset($this->options['expose']['identifier']) && !isset($this->view->exposed_raw_input[$this->options['expose']['identifier']]['value'])) { return str_replace(' ', 'T', $this->date_default_value($prefix)); } } @@ -114,9 +134,19 @@ function get_filter_value($prefix, $input) { function accept_exposed_input($input) { if (!empty($this->options['exposed'])) { $element_input = $input[$this->options['expose']['identifier']]; - $element_input['value'] = $this->get_filter_value('value', !empty($element_input['value']) ? $element_input['value'] : ''); - $element_input['min'] = $this->get_filter_value('min', !empty($element_input['min']) ? $element_input['min'] : ''); - $element_input['max'] = $this->get_filter_value('max', !empty($element_input['max']) ? $element_input['max'] : ''); + if ($this->options['is_grouped']) { + foreach (array('value', 'min', 'max') as $key) { + $element_input[$key] = ''; + if (!empty($element_input[$key . '_group'])) { + $element_input[$key] = $this->date_default_value($key, $element_input[$key . '_group']); + } + } + } + else { + $element_input['value'] = $this->get_filter_value('value', !empty($element_input['value']) ? $element_input['value'] : ''); + $element_input['min'] = $this->get_filter_value('min', !empty($element_input['min']) ? $element_input['min'] : ''); + $element_input['max'] = $this->get_filter_value('max', !empty($element_input['max']) ? $element_input['max'] : ''); + } if (is_array($element_input) && isset($element_input['default_date'])) { unset($element_input['default_date']); } @@ -127,7 +157,6 @@ function accept_exposed_input($input) { $input[$this->options['expose']['identifier']] = $element_input; } return parent::accept_exposed_input($input); - } function op_between($field) { @@ -299,14 +328,22 @@ function value_form(&$form, &$form_state) { } if ($which == 'all' || $which == 'value') { - $form['value'] += $this->date_parts_form($form_state, 'value', $source, $which, $this->operator_values(1), $identifier, 'default_date'); + $form['value'] += $this->date_parts_form($form_state, 'value', $source, $which, $this->operator_values(1), $identifier, 'default_date', $this->element_number); } if ($which == 'all' || $which == 'minmax') { - $form['value'] += $this->date_parts_form($form_state, 'min', $source, $which, $this->operator_values(2), $identifier, 'default_date'); - $form['value'] += $this->date_parts_form($form_state, 'max', $source, $which, $this->operator_values(2), $identifier, 'default_to_date'); + $value2 = $this->operator_values(2); + + foreach (array( + 'min' => 'default_date', + 'max' => 'default_to_date', + ) as $type => $prop) { + $form['value'] += $this->date_parts_form($form_state, $type, $source, $which, $value2, $identifier, $prop, $this->element_number); + } } + $this->element_number++; + // Add some extra validation for the select widget to be sure that // the user inputs all parts of the date. if ($this->options['form_type'] == 'date_select') { @@ -334,7 +371,7 @@ function value_form(&$form, &$form_state) { * @return * The form date part element for this instance. */ - function date_parts_form(&$form_state, $prefix, $source, $which, $operator_values, $identifier, $relative_id) { + function date_parts_form(&$form_state, $prefix, $source, $which, $operator_values, $identifier, $relative_id, $element_no = 0) { module_load_include('inc', 'date_api', 'date_api_elements'); switch ($prefix) { case 'min': @@ -358,11 +395,10 @@ function date_parts_form(&$form_state, $prefix, $source, $which, $operator_value $format = $this->date_handler->views_formats($this->options['granularity'], 'display'); $granularity = array_keys($this->date_handler->date_parts($this->options['granularity'])); - $relative_value = ($prefix == 'max' ? $this->options['default_to_date'] : $this->options['default_date']); + $default_date = $this->date_default_value($prefix); 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), @@ -389,24 +425,42 @@ function date_parts_form(&$form_state, $prefix, $source, $which, $operator_value } else { // UI when the date selector is on the views configuration screen. - $default_date = ''; + + // Fix for grouped between filter. Input class has to be different + // for each group and default value needs to be taken from group item. + $relative_value_key = $prefix == 'max' ? 'default_to_date' : 'default_date'; + $input_type_key = $prefix . '_choose_input_type'; + if ($this->options['is_grouped']) { + $input_class = "$prefix-$element_no-choose-input-type"; + $default_select_date_value = !empty($this->options['group_info']['group_items'][$element_no]['value'][$prefix . '_group'][$prefix]) ? $this->options['group_info']['group_items'][$element_no]['value'][$prefix . '_group'][$prefix] : $default_date; + $relative_value = !empty($this->options['group_info']['group_items'][$element_no]['value'][$prefix . '_group'][$relative_value_key]) ? $this->options['group_info']['group_items'][$element_no]['value'][$prefix . '_group'][$relative_value_key] : $this->options[$relative_value_key]; + $default_select_date_type = !empty($this->options['group_info']['group_items'][$element_no]['value'][$prefix . '_group'][$input_type_key]) + ? $this->options['group_info']['group_items'][$element_no]['value'][$prefix . '_group'][$input_type_key] : 'date'; + } + else { + $input_class = "$prefix-choose-input-type"; + $default_select_date_value = !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date; + $relative_value = $this->options[$relative_value_key]; + $default_select_date_type = $this->options['value']['type']; + } + $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( + $form[$prefix . '_group'][$input_type_key] = 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', + '#attributes' => array('class' => array($input_class)), + '#default_value' => $default_select_date_type, ); $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, + '#default_value' => $default_select_date_value, '#date_format' => date_limit_format($format, $granularity), '#date_label_position' => 'within', '#date_year_range' => $this->options['year_range'], @@ -415,7 +469,7 @@ function date_parts_form(&$form_state, $prefix, $source, $which, $operator_value '#suffix' => '', '#states' => array( 'visible' => array( - ":input.{$prefix}-choose-input-type" => array('value' => 'date'), + ":input.$input_class" => array('value' => 'date'), ), ), ); @@ -426,7 +480,7 @@ function date_parts_form(&$form_state, $prefix, $source, $which, $operator_value '#description' => t("Relative dates are computed when the view is displayed. Examples: now, now +1 day, 12AM today, Monday next week. More examples of relative date formats in the PHP documentation.", 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'), + ":input.$input_class" => array('value' => 'relative'), ), ), ); @@ -452,6 +506,10 @@ function value_validate($form, &$form_state) { $options = &$form_state['values']['options']; + if (!isset($options['operator'])) { + return; + } + if ($options['operator'] == 'between' || $options['operator'] == 'not between') { if ($options['value']['min_group']['min_choose_input_type'] == 'relative') { if (empty($options['value']['min_group']['default_date'])) { @@ -491,6 +549,7 @@ function value_validate($form, &$form_state) { $this->options['default_date'] = $options['value']['value_group']['default_date']; // NULL out the value field, user wanted the relative value to take hold. $options['value']['value_group']['value'] = NULL; + $this->options['value']['type'] = 'relative'; } } // If an absolute date was used, be sure to wipe the relative date.