Apache-Wyrd

 view release on metacpan or  search on metacpan

Wyrd/SQLForm.pm  view on Meta::CPAN

use 5.006;
use strict;
use warnings;
no warnings qw(uninitialized);

package Apache::Wyrd::SQLForm;
our $VERSION = '0.98';
use base qw(Apache::Wyrd::Form);
use Apache::Wyrd::Services::SAK qw(:db);
use warnings qw(all);
no warnings qw(uninitialized);

=pod

=head1 NAME

Apache::Wyrd::SQLForm - General Form Wyrd for editing data in SQL

=head1 SYNOPSIS

    <BASENAME::SQLForm index="user_id" table="users">
      <BASENAME::Form::Template name="password">
        <BASENAME::Form::Preload>
          <BASENAME::Defaults>
            select 'root' as user_id;
          </BASENAME::Defaults>
          <BASENAME::Query>
            select user_id from users where name='Groucho'
          </BASENAME::Query>
        </BASENAME::Form::Preload>
        <b>Enter Password:</b><br>
        <BASENAME::Input name="password" type="password" />
        <BASENAME::Input name="user_id" type="hidden" />
      </BASENAME::Form::Template>
      <BASENAME::Form::Template name="result">
        <H1>Status: $:_status</H1>
        <HR>
        <P>$:_message</P>
      </BASENAME::Form::Template>
    </BASENAME::SQLForm>

=head1 DESCRIPTION

The SQLForm is a subclass of Apache::Wyrd::Form.  It is meant to simplify
the creation of forms that are used to edit data within a database connected
to via the C<Apache::Wyrd::DBL>.

The SQLForm makes the assumption that there is a primary table on which the
edit is operating and that other tables will, if necessary, have elements
inserted, changed or deleted from them as they relate to the primary table.

This module is meant to be subclassed, so a large number of its methods are
only defined in order to be overridden.  Any changes to secondary tables,
for example, need to be handled by subclassing C<_prep_secondary>,
C<_submit_secondary>, and C<_perform_secondary_deletes>, all of which do
nothing by default.

=head2 HTML ATTRIBUTES

=over

=item index

The index column of the primary table, i.e. the name of the primary key column.

=item table

The name of the primary table.

=back

=head2 PERL METHODS

I<(format: (returns) name (arguments after self))>

=over

=item (scalar) C<cancelled> (void)

Determine if the action has been cancelled.  Defaults to assuming the form is cancelled if the action parameter is set to B<cancel>.

=cut

sub cancelled {
	my ($self) = @_;
	if ($self->dbl->param('action') eq 'cancel') {
		$self->_set_feedback('Cancelled', '<BR>No changes were made to the Record');
		return 1;
	}
	return;
}

=item (scalar) C<index> (void)

Returns the name of the primary key column, or index.

=cut

sub index {
	my ($self) = @_;
	return $self->{'index'};
}

=item (scalar) C<table> (void)

Returns the name of the primary table.

=cut

Wyrd/SQLForm.pm  view on Meta::CPAN

results, and these results will be fed back to the user as a log. 
Defaults to undef.

=cut

sub default_log {
	my ($self) = @_;
	return;
}

=item (scalar) C<default_insert_error> (scalar)

Format the error that occurs when there has been a database error during
an insert command.  The error itself is the argument.

=cut

sub default_insert_error {
	my ($self, $err) = @_;
	return "<BR>Unable to create the record: $err";
}

=item (scalar) C<default_update_error> (scalar)

Format the error that occurs when there has been a database error during
an update command.  The error itself is the argument.

=cut

sub default_update_error {
	my ($self, $err) = @_;
	return "<BR>Unable to update the record: $err";
}

=item (scalar) C<default_insert_ok> (void)

Format the log entry that occurs when there has been a successful insert
command.

=cut

sub default_insert_ok {
	my ($self) = @_;
	return "<BR>The record was successfully created";
}

=item (scalar) C<default_update_ok> (void)

Format the log entry that occurs when there has been a successful update
command.

=cut

sub default_update_ok {
	my ($self) = @_;
	return "<BR>The record was successfully updated";
}

=item (scalar) C<primary_delete_error> (void)

Format the log entry that occurs when the edited record cannot be
deleted.  The error is the argument.

=cut

sub primary_delete_error {
	my ($self, $err) = @_;
	return "Falied to delete the record: $err";
}

=item (scalar) C<deleted> (void)

Checks to see if the B<action> CGI parameter is set to "really_delete", and
if so, deletes the record from the primary table and calls the
C<_perform_secondary_deletes> method to remove associated records in
secondary tables.  Returns a 1 if the deletion occurred, undef otherwise.

=cut

sub deleted {
	my ($self) = @_;
	my $table = $self->table;
	my $index = $self->index;
	$self->_raise_exception('table and index need to be provided in an SQLForm') unless ($table and $index);
	my %var = %{$self->{'_variables'}};
	my ($log) = $self->default_log;
	my ($log_title) = $self->default_ok;
	if ($self->dbl->param('action') eq 'really_delete') {
		if ($var{$index}) {#Delete it only if it actually exists
			my $sh = $self->do_query("delete from $table where $index=\$:$index", \%var);
			my $err = $sh->errstr;
			if ($err) {
				$log_title = $self->default_error;
				$log .= $self->primary_delete_error($err);
			} else {
				$self->_deleted_hook({table=>$table,column=>$index,value=>$var{$index}});
				my ($log_title, $addendum) = $self->_perform_secondary_deletes;
				$log .= $addendum;
			}
		}
		$self->_set_feedback($log_title, $log);
		return 1;
	}
	return;
}

=item (void) C<_prep_submission> (void)

Hook method for preparing the data submission.  Performed prior to
submitting, but after a check for deletions.  Does nothing by default.

=cut

sub _prep_submission {
	my ($self) = @_;
	$self->_join_sets;
	return;
}

=item (void) C<_prep_secondary> (void)



( run in 1.036 second using v1.01-cache-2.11-cpan-d8267643d1d )