Upgrade Event object to use the common Object methods. Add deleting files from Secondary storage

pull/2696/head
Isaac Connor 2019-08-29 11:25:37 -04:00
parent 93d642304e
commit a4b057fa2b
1 changed files with 51 additions and 163 deletions

View File

@ -2,76 +2,50 @@
namespace ZM;
require_once('Storage.php');
require_once('functions.php');
require_once('Object.php');
$event_cache = array();
class Event extends ZM_Object {
protected static $table = 'Events';
class Event {
private $fields = array(
'Id',
'Name',
'MonitorId',
'StorageId',
'SecondaryStorageId',
'Name',
'Cause',
'StartTime',
'EndTime',
'Width',
'Height',
'Length',
'Frames',
'AlarmFrames',
'DefaultVideo',
'SaveJPEGs',
'TotScore',
'AvgScore',
'MaxScore',
'Archived',
'Videoed',
'Uploaded',
'Emailed',
'Messaged',
'Executed',
'Notes',
'StateId',
'Orientation',
'DiskSpace',
'Scheme',
'Locked',
protected $defaults = array(
'Id' => null,
'Name' => '',
'MonitorId' => null,
'StorageId' => null,
'SecondaryStorageId' => null,
'Cause' => '',
'StartTime' => null,
'EndTime' => null,
'Width' => null,
'Height' => null,
'Length' => null,
'Frames' => null,
'AlarmFrames' => null,
'DefaultVideo' => '',
'SaveJPEGs' => 0,
'TotScore' => 0,
'AvgScore' => 0,
'MaxScore' => 0,
'Archived' => 0,
'Videoed' => 0,
'Uploaded' => 0,
'Emailed' => 0,
'Messaged' => 0,
'Executed' => 0,
'Notes' => '',
'StateId' => 0,
'Orientation' => 0,
'DiskSpace' => null,
'Scheme' => 0,
'Locked' => 0,
);
public function __construct( $IdOrRow = null ) {
$row = NULL;
if ( $IdOrRow ) {
if ( is_integer($IdOrRow) or is_numeric($IdOrRow) ) {
$row = dbFetchOne('SELECT *,unix_timestamp(StartTime) AS Time FROM Events WHERE Id=?', NULL, array($IdOrRow));
if ( ! $row ) {
Error('Unable to load Event 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 Event Constructor from $file:$line) Id was $IdOrRow");
return;
}
public static function find( $parameters = array(), $options = array() ) {
return ZM_Object::_find(get_class(), $parameters, $options);
}
if ( $row ) {
foreach ($row as $k => $v) {
$this->{$k} = $v;
}
global $event_cache;
$event_cache[$row['Id']] = $this;
} else {
$backTrace = debug_backtrace();
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Error('No row for Event ' . $IdOrRow . " from $file:$line");
}
} # end if isset($IdOrRow)
} // end function __construct
public static function find_one( $parameters = array(), $options = array() ) {
return ZM_Object::_find_one(get_class(), $parameters, $options);
}
public function Storage( $new = null ) {
if ( $new ) {
@ -108,24 +82,6 @@ class Event {
return new Monitor();
}
public function __call($fn, array $args){
if ( count($args) ) {
$this->{$fn} = $args[0];
}
if ( array_key_exists($fn, $this) ) {
return $this->{$fn};
$backTrace = debug_backtrace();
$file = $backTrace[0]['file'];
$line = $backTrace[0]['line'];
Warning("Unknown function call Event->$fn from $file:$line");
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Warning("Unknown function call Event->$fn from $file:$line");
Warning(print_r($this, true));
}
}
public function Time() {
if ( ! isset($this->{'Time'}) ) {
$this->{'Time'} = strtotime($this->{'StartTime'});
@ -219,6 +175,17 @@ class Event {
return;
}
deletePath($eventPath);
if ( $this->SecondaryStorageId() ) {
$Storage = $this->SecondaryStorage();
if ( $Storage->Id() ) {
$eventPath = $Storage->Path().'/'.$this->Relative_Path();
if ( ! $eventPath ) {
Error('No event Path in Event delete. Not deleting');
} else {
deletePath($eventPath);
}
} # end if Storage
} # end if has Secondary Storage
} # USE_DEEP_STORAGE OR NOT
} # ! ZM_OPT_FAST_DELETE
dbQuery('DELETE FROM Events WHERE Id = ?', array($this->{'Id'}));
@ -509,81 +476,6 @@ class Event {
return $imageData;
}
public static function find_one( $parameters = null, $options = null ) {
global $event_cache;
if (
( count($parameters) == 1 ) and
isset($parameters['Id']) and
isset($event_cache[$parameters['Id']]) ) {
return $event_cache[$parameters['Id']];
}
$results = Event::find( $parameters, $options );
if ( count($results) > 1 ) {
Error("Event Returned more than 1");
return $results[0];
} else if ( count($results) ) {
return $results[0];
} else {
return null;
}
}
public static function find( $parameters = null, $options = null ) {
$sql = 'SELECT * FROM Events ';
$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 Event::find from $file:$line");
return array();
}
}
}
$filters = array();
$result = dbQuery($sql, $values);
if ( $result ) {
$results = $result->fetchALL();
foreach ( $results as $row ) {
$filters[] = new Event($row);
}
}
return $filters;
}
public function save( ) {
$sql = 'UPDATE Events SET '.implode(', ', array_map( function($field) {return $field.'=?';}, $this->fields ) ) . ' WHERE Id=?';
$values = array_map( function($field){return $this->{$field};}, $this->fields );
$values[] = $this->{'Id'};
dbQuery( $sql, $values );
}
public function link_to($text=null) {
if ( !$text )
$text = $this->{'Id'};
@ -686,18 +578,14 @@ class Event {
public function can_delete() {
if ( $this->Archived() ) {
Logger::Debug("Am archived, can't delete");
return false;
}
if ( !$this->EndTime() ) {
Logger::Debug("No EndTime can't delete");
return false;
}
if ( !canEdit('Events') ) {
Logger::Debug("No permission to edit events, can't delete");
return false;
}
Logger::Debug("Can delete: archived: " . $this->Archived() . " endtime: " . $this->EndTime() );
return true;
}