add functions to Filter class to build up the sql, hidden_fields and query_string from the list of terms

This commit is contained in:
Isaac Connor 2020-08-17 16:55:05 -04:00
parent e452c23c4d
commit ae91e9c58f
1 changed files with 94 additions and 2 deletions

View File

@ -30,9 +30,101 @@ class Filter extends ZM_Object {
'Query_json' => '',
);
protected $_querystring;
protected $_sql;
protected $_hidden_fields;
public $_pre_sql_conditions;
public $_post_sql_conditions;
protected $_Terms;
public function sql() {
if ( ! isset($this->_sql) ) {
foreach ( $this->FilterTerms() as $term ) {
if ( ! ($term->is_pre_sql() or $term->is_post_sql()) )
$this->_sql .= $term->sql();
} # end foreach term
}
return $this->_sql;
}
public function querystring() {
if ( ! isset($this->_querystring) ) {
foreach ( $this->FilterTerms() as $term ) {
$this->_querystring .= $term->querystring();
} # end foreach term
}
return $this->_querystring;
}
public function hidden_fields() {
if ( ! isset($this->_hidden_fields) ) {
foreach ( $this->FilterTerms() as $term ) {
$this->_hidden_fields .= $term->hidden_fields();
} # end foreach term
}
return $this->_hidden_fields;
}
public function pre_sql_conditions() {
if ( ! isset($this->_pre_sql_conditions) ) {
$this->_pre_sql_conditions = array();
foreach ( $this->FilterTerms() as $term ) {
if ( $term->is_pre_sql() )
$this->_pre_sql_conditions[] = $term;
} # end foreach term
}
return $this->_pre_sql_conditions;
}
public function post_sql_conditions() {
if ( ! isset($this->_post_sql_conditions) ) {
$this->_post_sql_conditions = array();
foreach ( $this->FilterTerms() as $term ) {
if ( $term->is_post_sql() )
$this->_post_sql_conditions[] = $term;
} # end foreach term
}
return $this->_post_sql_conditions;
}
public function FilterTerms() {
if ( ! isset($this->Terms) ) {
$this->Terms = array();
$_terms = $this->terms();
for ( $i = 0; $i < count($_terms); $i++ ) {
$term = new FilterTerm($this, $_terms[$i], $i);
$this->Terms[] = $term;
} # end foreach term
}
return $this->Terms;
}
public static function parse($new_filter, $querySep='&amp;') {
$filter = new Filter();
$filter->Query($new_filter['Query']);
return $filter;
}
# If no storage areas are specified in the terms, then return all
public function get_StorageAreas() {
$storage_ids = array();
foreach ( $this->Terms as $term ) {
if ( $term->attr == 'StorageId' ) {
# TODO handle other operators like !=
$storage_ids[] = $term->value;
}
}
if ( count($storage_ids) ) {
return Storage::find(array('Id'=>$storage_ids));
} else {
return Storage::find();
}
} # end function get_StorageAreas
public function Query_json() {
if ( func_num_args( ) ) {
$this->{'Query_json'} = func_get_arg(0);;
$this->{'Query_json'} = func_get_arg(0);
$this->{'Query'} = jsonDecode($this->{'Query_json'});
}
return $this->{'Query_json'};
@ -40,7 +132,7 @@ class Filter extends ZM_Object {
public function Query() {
if ( func_num_args( ) ) {
$this->{'Query'} = func_get_arg(0);;
$this->{'Query'} = func_get_arg(0);
$this->{'Query_json'} = jsonEncode($this->{'Query'});
}
if ( !property_exists($this, 'Query') ) {