1 package FS::option_Common;
4 use vars qw( @ISA $DEBUG );
5 use Scalar::Util qw( blessed );
6 use FS::Record qw( qsearch qsearchs dbh );
8 @ISA = qw( FS::Record );
14 FS::option_Common - Base class for option sub-classes
18 use FS::option_Common;
20 @ISA = qw( FS::option_Common );
22 #optional for non-standard names
23 sub _option_table { 'table_name'; } #defaults to ${table}_option
24 sub _option_namecol { 'column_name'; } #defaults to optionname
25 sub _option_valuecol { 'column_name'; } #defaults to optionvalue
29 FS::option_Common is intended as a base class for classes which have a
30 simple one-to-many class associated with them, used to store a hash-like data
31 structure of keys and values.
37 =item insert [ HASHREF | OPTION => VALUE ... ]
39 Adds this record to the database. If there is an error, returns the error,
40 otherwise returns false.
42 If a list or hash reference of options is supplied, option records are also
47 #false laziness w/queue.pm
51 ( ref($_[0]) eq 'HASH' )
54 warn "FS::option_Common::insert called on $self with options ".
55 join(', ', map "$_ => ".$options->{$_}, keys %$options)
58 local $SIG{HUP} = 'IGNORE';
59 local $SIG{INT} = 'IGNORE';
60 local $SIG{QUIT} = 'IGNORE';
61 local $SIG{TERM} = 'IGNORE';
62 local $SIG{TSTP} = 'IGNORE';
63 local $SIG{PIPE} = 'IGNORE';
65 my $oldAutoCommit = $FS::UID::AutoCommit;
66 local $FS::UID::AutoCommit = 0;
69 my $error = $self->SUPER::insert;
71 $dbh->rollback if $oldAutoCommit;
75 my $pkey = $self->primary_key;
76 my $option_table = $self->option_table;
78 my $namecol = $self->_option_namecol;
79 my $valuecol = $self->_option_valuecol;
81 foreach my $optionname ( keys %{$options} ) {
83 my $optionvalue = $options->{$optionname};
86 $pkey => $self->get($pkey),
87 $namecol => $optionname,
88 $valuecol => ( ref($optionvalue) || $optionvalue ),
91 #my $option_record = eval "new FS::$option_table \$href";
93 # $dbh->rollback if $oldAutoCommit;
96 my $option_record = "FS::$option_table"->new($href);
99 push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
101 $error = $option_record->insert(@args);
103 $dbh->rollback if $oldAutoCommit;
109 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
117 Delete this record from the database. Any associated option records are also
122 #foreign keys would make this much less tedious... grr dumb mysql
125 local $SIG{HUP} = 'IGNORE';
126 local $SIG{INT} = 'IGNORE';
127 local $SIG{QUIT} = 'IGNORE';
128 local $SIG{TERM} = 'IGNORE';
129 local $SIG{TSTP} = 'IGNORE';
130 local $SIG{PIPE} = 'IGNORE';
132 my $oldAutoCommit = $FS::UID::AutoCommit;
133 local $FS::UID::AutoCommit = 0;
136 my $error = $self->SUPER::delete;
138 $dbh->rollback if $oldAutoCommit;
142 my $pkey = $self->primary_key;
143 #my $option_table = $self->option_table;
145 foreach my $obj ( $self->option_objects ) {
146 my $error = $obj->delete;
148 $dbh->rollback if $oldAutoCommit;
153 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
159 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
161 Replaces the OLD_RECORD with this one in the database. If there is an error,
162 returns the error, otherwise returns false.
164 If a list or hash reference of options is supplied, option records are created
172 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
174 : $self->replace_old;
177 my $options_supplied = 0;
178 if ( ref($_[0]) eq 'HASH' ) {
180 $options_supplied = 1;
183 $options_supplied = scalar(@_) ? 1 : 0;
186 warn "FS::option_Common::replace called on $self with options ".
187 join(', ', map "$_ => ". $options->{$_}, keys %$options)
190 local $SIG{HUP} = 'IGNORE';
191 local $SIG{INT} = 'IGNORE';
192 local $SIG{QUIT} = 'IGNORE';
193 local $SIG{TERM} = 'IGNORE';
194 local $SIG{TSTP} = 'IGNORE';
195 local $SIG{PIPE} = 'IGNORE';
197 my $oldAutoCommit = $FS::UID::AutoCommit;
198 local $FS::UID::AutoCommit = 0;
201 my $error = $self->SUPER::replace($old);
203 $dbh->rollback if $oldAutoCommit;
207 my $pkey = $self->primary_key;
208 my $option_table = $self->option_table;
210 my $namecol = $self->_option_namecol;
211 my $valuecol = $self->_option_valuecol;
213 foreach my $optionname ( keys %{$options} ) {
215 warn "FS::option_Common::replace: inserting or replacing option: $optionname"
218 my $oldopt = qsearchs( $option_table, {
219 $pkey => $self->get($pkey),
220 $namecol => $optionname,
223 my $optionvalue = $options->{$optionname};
225 my %oldhash = $oldopt ? $oldopt->hash : ();
229 $pkey => $self->get($pkey),
230 $namecol => $optionname,
231 $valuecol => ( ref($optionvalue) || $optionvalue ),
234 #my $newopt = eval "new FS::$option_table \$href";
236 # $dbh->rollback if $oldAutoCommit;
239 my $newopt = "FS::$option_table"->new($href);
241 my $opt_pkey = $newopt->primary_key;
243 $newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt;
246 push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
248 warn "FS::option_Common::replace: ".
249 ( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" )
251 my $error = $oldopt ? $newopt->replace($oldopt, @args)
252 : $newopt->insert( @args);
254 $dbh->rollback if $oldAutoCommit;
259 #remove extraneous old options
260 if ( $options_supplied ) {
262 grep { !exists $options->{$_->$namecol()} } $old->option_objects
264 my $error = $opt->delete;
266 $dbh->rollback if $oldAutoCommit;
272 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
280 Returns all options as FS::I<tablename>_option objects.
286 my $pkey = $self->primary_key;
287 my $option_table = $self->option_table;
288 qsearch($option_table, { $pkey => $self->get($pkey) } );
293 Returns a list of option names and values suitable for assigning to a hash.
299 my $namecol = $self->_option_namecol;
300 my $valuecol = $self->_option_valuecol;
301 map { $_->$namecol() => $_->$valuecol() } $self->option_objects;
304 =item option OPTIONNAME
306 Returns the option value for the given name, or the empty string.
312 my $pkey = $self->primary_key;
313 my $option_table = $self->option_table;
314 my $namecol = $self->_option_namecol;
315 my $valuecol = $self->_option_valuecol;
317 $pkey => $self->get($pkey),
320 warn "$self -> option: searching for ".
321 join(' / ', map { "$_ => ". $hashref->{$_} } keys %$hashref )
323 my $obj = qsearchs($option_table, $hashref);
324 $obj ? $obj->$valuecol() : '';
330 my $option_table = $self->_option_table;
331 eval "use FS::$option_table";
337 sub _option_table { shift->table .'_option'; }
338 sub _option_namecol { 'optionname'; }
339 sub _option_valuecol { 'optionvalue'; }