Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- File List
- File Members

# FormTool.php

Go to the documentation of this file.

    00001 <?PHP
    00002
    00003 #
    00004 #   FILE:  FormTool.php
    00005 #
    00006 #   Part of the Collection Workflow Integration System (CWIS)
    00007 #   Copyright 2006-2011 Edward Almasy and Internet Scout
    00008 #   http://scout.wisc.edu
    00009 #
    00010
    00011 class FormTool {
    00012
    00013     # ---- PUBLIC INTERFACE --------------------------------------------------
    00014
    00015     # object constructor
    00016     function FormTool($FormFields, $AdditionalErrorMessages = NULL)
    00017     {
    00018         # save field info with field name as index
    00019         foreach ($FormFields as $Field)
    00020         {
    00021             $this->Fields[$Field->Name()] = $Field;
    00022         }
    00023
    00024         # save additional error messages (if any)
    00025         $this->AdditionalErrorMessages = $AdditionalErrorMessages;
    00026
    00027         # set default error color
    00028         $this->ErrorColor = "red";
    00029
    00030         # save any additional fields indicated to be marked for error
    00031         if (isset($_GET["FTAddErrFields"]) && count($_GET["FTAddErrFields"]))
    00032         {
    00033             $this->AdditionalErrorFields = explode("!", $_GET["FTAddErrFields"]);
    00034         }
    00035         else
    00036         {
    00037             $this->AdditionalErrorFields = array();
    00038         }
    00039     }
    00040
    00041     # get/set field values
    00042     function ValueForField($FieldName, $NewValue = NULL)
    00043     {
    00044         return $this->Fields[$FieldName]->Value($NewValue);
    00045     }
    00046
    00047     # checks whether all required form variables are set in $_POST
    00048     function AllRequiredVarsAreSet()
    00049     {
    00050         # assume that all required variables will be found
    00051         $AllFound = TRUE;
    00052
    00053         # for each required form variable
    00054         foreach ($this->Fields as $FieldName => $Field)
    00055         {
    00056             # if variable is not set in $_POST
    00057             if (!isset($_POST[$FieldName]) || !strlen($_POST[$FieldName]))
    00058             {
    00059                 # set flag indicating we found a missing var and exit loop
    00060                 $AllFound = FALSE;
    00061                 break;
    00062             }
    00063         }
    00064
    00065         # report back to caller whether we found on required vars
    00066         return $AllFound;
    00067     }
    00068
    00069     # return URL parameter string with form values that are set (from $_POST)
    00070     # (returns empty string if none of the form values are set)
    00071     # (URL parameter string does not include leading separator (? or &))
    00072     function GetValsAsUrlParams($IncludePasswords = FALSE)
    00073     {
    00074         # assume no values will be found
    00075         $UrlParams = "";
    00076
    00077         # for each form field
    00078         foreach ($this->Fields as $FieldName => $Field)
    00079         {
    00080             # if form value is set and contains something and is not excluded
    00081             if (isset($_POST[$FieldName])
    00082                     && strlen(trim($_POST[$FieldName]))
    00083                     && (!$Field->IsPassword() || $IncludePasswords))
    00084             {
    00085                 # add value to URL param string
    00086                 $UrlParams = strlen($UrlParams)
    00087                         ? $UrlParams."&".$FieldName."=".urlencode($_POST[$FieldName])
    00088                         : $FieldName."=".urlencode($_POST[$FieldName]);
    00089             }
    00090         }
    00091
    00092         # return parameter string to caller
    00093         return $UrlParams;
    00094     }
    00095
    00096     # set field values from URL parameters where available
    00097     function SetFieldValuesFromUrlParams()
    00098     {
    00099         # for each field
    00100         foreach ($this->Fields as $FieldName => $Field)
    00101         {
    00102             # if value is available for field in incoming GET parameters
    00103             if (isset($_GET[$FieldName]))
    00104             {
    00105                 # set field value
    00106                 $Field->Value($_GET[$FieldName]);
    00107             }
    00108         }
    00109     }
    00110
    00111     # check form values for each field and report whether errors found
    00112     function IncomingFieldValuesHaveErrors()
    00113     {
    00114         return (count($_GET) || count($_POST)) ?
    00115             (strlen($this->GetErrorCodesAsUrlParams()) ? TRUE : FALSE) : FALSE;
    00116     }
    00117
    00118     # return URL parameter string with codes for any form value errors
    00119     # (returns empty string if no errors found)
    00120     # (URL parameter string does not include leading separator (? or &))
    00121     function GetErrorCodesAsUrlParams()
    00122     {
    00123         # start with empty error code string
    00124         $ErrorCodeString = "";
    00125
    00126         # for each field value
    00127         foreach ($this->Fields as $FieldName => $Field)
    00128         {
    00129             # if validation function says that value is invalid
    00130             $ErrorCode = $this->CheckFieldValue($FieldName);
    00131             if ($ErrorCode)
    00132             {
    00133                 # add error code for value to error code string
    00134                 $ErrorCodeString .= (strlen($ErrorCodeString) ? "!" : "")
    00135                         .$FieldName."-".$ErrorCode;
    00136             }
    00137         }
    00138
    00139         # if values were added to error code string
    00140         if (strlen($ErrorCodeString))
    00141         {
    00142             # prepend name of GET variable to contain error codes
    00143             $ErrorCodeString = "FTFieldErrs=".$ErrorCodeString;
    00144         }
    00145
    00146         # if additional error codes were supplied
    00147         if (isset($this->AdditionalErrorCodes) && count($this->AdditionalErrorCodes))
    00148         {
    00149             # for each additional error code
    00150             foreach ($this->AdditionalErrorCodes as $Code)
    00151             {
    00152                 # append code to string
    00153                 $AddCodeString = isset($AddCodeString) ? $AddCodeString."!".$Code
    00154                         : $Code;
    00155             }
    00156
    00157             # append additional error code string to error code string
    00158             $ErrorCodeString .= (strlen($ErrorCodeString) ? "&" : "")
    00159                     ."FTAddErrCodes=".$AddCodeString;
    00160         }
    00161
    00162         # if additional fields were supplied to be marked as erroneous
    00163         if (count($this->AdditionalErrorFields))
    00164         {
    00165             # for each additional error code
    00166             foreach ($this->AdditionalErrorFields as $FieldName)
    00167             {
    00168                 # append code to string
    00169                 $AddFieldString = isset($AddFieldString) ? $AddFieldString."!".$FieldName
    00170                         : $FieldName;
    00171             }
    00172
    00173             # append additional error code string to error code string
    00174             $ErrorCodeString .= (strlen($ErrorCodeString) ? "&" : "")
    00175                     ."FTAddErrFields=".$AddFieldString;
    00176         }
    00177
    00178         # return error code string to caller
    00179         return $ErrorCodeString;
    00180     }
    00181
    00182     # save additional fields to be marked as having errors
    00183     function SetAdditionalErrorFields($FieldNames)
    00184     {
    00185         # convert to array if needed
    00186         if (!is_array($FieldNames))
    00187         {
    00188             $FieldNames = array($FieldNames);
    00189         }
    00190
    00191         # save fields (if not already present)
    00192         foreach ($FieldNames as $FieldName)
    00193         {
    00194             if (!in_array($FieldName, $this->AdditionalErrorFields))
    00195             {
    00196                 $this->AdditionalErrorFields[] = $FieldName;
    00197             }
    00198         }
    00199     }
    00200
    00201     # save additional error codes
    00202     function SetAdditionalErrorCodes($Codes)
    00203     {
    00204         # convert to array if needed
    00205         if (!is_array($Codes))
    00206         {
    00207             $Codes = array($Codes);
    00208         }
    00209
    00210         # save codes (if not already present)
    00211         foreach ($Codes as $Code)
    00212         {
    00213             if (!isset($this->AdditionalErrorCodes)
    00214                     || !in_array($Code, $this->AdditionalErrorCodes))
    00215             {
    00216                 $this->AdditionalErrorCodes[] = $Code;
    00217             }
    00218         }
    00219     }
    00220
    00221     # convenience function that adds value and err codes to URL
    00222     function GetUrlWithValuesAndErrorCodes($BaseUrl, $IncludePasswords = FALSE)
    00223     {
    00224         $ValParams = $this->GetValsAsUrlParams($IncludePasswords);
    00225         $ErrParams = $this->GetErrorCodesAsUrlParams();
    00226         $ParamStart = strpos($BaseUrl, "?") ? "&" : "?";
    00227         return $BaseUrl
    00228                 .(strlen($ValParams) ? $ParamStart.$ValParams : "")
    00229                 .(strlen($ErrParams) ?
    00230                         (strlen($ValParams) ? "&" : $ParamStart).$ErrParams : "");
    00231     }
    00232
    00233     # get list of error messages based on codes from URL ($_GET)
    00234     function GetErrorMessages($EliminateDuplicateMessages = TRUE)
    00235     {
    00236         # start with empty list
    00237         $ErrorList = array();
    00238
    00239         # if it looks like there are field-specific error messages to be had
    00240         if (isset($_GET["FTFieldErrs"]))
    00241         {
    00242             # split error data into list of fields
    00243             $FieldList = explode("!", $_GET["FTFieldErrs"]);
    00244
    00245             # for each field found
    00246             foreach ($FieldList as $FieldListEntry)
    00247             {
    00248                 # split field entry into name and code
    00249                 list($FieldName, $ErrorCode) = explode("-", $FieldListEntry);
    00250
    00251                 # if we know about this field
    00252                 if (isset($this->Fields[$FieldName]))
    00253                 {
    00254                     # translate error code into message and add to list
    00255                     $Field = $this->Fields[$FieldName];
    00256                     $Replacements = array(
    00257                             "%N" => "<i>".$Field->Name()."</i>",
    00258                             "%V" => "<i>".$Field->Value()."</i>",
    00259                             "%L" => "<i>".preg_replace("/:$/", "", $Field->Label())."</i>",
    00260                             "%C" => "<i>".$ErrorCode."</i>",
    00261                             );
    00262                     $Message = $Field->GetInvalidValueMessage($ErrorCode);
    00263                     $ErrorList[$FieldName] = str_replace(
    00264                             array_keys($Replacements), $Replacements, $Message);
    00265                 }
    00266             }
    00267         }
    00268
    00269         # if it looks like there are additional general error messages to be had
    00270         if (isset($_GET["FTAddErrCodes"]) && count($this->AdditionalErrorMessages))
    00271         {
    00272             # split error data into list of codes
    00273             $CodeList = explode("!", $_GET["FTAddErrCodes"]);
    00274
    00275             # for each code found
    00276             foreach ($CodeList as $Code)
    00277             {
    00278                 # if there is a message corresponding to this code
    00279                 if (isset($this->AdditionalErrorMessages[$Code]))
    00280                 {
    00281                     # add message to list
    00282                     $ErrorList[$Code] = $this->AdditionalErrorMessages[$Code];
    00283                 }
    00284             }
    00285         }
    00286
    00287         # remove duplicate messages (if requested by caller)
    00288         if ($EliminateDuplicateMessages)
    00289         {
    00290             $NewErrorList = array();
    00291             foreach ($ErrorList as $Code => $Message)
    00292             {
    00293                 if (!in_array($Message, $NewErrorList))
    00294                 {
    00295                     $NewErrorList[$Code] = $Message;
    00296                 }
    00297             }
    00298             $ErrorList = $NewErrorList;
    00299         }
    00300
    00301         # return list of error messages to caller
    00302         return $ErrorList;
    00303     }
    00304
    00305     # print tags for specified field
    00306     function PrintField($FieldName)
    00307     {
    00308         $this->Fields[$FieldName]->PrintField(
    00309                 ($this->ErrorCodesAvailable() && $this->CheckFieldValue($FieldName))
    00310                 || in_array($FieldName, $this->AdditionalErrorFields));
    00311     }
    00312     function PrintLabelForField($FieldName)
    00313     {
    00314         $this->Fields[$FieldName]->PrintLabel(
    00315                 ($this->ErrorCodesAvailable() && $this->CheckFieldValue($FieldName))
    00316                 || in_array($FieldName, $this->AdditionalErrorFields));
    00317     }
    00318     function PrintInputForField($FieldName)
    00319     {
    00320         $this->Fields[$FieldName]->PrintInput(
    00321                 ($this->ErrorCodesAvailable() && $this->CheckFieldValue($FieldName))
    00322                 || in_array($FieldName, $this->AdditionalErrorFields));
    00323     }
    00324
    00325     # report whether error codes are available
    00326     function ErrorCodesAvailable()
    00327     {
    00328         return isset($_GET["FTFieldErrs"]) || isset($_GET["FTAddErrCodes"]);
    00329     }
    00330
    00331     # return array of US state names with two-letter abbreviations as index
    00332     static function GetArrayOfUsStates()
    00333     {
    00334         return array(
    00335                 "" => "--",
    00336                 "AL" => "Alabama",
    00337                 "AK" => "Alaska",
    00338                 "AZ" => "Arizona",
    00339                 "AR" => "Arkansas",
    00340                 "CA" => "California",
    00341                 "CO" => "Colorado",
    00342                 "CT" => "Connecticut",
    00343                 "DE" => "Delaware",
    00344                 "DC" => "District of Columbia",
    00345                 "FL" => "Florida",
    00346                 "GA" => "Georgia",
    00347                 "HI" => "Hawaii",
    00348                 "ID" => "Idaho",
    00349                 "IL" => "Illinois",
    00350                 "IN" => "Indiana",
    00351                 "IA" => "Iowa",
    00352                 "KS" => "Kansas",
    00353                 "KY" => "Kentucky",
    00354                 "LA" => "Louisiana",
    00355                 "ME" => "Maine",
    00356                 "MD" => "Maryland",
    00357                 "MA" => "Massachusetts",
    00358                 "MI" => "Michigan",
    00359                 "MN" => "Minnesota",
    00360                 "MS" => "Mississippi",
    00361                 "MO" => "Missouri",
    00362                 "MT" => "Montana",
    00363                 "NE" => "Nebraska",
    00364                 "NV" => "Nevada",
    00365                 "NH" => "New Hampshire",
    00366                 "NJ" => "New Jersey",
    00367                 "NM" => "New Mexico",
    00368                 "NY" => "New York",
    00369                 "NC" => "North Carolina",
    00370                 "ND" => "North Dakota",
    00371                 "OH" => "Ohio",
    00372                 "OK" => "Oklahoma",
    00373                 "OR" => "Oregon",
    00374                 "PA" => "Pennsylvania",
    00375                 "RI" => "Rhode Island",
    00376                 "SC" => "South Carolina",
    00377                 "SD" => "South Dakota",
    00378                 "TN" => "Tennessee",
    00379                 "TX" => "Texas",
    00380                 "UT" => "Utah",
    00381                 "VT" => "Vermont",
    00382                 "VA" => "Virginia",
    00383                 "WA" => "Washington",
    00384                 "WV" => "West Virginia",
    00385                 "WI" => "Wisconsin",
    00386                 "WY" => "Wyoming",
    00387                 );
    00388     }
    00389
    00390
    00391     # ---- PRIVATE INTERFACE -------------------------------------------------
    00392
    00393     var $Fields;
    00394     var $ErrorColor;
    00395     var $AdditionalErrorCodes;
    00396     var $AdditionalErrorFields;
    00397     var $AdditionalErrorMessages;
    00398
    00399     # check form (POST) value for specified field and return error code
    00400     function CheckFieldValue($FieldName)
    00401     {
    00402         $Value = isset($_POST[$FieldName]) ? $_POST[$FieldName]
    00403                 : (isset($_GET[$FieldName]) ? $_GET[$FieldName] : NULL);
    00404         $ErrorCode = $this->Fields[$FieldName]->IsInvalidValue($Value);
    00405         return $ErrorCode;
    00406     }
    00407 }
    00408
    00409 ?>

Copyright 2010 Internet Scout