CleanCode Perl Libraries
Multi-Lingual Library Maintainability
available: Perl not available: Java not available: JavaScript not available: Certified
Class
not available: Testable
Class
not available: Standalone
Mode
available: Diagnostic
Enabled

NAME

CGI::Application::Helper - Provides CGI::Application convenience methods.

SYNOPSIS

        use CGI::Application::Helper;
        $cgiAppHelper = CGI::Application::Helper->new($cgiApplication, "admin");
        
        $cgiApplication->run_modes(
                $cgiAppHelper->genDispatchMap("templates", $DISPLAY_METHOD_PREFIX));
        $runmode = $cgiAppHelper->redirectRunMode($LOGIN_MODE,"login required");
        
        $cgiAppHelper->safeTmplParam(dbError => $cgiApplication->param('dbError'));
        $cgiAppHelper->safeTmplParamNoEscape(multipleMessages => \@argList);
        
        $cgiAppHelper->setUserID($form_user_name);
        if ($cgiAppHelper->isAdmin()) ...
        $cgiAppHelper->setOverride($override_name);
        if ($cgiAppHelper->isOverride()) ...
        $cgiAppHelper->clearOverride() if $cgi->param("clearOverride");
        $cgiAppHelper->logout();
        
        $cgiAppHelper->setSimpleMsg($@) if $@;
        $cgiAppHelper->setUserMsg("username or password not found");
        print join(",", $cgiAppHelper->getUserMsg());
        
        $cgiAppHelper->setRevertReason("address and phone must be filled in");
        $cgiAppHelper->getRevertReason();
        
        $diag->print($cgiAppHelper->genResourceTable("templates","/image"));
        $diag->print($cgiAppHelper->dumpSession());
        my $userSessionDetails=$cgiAppHelper->studySessions("sess_dir")->{$userid};
        
        $cgiAppHelper->doEmail($recipient, $subject, $body);
        $cgiAppHelper->upload("proofname", $path, "application/pdf", "pdf");
        $success = $cgiAppHelper->upload("filename", $path,
                "image/(p?jpeg|png|gif)", "jpeg, jpg, png, or gif");
        
        $cgiAppHelper->getTemplateList("template_dir")

REQUIRES

Perl5.005, CGI, CGI::Session::Serialize::Default File::stat, File::Path, File::Basename, Data::Diagnostic, Data::Handy, File::Handy, Net::SimpleMail,

DESCRIPTION

Provides a variety of convenience methods for use with the CGI::Application module.

CLASS VARIABLES

VERSION

Current version of this class.

TMPL_EXTENSION

File extension for template files. (Value = "thtml")

CONSTRUCTOR

new

PACKAGE->new(cgiApp, admin)

Creates a DBI::Helper object.

Parameters:

cgiApp - instance of CGI::Application

admin - admin user name

Returns:

a newly created object

METHODS

param

OBJ->param(name, value)

OBJ->param(name)

Sets or gets the value of a parameter stored in the current instance.

Parameters:

name - string; name of parameter

value - optional; value to assign to parameter

Returns:

current or updated value of the named parameter

setUserID

OBJ->setUserID(user)

Sets the current user ID for use by default where clauses peppered throughout this module. If no argument is provided, no action is taken. This allows passing in a CGI value without the caller checking whether it has a value or not.

Parameters:

user - optional; string; user id

clearUserID

OBJ->clearUserID()

Clears the current user ID established by setUserID.

getUserID

OBJ->getUserID()

Returns the current user ID or empty string if not logged in. If the admin is the actual logged-in user and has effected an override, then the user who's identity has been assumed has been returned.

Parameters:

real - optional; boolean flag to return actual logged in user rather than current assumed user, if different

Returns:

overridden user id, current user ID, or empty string

setOverride

OBJ->setOverride(user)

Sets the admin's user override. If no argument is provided, no action is taken. This allows passing in a CGI value without the caller checking whether it has a value or not. Once set, the system acts as though the specified user is logged in, rather than the admin until such time as the override is cleared.

Parameters:

user - string; user id to assume

clearOverride

OBJ->clearOverride()

Clears the current admin override established by setOverride.

isOverride

OBJ->isOverride()

Identifies if the admin user is in override mode (i.e. assuming the identity of a regular user).

Returns:

true if admin has user override in effect.

isAdmin

OBJ->isAdmin()

Identifies if the current user is an admin user. Even if the admin has assumed the role of a regular user with a user-override, this will still return true; it reports on the actual logged-in user.

Returns:

true if logged in user is the admin user

logout

OBJ->logout()

Logout causes the current user ID to be erased, the user override cleared, and resets the login flag in the session.

setRevertReason

OBJ->setRevertReason(reason)

Stores reason for reverting from entry runmode to a new runmode. At the appropriate time, use getRevertReason to retrieve the message for display.

Parameters:

reason - reason to display to user

getRevertReason

OBJ->getRevertReason()

Returns the previously stored reason to revert the runmode.

Returns:

reversion reason, or a default string 'Validation failed'

genResourceTable

OBJ->genResourceTable(tmplDir, imageDir, dispatchList)

Display diagnostic table of methods, templates, and modes.

Parameters:

tmplDir - string; directory containing templates

imageDir - string; image directory containing required check.gif checkmark.

dispatchList - list of pre-defined run modes

Returns:

HTML table for insertion into diagnostic output

studySessions

OBJ->studySessions(dir)

Organizes data from all session files into a hash organized by user ID. It is quite common that multiple session files will have the same user ID; the latest one will be stored in the resultant hash. Any session files without a user ID (i.e. where the user has not logged in) will be ignored. The object derefenced by user ID is itself a hash, indexed by the various field names in the session. See CGI::Session or invoke dumpSession for details.

Parameters:

dir - string; directory containing session files

Returns:

hash of session data for all sessions

dumpSession

OBJ->dumpSession()

Formats the contents of the current session as a printable map, and wraps inside an HTML div element with a CSS class of dump.

Returns:

Formatted HTML as a string

setUserMsg

OBJ->setUserMsg(msg)

Stores error message for later display on screen.

Parameters:

msg - string to display

setSimpleMsg

OBJ->setSimpleMsg(msg)

Stores error message for later display on screen. This differs from setUserMsg in that if the user is not an administrator, the stored message will simply report a generic error, rather than the supplied message, for reasons of security and user-friendliness.

Parameters:

msg - string to display

getUserMsg

OBJ->getUserMsg()

Return string previously stored by setUserMsg and/or setSimpleMsg (for inserting into template).

Returns:

error string, if any.

redirectRunMode

OBJ->redirectRunMode(toMode, reason)

Redirects run mode either for login purposes, because a particular mode is dictated by user actions, or for failed data validation or other exceptions.

Parameters:

toMode - string; new run mode

reason - string; explanation of why the run mode was redirected (for logging).

Returns:

The new run mode (same as was passed in) for convenience.

genDispatchMap

OBJ->genDispatchMap(tmplDir, methNamePrefix)

Creates a run mode-to-display-method mapping from a list of templates. As a side effect it stores the dispatch list of run modes for other uses.

Parameters:

tmplDir - string; directory containing templates

methNamePrefix - string; prefix for method names for each run mode

Returns:

hash mapping run modes to display methods

upload

OBJ->upload(paramName, path, typeRegExp, typeMsg)

Upload a file from CGI upload field $paramName, storing it on $path specified. The mime type (from the HTTP Content-Type header) must match the regular expression $typeRegExp; if it does not, the supplied english rendition of the regular expression ($typeMsg) is inserted in the error message stored for later display to the user.

Parameters:

paramName - string; CGI field name from which to obtain both the file name and its contents (from HTML input element with file type)

path - string; partial path to store uploaded file; file name from paramName is concatenated to create full path

typeRegExp - string; regular expression specifying allowing mime types, e.g. "image/(p?jpeg|png|gif)"

typeMsg - string; English phrase to add to error message if typeRegExp is not matched, e.g. "jpeg, jpg, png, or gif"

Returns:

boolean success indication

doEmail

OBJ->doEmail(recipient, subject, body)

Send email to $recipient with the specified $subject and $body. The admin defined in the configuration file (ADMIN_EMAIL), if present, is blind-copied on all emails (unless the admin is the recipient). The developer defined in the configuration file (DIAG_EMAIL), if present, is also blind-copied on all emails.

Parameters:

recipient - string; recipient email address

subject - string; contents of subject field

body - string; body of email

safeTmplParam

OBJ->safeTmplParam(paramName, paramValue)

Fill the specified template field safely, escaping any special characters. That is, do not attempt to fill it unless such a field exists (otherwise, a runtime error would occur).

Parameters:

paramName - string; name of template parameter

paramValue - string; value of template parameter

safeTmplParamNoEscape

OBJ->safeTmplParamNoEscape(paramName, paramValue)

Fill the specified template field safely, without escaping any special characters. That is, do not attempt to fill it unless such a field exists (otherwise, a runtime error would occur). This method (contrasted with safeTmplParam) is used when you have pre-formatted HTML to pass in.

Parameters:

paramName - string; name of template parameter

paramValue - string; value of template parameter

getDispatchList

OBJ->getDispatchList(tmplDir)

Returns a list of run modes automatically defined by corresponding templates. This will be the basename of each template ending with the TMPL_EXTENSION but not a partial template (i.e. those included in other templates). Partial templates are designated by basename.inc.ext as compared to basename.ext for complete templates.

Parameters:

tmplDir - string; directory containing templates

Returns:

dispatch list (run modes)

getTemplateList

OBJ->getTemplateList(tmplDir)

Returns list of template files in the specified directory, identified by TMPL_EXTENSION extension.

Parameters:

tmplDir - string; directory containing templates

Returns:

list of existing templates

TO BE DONE

N/A

BUGS

None

AUTHOR

Michael Sorens

VERSION

$Revision: 8 $ $Date: 2006-12-19 21:13:43 -0800 (Tue, 19 Dec 2006) $

SINCE

CleanCode 0.9.05

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 139:

=back doesn't take any parameters, but you said =back -- end of CLASS VARIABLES section

Around line 176:

=back doesn't take any parameters, but you said =back -- end of CONSTRUCTOR section

Around line 1016:

=back doesn't take any parameters, but you said =back -- end of METHOD section


CleanCode Perl Libraries Copyright © 2001-2013 Michael Sorens - Revised 2013.06.30 Get CleanCode at SourceForge.net. Fast, secure and Free Open Source software downloads