diff --git a/db/zm_create.sql.in b/db/zm_create.sql.in index dc0fadd53..90fc319f3 100644 --- a/db/zm_create.sql.in +++ b/db/zm_create.sql.in @@ -205,6 +205,7 @@ CREATE TABLE `Events` ( `Messaged` tinyint(3) unsigned NOT NULL default '0', `Executed` tinyint(3) unsigned NOT NULL default '0', `Notes` text, + `StateId` int(10) unsigned NOT NULL, `Orientation` enum('0','90','180','270','hori','vert') NOT NULL default '0', PRIMARY KEY (`Id`,`MonitorId`), KEY `MonitorId` (`MonitorId`), diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm index 0ab2aee3b..19374543e 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm @@ -75,6 +75,8 @@ sub zmDbConnect { if ( $force ) { zmDbDisconnect(); } + my $options = shift; + if ( ( ! defined( $dbh ) ) or ! $dbh->ping() ) { my ( $host, $portOrSocket ) = ( $ZoneMinder::Config::Config{ZM_DB_HOST} =~ /^([^:]+)(?::(.+))?$/ ); my $socket; @@ -89,7 +91,7 @@ sub zmDbConnect { $socket = ";host=".$Config{ZM_DB_HOST}; } $dbh = DBI->connect( "DBI:mysql:database=".$Config{ZM_DB_NAME} - .$socket + .$socket . ($options?';'.join(';', map { $_.'='.$$options{$_} } keys %{$options} ) : '' ) , $Config{ZM_DB_USER} , $Config{ZM_DB_PASS} ); diff --git a/scripts/zmupdate.pl.in b/scripts/zmupdate.pl.in index cb0781c2e..ee1829a99 100644 --- a/scripts/zmupdate.pl.in +++ b/scripts/zmupdate.pl.in @@ -116,7 +116,7 @@ GetOptions( 'dir:s' =>\$updateDir ) or pod2usage(-exitstatus => -1); -my $dbh = zmDbConnect(); +my $dbh = zmDbConnect(undef, { mysql_multi_statements=>1 } ); $Config{ZM_DB_USER} = $dbUser; $Config{ZM_DB_PASS} = $dbPass; @@ -449,47 +449,19 @@ if ( $version ) my $dbh = shift; my $version = shift; - my ( $host, $port ) = ( $Config{ZM_DB_HOST} =~ /^([^:]+)(?::(.+))?$/ ); - my $command = "mysql -h".$host; - $command .= " -P".$port if defined($port); - if ( $dbUser ) - { - $command .= " -u".$dbUser; - $command .= ' -p"'.$dbPass.'"' if $dbPass; - } - $command .= " ".$Config{ZM_DB_NAME}." < "; - if ( $updateDir ) - { - $command .= $updateDir; - } - else - { - $command .= $Config{ZM_PATH_DATA}."/db"; - } - $command .= "/zm_update-".$version.".sql"; + my $file = ( $updateDir ? $updateDir : $Config{ZM_PATH_DATA}.'/db' ) . "/zm_update-$version.sql"; - print( "Executing '$command'\n" ) if ( logDebugging() ); - my $output = qx($command); - my $status = $? >> 8; - if ( $status || logDebugging() ) - { - chomp( $output ); - print( "Output: $output\n" ); - } - if ( $status ) - { - die( "Command '$command' exited with status: $status\n" ); - } - else - { - print( "\nDatabase successfully upgraded to version $version.\n" ); - my $sql = "update Config set Value = ? where Name = 'ZM_DYN_DB_VERSION'"; - my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); - my $res = $sth->execute( $version ) or die( "Can't execute: ".$sth->errstr() ); - } + open( my $fh, '<', $file ) or die "Unable to open $file $!"; + $/ = undef; + my $sql = <$fh>; + close $fh; + $dbh->do($sql) or die $dbh->errstr(); + print( "\nDatabase successfully upgraded to version $version.\n" ); + $sql = "update Config set Value = ? where Name = 'ZM_DYN_DB_VERSION'"; + my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $version ) or die( "Can't execute: ".$sth->errstr() ); } - print( "\nUpgrading database to version ".ZM_VERSION."\n" ); # Update config first of all diff --git a/src/zm_event.cpp b/src/zm_event.cpp index b9c8da6a1..a581e1c60 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -81,10 +81,26 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string Storage * storage = monitor->getStorage(); - static char sql[ZM_SQL_MED_BUFSIZ]; + unsigned int state_id = 0; + zmDbRow dbrow; + if ( dbrow.fetch( "SELECT Id FROM States WHERE IsActive=1") ) { + state_id = atoi(dbrow[0]); + } + static char sql[ZM_SQL_MED_BUFSIZ]; struct tm *stime = localtime( &start_time.tv_sec ); - snprintf( sql, sizeof(sql), "insert into Events ( MonitorId, StorageId, Name, StartTime, Width, Height, Cause, Notes, Orientation, Videoed ) values ( %d, %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s', %d, %d )", monitor->Id(), storage->Id(), start_time.tv_sec, monitor->Width(), monitor->Height(), cause.c_str(), notes.c_str(), monitor->getOrientation(), videoEvent ); + snprintf( sql, sizeof(sql), "insert into Events ( MonitorId, StorageId, Name, StartTime, Width, Height, Cause, Notes, StateId, Orientation, Videoed ) values ( %d, %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s', %d, %d )", + monitor->Id(), + storage->Id(), + start_time.tv_sec, + monitor->Width(), + monitor->Height(), + cause.c_str(), + notes.c_str(), + state_id, + monitor->getOrientation(), + videoEvent + ); if ( mysql_query( &dbconn, sql ) ) { Error( "Can't insert event: %s. sql was (%s)", mysql_error( &dbconn ), sql ); exit( mysql_errno( &dbconn ) ); diff --git a/web/includes/functions.php b/web/includes/functions.php index 801017071..364dc9cee 100644 --- a/web/includes/functions.php +++ b/web/includes/functions.php @@ -1196,6 +1196,7 @@ function parseFilter( &$filter, $saveToSession=false, $querySep='&' ) { case 'MaxScore': case 'Cause': case 'Notes': + case 'StateId': case 'Archived': $filter['sql'] .= 'E.'.$filter['terms'][$i]['attr']; break; diff --git a/web/lang/en_gb.php b/web/lang/en_gb.php index e715e7701..51825caae 100644 --- a/web/lang/en_gb.php +++ b/web/lang/en_gb.php @@ -127,6 +127,7 @@ $SLANG = array( 'AttrMonitorName' => 'Monitor Name', 'AttrStorageArea' => 'Storage Area', 'AttrServer' => 'Server', + 'AttrStateId' => 'Run State', 'AttrName' => 'Name', 'AttrNotes' => 'Notes', 'AttrSystemLoad' => 'System Load', diff --git a/web/skins/classic/views/filter.php b/web/skins/classic/views/filter.php index b633b7da3..0baeebcf0 100644 --- a/web/skins/classic/views/filter.php +++ b/web/skins/classic/views/filter.php @@ -99,6 +99,7 @@ $attrTypes = array( 'SystemLoad' => translate('AttrSystemLoad'), 'StorageId' => translate('AttrStorageArea'), 'ServerId' => translate('AttrServer'), + 'StateId' => translate('AttrStateId'), ); $opTypes = array( '=' => translate('OpEq'), @@ -206,7 +207,20 @@ for ( $i = 0; isset($_REQUEST['filter']) && $i < count($_REQUEST['filter']['term