1 package FS::option_Common;
4 use vars qw( @ISA $DEBUG );
5 use FS::Record qw( qsearch qsearchs dbh );
7 @ISA = qw( FS::Record );
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 $pkey => $self->get($pkey),
83 $namecol => $optionname,
84 $valuecol => $options->{$optionname},
87 #my $option_record = eval "new FS::$option_table \$href";
89 # $dbh->rollback if $oldAutoCommit;
92 my $option_record = "FS::$option_table"->new($href);
94 $error = $option_record->insert;
96 $dbh->rollback if $oldAutoCommit;
101 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
109 Delete this record from the database. Any associated option records are also
114 #foreign keys would make this much less tedious... grr dumb mysql
117 local $SIG{HUP} = 'IGNORE';
118 local $SIG{INT} = 'IGNORE';
119 local $SIG{QUIT} = 'IGNORE';
120 local $SIG{TERM} = 'IGNORE';
121 local $SIG{TSTP} = 'IGNORE';
122 local $SIG{PIPE} = 'IGNORE';
124 my $oldAutoCommit = $FS::UID::AutoCommit;
125 local $FS::UID::AutoCommit = 0;
128 my $error = $self->SUPER::delete;
130 $dbh->rollback if $oldAutoCommit;
134 my $pkey = $self->primary_key;
135 #my $option_table = $self->option_table;
137 foreach my $obj ( $self->option_objects ) {
138 my $error = $obj->delete;
140 $dbh->rollback if $oldAutoCommit;
145 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
151 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
153 Replaces the OLD_RECORD with this one in the database. If there is an error,
154 returns the error, otherwise returns false.
156 If a list hash reference of options is supplied, part_export_option records are
157 created or modified (see L<FS::part_export_option>).
164 my $old = ( ref($_[0]) eq ref($self) )
166 : $self->replace_old;
169 ( ref($_[0]) eq 'HASH' )
172 warn "FS::option_Common::replace called on $self with options ".
173 join(', ', map "$_ => ". $options->{$_}, keys %$options)
176 local $SIG{HUP} = 'IGNORE';
177 local $SIG{INT} = 'IGNORE';
178 local $SIG{QUIT} = 'IGNORE';
179 local $SIG{TERM} = 'IGNORE';
180 local $SIG{TSTP} = 'IGNORE';
181 local $SIG{PIPE} = 'IGNORE';
183 my $oldAutoCommit = $FS::UID::AutoCommit;
184 local $FS::UID::AutoCommit = 0;
187 my $error = $self->SUPER::replace($old);
189 $dbh->rollback if $oldAutoCommit;
193 my $pkey = $self->primary_key;
194 my $option_table = $self->option_table;
196 my $namecol = $self->_option_namecol;
197 my $valuecol = $self->_option_valuecol;
199 foreach my $optionname ( keys %{$options} ) {
201 warn "FS::option_Common::replace: inserting or replacing option: $optionname"
204 my $oldopt = qsearchs( $option_table, {
205 $pkey => $self->get($pkey),
206 $namecol => $optionname,
210 $pkey => $self->get($pkey),
211 $namecol => $optionname,
212 $valuecol => $options->{$optionname},
215 #my $newopt = eval "new FS::$option_table \$href";
217 # $dbh->rollback if $oldAutoCommit;
220 my $newopt = "FS::$option_table"->new($href);
222 my $opt_pkey = $newopt->primary_key;
224 $newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt;
225 warn "FS::option_Common::replace: ".
226 ( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" )
228 my $error = $oldopt ? $newopt->replace($oldopt) : $newopt->insert;
230 $dbh->rollback if $oldAutoCommit;
235 #remove extraneous old options
237 grep { !exists $options->{$_->$namecol()} } $old->option_objects
239 my $error = $opt->delete;
241 $dbh->rollback if $oldAutoCommit;
246 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
254 Returns all options as FS::I<tablename>_option objects.
260 my $pkey = $self->primary_key;
261 my $option_table = $self->option_table;
262 qsearch($option_table, { $pkey => $self->get($pkey) } );
267 Returns a list of option names and values suitable for assigning to a hash.
273 my $namecol = $self->_option_namecol;
274 my $valuecol = $self->_option_valuecol;
275 map { $_->$namecol() => $_->$valuecol() } $self->option_objects;
278 =item option OPTIONNAME
280 Returns the option value for the given name, or the empty string.
286 my $pkey = $self->primary_key;
287 my $option_table = $self->option_table;
288 my $namecol = $self->_option_namecol;
289 my $valuecol = $self->_option_valuecol;
291 $pkey => $self->get($pkey),
294 warn "$self -> option: searching for ".
295 join(' / ', map { "$_ => ". $hashref->{$_} } keys %$hashref )
297 my $obj = qsearchs($option_table, $hashref);
298 $obj ? $obj->$valuecol() : '';
304 my $option_table = $self->_option_table;
305 eval "use FS::$option_table";
311 sub _option_table { shift->table .'_option'; }
312 sub _option_namecol { 'optionname'; }
313 sub _option_valuecol { 'optionvalue'; }