Gossamer Forum
Home : Products : DBMan SQL : Development, Plugins and Globals :

Split Form Input Plugin

Quote Reply
Split Form Input Plugin
Hi,

The following question is part of another thread in the main DBManSQL section but I haven't had any feedback yet. As it relates to a plugin, I thought that I would start a new thread here incase anyone tries to do the same thing in the future.

I'm writing a plugin that splits dates entered as 01/21,02/05,03/27,04/30,etc... in one form field and adds them to the database as individual dates.

I got the plugin working using the sub below but I am not sure about how to handle the add_success part indicated in red. It works with (1) but I'm not sure what I should really use. The Home.pm add_record sub uses $self->add_success($ret); where $ret return 1. I'm assuming that the 1 means that the record was added successfully. What can I use to make sure that ALL my records were added successfully????

Any ideas?

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) = @_;
# 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 );
#----------------------------------------------------------------------

my $splitdate;
my @dates = split ',', $IN->param('public_holiday_202');
foreach $splitdate (@dates) {

$self->{cgi}->{public_holiday_202}=$splitdate;


#----------------------------------------------------------------------
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');

if ( defined (my $ret = $self->{db}->add($self->{cgi})) ) {



$self->auth_logging('add record ') if ( $self->{cfg}->{log_file} );

#------------demo code-----------


#Don't call up the success page
###$self->add_success($ret);
}
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
}


$self->add_success(1);

#end sub
}

# Always end with a 1.
1;
Quote Reply
Re: [jai] Split Form Input Plugin In reply to
Anyone have any ideas?????????
Quote Reply
Re: [jai] Split Form Input Plugin In reply to
Hi Simon,

Sorry it took me so long to get to this post cuz we're pretty busy at the moment.
The $ret var should get an ID value (assuming that ID is your primary key with AUTO INCREMENT) just in case you add a record to a table which contains AI field, so it can look up the record you've just added into the database then display them to the form add_success.html

Cheers,
Jean
Gossamer Threads Inc.

Last edited by:

jean: Feb 7, 2003, 11:54 AM
Quote Reply
Re: [jean] Split Form Input Plugin In reply to
Hi Jean,

Thank you for your help. Your answer solved my problem!! Smile

I added the red code below and my plugin seems to work (although, I haven't checked the error side of things yet). This plugin (along with another global that re-combines the dates on the success page) now does what I wanted. My next step is to work out the search and modify side of things.

Thanks again.

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) = @_;
# 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 );
#----------------------------------------------------------------------

my $splitdate;
my @dates = split ',', $IN->param('public_holiday_202');
foreach $splitdate (@dates) {

$self->{cgi}->{public_holiday_202}=$splitdate;


#----------------------------------------------------------------------
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');

if ( defined (my $ret = $self->{db}->add($self->{cgi})) ) {



$self->auth_logging('add record ') if ( $self->{cfg}->{log_file} );

#------------demo code-----------


#Don't call up the success page
###$self->add_success($ret);
}
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

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);



#end sub
}

# Always end with a 1.
1;
Quote Reply
Re: [jean] Split Form Input Plugin In reply to
Hi,

Well I thought I had it working but I have hit problems again with the error side of things.

What I want to do is check the users input before I start to split the dates up and add them seperatly.

So what I did was add the following code to call up another sub (in the same plugin)

&add_check_errors($self->{cgi});

but I keep geting errors.

My add_check_errors sub is basically the same as GT/SQL/Table.pm sub add but I have deleted the part that inserts the record and added the code that prints out the errors.

I'm pretty sure that my problem is something to do with the parsing of form values between the two subs (e.g &add_check_errors($self->{cgi});) but I can't work out what it should be. Why doesn't $self->{cgi} work?

I would appreciate it if someone could help me out.

Here are my two subs -

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) = @_;
# 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');


#Go and check for errors. If everything is ok, come back and continue. If not, print the errors.
&add_check_errors($self->{in});


#Split the the dates and add them 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} );

#Don't call up the success page
###$self->add_success($ret);
}
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

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);

#end sub
}




sub add_check_errors {

#Copied from GT/SQL/Table.pm sub add
# -----------------------------------------------------------
# add()
# IN : hash/hash_ref/cgi/GT::CGI of col => val pairs to add.
# OUT: ID number if auto_incremented table, or undef if failure
#
my $self = shift;
my $input = $self->common_param(@_) or return $self->error ('BADARGS', 'FATAL', '$obj->insert( HASH or HASH_REF or CGI ) only.');
my $table = $self->name or return $self->error ('NOTABLE', 'FATAL');


my $c = $self->{schema}->{cols};
my $ai = $self->{schema}->{ai};
my $err = 0;




# Clear errors.
$self->{_error} = [];
foreach my $col (keys %$c) {
next if ($col eq $ai);
if ($c->{$col}->{not_null} and (!defined $input->{$col} or $input->{$col} !~ /\S/)) {
$self->error ('NOTNULL', 'WARN', $c->{$col}->{form_display} || $col);
$err = 1;
}
}
if ($err) {
if (ref $self->{_error} and @{$self->{_error}}) {
$GT::SQL::error = join "\n", @{$self->{_error}};


#Print the errors or return if there are no errors
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>");
}
}

else { return; }
#end sub


}



Thank you.

Simon.



Quote Reply
Re: [jai] Split Form Input Plugin In reply to
Hi,

I still haven't been able to solve this problem.

Does ANYONE have any idea what value needs to be parsed to my add_check_errors subroutine (shown in blue in my previous post above).

&add_check_errors(????????);

As I said before I thought it should be $self->{cgi} or even $self->{in} or maybe @_ but nothing seems to work.

Simon.
Quote Reply
Re: [jai] Split Form Input Plugin In reply to
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
}

Quote Reply
Re: [jean] Split Form Input Plugin In reply to
Hi Jean (or TheStone),

I know you are busy but I really need help with this one.

Thank you.

Simon.
Quote Reply
Re: [jai] Split Form Input Plugin In reply to
Could you just send me your DbSQL admin info (user/pass) via private message? I'll take a closer look and will be able to help.

Cheers,
Jean
Gossamer Threads Inc.
Quote Reply
Re: [jean] Split Form Input Plugin In reply to
Hi Jean,

This particular database has a heap of different modifications and plugins and by the time you try and work out what I have done I think it would easier if you could just give me the few lines of code that can be used to check for errors. It should be something like the following (which I took from GT/SQL/Table.pm). How can I modify this to work within the blue area of my sub (just above my last post).

Thanks

Simon.

# Clear errors.
$self->{_error} = [];
foreach my $col (keys %$c) {
next if ($col eq $ai);
if ($c->{$col}->{not_null} and (!defined $input->{$col} or $input->{$col} !~ /\S/)) {
$self->error ('NOTNULL', 'WARN', $c->{$col}->{form_display} || $col);
$err = 1;
}
}
if ($err) {
if (ref $self->{_error} and @{$self->{_error}}) {
$GT::SQL::error = join "\n", @{$self->{_error}};
return;
}
}

Quote Reply
Re: [jai] Split Form Input Plugin In reply to
OK, I did it !!

Instead of adding a record just to test it's validity I wrote my own piece of error testing code for the form input (which are dates in the format mm/dd,mm/dd,..etc.). If the form input passes my error checking the plugin goes ahead and splits each date set then adds them seperatly. The split date sets are tested by the normal add process.

Simon.