1 package FS::option_Common;
4 use base qw( FS::Record );
6 use Scalar::Util qw( blessed );
7 use FS::Record qw( qsearch qsearchs dbh );
13 FS::option_Common - Base class for option sub-classes
17 use FS::option_Common;
19 @ISA = qw( FS::option_Common );
21 #optional for non-standard names
22 sub _option_table { 'table_name'; } #defaults to ${table}_option
23 sub _option_namecol { 'column_name'; } #defaults to optionname
24 sub _option_valuecol { 'column_name'; } #defaults to optionvalue
28 FS::option_Common is intended as a base class for classes which have a
29 simple one-to-many class associated with them, used to store a hash-like data
30 structure of keys and values.
36 =item insert [ HASHREF | OPTION => VALUE ... ]
38 Adds this record to the database. If there is an error, returns the error,
39 otherwise returns false.
41 If a list or hash reference of options is supplied, option records are also
46 #false laziness w/queue.pm
50 ( ref($_[0]) eq 'HASH' )
53 warn "FS::option_Common::insert called on $self with options ".
54 join(', ', map "$_ => ".$options->{$_}, keys %$options)
57 local $SIG{HUP} = 'IGNORE';
58 local $SIG{INT} = 'IGNORE';
59 local $SIG{QUIT} = 'IGNORE';
60 local $SIG{TERM} = 'IGNORE';
61 local $SIG{TSTP} = 'IGNORE';
62 local $SIG{PIPE} = 'IGNORE';
64 my $oldAutoCommit = $FS::UID::AutoCommit;
65 local $FS::UID::AutoCommit = 0;
68 my $error = $self->SUPER::insert;
70 $dbh->rollback if $oldAutoCommit;
74 my $pkey = $self->primary_key;
75 my $option_table = $self->option_table;
77 my $namecol = $self->_option_namecol;
78 my $valuecol = $self->_option_valuecol;
80 foreach my $optionname ( keys %{$options} ) {
82 my $optionvalue = $options->{$optionname};
85 $pkey => $self->get($pkey),
86 $namecol => $optionname,
87 $valuecol => ( ref($optionvalue) || $optionvalue ),
90 #my $option_record = eval "new FS::$option_table \$href";
92 # $dbh->rollback if $oldAutoCommit;
95 my $option_record = "FS::$option_table"->new($href);
98 push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
100 $error = $option_record->insert(@args);
102 $dbh->rollback if $oldAutoCommit;
108 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
116 Delete this record from the database. Any associated option records are also
121 #foreign keys would make this much less tedious... grr dumb mysql
124 local $SIG{HUP} = 'IGNORE';
125 local $SIG{INT} = 'IGNORE';
126 local $SIG{QUIT} = 'IGNORE';
127 local $SIG{TERM} = 'IGNORE';
128 local $SIG{TSTP} = 'IGNORE';
129 local $SIG{PIPE} = 'IGNORE';
131 my $oldAutoCommit = $FS::UID::AutoCommit;
132 local $FS::UID::AutoCommit = 0;
135 my $error = $self->SUPER::delete;
137 $dbh->rollback if $oldAutoCommit;
141 my $pkey = $self->primary_key;
142 #my $option_table = $self->option_table;
144 foreach my $obj ( $self->option_objects ) {
145 my $error = $obj->delete;
147 $dbh->rollback if $oldAutoCommit;
152 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
158 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
160 Replaces the OLD_RECORD with this one in the database. If there is an error,
161 returns the error, otherwise returns false.
163 If a list or hash reference of options is supplied, option records are created
171 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
173 : $self->replace_old;
176 my $options_supplied = 0;
177 if ( ref($_[0]) eq 'HASH' ) {
179 $options_supplied = 1;
182 $options_supplied = scalar(@_) ? 1 : 0;
185 warn "FS::option_Common::replace called on $self with options ".
186 join(', ', map "$_ => ". $options->{$_}, keys %$options)
189 local $SIG{HUP} = 'IGNORE';
190 local $SIG{INT} = 'IGNORE';
191 local $SIG{QUIT} = 'IGNORE';
192 local $SIG{TERM} = 'IGNORE';
193 local $SIG{TSTP} = 'IGNORE';
194 local $SIG{PIPE} = 'IGNORE';
196 my $oldAutoCommit = $FS::UID::AutoCommit;
197 local $FS::UID::AutoCommit = 0;
200 my $error = $self->SUPER::replace($old);
202 $dbh->rollback if $oldAutoCommit;
206 my $pkey = $self->primary_key;
207 my $option_table = $self->option_table;
209 my $namecol = $self->_option_namecol;
210 my $valuecol = $self->_option_valuecol;
212 foreach my $optionname ( keys %{$options} ) {
214 warn "FS::option_Common::replace: inserting or replacing option: $optionname"
217 my $oldopt = qsearchs( $option_table, {
218 $pkey => $self->get($pkey),
219 $namecol => $optionname,
222 my $optionvalue = $options->{$optionname};
224 my %oldhash = $oldopt ? $oldopt->hash : ();
228 $pkey => $self->get($pkey),
229 $namecol => $optionname,
230 $valuecol => ( ref($optionvalue) || $optionvalue ),
233 #my $newopt = eval "new FS::$option_table \$href";
235 # $dbh->rollback if $oldAutoCommit;
238 my $newopt = "FS::$option_table"->new($href);
240 my $opt_pkey = $newopt->primary_key;
242 $newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt;
245 push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
247 warn "FS::option_Common::replace: ".
248 ( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" )
250 my $error = $oldopt ? $newopt->replace($oldopt, @args)
251 : $newopt->insert( @args);
253 $dbh->rollback if $oldAutoCommit;
258 #remove extraneous old options
259 if ( $options_supplied ) {
261 grep { !exists $options->{$_->$namecol()} } $old->option_objects
263 my $error = $opt->delete;
265 $dbh->rollback if $oldAutoCommit;
271 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
279 Returns all options as FS::I<tablename>_option objects.
285 my $pkey = $self->primary_key;
286 my $option_table = $self->option_table;
287 qsearch($option_table, { $pkey => $self->get($pkey) } );
292 Returns a list of option names and values suitable for assigning to a hash.
298 my $namecol = $self->_option_namecol;
299 my $valuecol = $self->_option_valuecol;
300 map { $_->$namecol() => $_->$valuecol() } $self->option_objects;
303 =item option OPTIONNAME
305 Returns the option value for the given name, or the empty string.
311 my $pkey = $self->primary_key;
312 my $option_table = $self->option_table;
313 my $namecol = $self->_option_namecol;
314 my $valuecol = $self->_option_valuecol;
316 $pkey => $self->get($pkey),
319 warn "$self -> option: searching for ".
320 join(' / ', map { "$_ => ". $hashref->{$_} } keys %$hashref )
322 my $obj = qsearchs($option_table, $hashref);
323 $obj ? $obj->$valuecol() : '';
329 my $option_table = $self->_option_table;
330 eval "use FS::$option_table";
336 sub _option_table { shift->table .'_option'; }
337 sub _option_namecol { 'optionname'; }
338 sub _option_valuecol { 'optionvalue'; }