2019-07-23 21:57:44 +08:00
|
|
|
<?php
|
|
|
|
namespace ZM;
|
|
|
|
require_once('database.php');
|
|
|
|
|
|
|
|
$object_cache = array();
|
|
|
|
|
|
|
|
class ZM_Object {
|
|
|
|
|
|
|
|
public function __construct($IdOrRow = NULL) {
|
|
|
|
$class = get_class($this);
|
|
|
|
|
|
|
|
$row = NULL;
|
|
|
|
if ( $IdOrRow ) {
|
|
|
|
if ( is_integer($IdOrRow) or ctype_digit($IdOrRow) ) {
|
2019-08-16 04:04:56 +08:00
|
|
|
$table = $class::$table;
|
2019-07-23 21:57:44 +08:00
|
|
|
$row = dbFetchOne("SELECT * FROM `$table` WHERE `Id`=?", NULL, array($IdOrRow));
|
|
|
|
if ( !$row ) {
|
|
|
|
Error("Unable to load $class record for Id=$IdOrRow");
|
|
|
|
}
|
|
|
|
} elseif ( is_array($IdOrRow) ) {
|
|
|
|
$row = $IdOrRow;
|
|
|
|
}
|
2019-08-16 04:04:56 +08:00
|
|
|
|
|
|
|
if ( $row ) {
|
|
|
|
global $object_cache;
|
|
|
|
if ( ! isset($object_cache[$class]) ) {
|
|
|
|
$object_cache[$class] = array();
|
|
|
|
}
|
|
|
|
$cache = &$object_cache[$class];
|
|
|
|
|
|
|
|
foreach ($row as $k => $v) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
|
|
|
$cache[$row['Id']] = $this;
|
2019-07-23 21:57:44 +08:00
|
|
|
}
|
2019-08-16 04:04:56 +08:00
|
|
|
} # end if isset($IdOrRow)
|
|
|
|
} # end function __construct
|
2019-07-23 21:57:44 +08:00
|
|
|
|
|
|
|
public function __call($fn, array $args){
|
2019-09-20 02:55:45 +08:00
|
|
|
$type = (array_key_exists($fn, $this->defaults) && is_array($this->defaults[$fn])) ? $this->defaults[$fn]['type'] : 'scalar';
|
2019-07-23 21:57:44 +08:00
|
|
|
if ( count($args) ) {
|
2020-04-11 00:17:09 +08:00
|
|
|
if ( $type == 'set' and is_array($args[0]) ) {
|
2019-09-20 02:55:45 +08:00
|
|
|
$this->{$fn} = implode(',', $args[0]);
|
2020-04-11 00:17:09 +08:00
|
|
|
} else if ( array_key_exists($fn, $this->defaults) && is_array($this->defaults[$fn]) && isset($this->defaults[$fn]['filter_regexp']) ) {
|
2020-07-26 01:50:59 +08:00
|
|
|
if ( is_array($this->defaults[$fn]['filter_regexp']) ) {
|
|
|
|
foreach ( $this->defaults[$fn]['filter_regexp'] as $regexp ) {
|
|
|
|
$this->{$fn} = preg_replace($regexp, '', $args[0]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->{$fn} = preg_replace($this->defaults[$fn]['filter_regexp'], '', $args[0]);
|
|
|
|
}
|
2020-04-11 00:17:09 +08:00
|
|
|
} else {
|
2019-09-20 02:55:45 +08:00
|
|
|
$this->{$fn} = $args[0];
|
2020-04-11 00:17:09 +08:00
|
|
|
}
|
2019-07-23 21:57:44 +08:00
|
|
|
}
|
2019-09-20 02:55:45 +08:00
|
|
|
|
2019-12-08 00:45:32 +08:00
|
|
|
if ( property_exists($this, $fn) ) {
|
2019-07-23 21:57:44 +08:00
|
|
|
return $this->{$fn};
|
|
|
|
} else {
|
|
|
|
if ( array_key_exists($fn, $this->defaults) ) {
|
2019-09-20 02:55:45 +08:00
|
|
|
if ( is_array($this->defaults[$fn]) ) {
|
|
|
|
return $this->defaults[$fn]['default'];
|
|
|
|
}
|
|
|
|
return $this->defaults[$fn];
|
2019-07-23 21:57:44 +08:00
|
|
|
} else {
|
|
|
|
$backTrace = debug_backtrace();
|
2019-08-16 04:04:56 +08:00
|
|
|
Warning("Unknown function call Object->$fn from ".print_r($backTrace,true));
|
2019-07-23 21:57:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function _find($class, $parameters = null, $options = null ) {
|
|
|
|
$table = $class::$table;
|
|
|
|
$filters = array();
|
2020-04-11 00:17:09 +08:00
|
|
|
$sql = 'SELECT * FROM `'.$table.'` ';
|
2019-07-23 21:57:44 +08:00
|
|
|
$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 {
|
2019-09-27 04:26:28 +08:00
|
|
|
$backTrace = debug_backtrace();
|
2019-07-23 21:57:44 +08:00
|
|
|
Error('Invalid value for limit('.$options['limit'].') passed to '.get_class()."::find from ".print_r($backTrace,true));
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$rows = dbFetchAll($sql, NULL, $values);
|
|
|
|
$results = array();
|
|
|
|
if ( $rows ) {
|
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
array_push($results , new $class($row));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
2019-08-16 04:04:56 +08:00
|
|
|
} # end public function _find()
|
2019-07-23 21:57:44 +08:00
|
|
|
|
|
|
|
public static function _find_one($class, $parameters = array(), $options = array() ) {
|
|
|
|
global $object_cache;
|
2020-01-11 01:44:59 +08:00
|
|
|
if ( ! isset($object_cache[$class]) ) {
|
2019-07-23 21:57:44 +08:00
|
|
|
$object_cache[$class] = array();
|
2020-01-11 01:44:59 +08:00
|
|
|
}
|
2019-08-16 04:04:56 +08:00
|
|
|
$cache = &$object_cache[$class];
|
2019-07-23 21:57:44 +08:00
|
|
|
if (
|
|
|
|
( count($parameters) == 1 ) and
|
|
|
|
isset($parameters['Id']) and
|
|
|
|
isset($cache[$parameters['Id']]) ) {
|
|
|
|
return $cache[$parameters['Id']];
|
|
|
|
}
|
|
|
|
$options['limit'] = 1;
|
|
|
|
$results = ZM_Object::_find($class, $parameters, $options);
|
|
|
|
if ( ! sizeof($results) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $results[0];
|
|
|
|
}
|
|
|
|
|
2020-01-13 23:57:56 +08:00
|
|
|
public static function _clear_cache($class) {
|
|
|
|
global $object_cache;
|
|
|
|
$object_cache[$class] = array();
|
|
|
|
}
|
|
|
|
|
2019-07-23 21:57:44 +08:00
|
|
|
public static function Objects_Indexed_By_Id($class) {
|
|
|
|
$results = array();
|
|
|
|
foreach ( ZM_Object::_find($class, null, array('order'=>'lower(Name)')) as $Object ) {
|
|
|
|
$results[$Object->Id()] = $Object;
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function to_json() {
|
|
|
|
$json = array();
|
|
|
|
foreach ($this->defaults as $key => $value) {
|
|
|
|
if ( is_callable(array($this, $key)) ) {
|
|
|
|
$json[$key] = $this->$key();
|
2019-12-08 00:45:32 +08:00
|
|
|
} else if ( property_exists($this, $key) ) {
|
2019-07-23 21:57:44 +08:00
|
|
|
$json[$key] = $this->{$key};
|
|
|
|
} else {
|
2019-12-08 00:45:32 +08:00
|
|
|
$json[$key] = $this->defaults[$key];
|
2019-07-23 21:57:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return json_encode($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set($data) {
|
2020-07-26 02:26:49 +08:00
|
|
|
foreach ( $data as $field => $value ) {
|
2020-07-27 10:32:55 +08:00
|
|
|
if ( is_callable(array($this, $field)) ) {
|
2020-07-26 02:26:49 +08:00
|
|
|
$this->{$field}($value);
|
2019-07-23 21:57:44 +08:00
|
|
|
} else {
|
2020-07-26 02:26:49 +08:00
|
|
|
if ( is_array($value) ) {
|
2019-07-23 21:57:44 +08:00
|
|
|
# perhaps should turn into a comma-separated string
|
2020-07-26 02:26:49 +08:00
|
|
|
$this->{$field} = implode(',', $value);
|
|
|
|
} else if ( is_string($value) ) {
|
|
|
|
if ( array_key_exists($field, $this->defaults) && is_array($this->defaults[$field]) && isset($this->defaults[$field]['filter_regexp']) ) {
|
|
|
|
if ( is_array($this->defaults[$feild]['filter_regexp']) ) {
|
|
|
|
foreach ( $this->defaults[$field]['filter_regexp'] as $regexp ) {
|
|
|
|
$this->{$field} = preg_replace($regexp, '', $trim($value));
|
2020-07-26 01:50:59 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-26 02:26:49 +08:00
|
|
|
$this->{$field} = preg_replace($this->defaults[$field]['filter_regexp'], '', trim($value));
|
2020-07-26 01:50:59 +08:00
|
|
|
}
|
2020-04-11 00:17:09 +08:00
|
|
|
} else {
|
2020-07-26 02:26:49 +08:00
|
|
|
$this->{$field} = trim($value);
|
2019-07-23 21:57:44 +08:00
|
|
|
}
|
2020-07-26 02:26:49 +08:00
|
|
|
} else if ( is_integer($value) ) {
|
|
|
|
$this->{$field} = $value;
|
|
|
|
} else if ( is_bool($value) ) {
|
|
|
|
$this->{$field} = $value;
|
|
|
|
} else if ( is_null($value) ) {
|
|
|
|
$this->{$field} = $value;
|
2019-07-23 21:57:44 +08:00
|
|
|
} else {
|
2020-07-26 02:26:49 +08:00
|
|
|
Error("Unknown type $field => $value of var " . gettype($value));
|
|
|
|
$this->{$field} = $value;
|
2019-07-23 21:57:44 +08:00
|
|
|
}
|
|
|
|
} # end if method_exists
|
2020-07-26 02:26:49 +08:00
|
|
|
} # end foreach $data as $field=>$value
|
2019-09-20 02:55:45 +08:00
|
|
|
} # end function set($data)
|
2019-07-23 21:57:44 +08:00
|
|
|
|
2019-09-20 02:55:45 +08:00
|
|
|
/* types is an array of fields telling use that the input might be a checkbox so not present in the input, but therefore has a value
|
|
|
|
*/
|
|
|
|
public function changes($new_values, $defaults=null) {
|
2019-07-23 21:57:44 +08:00
|
|
|
$changes = array();
|
2019-09-20 02:55:45 +08:00
|
|
|
|
|
|
|
if ( $defaults ) {
|
|
|
|
foreach ( $defaults as $field => $type ) {
|
|
|
|
if ( isset($new_values[$field]) ) {
|
|
|
|
# Will have already been handled above
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-03 01:30:03 +08:00
|
|
|
if ( isset($this->defaults[$field]) ) {
|
2019-09-20 02:55:45 +08:00
|
|
|
if ( is_array($this->defaults[$field]) ) {
|
|
|
|
$new_values[$field] = $this->defaults[$field]['default'];
|
|
|
|
} else {
|
|
|
|
$new_values[$field] = $this->defaults[$field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} # end foreach default
|
2020-03-10 02:39:22 +08:00
|
|
|
} # end if defaults
|
|
|
|
|
2019-07-24 23:24:14 +08:00
|
|
|
foreach ( $new_values as $field => $value ) {
|
|
|
|
|
|
|
|
if ( method_exists($this, $field) ) {
|
2020-07-26 02:26:49 +08:00
|
|
|
|
|
|
|
if ( array_key_exists($field, $this->defaults) && is_array($this->defaults[$field]) && isset($this->defaults[$field]['filter_regexp']) ) {
|
|
|
|
if ( is_array($this->defaults[$field]['filter_regexp']) ) {
|
|
|
|
foreach ( $this->defaults[$field]['filter_regexp'] as $regexp ) {
|
|
|
|
$value = preg_replace($regexp, '', trim($value));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$value = preg_replace($this->defaults[$field]['filter_regexp'], '', trim($value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 23:24:14 +08:00
|
|
|
$old_value = $this->$field();
|
|
|
|
if ( is_array($old_value) ) {
|
|
|
|
$diff = array_recursive_diff($old_value, $value);
|
|
|
|
if ( count($diff) ) {
|
|
|
|
$changes[$field] = $value;
|
|
|
|
}
|
|
|
|
} else if ( $this->$field() != $value ) {
|
|
|
|
$changes[$field] = $value;
|
|
|
|
}
|
2019-12-08 00:45:32 +08:00
|
|
|
} else if ( property_exists($this, $field) ) {
|
2019-09-20 02:55:45 +08:00
|
|
|
$type = (array_key_exists($field, $this->defaults) && is_array($this->defaults[$field])) ? $this->defaults[$field]['type'] : 'scalar';
|
|
|
|
if ( $type == 'set' ) {
|
|
|
|
$old_value = is_array($this->$field) ? $this->$field : explode(',', $this->$field);
|
|
|
|
$new_value = is_array($value) ? $value : explode(',', $value);
|
|
|
|
|
|
|
|
$diff = array_recursive_diff($old_value, $new_value);
|
|
|
|
if ( count($diff) ) {
|
|
|
|
$changes[$field] = $new_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Input might be a command separated string, or an array
|
2020-07-26 01:50:59 +08:00
|
|
|
|
2019-09-20 02:55:45 +08:00
|
|
|
} else {
|
2020-04-11 00:17:09 +08:00
|
|
|
if ( array_key_exists($field, $this->defaults) && is_array($this->defaults[$field]) && isset($this->defaults[$field]['filter_regexp']) ) {
|
2020-07-26 01:50:59 +08:00
|
|
|
if ( is_array($this->defaults[$field]['filter_regexp']) ) {
|
|
|
|
foreach ( $this->defaults[$field]['filter_regexp'] as $regexp ) {
|
2020-07-26 02:26:49 +08:00
|
|
|
$value = preg_replace($regexp, '', trim($value));
|
2020-07-26 01:50:59 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$value = preg_replace($this->defaults[$field]['filter_regexp'], '', trim($value));
|
|
|
|
}
|
2020-04-11 00:17:09 +08:00
|
|
|
}
|
2019-09-20 02:55:45 +08:00
|
|
|
if ( $this->{$field} != $value ) {
|
|
|
|
$changes[$field] = $value;
|
|
|
|
}
|
2019-07-24 23:24:14 +08:00
|
|
|
}
|
|
|
|
} else if ( array_key_exists($field, $this->defaults) ) {
|
2020-05-02 03:32:54 +08:00
|
|
|
if ( is_array($this->defaults[$field]) and isset($this->defaults[$field]['default']) ) {
|
2019-09-20 02:55:45 +08:00
|
|
|
$default = $this->defaults[$field]['default'];
|
|
|
|
} else {
|
|
|
|
$default = $this->defaults[$field];
|
|
|
|
}
|
2019-07-24 23:24:14 +08:00
|
|
|
|
2019-09-20 02:55:45 +08:00
|
|
|
if ( $default != $value ) {
|
2019-07-24 23:24:14 +08:00
|
|
|
$changes[$field] = $value;
|
2019-07-23 21:57:44 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-20 02:55:45 +08:00
|
|
|
} # end foreach newvalue
|
|
|
|
|
|
|
|
|
2019-07-23 21:57:44 +08:00
|
|
|
return $changes;
|
|
|
|
} # end public function changes
|
|
|
|
|
|
|
|
public function save($new_values = null) {
|
|
|
|
$class = get_class($this);
|
|
|
|
$table = $class::$table;
|
|
|
|
|
|
|
|
if ( $new_values ) {
|
|
|
|
$this->set($new_values);
|
|
|
|
}
|
|
|
|
|
2020-01-11 05:42:41 +08:00
|
|
|
# Set defaults. Note that we only replace "" with null, not other values
|
|
|
|
# because for example if we want to clear TimestampFormat, we clear it, but the default is a string value
|
2020-01-11 01:44:59 +08:00
|
|
|
foreach ( $this->defaults as $field => $default ) {
|
2020-03-10 02:39:22 +08:00
|
|
|
if ( (!property_exists($this, $field)) or ($this->{$field} === '') ) {
|
2020-01-11 01:44:59 +08:00
|
|
|
if ( is_array($default) ) {
|
|
|
|
$this->{$field} = $default['default'];
|
2020-01-11 05:42:41 +08:00
|
|
|
} else if ( $default == null ) {
|
2020-01-11 01:44:59 +08:00
|
|
|
$this->{$field} = $default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 02:55:45 +08:00
|
|
|
$fields = array_filter(
|
|
|
|
$this->defaults,
|
|
|
|
function($v) {
|
|
|
|
return !(
|
|
|
|
is_array($v)
|
|
|
|
and
|
|
|
|
isset($v['do_not_update'])
|
|
|
|
and
|
|
|
|
$v['do_not_update']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$fields = array_keys($fields);
|
|
|
|
|
2019-07-23 21:57:44 +08:00
|
|
|
if ( $this->Id() ) {
|
2020-06-04 23:44:59 +08:00
|
|
|
$sql = 'UPDATE `'.$table.'` SET '.implode(', ', array_map(function($field) {return '`'.$field.'`=?';}, $fields)).' WHERE Id=?';
|
2019-09-20 02:55:45 +08:00
|
|
|
$values = array_map(function($field){ return $this->{$field};}, $fields);
|
2019-07-23 21:57:44 +08:00
|
|
|
$values[] = $this->{'Id'};
|
|
|
|
if ( dbQuery($sql, $values) )
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
unset($fields['Id']);
|
|
|
|
|
2020-06-04 23:44:59 +08:00
|
|
|
$sql = 'INSERT INTO `'.$table.
|
|
|
|
'` ('.implode(', ', array_map(function($field) {return '`'.$field.'`';}, $fields)).
|
2019-09-20 02:55:45 +08:00
|
|
|
') VALUES ('.
|
|
|
|
implode(', ', array_map(function($field){return '?';}, $fields)).')';
|
|
|
|
|
|
|
|
$values = array_map(function($field){return $this->$field();}, $fields);
|
2019-07-23 21:57:44 +08:00
|
|
|
if ( dbQuery($sql, $values) ) {
|
|
|
|
$this->{'Id'} = dbInsertId();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} // end function save
|
|
|
|
|
|
|
|
public function delete() {
|
|
|
|
$class = get_class($this);
|
|
|
|
$table = $class::$table;
|
2020-06-04 23:44:59 +08:00
|
|
|
dbQuery("DELETE FROM `$table` WHERE Id=?", array($this->{'Id'}));
|
2019-07-23 21:57:44 +08:00
|
|
|
if ( isset($object_cache[$class]) and isset($object_cache[$class][$this->{'Id'}]) )
|
|
|
|
unset($object_cache[$class][$this->{'Id'}]);
|
|
|
|
}
|
|
|
|
|
2019-09-04 22:07:08 +08:00
|
|
|
public function lock() {
|
|
|
|
$class = get_class($this);
|
|
|
|
$table = $class::$table;
|
|
|
|
$row = dbFetchOne("SELECT * FROM `$table` WHERE `Id`=?", NULL, array($this->Id()));
|
|
|
|
if ( !$row ) {
|
|
|
|
Error("Unable to lock $class record for Id=".$this->Id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} # end class Object
|
2019-07-23 21:57:44 +08:00
|
|
|
?>
|