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;
70 $error = $self->check_options($options)
71 || $self->SUPER::insert;
73 $dbh->rollback if $oldAutoCommit;
77 my $pkey = $self->primary_key;
78 my $option_table = $self->option_table;
80 my $namecol = $self->_option_namecol;
81 my $valuecol = $self->_option_valuecol;
83 foreach my $optionname ( keys %{$options} ) {
85 my $optionvalue = $options->{$optionname};
88 $pkey => $self->get($pkey),
89 $namecol => $optionname,
90 $valuecol => ( ref($optionvalue) || $optionvalue ),
93 #my $option_record = eval "new FS::$option_table \$href";
95 # $dbh->rollback if $oldAutoCommit;
98 my $option_record = "FS::$option_table"->new($href);
101 push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
103 $error = $option_record->insert(@args);
105 $dbh->rollback if $oldAutoCommit;
111 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
119 Delete this record from the database. Any associated option records are also
124 #foreign keys would make this much less tedious... grr dumb mysql
127 local $SIG{HUP} = 'IGNORE';
128 local $SIG{INT} = 'IGNORE';
129 local $SIG{QUIT} = 'IGNORE';
130 local $SIG{TERM} = 'IGNORE';
131 local $SIG{TSTP} = 'IGNORE';
132 local $SIG{PIPE} = 'IGNORE';
134 my $oldAutoCommit = $FS::UID::AutoCommit;
135 local $FS::UID::AutoCommit = 0;
138 my $error = $self->SUPER::delete;
140 $dbh->rollback if $oldAutoCommit;
144 my $pkey = $self->primary_key;
145 #my $option_table = $self->option_table;
147 foreach my $obj ( $self->option_objects ) {
148 my $error = $obj->delete;
150 $dbh->rollback if $oldAutoCommit;
155 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
161 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
163 Replaces the OLD_RECORD with this one in the database. If there is an error,
164 returns the error, otherwise returns false.
166 If a list or hash reference of options is supplied, option records are created
174 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
176 : $self->replace_old;
179 my $options_supplied = 0;
180 if ( ref($_[0]) eq 'HASH' ) {
182 $options_supplied = 1;
185 $options_supplied = scalar(@_) ? 1 : 0;
188 warn "FS::option_Common::replace called on $self with options ".
189 join(', ', map "$_ => ". $options->{$_}, keys %$options)
192 local $SIG{HUP} = 'IGNORE';
193 local $SIG{INT} = 'IGNORE';
194 local $SIG{QUIT} = 'IGNORE';
195 local $SIG{TERM} = 'IGNORE';
196 local $SIG{TSTP} = 'IGNORE';
197 local $SIG{PIPE} = 'IGNORE';
199 my $oldAutoCommit = $FS::UID::AutoCommit;
200 local $FS::UID::AutoCommit = 0;
205 if ($options_supplied) {
206 $error = $self->check_options($options);
208 $dbh->rollback if $oldAutoCommit;
213 $error = $self->SUPER::replace($old);
215 $dbh->rollback if $oldAutoCommit;
219 my $pkey = $self->primary_key;
220 my $option_table = $self->option_table;
222 my $namecol = $self->_option_namecol;
223 my $valuecol = $self->_option_valuecol;
225 foreach my $optionname ( keys %{$options} ) {
227 warn "FS::option_Common::replace: inserting or replacing option: $optionname"
230 my $oldopt = qsearchs( $option_table, {
231 $pkey => $self->get($pkey),
232 $namecol => $optionname,
235 my $optionvalue = $options->{$optionname};
237 my %oldhash = $oldopt ? $oldopt->hash : ();
241 $pkey => $self->get($pkey),
242 $namecol => $optionname,
243 $valuecol => ( ref($optionvalue) || $optionvalue ),
246 #my $newopt = eval "new FS::$option_table \$href";
248 # $dbh->rollback if $oldAutoCommit;
251 my $newopt = "FS::$option_table"->new($href);
253 my $opt_pkey = $newopt->primary_key;
255 $newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt;
258 push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
260 warn "FS::option_Common::replace: ".
261 ( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" )
263 my $error = $oldopt ? $newopt->replace($oldopt, @args)
264 : $newopt->insert( @args);
266 $dbh->rollback if $oldAutoCommit;
271 #remove extraneous old options
272 if ( $options_supplied ) {
274 grep { !exists $options->{$_->$namecol()} } $old->option_objects
276 my $error = $opt->delete;
278 $dbh->rollback if $oldAutoCommit;
284 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
290 =item check_options HASHREF
292 This method is called by 'insert' and 'replace' to check the options that were supplied.
294 Return error-message, or false.
296 (In this class, this is a do-nothing routine that always returns false. Override as necessary. No need to call superclass.)
301 my ($self, $options) = @_;
307 Returns all options as FS::I<tablename>_option objects.
313 my $pkey = $self->primary_key;
314 my $option_table = $self->option_table;
315 qsearch($option_table, { $pkey => $self->get($pkey) } );
320 Returns a list of option names and values suitable for assigning to a hash.
326 my $namecol = $self->_option_namecol;
327 my $valuecol = $self->_option_valuecol;
328 map { $_->$namecol() => $_->$valuecol() } $self->option_objects;
331 =item option OPTIONNAME
333 Returns the option value for the given name, or the empty string.
339 my $pkey = $self->primary_key;
340 my $option_table = $self->option_table;
341 my $namecol = $self->_option_namecol;
342 my $valuecol = $self->_option_valuecol;
344 $pkey => $self->get($pkey),
347 warn "$self -> option: searching for ".
348 join(' / ', map { "$_ => ". $hashref->{$_} } keys %$hashref )
350 my $obj = qsearchs($option_table, $hashref);
351 $obj ? $obj->$valuecol() : '';
354 =item option_cacheable OPTIONNAME
356 Same as the option method, but may cache and return a cached value.
357 Good for use within loops; otherwise, probably avoid.
361 sub option_cacheable {
362 my( $self, $name ) = @_;
363 return $self->{option_cache}{$name} if exists $self->{option_cache}{$name};
364 $self->{option_cache}{$name} = $self->option($name,1);
370 my $option_table = $self->_option_table;
371 eval "use FS::$option_table";
377 sub _option_table { shift->table .'_option'; }
378 sub _option_namecol { 'optionname'; }
379 sub _option_valuecol { 'optionvalue'; }