Tripal v1.0 (6.x-1.0)
|
00001 <?php 00002 00013 class chado_views_handler_filter_date extends views_handler_filter_date { 00014 00018 function options_form(&$form, &$form_state) { 00019 $form['msg'] = array( 00020 '#type' => 'item', 00021 '#value' => '<b>If this filter applies to a table that is aggregated, additionally options may be ignored.</b>' 00022 ); 00023 00024 parent::options_form($form, $form_state); 00025 00026 $form['agg'] = array( 00027 '#type' => 'fieldset', 00028 '#title' => 'Apply to fields that are aggregated' 00029 ); 00030 00031 $form['agg']['records_with'] = array( 00032 '#type' => 'checkbox', 00033 '#title' => t('Filter base table records'), 00034 '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)), 00035 '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE, 00036 ); 00037 00038 $form['agg']['aggregates_with'] = array( 00039 '#type' => 'checkbox', 00040 '#title' => t('Filter aggregates displayed'), 00041 '#description' => t('Filters the aggregates shown based on the value. Doesn\'t affect the number of %base_table records.', array('%base_table' => $this->view->base_table)), 00042 '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE, 00043 ); 00044 00045 } 00046 00050 function op_between($field) { 00051 00052 // Check whether we have a UNIX timestamp or an ISO Timestamp 00053 $check = db_fetch_object(db_query("SELECT $this->real_field as val FROM $this->table WHERE $this->real_field IS NOT NULL LIMIT 1")); 00054 if (preg_match('/^\d+$/', $check->val)) { 00055 // this is a unix timestamp 00056 $is_unix = TRUE; 00057 } 00058 else { 00059 // this is an ISO Timestamp 00060 $is_unix = FALSE; 00061 } 00062 00063 if ($this->operator == 'between') { 00064 $a = intval(strtotime($this->value['min'], 0)); 00065 $b = intval(strtotime($this->value['max'], 0)); 00066 } 00067 else { 00068 $a = intval(strtotime($this->value['max'], 0)); 00069 $b = intval(strtotime($this->value['min'], 0)); 00070 } 00071 00072 if ($this->value['type'] == 'offset') { 00073 $a = '***CURRENT_TIME***' . sprintf('%+d', $a); // keep sign 00074 $b = '***CURRENT_TIME***' . sprintf('%+d', $b); // keep sign 00075 } 00076 // %s is safe here because strtotime scrubbed the input and we might 00077 // have a string if using offset. 00078 if ($is_unix) { 00079 if ($this->operator == 'between') { 00080 $this->query->add_where($this->options['group'], "$field >= %s", $a); 00081 $this->query->add_where($this->options['group'], "$field <= %s", $b); 00082 } 00083 else { 00084 $this->query->add_where($this->options['group'], "$field >= %s OR $field <= %s", array($a, $b)); 00085 } 00086 } 00087 else { 00088 if ($this->operator == 'between') { 00089 $this->query->add_where($this->options['group'], "CAST(EXTRACT(EPOCH FROM $field) as integer) >= %s", $a); 00090 $this->query->add_where($this->options['group'], "CAST(EXTRACT(EPOCH FROM $field) as integer) <= %s", $b); 00091 } 00092 else { 00093 $this->query->add_where($this->options['group'], "CAST(EXTRACT(EPOCH FROM $field) as integer) >= %s OR CAST(EXTRACT(EPOCH FROM $field) as integer) <= %s", array($a, $b)); 00094 } 00095 } 00096 } 00097 00101 function op_simple($field) { 00102 $value = intval(strtotime($this->value['value'], 0)); 00103 00104 // Check whether we have a UNIX timestamp or an ISO Timestamp 00105 $check = db_fetch_object(db_query("SELECT $this->real_field as val FROM $this->table WHERE $this->real_field IS NOT NULL LIMIT 1")); 00106 if (preg_match('/^\d+$/', $check->val)) { 00107 // this is a unix timestamp 00108 $is_unix = TRUE; 00109 } 00110 else { 00111 // this is an ISO Timestamp 00112 $is_unix = FALSE; 00113 } 00114 00115 if ($is_unix) { 00116 if (!empty($this->value['type']) && $this->value['type'] == 'offset') { 00117 $value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign 00118 } 00119 $this->query->add_where($this->options['group'], "$field $this->operator %s", $value); 00120 } 00121 else { 00122 if (!empty($this->value['type']) && $this->value['type'] == 'offset') { 00123 $value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign 00124 } 00125 $this->query->add_where($this->options['group'], "CAST(EXTRACT(EPOCH FROM $field) as integer) $this->operator %s", $value); 00126 } 00127 } 00128 00138 function validate_valid_time(&$form, $operator, $value) { 00139 $operators = $this->operators(); 00140 00141 if ($operators[$operator]['values'] == 1) { 00142 if (!empty($value['value'])) { 00143 $convert = strtotime($value['value']); 00144 if (!empty($form) && ($convert == -1 || $convert === FALSE)) { 00145 form_set_error($form['value'], t('Invalid date format.')); 00146 } 00147 } 00148 } 00149 elseif ($operators[$operator]['values'] == 2) { 00150 $min = strtotime($value['min']); 00151 if ($min == -1 || $min === FALSE) { 00152 form_set_error($form['min'], t('Invalid date format.')); 00153 } 00154 $max = strtotime($value['max']); 00155 if ($max == -1 || $max === FALSE) { 00156 form_set_error($form['max'], t('Invalid date format.')); 00157 } 00158 } 00159 } 00160 00161 }