graphical selection of deployment zones and automatic block lookup, #30260
[freeside.git] / FS / FS / msg_template.pm
index 827bb98..4940388 100644 (file)
@@ -5,7 +5,7 @@ use strict;
 use vars qw( $DEBUG $conf );
 
 use FS::Conf;
-use FS::Record qw( qsearch qsearchs );
+use FS::Record qw( qsearch qsearchs dbh );
 
 use FS::cust_msg;
 use FS::template_content;
@@ -94,12 +94,18 @@ sub _rebless {
   my $class = 'FS::msg_template::' . $self->msgclass;
   eval "use $class;";
   bless($self, $class) unless $@;
+  warn "Error loading msg_template msgclass: " . $@ if $@; #or die?
 
-  # merge in the extension fields
+  # merge in the extension fields (but let fields in $self override them)
+  # except don't ever override the extension's primary key, it's immutable
   if ( $self->msgnum and $self->extension_table ) {
     my $extension = $self->_extension;
     if ( $extension ) {
-      $self->{Hash} = { $self->hash, $extension->hash };
+      my $ext_key = $extension->get($extension->primary_key);
+      $self->{Hash} = { $extension->hash,
+                        $self->hash,
+                        $extension->primary_key => $ext_key
+                      };
     }
   }
 
@@ -194,6 +200,8 @@ sub replace {
 
   my $extension = $new->_extension;
   if ( $extension ) {
+    # merge changes into the extension record and replace it
+    $extension->{Hash} = { $extension->hash, $new->hash };
     $error ||= $extension->replace;
   }
 
@@ -212,7 +220,7 @@ sub replace_check {
   if ( $old->msgclass ) {
     if ( !$self->msgclass ) {
       $self->set('msgclass', $old->msgclass);
-    } else {
+    } elsif ( $old->msgclass ne $self->msgclass ) {
       return "Can't change message template class from ".$old->msgclass.
              " to ".$self->msgclass.".";
     }
@@ -650,20 +658,22 @@ sub _upgrade_data {
     [ 'decline_msgnum',  'declinetemplate',    '',               '', '' ],
     [ 'impending_recur_msgnum', 'impending_recur_template', '',  '', 'impending_recur_bcc' ],
     [ 'payment_receipt_msgnum', 'payment_receipt_email', '',     '', '' ],
-    [ 'welcome_msgnum',  'welcome_email',      'welcome_email-subject', 'welcome_email-from', '' ],
-    [ 'warning_msgnum',  'warning_email',      'warning_email-subject', 'warning_email-from', '' ],
+    [ 'welcome_msgnum',  'welcome_email',      'welcome_email-subject', 'welcome_email-from', '', 'welcome_email-mimetype' ],
+    [ 'threshold_warning_msgnum',  'warning_email',      'warning_email-subject', 'warning_email-from', 'warning_email-cc', 'warning_email-mimetype' ],
   );
  
   my @agentnums = ('', map {$_->agentnum} qsearch('agent', {}));
   foreach my $agentnum (@agentnums) {
     foreach (@fixes) {
-      my ($newname, $oldname, $subject, $from, $bcc) = @$_;
+      my ($newname, $oldname, $subject, $from, $bcc, $mimetype) = @$_;
+      
       if ($conf->exists($oldname, $agentnum)) {
         my $new = new FS::msg_template({
+          'msgclass'  => 'email',
           'msgname'   => $oldname,
           'agentnum'  => $agentnum,
           'from_addr' => ($from && $conf->config($from, $agentnum)) || '',
-          'bcc_addr'  => ($bcc && $conf->config($from, $agentnum)) || '',
+          'bcc_addr'  => ($bcc && $conf->config($bcc, $agentnum)) || '',
           'subject'   => ($subject && $conf->config($subject, $agentnum)) || '',
           'mime_type' => 'text/html',
           'body'      => join('<BR>',$conf->config($oldname, $agentnum)),
@@ -674,6 +684,8 @@ sub _upgrade_data {
         $conf->delete($oldname, $agentnum);
         $conf->delete($from, $agentnum) if $from;
         $conf->delete($subject, $agentnum) if $subject;
+        $conf->delete($bcc, $agentnum) if $bcc;
+        $conf->delete($mimetype, $agentnum) if $mimetype;
       }
     }