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 );
23 FS::option_Common is intended as a base class for classes which have a
24 simple one-to-many class associated with them, used to store a hash-like data
25 structure of keys and values.
31 =item insert [ HASHREF | OPTION => VALUE ... ]
33 Adds this record to the database. If there is an error, returns the error,
34 otherwise returns false.
36 If a list or hash reference of options is supplied, option records are also
41 #false laziness w/queue.pm
45 ( ref($_[0]) eq 'HASH' )
48 warn "FS::option_Common::insert called on $self with options ".
49 join(', ', map "$_ => ".$options->{$_}, keys %$options)
52 local $SIG{HUP} = 'IGNORE';
53 local $SIG{INT} = 'IGNORE';
54 local $SIG{QUIT} = 'IGNORE';
55 local $SIG{TERM} = 'IGNORE';
56 local $SIG{TSTP} = 'IGNORE';
57 local $SIG{PIPE} = 'IGNORE';
59 my $oldAutoCommit = $FS::UID::AutoCommit;
60 local $FS::UID::AutoCommit = 0;
63 my $error = $self->SUPER::insert;
65 $dbh->rollback if $oldAutoCommit;
69 my $pkey = $self->pkey;
70 my $option_table = $self->option_table;
72 foreach my $optionname ( keys %{$options} ) {
74 $pkey => $self->get($pkey),
75 'optionname' => $optionname,
76 'optionvalue' => $options->{$optionname},
79 #my $option_record = eval "new FS::$option_table \$href";
81 # $dbh->rollback if $oldAutoCommit;
84 my $option_record = "FS::$option_table"->new($href);
86 $error = $option_record->insert;
88 $dbh->rollback if $oldAutoCommit;
93 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
101 Delete this record from the database. Any associated option records are also
106 #foreign keys would make this much less tedious... grr dumb mysql
109 local $SIG{HUP} = 'IGNORE';
110 local $SIG{INT} = 'IGNORE';
111 local $SIG{QUIT} = 'IGNORE';
112 local $SIG{TERM} = 'IGNORE';
113 local $SIG{TSTP} = 'IGNORE';
114 local $SIG{PIPE} = 'IGNORE';
116 my $oldAutoCommit = $FS::UID::AutoCommit;
117 local $FS::UID::AutoCommit = 0;
120 my $error = $self->SUPER::delete;
122 $dbh->rollback if $oldAutoCommit;
126 my $pkey = $self->pkey;
127 #my $option_table = $self->option_table;
129 foreach my $obj ( $self->option_objects ) {
130 my $error = $obj->delete;
132 $dbh->rollback if $oldAutoCommit;
137 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
143 =item replace OLD_RECORD [ HASHREF | OPTION => VALUE ... ]
145 Replaces the OLD_RECORD with this one in the database. If there is an error,
146 returns the error, otherwise returns false.
148 If a list hash reference of options is supplied, part_export_option records are
149 created or modified (see L<FS::part_export_option>).
157 ( ref($_[0]) eq 'HASH' )
160 warn "FS::option_Common::insert called on $self with options ".
161 join(', ', map "$_ => ". $options->{$_}, keys %$options)
164 local $SIG{HUP} = 'IGNORE';
165 local $SIG{INT} = 'IGNORE';
166 local $SIG{QUIT} = 'IGNORE';
167 local $SIG{TERM} = 'IGNORE';
168 local $SIG{TSTP} = 'IGNORE';
169 local $SIG{PIPE} = 'IGNORE';
171 my $oldAutoCommit = $FS::UID::AutoCommit;
172 local $FS::UID::AutoCommit = 0;
175 my $error = $self->SUPER::replace($old);
177 $dbh->rollback if $oldAutoCommit;
181 my $pkey = $self->pkey;
182 my $option_table = $self->option_table;
184 foreach my $optionname ( keys %{$options} ) {
185 my $old = qsearchs( $option_table, {
186 $pkey => $self->get($pkey),
187 'optionname' => $optionname,
191 $pkey => $self->get($pkey),
192 'optionname' => $optionname,
193 'optionvalue' => $options->{$optionname},
196 #my $new = eval "new FS::$option_table \$href";
198 # $dbh->rollback if $oldAutoCommit;
201 my $new = "FS::$option_table"->new($href);
203 $new->optionnum($old->optionnum) if $old;
204 my $error = $old ? $new->replace($old) : $new->insert;
206 $dbh->rollback if $oldAutoCommit;
211 #remove extraneous old options
213 grep { !exists $options->{$_->optionname} } $old->option_objects
215 my $error = $opt->delete;
217 $dbh->rollback if $oldAutoCommit;
222 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
230 Returns all options as FS::I<tablename>_option objects.
236 my $pkey = $self->pkey;
237 my $option_table = $self->option_table;
238 qsearch($option_table, { $pkey => $self->get($pkey) } );
243 Returns a list of option names and values suitable for assigning to a hash.
249 map { $_->optionname => $_->optionvalue } $self->option_objects;
252 =item option OPTIONNAME
254 Returns the option value for the given name, or the empty string.
260 my $pkey = $self->pkey;
261 my $option_table = $self->option_table;
263 qsearchs($option_table, {
264 $pkey => $self->get($pkey),
267 $obj ? $obj->optionvalue : '';
273 my $pkey = $self->dbdef_table->primary_key;
278 my $option_table = $self->table . '_option';
279 eval "use FS::$option_table";