|  | @@ -7,6 +7,8 @@
 | 
											
												
													
														|  |   * Handles fields which may be aggregated during the chado join process. There are options
 |  |   * Handles fields which may be aggregated during the chado join process. There are options
 | 
											
												
													
														|  |   * to filter the base table based on an aggregated table or just filter the aggregated
 |  |   * to filter the base table based on an aggregated table or just filter the aggregated
 | 
											
												
													
														|  |   * table (showing blank for that field if there are no records in the aggregated table).
 |  |   * table (showing blank for that field if there are no records in the aggregated table).
 | 
											
												
													
														|  | 
 |  | + *
 | 
											
												
													
														|  | 
 |  | + * @todo make handle aggregation
 | 
											
												
													
														|  |   */
 |  |   */
 | 
											
												
													
														|  |  class chado_views_handler_filter_date extends views_handler_filter_date {
 |  |  class chado_views_handler_filter_date extends views_handler_filter_date {
 | 
											
												
													
														|  |  
 |  |  
 | 
											
										
											
												
													
														|  | @@ -43,49 +45,115 @@ class chado_views_handler_filter_date extends views_handler_filter_date {
 | 
											
												
													
														|  |    }
 |  |    }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |    /**
 |  |    /**
 | 
											
												
													
														|  | -   * If the table to be filtered is not aggregated uses the parent::query()
 |  | 
 | 
											
												
													
														|  | -   * However, if it is uses postgresql any() function to compare
 |  | 
 | 
											
												
													
														|  | 
 |  | +   * Called by query if the operator is between
 | 
											
												
													
														|  |     */
 |  |     */
 | 
											
												
													
														|  | -  function query() {
 |  | 
 | 
											
												
													
														|  | 
 |  | +  function op_between($field) {
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    // make optional
 |  | 
 | 
											
												
													
														|  | -    // if it is not set or empty then don't restrict the query
 |  | 
 | 
											
												
													
														|  | -    if (!$this->value) {
 |  | 
 | 
											
												
													
														|  | -      return;
 |  | 
 | 
											
												
													
														|  | 
 |  | +    // Check whether we have a UNIX timestamp or an ISO Timestamp
 | 
											
												
													
														|  | 
 |  | +    $check = db_fetch_object(db_query("SELECT $this->real_field as val FROM $this->table WHERE $this->real_field IS NOT NULL LIMIT 1"));
 | 
											
												
													
														|  | 
 |  | +    if (preg_match('/^\d+$/',$check->val)) {
 | 
											
												
													
														|  | 
 |  | +      // this is a unix timestamp
 | 
											
												
													
														|  | 
 |  | +      $is_unix = TRUE;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    else {
 | 
											
												
													
														|  | 
 |  | +      // this is an ISO Timestamp
 | 
											
												
													
														|  | 
 |  | +      $is_unix = FALSE;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    $this->ensure_my_table();
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if ($this->operator == 'between') {
 | 
											
												
													
														|  | 
 |  | +      $a = intval(strtotime($this->value['min'], 0));
 | 
											
												
													
														|  | 
 |  | +      $b = intval(strtotime($this->value['max'], 0));
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    else {
 | 
											
												
													
														|  | 
 |  | +      $a = intval(strtotime($this->value['max'], 0));
 | 
											
												
													
														|  | 
 |  | +      $b = intval(strtotime($this->value['min'], 0));
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    $table = $this->query->get_table_info($this->table);
 |  | 
 | 
											
												
													
														|  | -    if (preg_match('/aggregator/', $table['join']->definition['handler'])) {
 |  | 
 | 
											
												
													
														|  | -      $this->aggregated = TRUE;
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if ($this->value['type'] == 'offset') {
 | 
											
												
													
														|  | 
 |  | +      $a = '***CURRENT_TIME***' . sprintf('%+d', $a); // keep sign
 | 
											
												
													
														|  | 
 |  | +      $b = '***CURRENT_TIME***' . sprintf('%+d', $b); // keep sign
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    // %s is safe here because strtotime scrubbed the input and we might
 | 
											
												
													
														|  | 
 |  | +    // have a string if using offset.
 | 
											
												
													
														|  | 
 |  | +    if ($is_unix) {
 | 
											
												
													
														|  | 
 |  | +      if ($this->operator == 'between') {
 | 
											
												
													
														|  | 
 |  | +        $this->query->add_where($this->options['group'], "$field >= %s", $a);
 | 
											
												
													
														|  | 
 |  | +        $this->query->add_where($this->options['group'], "$field <= %s", $b);
 | 
											
												
													
														|  | 
 |  | +      }
 | 
											
												
													
														|  | 
 |  | +      else {
 | 
											
												
													
														|  | 
 |  | +        $this->query->add_where($this->options['group'], "$field >= %s OR $field <= %s", array($a, $b));
 | 
											
												
													
														|  | 
 |  | +      }
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |      else {
 |  |      else {
 | 
											
												
													
														|  | -      $this->aggregated = FALSE;
 |  | 
 | 
											
												
													
														|  | 
 |  | +      if ($this->operator == 'between') {
 | 
											
												
													
														|  | 
 |  | +        $this->query->add_where($this->options['group'], "CAST(EXTRACT(EPOCH FROM $field) as integer) >= %s", $a);
 | 
											
												
													
														|  | 
 |  | +        $this->query->add_where($this->options['group'], "CAST(EXTRACT(EPOCH FROM $field) as integer) <= %s", $b);
 | 
											
												
													
														|  | 
 |  | +      }
 | 
											
												
													
														|  | 
 |  | +      else {
 | 
											
												
													
														|  | 
 |  | +        $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));
 | 
											
												
													
														|  | 
 |  | +      }
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  | 
 |  | +  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    if (!$this->aggregated) {
 |  | 
 | 
											
												
													
														|  | -      parent::query();
 |  | 
 | 
											
												
													
														|  | 
 |  | +  /**
 | 
											
												
													
														|  | 
 |  | +   * Called by query if the operator is not between or empty
 | 
											
												
													
														|  | 
 |  | +   */
 | 
											
												
													
														|  | 
 |  | +  function op_simple($field) {
 | 
											
												
													
														|  | 
 |  | +    $value = intval(strtotime($this->value['value'], 0));
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    // Check whether we have a UNIX timestamp or an ISO Timestamp
 | 
											
												
													
														|  | 
 |  | +    $check = db_fetch_object(db_query("SELECT $this->real_field as val FROM $this->table WHERE $this->real_field IS NOT NULL LIMIT 1"));
 | 
											
												
													
														|  | 
 |  | +    if (preg_match('/^\d+$/',$check->val)) {
 | 
											
												
													
														|  | 
 |  | +      // this is a unix timestamp
 | 
											
												
													
														|  | 
 |  | +      $is_unix = TRUE;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |      else {
 |  |      else {
 | 
											
												
													
														|  | 
 |  | +      // this is an ISO Timestamp
 | 
											
												
													
														|  | 
 |  | +      $is_unix = FALSE;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -      // Only base records with value in the aggregated field
 |  | 
 | 
											
												
													
														|  | -      // This doesn't restrict the items in the aggregate field
 |  | 
 | 
											
												
													
														|  | -      $this->ensure_my_table();
 |  | 
 | 
											
												
													
														|  | -      $field = "$this->table_alias.$this->real_field";
 |  | 
 | 
											
												
													
														|  | -      if ($this->options['agg']['records_with']) {
 |  | 
 | 
											
												
													
														|  | -        $where = "'%s' = ANY($field)";
 |  | 
 | 
											
												
													
														|  | -        $this->query->add_where($this->options['group'], $where, $this->value);
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if ($is_unix) {
 | 
											
												
													
														|  | 
 |  | +      if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
 | 
											
												
													
														|  | 
 |  | +        $value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign
 | 
											
												
													
														|  |        }
 |  |        }
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -      // To restrict the items in the aggregate...
 |  | 
 | 
											
												
													
														|  | -      // Tell the join handler about the filter
 |  | 
 | 
											
												
													
														|  | -      // so it can be done in the join query
 |  | 
 | 
											
												
													
														|  | -      if ($this->options['agg']['aggregates_with']) {
 |  | 
 | 
											
												
													
														|  | -        $table['join']->filter[] = $field . " = '" . $this->value . "'";
 |  | 
 | 
											
												
													
														|  | 
 |  | +      $this->query->add_where($this->options['group'], "$field $this->operator %s", $value);
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    else {
 | 
											
												
													
														|  | 
 |  | +      if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
 | 
											
												
													
														|  | 
 |  | +        $value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign
 | 
											
												
													
														|  |        }
 |  |        }
 | 
											
												
													
														|  | 
 |  | +      $this->query->add_where($this->options['group'], "CAST(EXTRACT(EPOCH FROM $field) as integer) $this->operator %s", $value);
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  | 
 |  | +  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +  /**
 | 
											
												
													
														|  | 
 |  | +   * Validate that the time values convert to something usable.
 | 
											
												
													
														|  | 
 |  | +   *
 | 
											
												
													
														|  | 
 |  | +   * We modify it to
 | 
											
												
													
														|  | 
 |  | +   * - fix a bug in the views handler for single values
 | 
											
												
													
														|  | 
 |  | +   *    $value['value'] didn't exist
 | 
											
												
													
														|  | 
 |  | +   * - fix a pass by reference error
 | 
											
												
													
														|  | 
 |  | +   *    changed form_error to form_set_error
 | 
											
												
													
														|  | 
 |  | +   */
 | 
											
												
													
														|  | 
 |  | +  function validate_valid_time(&$form, $operator, $value) {
 | 
											
												
													
														|  | 
 |  | +    $operators = $this->operators();
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    if ($operators[$operator]['values'] == 1) {
 | 
											
												
													
														|  | 
 |  | +      $convert = strtotime($value);
 | 
											
												
													
														|  | 
 |  | +      if (!empty($form) && ($convert == -1 || $convert === FALSE)) {
 | 
											
												
													
														|  | 
 |  | +        form_set_error($form['value'], t('Invalid date format.'));
 | 
											
												
													
														|  | 
 |  | +      }
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    elseif ($operators[$operator]['values'] == 2) {
 | 
											
												
													
														|  | 
 |  | +      $min = strtotime($value['min']);
 | 
											
												
													
														|  | 
 |  | +      if ($min == -1 || $min === FALSE) {
 | 
											
												
													
														|  | 
 |  | +        form_set_error($form['min'], t('Invalid date format.'));
 | 
											
												
													
														|  | 
 |  | +      }
 | 
											
												
													
														|  | 
 |  | +      $max = strtotime($value['max']);
 | 
											
												
													
														|  | 
 |  | +      if ($max == -1 || $max === FALSE) {
 | 
											
												
													
														|  | 
 |  | +        form_set_error($form['max'], t('Invalid date format.'));
 | 
											
												
													
														|  | 
 |  | +      }
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  |    }
 |  |    }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  }
 |  |  }
 |