CleanCode Perl Libraries |
Home | Perl | Java | PowerShell | C# | SQL | Index | Tools | Download | What's New |
Multi-Lingual Library | Maintainability | ||||||||||||
Perl | Java | JavaScript | Certified Class |
Testable Class |
Standalone Mode |
Diagnostic Enabled |
CGI::Application::Helper - Provides CGI::Application convenience methods.
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")
Perl5.005, CGI, CGI::Session::Serialize::Default File::stat, File::Path, File::Basename, Data::Diagnostic, Data::Handy, File::Handy, Net::SimpleMail,
Provides a variety of convenience methods for use with the CGI::Application
module.
Current version of this class.
File extension for template files. (Value = "thtml")
PACKAGE->new(cgiApp, admin)
Creates a DBI::Helper
object.
cgiApp
- instance of CGI::Application
admin
- admin user name
a newly created object
OBJ->param(name, value)
OBJ->param(name)
Sets or gets the value of a parameter stored in the current instance.
name
- string; name of parameter
value
- optional; value to assign to parameter
current or updated value of the named parameter
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.
user
- optional; string; user id
OBJ->clearUserID()
Clears the current user ID established by setUserID.
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.
real
- optional; boolean flag to return actual logged in user rather than current assumed user, if different
overridden user id, current user ID, or empty string
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.
user
- string; user id to assume
OBJ->clearOverride()
Clears the current admin override established by setOverride.
OBJ->isOverride()
Identifies if the admin user is in override mode (i.e. assuming the identity of a regular user).
true if admin has user override in effect.
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.
true if logged in user is the admin user
OBJ->logout()
Logout causes the current user ID to be erased, the user override cleared, and resets the login flag in the session.
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.
reason
- reason to display to user
OBJ->getRevertReason()
Returns the previously stored reason to revert the runmode.
reversion reason, or a default string 'Validation failed'
OBJ->genResourceTable(tmplDir, imageDir, dispatchList)
Display diagnostic table of methods, templates, and modes.
tmplDir
- string; directory containing templates
imageDir
- string; image directory containing required check.gif checkmark.
dispatchList
- list of pre-defined run modes
HTML table for insertion into diagnostic output
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.
dir
- string; directory containing session files
hash of session data for all sessions
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
.
Formatted HTML as a string
OBJ->setUserMsg(msg)
Stores error message for later display on screen.
msg
- string to display
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.
msg
- string to display
OBJ->getUserMsg()
Return string previously stored by setUserMsg
and/or setSimpleMsg
(for inserting into template).
error string, if any.
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.
toMode
- string; new run mode
reason
- string; explanation of why the run mode was redirected (for logging).
The new run mode (same as was passed in) for convenience.
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.
tmplDir
- string; directory containing templates
methNamePrefix
- string; prefix for method names for each run mode
hash mapping run modes to display methods
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.
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"
boolean success indication
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.
recipient
- string; recipient email address
subject
- string; contents of subject field
body
- string; body of email
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).
paramName
- string; name of template parameter
paramValue
- string; value of template parameter
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.
paramName
- string; name of template parameter
paramValue
- string; value of template parameter
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.
tmplDir
- string; directory containing templates
dispatch list (run modes)
OBJ->getTemplateList(tmplDir)
Returns list of template files in the specified directory, identified by TMPL_EXTENSION extension.
tmplDir
- string; directory containing templates
list of existing templates
N/A
None
Michael Sorens
$Revision: 8 $ $Date: 2006-12-19 21:13:43 -0800 (Tue, 19 Dec 2006) $
CleanCode 0.9.05
Hey! The above document had some coding errors, which are explained below:
=back doesn't take any parameters, but you said =back -- end of CLASS VARIABLES section
=back doesn't take any parameters, but you said =back -- end of CONSTRUCTOR section
=back doesn't take any parameters, but you said =back -- end of METHOD section
Home | Perl | Java | PowerShell | C# | SQL | Index | Tools | Download | What's New |
CleanCode Perl Libraries | Copyright © 2001-2013 Michael Sorens - Revised 2013.06.30 |