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 $pkey => $self->get($pkey),
84 $namecol => $optionname,
85 $valuecol => $options->{$optionname},
88 #my $option_record = eval "new FS::$option_table \$href";
90 # $dbh->rollback if $oldAutoCommit;
93 my $option_record = "FS::$option_table"->new($href);
95 $error = $option_record->insert;
97 $dbh->rollback if $oldAutoCommit;
102 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
110 Delete this record from the database. Any associated option records are also
115 #foreign keys would make this much less tedious... grr dumb mysql
118 local $SIG{HUP} = 'IGNORE';
119 local $SIG{INT} = 'IGNORE';
120 local $SIG{QUIT} = 'IGNORE';
121 local $SIG{TERM} = 'IGNORE';
122 local $SIG{TSTP} = 'IGNORE';
123 local $SIG{PIPE} = 'IGNORE';
125 my $oldAutoCommit = $FS::UID::AutoCommit;
126 local $FS::UID::AutoCommit = 0;
129 my $error = $self->SUPER::delete;
131 $dbh->rollback if $oldAutoCommit;
135 my $pkey = $self->primary_key;
136 #my $option_table = $self->option_table;
138 foreach my $obj ( $self->option_objects ) {
139 my $error = $obj->delete;
141 $dbh->rollback if $oldAutoCommit;
146 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
152 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
154 Replaces the OLD_RECORD with this one in the database. If there is an error,
155 returns the error, otherwise returns false.
157 If a list hash reference of options is supplied, part_export_option records are
158 created or modified (see L<FS::part_export_option>).
165 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
167 : $self->replace_old;
170 ( ref($_[0]) eq 'HASH' )
174 warn "FS::option_Common::replace called on $self with options ".
175 join(', ', map "$_ => ". $options->{$_}, keys %$options)
178 local $SIG{HUP} = 'IGNORE';
179 local $SIG{INT} = 'IGNORE';
180 local $SIG{QUIT} = 'IGNORE';
181 local $SIG{TERM} = 'IGNORE';
182 local $SIG{TSTP} = 'IGNORE';
183 local $SIG{PIPE} = 'IGNORE';
185 my $oldAutoCommit = $FS::UID::AutoCommit;
186 local $FS::UID::AutoCommit = 0;
189 my $error = $self->SUPER::replace($old);
191 $dbh->rollback if $oldAutoCommit;
195 my $pkey = $self->primary_key;
196 my $option_table = $self->option_table;
198 my $namecol = $self->_option_namecol;
199 my $valuecol = $self->_option_valuecol;
201 foreach my $optionname ( keys %{$options} ) {
203 warn "FS::option_Common::replace: inserting or replacing option: $optionname"
206 my $oldopt = qsearchs( $option_table, {
207 $pkey => $self->get($pkey),
208 $namecol => $optionname,
212 $pkey => $self->get($pkey),
213 $namecol => $optionname,
214 $valuecol => $options->{$optionname},
217 #my $newopt = eval "new FS::$option_table \$href";
219 # $dbh->rollback if $oldAutoCommit;
222 my $newopt = "FS::$option_table"->new($href);
224 my $opt_pkey = $newopt->primary_key;
226 $newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt;
227 warn "FS::option_Common::replace: ".
228 ( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" )
230 my $error = $oldopt ? $newopt->replace($oldopt) : $newopt->insert;
232 $dbh->rollback if $oldAutoCommit;
237 #remove extraneous old options
239 grep { !exists $options->{$_->$namecol()} } $old->option_objects
241 my $error = $opt->delete;
243 $dbh->rollback if $oldAutoCommit;
248 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
256 Returns all options as FS::I<tablename>_option objects.
262 my $pkey = $self->primary_key;
263 my $option_table = $self->option_table;
264 qsearch($option_table, { $pkey => $self->get($pkey) } );
269 Returns a list of option names and values suitable for assigning to a hash.
275 my $namecol = $self->_option_namecol;
276 my $valuecol = $self->_option_valuecol;
277 map { $_->$namecol() => $_->$valuecol() } $self->option_objects;
280 =item option OPTIONNAME
282 Returns the option value for the given name, or the empty string.
288 my $pkey = $self->primary_key;
289 my $option_table = $self->option_table;
290 my $namecol = $self->_option_namecol;
291 my $valuecol = $self->_option_valuecol;
293 $pkey => $self->get($pkey),
296 warn "$self -> option: searching for ".
297 join(' / ', map { "$_ => ". $hashref->{$_} } keys %$hashref )
299 my $obj = qsearchs($option_table, $hashref);
300 $obj ? $obj->$valuecol() : '';
306 my $option_table = $self->_option_table;
307 eval "use FS::$option_table";
313 sub _option_table { shift->table .'_option'; }
314 sub _option_namecol { 'optionname'; }
315 sub _option_valuecol { 'optionvalue'; }