+sub replace {
+ my $self = shift;
+ my $old = ( ref($_[0]) and $_[0]->isa('FS::Record') )
+ ? shift
+ : $self->replace_old;
+ my %content = @_;
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $error = $self->SUPER::replace($old);
+
+ if ( !$error and %content ) {
+ $content{'locale'} ||= '';
+ my $new_content = qsearchs('template_content', {
+ 'msgnum' => $self->msgnum,
+ 'locale' => $content{'locale'},
+ } );
+ if ( $new_content ) {
+ $new_content->subject($content{'subject'});
+ $new_content->body($content{'body'});
+ $error = $new_content->replace;
+ }
+ else {
+ $content{'msgnum'} = $self->msgnum;
+ $new_content = new FS::template_content \%content;
+ $error = $new_content->insert;
+ }
+ }
+
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+
+ warn "committing FS::msg_template->replace\n" if $DEBUG and $oldAutoCommit;
+ $dbh->commit if $oldAutoCommit;
+ return;
+}
+
+