diff --git a/web/includes/Event.php b/web/includes/Event.php index 7b15671d8..440619390 100644 --- a/web/includes/Event.php +++ b/web/includes/Event.php @@ -49,7 +49,9 @@ class Event { $this->{'Storage'} = $new; } if ( ! ( array_key_exists( 'Storage', $this ) and $this->{'Storage'} ) ) { - $this->{'Storage'} = new Storage( isset($this->{'StorageId'}) ? $this->{'StorageId'} : NULL ); + $this->{'Storage'} = isset($this->{'StorageId'}) ? + Storage::find_one(array('Id'=>$this->{'StorageId'})) : + new Storage(NULL); } return $this->{'Storage'}; } diff --git a/web/includes/Group.php b/web/includes/Group.php index 77ff3971d..be2a90f54 100644 --- a/web/includes/Group.php +++ b/web/includes/Group.php @@ -1,5 +1,7 @@ {$k} = $v; } } + $group_cache[$row['Id']] = $this; } // end function __construct - public function __call( $fn, array $args ) { - if ( count( $args ) ) { + public function __call($fn, array $args) { + if ( count($args) ) { $this->{$fn} = $args[0]; } - if ( array_key_exists( $fn, $this ) ) { + if ( array_key_exists($fn, $this) ) { return $this->{$fn}; } else if ( array_key_exists( $fn, $this->defaults ) ) { $this->{$fn} = $this->defaults{$fn}; @@ -53,6 +58,25 @@ class Group { } } + 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_all($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; + } + } + public static function find_all( $parameters = null ) { $filters = array(); $sql = 'SELECT * FROM Groups '; @@ -289,7 +313,7 @@ $group_options[$Group->Id()] = str_repeat( ' ', $depth ) . $Group->Name(); public function Parent( ) { if ( $this->{'ParentId'} ) { - return new Group($this->{'ParentId'}); + return Group::find_one(array('Id'=>$this->{'ParentId'})); } return null; } diff --git a/web/includes/Monitor.php b/web/includes/Monitor.php index d8f74892c..38e8ec441 100644 --- a/web/includes/Monitor.php +++ b/web/includes/Monitor.php @@ -348,7 +348,7 @@ private $control_fields = array( $url = '?user='.$_SESSION['username']; } } -Logger::Debug("sending command to $url"); + Logger::Debug("sending command to $url"); $data = array('Monitor[Function]' => $this->{'Function'} ); // use key 'http' even if you send the request to https://... @@ -442,10 +442,11 @@ Logger::Debug("sending command to $url"); $this->{'Storage'} = $new; } if ( ! ( array_key_exists('Storage', $this) and $this->{'Storage'} ) ) { - $this->{'Storage'} = new Storage( isset($this->{'StorageId'}) ? $this->{'StorageId'} : NULL ); + $this->{'Storage'} = isset($this->{'StorageId'}) ? + Storage::find_one(array('Id'=>$this->{'StorageId'})) : + new Storage(NULL); } return $this->{'Storage'}; } - } // end class Monitor ?> diff --git a/web/includes/Storage.php b/web/includes/Storage.php index 38ba83bf9..7497ab0f5 100644 --- a/web/includes/Storage.php +++ b/web/includes/Storage.php @@ -1,18 +1,14 @@ $v) { $this->{$k} = $v; } - #$storage_cache[$IdOrRow] = $this; + $storage_cache[$IdOrRow] = $this; } else { $this->{'Name'} = ''; $this->{'Path'} = ''; @@ -70,7 +66,25 @@ class Storage { Warning( "Unknown function call Storage->$fn from $file:$line" ); } } -public static function find_all( $parameters = null, $options = null ) { + public static function find_one( $parameters = null, $options = null ) { + global $storage_cache; + if ( + ( count($parameters) == 1 ) and + isset($parameters['Id']) and + isset($storage_cache[$parameters['Id']]) ) { + return $storage_cache[$parameters['Id']]; + } + $results = Storage::find_all( $parameters, $options ); + if ( count($results) > 1 ) { + Error("Storage Returned more than 1"); + return $results[0]; + } else if ( count($results) ) { + return $results[0]; + } else { + return null; + } + } + public static function find_all( $parameters = null, $options = null ) { $filters = array(); $sql = 'SELECT * FROM Storage '; $values = array(); @@ -97,9 +111,11 @@ public static function find_all( $parameters = null, $options = null ) { $sql .= ' ORDER BY ' . $options['order']; } $result = dbQuery($sql, $values); - $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Storage'); - foreach ( $results as $row => $obj ) { - $filters[] = $obj; + if ( $result ) { + $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Storage'); + foreach ( $results as $row => $obj ) { + $filters[] = $obj; + } } return $filters; }