5 # Part of the Collection Workflow Integration System (CWIS) 6 # Copyright 2002-2015 Edward Almasy and Internet Scout Research Group 7 # http://scout.wisc.edu/cwis/ 16 # ---- PUBLIC INTERFACE -------------------------------------------------- 25 public function __construct($FormFields, $AdditionalErrorMessages = NULL)
27 # if form field value is not an array 28 if (!is_array($FormFields))
30 # look for form field file to include 31 $PossibleFileNames = array(
32 "local/include/%NAME%",
33 "local/include/%NAME%.php",
34 "local/include/Form--%NAME%.php",
35 "local/include/SPT--Form--%NAME%.php",
38 "include/Form--%NAME%.php",
39 "include/SPT--Form--%NAME%.php",
41 foreach ($PossibleFileNames as $FileName)
43 $FileName = str_replace(
"%NAME%", $FormFields, $FileName);
44 if (file_exists($FileName))
46 $FormFieldFile = $FileName;
51 # if form field file was found 52 if (isset($FormFieldFile))
54 # load form field file (should set $FormFields) 55 include_once($FormFieldFile);
59 # save field info with field name as index 60 foreach ($FormFields as $Field)
62 $this->Fields[$Field->Name()] = $Field;
65 # save additional error messages (if any) 66 $this->AdditionalErrorMessages = $AdditionalErrorMessages;
68 # set default error color 69 $this->ErrorColor =
"red";
71 # save any additional fields indicated to be marked for error 72 if (isset($_GET[
"FTAddErrFields"]) && count($_GET[
"FTAddErrFields"]))
74 $this->AdditionalErrorFields = explode(
"!", $_GET[
"FTAddErrFields"]);
78 $this->AdditionalErrorFields = array();
91 return $this->Fields[$FieldName]->Value($NewValue);
102 # assume that all required variables will be found 105 # for each required form variable 106 foreach ($this->Fields as $FieldName => $Field)
108 # if variable is not set in $_POST 109 if (!isset($_POST[$FieldName]) || !strlen($_POST[$FieldName]))
111 # set flag indicating we found a missing var and exit loop 117 # report back to caller whether we found on required vars 130 # assume no values will be found 133 # for each form field 134 foreach ($this->Fields as $FieldName => $Field)
136 # if form value is set and contains something and is not excluded 137 if (isset($_POST[$FieldName])
138 && strlen(trim($_POST[$FieldName]))
139 && (!$Field->IsPassword() || $IncludePasswords))
141 # add value to URL param string 142 $UrlParams = strlen($UrlParams)
143 ? $UrlParams.
"&".$FieldName.
"=".urlencode($_POST[$FieldName])
144 : $FieldName.
"=".urlencode($_POST[$FieldName]);
148 # return parameter string to caller 158 foreach ($this->Fields as $FieldName => $Field)
160 # if value is available for field in incoming GET parameters 161 if (isset($_GET[$FieldName]))
164 $Field->Value($_GET[$FieldName]);
176 return (count($_GET) || count($_POST)) ?
189 # start with empty error code string 190 $ErrorCodeString =
"";
192 # for each field value 193 foreach ($this->Fields as $FieldName => $Field)
195 # if validation function says that value is invalid 196 $ErrorCode = $this->CheckFieldValue($FieldName);
199 # add error code for value to error code string 200 $ErrorCodeString .= (strlen($ErrorCodeString) ?
"!" :
"")
201 .$FieldName.
"-".$ErrorCode;
205 # if values were added to error code string 206 if (strlen($ErrorCodeString))
208 # prepend name of GET variable to contain error codes 209 $ErrorCodeString =
"FTFieldErrs=".$ErrorCodeString;
212 # if additional error codes were supplied 213 if (isset($this->AdditionalErrorCodes) && count($this->AdditionalErrorCodes))
215 # for each additional error code 216 foreach ($this->AdditionalErrorCodes as $Code)
218 # append code to string 219 $AddCodeString = isset($AddCodeString) ? $AddCodeString.
"!".$Code
223 # append additional error code string to error code string 224 $ErrorCodeString .= (strlen($ErrorCodeString) ?
"&" :
"")
225 .
"FTAddErrCodes=".$AddCodeString;
228 # if additional fields were supplied to be marked as erroneous 229 if (count($this->AdditionalErrorFields))
231 # for each additional error code 232 foreach ($this->AdditionalErrorFields as $FieldName)
234 # append code to string 235 $AddFieldString = isset($AddFieldString) ? $AddFieldString.
"!".$FieldName
239 # append additional error code string to error code string 240 $ErrorCodeString .= (strlen($ErrorCodeString) ?
"&" :
"")
241 .
"FTAddErrFields=".$AddFieldString;
244 # return error code string to caller 245 return $ErrorCodeString;
254 # convert to array if needed 255 if (!is_array($FieldNames))
257 $FieldNames = array($FieldNames);
260 # save fields (if not already present) 261 foreach ($FieldNames as $FieldName)
263 if (!in_array($FieldName, $this->AdditionalErrorFields))
265 $this->AdditionalErrorFields[] = $FieldName;
276 # convert to array if needed 277 if (!is_array($Codes))
279 $Codes = array($Codes);
282 # save codes (if not already present) 283 foreach ($Codes as $Code)
285 if (!isset($this->AdditionalErrorCodes)
286 || !in_array($Code, $this->AdditionalErrorCodes))
288 $this->AdditionalErrorCodes[] = $Code;
304 $ParamStart = strpos($BaseUrl,
"?") ?
"&" :
"?";
306 .(strlen($ValParams) ? $ParamStart.$ValParams :
"")
307 .(strlen($ErrParams) ?
308 (strlen($ValParams) ?
"&" : $ParamStart).$ErrParams :
"");
320 # start with empty list 321 $ErrorList = array();
323 # if it looks like there are field-specific error messages to be had 324 if (isset($_GET[
"FTFieldErrs"]))
326 # split error data into list of fields 327 $FieldList = explode(
"!", $_GET[
"FTFieldErrs"]);
329 # for each field found 330 foreach ($FieldList as $FieldListEntry)
332 # split field entry into name and code 333 list($FieldName, $ErrorCode) = explode(
"-", $FieldListEntry);
335 # if we know about this field 336 if (isset($this->Fields[$FieldName]))
338 # translate error code into message and add to list 339 $Field = $this->Fields[$FieldName];
340 $Replacements = array(
341 "%N" =>
"<i>".$Field->Name().
"</i>",
342 "%V" =>
"<i>".$Field->Value().
"</i>",
343 "%L" =>
"<i>".preg_replace(
"/:$/",
"",
344 $Field->Label()).
"</i>",
345 "%C" =>
"<i>".$ErrorCode.
"</i>",
347 $Message = $Field->GetInvalidValueMessage($ErrorCode);
348 $ErrorList[$FieldName] = str_replace(
349 array_keys($Replacements), $Replacements, $Message);
354 # if it looks like there are additional general error messages to be had 355 if (isset($_GET[
"FTAddErrCodes"]) && count($this->AdditionalErrorMessages))
357 # split error data into list of codes 358 $CodeList = explode(
"!", $_GET[
"FTAddErrCodes"]);
360 # for each code found 361 foreach ($CodeList as $Code)
363 # if there is a message corresponding to this code 364 if (isset($this->AdditionalErrorMessages[$Code]))
366 # add message to list 367 $ErrorList[$Code] = $this->AdditionalErrorMessages[$Code];
372 # remove duplicate messages (if requested by caller) 373 if ($EliminateDuplicateMessages)
375 $NewErrorList = array();
376 foreach ($ErrorList as $Code => $Message)
378 if (!in_array($Message, $NewErrorList))
380 $NewErrorList[$Code] = $Message;
383 $ErrorList = $NewErrorList;
386 # return list of error messages to caller 396 $this->Fields[$FieldName]->PrintField(
398 || in_array($FieldName, $this->AdditionalErrorFields));
407 $this->Fields[$FieldName]->PrintLabel(
409 || in_array($FieldName, $this->AdditionalErrorFields));
418 $this->Fields[$FieldName]->PrintInput(
420 || in_array($FieldName, $this->AdditionalErrorFields));
429 return isset($_GET[
"FTFieldErrs"]) || isset($_GET[
"FTAddErrCodes"]);
441 array_unshift($StatesList,
"--");
447 # ---- PRIVATE INTERFACE ------------------------------------------------- 451 private $AdditionalErrorCodes;
452 private $AdditionalErrorFields;
453 private $AdditionalErrorMessages;
461 private function CheckFieldValue($FieldName)
463 $Value = isset($_POST[$FieldName]) ? $_POST[$FieldName]
464 : (isset($_GET[$FieldName]) ? $_GET[$FieldName] : NULL);
465 $ErrorCode = $this->Fields[$FieldName]->IsInvalidValue($Value);
static GetUsStatesList()
Get an array of US state names with their two-letter abbreviations as the index.