RT# 83460 Fix validation bug on part_event_option
[freeside.git] / FS / FS / log.pm
index 2543aea..64d036e 100644 (file)
@@ -4,9 +4,11 @@ use strict;
 use base qw( FS::Record );
 use FS::Record qw( qsearch qsearchs dbdef );
 use FS::UID qw( dbh driver_name );
+use FS::Log;
 use FS::log_context;
 use FS::log_email;
 use FS::upgrade_journal;
+use Tie::IxHash;
 
 =head1 NAME
 
@@ -80,18 +82,22 @@ Will send emails according to the conditions in L<FS::log_email>.
 sub insert {
   # not using process_o2m for this, because we don't have a web interface
   my $self = shift;
+
   my $error = $self->SUPER::insert;
   return $error if $error;
-  my $contexts = {}; #for quick checks when sending emails
-  foreach ( @_ ) {
+
+  my $contexts = {};
+  my $context_height = @_;
+  foreach ( @_ ) { # ordered from least to most specific
     my $context = FS::log_context->new({
         'lognum'  => $self->lognum,
         'context' => $_
     });
     $error = $context->insert;
     return $error if $error;
-    $contexts->{$_} = 1;
+    $contexts->{$_} = $context_height--;
   }
+
   foreach my $log_email (
     qsearch('log_email',
       {
@@ -103,19 +109,20 @@ sub insert {
       }
     )
   ) {
-    # shouldn't be a lot of these, so not packing this into the qsearch
+    # shouldn't be a lot of log_email records, so not packing these checks into the qsearch
     next if $log_email->context && !$contexts->{$log_email->context};
+    next if $log_email->context_height && ($contexts->{$log_email->context} > $log_email->context_height);
     my $msg_template = qsearchs('msg_template',{ 'msgnum' => $log_email->msgnum });
     unless ($msg_template) {
       warn "Could not send email when logging, could not load message template for logemailnum " . $log_email->logemailnum;
       next;
     }
     my $emailerror = $msg_template->send(
-      'msgtype'       => 'admin',
-      'to'            => $log_email->to_addr,
+      'msgtype' => 'admin',
+      'to'      => $log_email->to_addr,
       'substitutions' => {
-        'loglevel'   => $FS::Log::LEVELS[$self->level], # which has hopefully been loaded...
-        'logcontext' => $log_email->context, # use the one that triggered the email
+        'loglevel'   => $FS::Log::LEVELS{$self->level} || 'unknown',
+        'logcontext' => join(', ', keys( %$contexts )) || 'unknown',
         'logmessage' => $self->message,
       },
     );
@@ -388,24 +395,19 @@ sub _upgrade_data {
   return if FS::upgrade_journal->is_done('log__remap_levels');
 
   tie my %levelmap, 'Tie::IxHash', 
-#    0 => 0, #debug
-#    1 => 1, #info
     2 => 1, #notice -> info
-    3 => 2, #warning
-    4 => 3, #error
-    5 => 4, #critical
-    6 => 4, #alert -> critical
-    7 => 4, #emergency -> critical
+    6 => 5, #alert -> critical
+    7 => 5, #emergency -> critical
   ;
 
   # this method should never autocommit
   # should have been set in upgrade, but just in case...
   local $FS::UID::AutoCommit = 0;
 
-  # FS::log has no replace method
-  # in practice, only debug/info/warning/error were used,
-  #   so this should only hit warning/error
+  # in practice, only debug/info/warning/error appear to have been used,
+  #   so this probably won't do anything, but just in case
   foreach my $old (keys %levelmap) {
+    # FS::log has no replace method
     my $sql = 'UPDATE log SET level=' . dbh->quote($levelmap{$old}) . ' WHERE level=' . dbh->quote($old);
     warn $sql unless $opts{'quiet'};
     my $sth = dbh->prepare($sql) or die dbh->errstr;
@@ -414,7 +416,9 @@ sub _upgrade_data {
   }
 
   foreach my $log_email (
-    qsearch('log_email',{ 'min_level' => { 'op' => '>=', 'value' => '2' } })
+    qsearch('log_email',{ 'min_level' => 2 }),
+    qsearch('log_email',{ 'min_level' => 6 }),
+    qsearch('log_email',{ 'min_level' => 7 }),
   ) {
     $log_email->min_level($levelmap{$log_email->min_level});
     my $error = $log_email->replace;