Home | Wiki | OI 1.x Docs | OI 2.x Docs |
OpenInteract2::Action::Common - Base class for common functionality
package OpenInteract2::Action::CommonSearch; use base qw( OpenInteract2::Action::Common );
This class is a subclass of OpenInteract2::Action and for now mostly provides placeholder methods to signal that an action does not implement certain common methods. It also has a few common functions as well. All common actions should subclass this class so that any inadvertent calls to other common methods get caught and a decent (if terse) message is returned. For instance, say I did this:
package OpenInteract2::Action::MyAction; use strict; use base qw( OpenInteract2::Action::CommonSearch );
and in my search results template I had:
<p>Your search results:</p> <ul> [% FOREACH record = records; display_url = OI.action.create_url( TASK = 'display', my_id = record.id ); %] <li><a href="[% display_url %]">[% record.title %]</li> [% END %] </ul>
Since I haven't inherited a 'display' task or defined one myself, when I click on the created link I can expect an ugly error message from the dispatcher telling me that the task does not exist. Instead, I'll get something like:
Display capability is not built into action 'foo'.
It also leaves us an option for locating future common functionality.
_common_fetch_object( [ $id ] )
Fetches an object of the type defined in the c_object_type
parameter. If an ID value is not passed in it looks for the ID using
the same algorithm found in _common_check_id
-- so you should run
that methods in your task initialization before calling this.
Returns: This method returns an object or throws an exception. If we encounter an error while fetching the object we add to the action parameter 'error_msg' stating the error and wrap the error in the appropriate OpenInteract2::Exception object and rethrow it. Appropriate: if we cannot fetch an object due to security we throw an OpenInteract2::Exception::Security exception.
If an object is not retrieved due to an ID value not being found or a matching object not being found, a new (empty) object is returned.
_common_assign_properties( $object, \%field_info )
Assign values from HTTP request into $object
as declared by
\%field_info
. The data in \%field_info
tells us the names and
types of data we'll be setting in the object. You can learn more about
the different types of parameters we're reading in the various
param_*
methods in
OpenInteract2::Request.
standard ($ or \@)
Fields that get copied as-is from the request data. (See OpenInteract2::Request/param.)
toggled ($ or \@)
Fields that get set to 'yes' if any data passed for the field, 'no' otherwise. (See OpenInteract2::Request/param_toggled.)
date ($ or \@)
Date fields. These are set to a DateTime object assuming
that we can build a date properly from the input data. (See
date_format
if you want to parse a single field, and also
OpenInteract2::Request/param_date.)
datetime ($ or \@)
Datetime fields. These are set to a DateTime object
assuming that we can build a date and time properly from the input
data. (See date_format
if you want to parse a single field, and
also OpenInteract2::Request/param_date.)
date_format ($)
The strptime
format for all date fields. (See
DateTime::Format::Strptime)
datetime_format ($)
The strptime
format for all datetime fields. (See
DateTime::Format::Strptime)
The following example will set in $object
the normal fields
'first_name' and 'last_name', the date field 'birth_date' (formatted
in the standard 'yyyy-mm-dd' format) and the toggled field 'opt_in':
$self->_common_assign_properties( $object, { standard => [ 'first_name', 'last_name' ], toggled => 'opt_in', date => 'birth_date', date_format => '%Y-%m-%d' } );
This class has a number of methods that subclasses can call to check
parameters. Each method returns the number of errors found (0 is
good). It also deposits a message in the error_msg
action parameter
so you and the user can find out what happened.
_common_check_object_class()
Ensures the parameter c_object_type
is present and refers to a
valid object class as returned by the context. We check the latter
condition like this:
my $object_class = eval { CTX->lookup_object( $object_type ) };
If nothing is returned or the lookup_object()
method throws an
exception the condition fails.
If both conditions are true we set the parameter c_object_class
so
you don't need to do the lookup yourself.
_common_check_id_field()
Ensures the object class (set in c_object_class
) has an ID field
specified. (Since we depend on c_object_class
you should run the
_common_check_object_class()
check first.) We check the ID field
from the class with:
my $object_class = $self->param( 'c_object_class' ); my $id_field = eval { $object_class->id_field };
If no ID field is returned or the method throws an exception the condition fails.
If the condition succeeds we set the parameter c_id_field
so you
don't need to do the lookup yourself.
_common_check_id()
Tries to find the ID for an object using a number of methods. We
depend on the c_id_field
parameter being set, so you should run
_common_check_id_field
before this check.
Here's how we find the ID, in order.
Is there an action parameter with the name c_id
?
Is there an action parameter with the same name as the ID field?
Is there a request parameter with the same name as the ID field?
Is there a request parameter with the name 'id'?
The first check that finds an ID is used. If no ID is found the check
fails. If an ID is found it's set in the action parameter c_id
so
you don't need to do the lookup.
_common_check_template_specified( @template_parameters )
Check to see that each of @template_parameters
-- an error message
is generated for each one that is not.
No side effects.
_common_check_param( @params )
Just check that each one of @params
is defined -- an error message
is generated for each one that is not. If you want to check that a
template is defined you should use
_common_check_template_specified()
since it provides a better error
message.
No side effects.
_common_set_defaults( \%defaults )
Treats each key/value pair in \%defaults
as default action
parameters to set.
common_error
Displays any error messages set in your action using the template
returned from _common_error_template
.
Example:
if ( $flubbed_up ) { $self->param_add( error_msg => 'Something is flubbed up' ); $self->task( 'common_error' ); return $self->execute; }
You could also use a shortcut:
if ( $flubbed_up ) { $self->param_add( error_msg => 'Something is flubbed up' ); return $self->execute({ task => 'common_error' }); }
_common_error_template
Returns a fully-qualified template name for when your action
encounters an error. By default this is defined as
common_action_error
, but you can also override this method and
define it yourself. If you do should take the same parameters as the
global error_message
template.
OpenInteract2::Action::CommonAdd
OpenInteract2::Action::CommonDisplay
OpenInteract2::Action::CommonRemove
OpenInteract2::Action::CommonSearch
OpenInteract2::Action::CommonUpdate
Copyright (c) 2003 Chris Winters. All rights reserved.
Chris Winters <chris@cwinters.com>
Generated from the OpenInteract 1.99_03 source.