• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

handlers/views_handler_filter_date.inc

00001 <?php
00002 
00008 class views_handler_filter_date extends views_handler_filter_numeric {
00009   function option_definition() {
00010     $options = parent::option_definition();
00011 
00012     // value is already set up properly, we're just adding our new field to it.
00013     $options['value']['contains']['type']['default'] = 'date';
00014 
00015     return $options;
00016   }
00017 
00021   function value_form(&$form, &$form_state) {
00022     if (empty($form_state['exposed'])) {
00023       $form['value']['type'] = array(
00024         '#type' => 'radios',
00025         '#title' => t('Value type'),
00026         '#options' => array(
00027           'date' => t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'),
00028           'offset' => t('An offset from the current time such as "!example1" or "!example2"', array('!example1' => '+1 day', '!example2' => '-2 hours -30 minutes')),
00029         ),
00030         '#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date',
00031       );
00032     }
00033     parent::value_form($form, $form_state);
00034   }
00035 
00036   function options_validate(&$form, &$form_state) {
00037     parent::options_validate($form, $form_state);
00038 
00039     if (!empty($this->options['exposed']) && empty($form_state['values']['options']['expose']['required'])) {
00040       // Who cares what the value is if it's exposed and non-required.
00041       return;
00042     }
00043 
00044     $this->validate_valid_time($form['value'], $form_state['values']['options']['operator'], $form_state['values']['options']['value']);
00045   }
00046 
00047   function exposed_validate(&$form, &$form_state) {
00048     if (empty($this->options['exposed'])) {
00049       return;
00050     }
00051 
00052     if (empty($this->options['expose']['required'])) {
00053       // Who cares what the value is if it's exposed and non-required.
00054       return;
00055     }
00056 
00057     $value = &$form_state['values'][$this->options['expose']['identifier']];
00058     if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
00059       $operator = $form_state['values'][$this->options['expose']['operator_id']];
00060     }
00061     else {
00062       $operator = $this->operator;
00063     }
00064 
00065     $this->validate_valid_time($this->options['expose']['identifier'], $operator, $value);
00066 
00067   }
00068 
00072   function validate_valid_time(&$form, $operator, $value) {
00073     $operators = $this->operators();
00074 
00075     if ($operators[$operator]['values'] == 1) {
00076       $convert = strtotime($value['value']);
00077       if (!empty($form['value']) && ($convert == -1 || $convert === FALSE)) {
00078         form_error($form['value'], t('Invalid date format.'));
00079       }
00080     }
00081     elseif ($operators[$operator]['values'] == 2) {
00082       $min = strtotime($value['min']);
00083       if ($min == -1 || $min === FALSE) {
00084         form_error($form['min'], t('Invalid date format.'));
00085       }
00086       $max = strtotime($value['max']);
00087       if ($max == -1 || $max === FALSE) {
00088         form_error($form['max'], t('Invalid date format.'));
00089       }
00090     }
00091   }
00092 
00093   function accept_exposed_input($input) {
00094     if (empty($this->options['exposed'])) {
00095       return TRUE;
00096     }
00097 
00098     // Store this because it will get overwritten.
00099     $type = $this->value['type'];
00100     $rc = parent::accept_exposed_input($input);
00101 
00102     // Don't filter if value(s) are empty.
00103     $operators = $this->operators();
00104     if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
00105       $operator = $input[$this->options['expose']['operator_id']];
00106     }
00107     else {
00108       $operator = $this->operator;
00109     }
00110 
00111     if ($operators[$operator]['values'] == 1) {
00112       if ($this->value['value'] == '') {
00113         return FALSE;
00114       }
00115     }
00116     else {
00117       if ($this->value['min'] == '' || $this->value['max'] == '') {
00118         return FALSE;
00119       }
00120     }
00121 
00122     // restore what got overwritten by the parent.
00123     $this->value['type'] = $type;
00124     return $rc;
00125   }
00126 
00127   function op_between($field) {
00128     $a = intval(strtotime($this->value['min'], 0));
00129     $b = intval(strtotime($this->value['max'], 0));
00130 
00131     if ($this->value['type'] == 'offset') {
00132       $a = '***CURRENT_TIME***' . sprintf('%+d', $a); // keep sign
00133       $b = '***CURRENT_TIME***' . sprintf('%+d', $b); // keep sign
00134     }
00135     // This is safe because we are manually scrubbing the values.
00136     // It is necessary to do it this way because $a and $b are formulas when using an offset.
00137     $operator = strtoupper($this->operator);
00138     $this->query->add_where_expression($this->options['group'], "$field $operator $a AND $b");
00139   }
00140 
00141   function op_simple($field) {
00142     $value = intval(strtotime($this->value['value'], 0));
00143     if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
00144       $value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign
00145     }
00146     // This is safe because we are manually scrubbing the value.
00147     // It is necessary to do it this way because $value is a formula when using an offset.
00148     $this->query->add_where_expression($this->options['group'], "$field $this->operator $value");
00149   }
00150 }

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1