Hi,
I have decided to go about this in a slightly different way, the following plugin sub does what I want without any errors.
BUT !!!!
I know it is stupid to add and delete a record just to see if it passes the error check (see blue code) but I don't know what code to use to check the errors without actually adding a record.
Can someone please give me the correct error checking code to replace my (blue) code. I want to keep it all in one sub if possible.
Thanks.
Simon.
sub split_add_record {
# -------------------------------------------------------------------
# This subroutine will get called whenever the hook 'add_record'
# is run. You should call GT::Plugins->action ( STOP ) if you don't
# want the regular code to run, otherwise the code will continue as
# normal.
#
#----------------------------------------------------------------------
my $self = shift;
# Do something useful here
# Ignores the plugin if this is not contact table
($self->{cgi}->{db} ne 'public_holiday') and return @_;
GT::Plugins->action ( STOP );
return $self->home($self->_language('PER_ADD')) unless ( $self->{user}->{add_p} );
#------------demo code----------------
# add data to related table
return $self->sadd_record() if ( $self->{cgi}->{sdb} and $self->{cgi}->{sdo} );
# Turn arrays into delimited fields
$self->format_insert_cgi;
if ( $self->{cfg}->{'auth_user_field'} ) {
$self->{cgi}->{$self->{cfg}->{'auth_user_field'}} = $self->{user}->{'Username'};
}
# Check foreign keys
my $msg = $self->_check_fk();
($msg) and return $self->add_form($msg);
# Setup the language for GT::SQL.
local $GT::SQL::ERRORS->{ILLEGALVAL} = $self->_language('ADD_ILLEGALVAL');
local $GT::SQL::ERRORS->{UNIQUE} = $self->_language('ADD_UNIQUE');
local $GT::SQL::ERRORS->{NOTNULL} = $self->_language('ADD_NOTNULL');
#check for error(s) on the un-split form input ('public_holiday_202')
#form input format - mm/dd,mm/dd,mm/dd, etc..........
#if there are no errors add the un-split record
#else there are error(s) return the add_form with the error message
if ( defined (my $ret = $self->{db}->add($self->{cgi})) ) {
#delete the unsplit record
my $delete_split_record = $DB->table('public_holiday');
my $split_record_id = $delete_split_record->select ({'public_holiday_owner_id' => $self->{user}->{'Username'}, 'public_holiday_201' => $self->{cgi}->{public_holiday_201}, 'public_holiday_202' => $self->{cgi}->{public_holiday_202} }, ['public_holiday_id'])->fetchrow_array();
$delete_split_record->delete($split_record_id);
#There are no errors on the form input so go ahead and add each date seperatly
my $splitdate;
my @dates = split ',', $IN->param('public_holiday_202');
foreach $splitdate (@dates) {
$self->{cgi}->{public_holiday_202}=$splitdate;
if ( defined (my $ret = $self->{db}->add($self->{cgi})) ) {
$self->auth_logging('add record ') if ( $self->{cfg}->{log_file} );
}
else {
local $^W;
my $error = $GT::SQL::error;
$error =~ s/\n/<br>\n<li>/g;
$self->add_form("<font color=red><ul><li>$error</ul></font>");
}
#end for
}
##Let's find the ID number of the last record added so we can parse it to the add_success
##we will combine all the dates for the record.html using a seperate global
my ($last_record_added) = $DB->table('public_holiday')->select({'public_holiday_owner_id' => $self->{user}->{'Username'}, 'public_holiday_201' => $self->{cgi}->{public_holiday_201}, 'public_holiday_202' => $self->{cgi}->{public_holiday_202} }, ['public_holiday_id'])->fetchrow_array();
##Call up the success page by parsing the ID number of the last record added
$self->add_success($last_record_added);
}
else {
local $^W;
my $error = $GT::SQL::error;
$error =~ s/\n/<br>\n<li>/g;
return $self->add_form("<font color=red><ul><li>$error</ul></font>");
}
#end sub
}
I have decided to go about this in a slightly different way, the following plugin sub does what I want without any errors.
BUT !!!!
I know it is stupid to add and delete a record just to see if it passes the error check (see blue code) but I don't know what code to use to check the errors without actually adding a record.
Can someone please give me the correct error checking code to replace my (blue) code. I want to keep it all in one sub if possible.
Thanks.
Simon.
sub split_add_record {
# -------------------------------------------------------------------
# This subroutine will get called whenever the hook 'add_record'
# is run. You should call GT::Plugins->action ( STOP ) if you don't
# want the regular code to run, otherwise the code will continue as
# normal.
#
#----------------------------------------------------------------------
my $self = shift;
# Do something useful here
# Ignores the plugin if this is not contact table
($self->{cgi}->{db} ne 'public_holiday') and return @_;
GT::Plugins->action ( STOP );
return $self->home($self->_language('PER_ADD')) unless ( $self->{user}->{add_p} );
#------------demo code----------------
# add data to related table
return $self->sadd_record() if ( $self->{cgi}->{sdb} and $self->{cgi}->{sdo} );
# Turn arrays into delimited fields
$self->format_insert_cgi;
if ( $self->{cfg}->{'auth_user_field'} ) {
$self->{cgi}->{$self->{cfg}->{'auth_user_field'}} = $self->{user}->{'Username'};
}
# Check foreign keys
my $msg = $self->_check_fk();
($msg) and return $self->add_form($msg);
# Setup the language for GT::SQL.
local $GT::SQL::ERRORS->{ILLEGALVAL} = $self->_language('ADD_ILLEGALVAL');
local $GT::SQL::ERRORS->{UNIQUE} = $self->_language('ADD_UNIQUE');
local $GT::SQL::ERRORS->{NOTNULL} = $self->_language('ADD_NOTNULL');
#check for error(s) on the un-split form input ('public_holiday_202')
#form input format - mm/dd,mm/dd,mm/dd, etc..........
#if there are no errors add the un-split record
#else there are error(s) return the add_form with the error message
if ( defined (my $ret = $self->{db}->add($self->{cgi})) ) {
#delete the unsplit record
my $delete_split_record = $DB->table('public_holiday');
my $split_record_id = $delete_split_record->select ({'public_holiday_owner_id' => $self->{user}->{'Username'}, 'public_holiday_201' => $self->{cgi}->{public_holiday_201}, 'public_holiday_202' => $self->{cgi}->{public_holiday_202} }, ['public_holiday_id'])->fetchrow_array();
$delete_split_record->delete($split_record_id);
#There are no errors on the form input so go ahead and add each date seperatly
my $splitdate;
my @dates = split ',', $IN->param('public_holiday_202');
foreach $splitdate (@dates) {
$self->{cgi}->{public_holiday_202}=$splitdate;
if ( defined (my $ret = $self->{db}->add($self->{cgi})) ) {
$self->auth_logging('add record ') if ( $self->{cfg}->{log_file} );
}
else {
local $^W;
my $error = $GT::SQL::error;
$error =~ s/\n/<br>\n<li>/g;
$self->add_form("<font color=red><ul><li>$error</ul></font>");
}
#end for
}
##Let's find the ID number of the last record added so we can parse it to the add_success
##we will combine all the dates for the record.html using a seperate global
my ($last_record_added) = $DB->table('public_holiday')->select({'public_holiday_owner_id' => $self->{user}->{'Username'}, 'public_holiday_201' => $self->{cgi}->{public_holiday_201}, 'public_holiday_202' => $self->{cgi}->{public_holiday_202} }, ['public_holiday_id'])->fetchrow_array();
##Call up the success page by parsing the ID number of the last record added
$self->add_success($last_record_added);
}
else {
local $^W;
my $error = $GT::SQL::error;
$error =~ s/\n/<br>\n<li>/g;
return $self->add_form("<font color=red><ul><li>$error</ul></font>");
}
#end sub
}