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 hash reference of options is supplied, option records are created or
172 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
174 : $self->replace_old;
177 ( ref($_[0]) eq 'HASH' )
181 warn "FS::option_Common::replace called on $self with options ".
182 join(', ', map "$_ => ". $options->{$_}, keys %$options)
185 local $SIG{HUP} = 'IGNORE';
186 local $SIG{INT} = 'IGNORE';
187 local $SIG{QUIT} = 'IGNORE';
188 local $SIG{TERM} = 'IGNORE';
189 local $SIG{TSTP} = 'IGNORE';
190 local $SIG{PIPE} = 'IGNORE';
192 my $oldAutoCommit = $FS::UID::AutoCommit;
193 local $FS::UID::AutoCommit = 0;
196 my $error = $self->SUPER::replace($old);
198 $dbh->rollback if $oldAutoCommit;
202 my $pkey = $self->primary_key;
203 my $option_table = $self->option_table;
205 my $namecol = $self->_option_namecol;
206 my $valuecol = $self->_option_valuecol;
208 foreach my $optionname ( keys %{$options} ) {
210 warn "FS::option_Common::replace: inserting or replacing option: $optionname"
213 my $oldopt = qsearchs( $option_table, {
214 $pkey => $self->get($pkey),
215 $namecol => $optionname,
218 my $optionvalue = $options->{$optionname};
220 my %oldhash = $oldopt ? $oldopt->hash : ();
224 $pkey => $self->get($pkey),
225 $namecol => $optionname,
226 $valuecol => ( ref($optionvalue) || $optionvalue ),
229 #my $newopt = eval "new FS::$option_table \$href";
231 # $dbh->rollback if $oldAutoCommit;
234 my $newopt = "FS::$option_table"->new($href);
236 my $opt_pkey = $newopt->primary_key;
238 $newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt;
241 push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
243 warn "FS::option_Common::replace: ".
244 ( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" )
246 my $error = $oldopt ? $newopt->replace($oldopt, @args)
247 : $newopt->insert( @args);
249 $dbh->rollback if $oldAutoCommit;
254 #remove extraneous old options
256 grep { !exists $options->{$_->$namecol()} } $old->option_objects
258 my $error = $opt->delete;
260 $dbh->rollback if $oldAutoCommit;
265 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
273 Returns all options as FS::I<tablename>_option objects.
279 my $pkey = $self->primary_key;
280 my $option_table = $self->option_table;
281 qsearch($option_table, { $pkey => $self->get($pkey) } );
286 Returns a list of option names and values suitable for assigning to a hash.
292 my $namecol = $self->_option_namecol;
293 my $valuecol = $self->_option_valuecol;
294 map { $_->$namecol() => $_->$valuecol() } $self->option_objects;
297 =item option OPTIONNAME
299 Returns the option value for the given name, or the empty string.
305 my $pkey = $self->primary_key;
306 my $option_table = $self->option_table;
307 my $namecol = $self->_option_namecol;
308 my $valuecol = $self->_option_valuecol;
310 $pkey => $self->get($pkey),
313 warn "$self -> option: searching for ".
314 join(' / ', map { "$_ => ". $hashref->{$_} } keys %$hashref )
316 my $obj = qsearchs($option_table, $hashref);
317 $obj ? $obj->$valuecol() : '';
323 my $option_table = $self->_option_table;
324 eval "use FS::$option_table";
330 sub _option_table { shift->table .'_option'; }
331 sub _option_namecol { 'optionname'; }
332 sub _option_valuecol { 'optionvalue'; }