2017-09-24 01:42:39 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Group {
|
|
|
|
|
|
|
|
public $defaults = array(
|
|
|
|
'Id' => null,
|
|
|
|
'Name' => '',
|
2017-10-01 02:19:32 +08:00
|
|
|
'ParentId' => null,
|
2017-09-24 01:42:39 +08:00
|
|
|
'MonitorIds' => '',
|
|
|
|
);
|
|
|
|
|
|
|
|
public function __construct( $IdOrRow=NULL ) {
|
|
|
|
$row = NULL;
|
|
|
|
if ( $IdOrRow ) {
|
|
|
|
if ( is_integer( $IdOrRow ) or is_numeric( $IdOrRow ) ) {
|
|
|
|
$row = dbFetchOne( 'SELECT * FROM Groups WHERE Id=?', NULL, array( $IdOrRow ) );
|
|
|
|
if ( ! $row ) {
|
|
|
|
Error('Unable to load Group record for Id=' . $IdOrRow );
|
|
|
|
}
|
|
|
|
} elseif ( is_array( $IdOrRow ) ) {
|
|
|
|
$row = $IdOrRow;
|
|
|
|
} else {
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$file = $backTrace[1]['file'];
|
|
|
|
$line = $backTrace[1]['line'];
|
|
|
|
Error("Unknown argument passed to Group Constructor from $file:$line)");
|
|
|
|
Error("Unknown argument passed to Group Constructor ($IdOrRow)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} # end if isset($IdOrRow)
|
|
|
|
|
|
|
|
if ( $row ) {
|
|
|
|
foreach ($row as $k => $v) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // 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 Group->$fn from $file:$line" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 02:19:32 +08:00
|
|
|
public static function find_all( $parameters = null ) {
|
2017-09-24 01:42:39 +08:00
|
|
|
$filters = array();
|
2017-10-01 02:19:32 +08:00
|
|
|
$sql = 'SELECT * FROM Groups ';
|
|
|
|
$values = array();
|
|
|
|
|
|
|
|
if ( $parameters ) {
|
|
|
|
$fields = array();
|
|
|
|
$sql .= 'WHERE ';
|
|
|
|
foreach ( $parameters as $field => $value ) {
|
|
|
|
if ( $value == null ) {
|
|
|
|
$fields[] = $field.' IS NULL';
|
|
|
|
} else {
|
|
|
|
$fields[] = $field.'=?';
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sql .= implode(' AND ', $fields );
|
|
|
|
}
|
|
|
|
$sql .= ' ORDER BY Name';
|
|
|
|
$result = dbQuery($sql, $values);
|
|
|
|
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Group');
|
2017-09-24 01:42:39 +08:00
|
|
|
foreach ( $results as $row => $obj ) {
|
|
|
|
$filters[] = $obj;
|
|
|
|
}
|
|
|
|
return $filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete() {
|
|
|
|
dbQuery( 'DELETE FROM Groups 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-01 02:19:32 +08:00
|
|
|
} # end class Group
|
2017-09-24 01:42:39 +08:00
|
|
|
|
|
|
|
?>
|