X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Foption_Common.pm;h=26bb7caefed72f7cc16a4f64b294c9a3475ac1a2;hp=5ef6d0f4cc49b0c76f258c21737d5dc333d24442;hb=90393980e5f2859ee1e186fa461f48f5129e803e;hpb=f913a319ef96ca1d39aaa11df3e31a573131f071 diff --git a/FS/FS/option_Common.pm b/FS/FS/option_Common.pm index 5ef6d0f4c..26bb7caef 100644 --- a/FS/FS/option_Common.pm +++ b/FS/FS/option_Common.pm @@ -2,6 +2,7 @@ package FS::option_Common; use strict; use vars qw( @ISA $DEBUG ); +use Scalar::Util qw( blessed ); use FS::Record qw( qsearch qsearchs dbh ); @ISA = qw( FS::Record ); @@ -78,10 +79,13 @@ sub insert { my $valuecol = $self->_option_valuecol; foreach my $optionname ( keys %{$options} ) { + + my $optionvalue = $options->{$optionname}; + my $href = { $pkey => $self->get($pkey), $namecol => $optionname, - $valuecol => $options->{$optionname}, + $valuecol => ( ref($optionvalue) || $optionvalue ), }; #my $option_record = eval "new FS::$option_table \$href"; @@ -91,11 +95,15 @@ sub insert { #} my $option_record = "FS::$option_table"->new($href); - $error = $option_record->insert; + my @args = (); + push @args, $optionvalue if ref($optionvalue); #only hashes supported so far + + $error = $option_record->insert(@args); if ( $error ) { $dbh->rollback if $oldAutoCommit; return $error; } + } $dbh->commit or die $dbh->errstr if $oldAutoCommit; @@ -153,22 +161,28 @@ sub delete { Replaces the OLD_RECORD with this one in the database. If there is an error, returns the error, otherwise returns false. -If a list hash reference of options is supplied, part_export_option records are -created or modified (see L). +If a list or hash reference of options is supplied, option records are created +or modified. =cut sub replace { my $self = shift; - my $old = ( ref($_[0]) eq ref($self) ) + my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') ) ? shift : $self->replace_old; - my $options = - ( ref($_[0]) eq 'HASH' ) - ? shift - : { @_ }; + my $options; + my $options_supplied = 0; + if ( ref($_[0]) eq 'HASH' ) { + $options = shift; + $options_supplied = 1; + } else { + $options = { @_ }; + $options_supplied = scalar(@_) ? 1 : 0; + } + warn "FS::option_Common::replace called on $self with options ". join(', ', map "$_ => ". $options->{$_}, keys %$options) if $DEBUG; @@ -206,10 +220,15 @@ sub replace { $namecol => $optionname, } ); + my $optionvalue = $options->{$optionname}; + + my %oldhash = $oldopt ? $oldopt->hash : (); + my $href = { + %oldhash, $pkey => $self->get($pkey), $namecol => $optionname, - $valuecol => $options->{$optionname}, + $valuecol => ( ref($optionvalue) || $optionvalue ), }; #my $newopt = eval "new FS::$option_table \$href"; @@ -222,12 +241,15 @@ sub replace { my $opt_pkey = $newopt->primary_key; $newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt; - warn $oldopt; + + my @args = (); + push @args, $optionvalue if ref($optionvalue); #only hashes supported so far + warn "FS::option_Common::replace: ". ( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" ) if $DEBUG > 2; - my $error = $oldopt ? $newopt->replace($oldopt) : $newopt->insert; - warn $error; + my $error = $oldopt ? $newopt->replace($oldopt, @args) + : $newopt->insert( @args); if ( $error ) { $dbh->rollback if $oldAutoCommit; return $error; @@ -235,13 +257,15 @@ sub replace { } #remove extraneous old options - foreach my $opt ( - grep { !exists $options->{$_->$namecol()} } $old->option_objects - ) { - my $error = $opt->delete; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return $error; + if ( $options_supplied ) { + foreach my $opt ( + grep { !exists $options->{$_->$namecol()} } $old->option_objects + ) { + my $error = $opt->delete; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } } }