package FS::option_Common;
use strict;
-use vars qw( @ISA $DEBUG );
+use base qw( FS::Record );
+use vars qw( $DEBUG );
+use Carp qw( cluck );
use Scalar::Util qw( blessed );
use FS::Record qw( qsearch qsearchs dbh );
-@ISA = qw( FS::Record );
-
$DEBUG = 0;
=head1 NAME
local $FS::UID::AutoCommit = 0;
my $dbh = dbh;
- my $error = $self->SUPER::insert;
+ my $error;
+
+ $error = $self->check_options($options)
+ || $self->SUPER::insert;
if ( $error ) {
$dbh->rollback if $oldAutoCommit;
return $error;
my $oldAutoCommit = $FS::UID::AutoCommit;
local $FS::UID::AutoCommit = 0;
my $dbh = dbh;
-
- my $error = $self->SUPER::delete;
- if ( $error ) {
- $dbh->rollback if $oldAutoCommit;
- return $error;
- }
-
+
my $pkey = $self->primary_key;
#my $option_table = $self->option_table;
}
}
+ my $error = $self->SUPER::delete;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+
$dbh->commit or die $dbh->errstr if $oldAutoCommit;
'';
local $FS::UID::AutoCommit = 0;
my $dbh = dbh;
- my $error = $self->SUPER::replace($old);
+ my $error;
+
+ if ($options_supplied) {
+ $error = $self->check_options($options);
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+ }
+
+ $error = $self->SUPER::replace($old);
if ( $error ) {
$dbh->rollback if $oldAutoCommit;
return $error;
}
+=item check_options HASHREF
+
+This method is called by 'insert' and 'replace' to check the options that were supplied.
+
+Return error-message, or false.
+
+(In this class, this is a do-nothing routine that always returns false. Override as necessary. No need to call superclass.)
+
+=cut
+
+sub check_options {
+ my ($self, $options) = @_;
+ '';
+}
+
=item option_objects
Returns all options as FS::I<tablename>_option objects.
$pkey => $self->get($pkey),
$namecol => shift,
};
- warn "$self -> option: searching for ".
+ cluck "$self -> option: searching for ".
join(' / ', map { "$_ => ". $hashref->{$_} } keys %$hashref )
if $DEBUG;
my $obj = qsearchs($option_table, $hashref);
$obj ? $obj->$valuecol() : '';
}
+=item option_cacheable OPTIONNAME
+
+Same as the option method, but may cache and return a cached value.
+Good for use within loops; otherwise, probably avoid.
+
+=cut
+
+sub option_cacheable {
+ my( $self, $name ) = @_;
+ return $self->{option_cache}{$name} if exists $self->{option_cache}{$name};
+ $self->{option_cache}{$name} = $self->option($name,1);
+}
+
sub option_table {
my $self = shift;