Update Filter Object to extend ZM_Object. Rename Query to Query_json and implement a Query function to parse Query_json
This commit is contained in:
parent
fd95ab23e9
commit
b05aff1d5d
|
@ -1,9 +1,11 @@
|
|||
<?php
|
||||
namespace ZM;
|
||||
require_once('Object.php');
|
||||
|
||||
class Filter {
|
||||
class Filter extends ZM_Object {
|
||||
protected static $table = 'Filters';
|
||||
|
||||
public $defaults = array(
|
||||
protected $defaults = array(
|
||||
'Id' => null,
|
||||
'Name' => '',
|
||||
'AutoExecute' => 0,
|
||||
|
@ -16,63 +18,44 @@ public $defaults = array(
|
|||
'AutoMessage' => 0,
|
||||
'AutoMove' => 0,
|
||||
'AutoMoveTo' => 0,
|
||||
'AutoCopy' => 0,
|
||||
'AutoCopyTo' => 0,
|
||||
'UpdateDiskSpace' => 0,
|
||||
'Background' => 0,
|
||||
'Concurrent' => 0,
|
||||
'limit' => 100,
|
||||
'Query' => array(),
|
||||
'sort_field' => ZM_WEB_EVENT_SORT_FIELD,
|
||||
'sort_asc' => ZM_WEB_EVENT_SORT_ORDER,
|
||||
);
|
||||
#'limit' => 100,
|
||||
'Query_json' => '',
|
||||
#'sort_field' => ZM_WEB_EVENT_SORT_FIELD,
|
||||
#'sort_asc' => ZM_WEB_EVENT_SORT_ORDER,
|
||||
);
|
||||
|
||||
public function __construct( $IdOrRow=NULL ) {
|
||||
$row = NULL;
|
||||
if ( $IdOrRow ) {
|
||||
if ( is_integer($IdOrRow) or is_numeric($IdOrRow) ) {
|
||||
$row = dbFetchOne('SELECT * FROM Filters WHERE Id=?', NULL, array($IdOrRow));
|
||||
if ( ! $row ) {
|
||||
Error('Unable to load Filter record for Id=' . $IdOrRow);
|
||||
public function Query($new = -1) {
|
||||
if ( $new and ( $new != -1 ) ) {
|
||||
$this->{'Query'} = $new;
|
||||
$this->{'Query_json'} = jsonEncode($new);
|
||||
Logger::Debug("Setting Query to " . $this->{'Query_json'});
|
||||
}
|
||||
} elseif ( is_array($IdOrRow) ) {
|
||||
$row = $IdOrRow;
|
||||
} else {
|
||||
$backTrace = debug_backtrace();
|
||||
$file = $backTrace[1]['file'];
|
||||
$line = $backTrace[1]['line'];
|
||||
Error("Unknown argument passed to Filter Constructor from $file:$line)");
|
||||
Error("Unknown argument passed to Filter Constructor ($IdOrRow)");
|
||||
return;
|
||||
}
|
||||
} # end if isset($IdOrRow)
|
||||
if ( !array_key_exists('Query', $this) ) {
|
||||
if ( array_key_exists('Query_json', $this) and $this->{'Query_json'} ) {
|
||||
$this->{'Query'} = jsonDecode($this->{'Query_json'});
|
||||
Logger::Debug("Decoded Query already" . print_r($this->{'Query'}, true ));
|
||||
|
||||
if ( $row ) {
|
||||
foreach ($row as $k => $v) {
|
||||
$this->{$k} = $v;
|
||||
}
|
||||
if ( array_key_exists('Query', $this) and $this->{'Query'} ) {
|
||||
$this->{'Query'} = jsonDecode($this->{'Query'});
|
||||
} else {
|
||||
Logger::Debug("No Have Query_json already");
|
||||
$this->{'Query'} = array();
|
||||
}
|
||||
}
|
||||
} // end function __construct
|
||||
|
||||
public function __call( $fn, array $args ) {
|
||||
if ( count( $args ) ) {
|
||||
$this->{$fn} = $args[0];
|
||||
}
|
||||
if ( array_key_exists( $fn, $this ) ) {
|
||||
return $this->{$fn};
|
||||
} else if ( array_key_exists( $fn, $this->defaults ) ) {
|
||||
$this->{$fn} = $this->defaults{$fn};
|
||||
return $this->{$fn};
|
||||
} else {
|
||||
|
||||
$backTrace = debug_backtrace();
|
||||
$file = $backTrace[1]['file'];
|
||||
$line = $backTrace[1]['line'];
|
||||
Warning( "Unknown function call Filter->$fn from $file:$line" );
|
||||
Logger::Debug("Have Query already" . print_r($this->{'Query'}, true ));
|
||||
}
|
||||
return $this->{'Query'};
|
||||
}
|
||||
|
||||
public static function find( $parameters = array(), $options = array() ) {
|
||||
return ZM_Object::_find(get_class(), $parameters, $options);
|
||||
}
|
||||
|
||||
public static function find_one( $parameters = array(), $options = array() ) {
|
||||
return ZM_Object::_find_one(get_class(), $parameters, $options);
|
||||
}
|
||||
|
||||
public function terms( ) {
|
||||
|
@ -93,101 +76,31 @@ public $defaults = array(
|
|||
if ( isset( $this->Query()['sort_field'] ) ) {
|
||||
return $this->{'Query'}['sort_field'];
|
||||
}
|
||||
return $this->defaults{'sort_field'};
|
||||
return ZM_WEB_EVENT_SORT_FIELD;
|
||||
#return $this->defaults{'sort_field'};
|
||||
}
|
||||
|
||||
public function sort_asc( ) {
|
||||
if ( func_num_args( ) ) {
|
||||
$this->{'Query'}['sort_asc'] = func_get_arg(0);
|
||||
$this->Query()['sort_asc'] = func_get_arg(0);
|
||||
}
|
||||
if ( isset( $this->Query()['sort_asc'] ) ) {
|
||||
return $this->{'Query'}['sort_asc'];
|
||||
}
|
||||
return $this->defaults{'sort_asc'};
|
||||
return ZM_WEB_EVENT_SORT_ORDER;
|
||||
#return $this->defaults{'sort_asc'};
|
||||
}
|
||||
|
||||
public function limit( ) {
|
||||
if ( func_num_args( ) ) {
|
||||
$this->{'Query'}['limit'] = func_get_arg(0);
|
||||
}
|
||||
if ( isset( $this->Query()['limit'] ) )
|
||||
return $this->{'Query'}['limit'];
|
||||
return $this->defaults{'limit'};
|
||||
return 100;
|
||||
#return $this->defaults{'limit'};
|
||||
}
|
||||
|
||||
public static function find( $parameters = null, $options = null ) {
|
||||
$filters = array();
|
||||
$sql = 'SELECT * FROM Filters ';
|
||||
$values = array();
|
||||
|
||||
if ( $parameters ) {
|
||||
$fields = array();
|
||||
$sql .= 'WHERE ';
|
||||
foreach ( $parameters as $field => $value ) {
|
||||
if ( $value == null ) {
|
||||
$fields[] = $field.' IS NULL';
|
||||
} else if ( is_array( $value ) ) {
|
||||
$func = function(){return '?';};
|
||||
$fields[] = $field.' IN ('.implode(',', array_map($func, $value)). ')';
|
||||
$values += $value;
|
||||
} else {
|
||||
$fields[] = $field.'=?';
|
||||
$values[] = $value;
|
||||
}
|
||||
}
|
||||
$sql .= implode(' AND ', $fields);
|
||||
}
|
||||
if ( $options ) {
|
||||
if ( isset($options['order']) ) {
|
||||
$sql .= ' ORDER BY ' . $options['order'];
|
||||
}
|
||||
if ( isset($options['limit']) ) {
|
||||
if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) {
|
||||
$sql .= ' LIMIT ' . $options['limit'];
|
||||
} else {
|
||||
$backTrace = debug_backtrace();
|
||||
$file = $backTrace[1]['file'];
|
||||
$line = $backTrace[1]['line'];
|
||||
Error("Invalid value for limit(".$options['limit'].") passed to Filter::find from $file:$line");
|
||||
return array();
|
||||
}
|
||||
}
|
||||
}
|
||||
$result = dbQuery($sql, $values);
|
||||
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Filter');
|
||||
foreach ( $results as $row => $obj ) {
|
||||
$filters[] = $obj;
|
||||
}
|
||||
return $filters;
|
||||
} # end find()
|
||||
|
||||
public static function find_one( $parameters = array() ) {
|
||||
$results = Filter::find($parameters, array('limit'=>1));
|
||||
if ( ! sizeof($results) ) {
|
||||
return;
|
||||
}
|
||||
return $results[0];
|
||||
} # end find_one()
|
||||
|
||||
public function delete() {
|
||||
dbQuery('DELETE FROM Filters WHERE Id=?', array($this->{'Id'}));
|
||||
} # end function delete()
|
||||
|
||||
public function set( $data ) {
|
||||
foreach ($data as $k => $v) {
|
||||
if ( is_array( $v ) ) {
|
||||
$this->{$k} = $v;
|
||||
} else if ( is_string( $v ) ) {
|
||||
$this->{$k} = trim( $v );
|
||||
} else if ( is_integer( $v ) ) {
|
||||
$this->{$k} = $v;
|
||||
} else if ( is_bool( $v ) ) {
|
||||
$this->{$k} = $v;
|
||||
} else {
|
||||
Error( "Unknown type $k => $v of var " . gettype( $v ) );
|
||||
$this->{$k} = $v;
|
||||
}
|
||||
}
|
||||
} # end function set
|
||||
|
||||
public function control($command, $server_id=null) {
|
||||
$Servers = $server_id ? Server::find(array('Id'=>$server_id)) : Server::find();
|
||||
if ( !count($Servers) and !$server_id ) {
|
||||
|
|
Loading…
Reference in New Issue