Update Group object to use shared code in Object.php. Should fix #2659

This commit is contained in:
Isaac Connor 2019-08-08 13:51:56 -04:00
parent 702cb65d2a
commit 5f77634aca
1 changed files with 8 additions and 131 deletions

View File

@ -1,127 +1,21 @@
<?php <?php
namespace ZM; namespace ZM;
$group_cache = array(); class Group extends ZM_Object {
protected static $table = 'Groups';
class Group { protected $defaults = array(
public $defaults = array(
'Id' => null, 'Id' => null,
'Name' => '', 'Name' => '',
'ParentId' => null, 'ParentId' => null,
); );
public function __construct( $IdOrRow=NULL ) { public static function find( $parameters = array(), $options = array() ) {
global $group_cache; return ZM_Object::_find(get_class(), $parameters, $options);
$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;
}
$group_cache[$row['Id']] = $this;
}
} // 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" );
}
} }
public static function find( $parameters = null, $options = null ) { public static function find_one( $parameters = array(), $options = array() ) {
$sql = 'SELECT * FROM Groups '; return ZM_Object::_find_one(get_class(), $parameters, $options);
$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);
} # end if parameters
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 Group::find from $file:$line");
return array();
}
}
} # end if options
$results = dbFetchAll($sql, NULL, $values);
if ( $results ) {
return array_map( function($row){ return new Group($row); }, $results );
}
return array();
} # end find()
public static function find_one($parameters = null, $options = null) {
global $group_cache;
if (
( count($parameters) == 1 ) and
isset($parameters['Id']) and
isset($group_cache[$parameters['Id']]) ) {
return $group_cache[$parameters['Id']];
}
$results = Group::find($parameters, $options);
if ( count($results) > 1 ) {
Error("Group::find_one Returned more than 1");
return $results[0];
} else if ( count($results) ) {
return $results[0];
} else {
return null;
}
} # end function find_one
public function delete() { public function delete() {
if ( array_key_exists('Id', $this) ) { if ( array_key_exists('Id', $this) ) {
@ -137,23 +31,6 @@ class Group {
} }
} # end function delete() } # 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 depth( $new = null ) { public function depth( $new = null ) {
if ( isset($new) ) { if ( isset($new) ) {
$this->{'depth'} = $new; $this->{'depth'} = $new;