5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2002-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
16 # ---- PUBLIC INTERFACE --------------------------------------------------
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)
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
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(
"/:$/",
"", $Field->Label()).
"</i>",
344 "%C" =>
"<i>".$ErrorCode.
"</i>",
346 $Message = $Field->GetInvalidValueMessage($ErrorCode);
347 $ErrorList[$FieldName] = str_replace(
348 array_keys($Replacements), $Replacements, $Message);
353 # if it looks like there are additional general error messages to be had
354 if (isset($_GET[
"FTAddErrCodes"]) && count($this->AdditionalErrorMessages))
356 # split error data into list of codes
357 $CodeList = explode(
"!", $_GET[
"FTAddErrCodes"]);
359 # for each code found
360 foreach ($CodeList as $Code)
362 # if there is a message corresponding to this code
363 if (isset($this->AdditionalErrorMessages[$Code]))
365 # add message to list
366 $ErrorList[$Code] = $this->AdditionalErrorMessages[$Code];
371 # remove duplicate messages (if requested by caller)
372 if ($EliminateDuplicateMessages)
374 $NewErrorList = array();
375 foreach ($ErrorList as $Code => $Message)
377 if (!in_array($Message, $NewErrorList))
379 $NewErrorList[$Code] = $Message;
382 $ErrorList = $NewErrorList;
385 # return list of error messages to caller
395 $this->Fields[$FieldName]->PrintField(
397 || in_array($FieldName, $this->AdditionalErrorFields));
406 $this->Fields[$FieldName]->PrintLabel(
408 || in_array($FieldName, $this->AdditionalErrorFields));
417 $this->Fields[$FieldName]->PrintInput(
419 || in_array($FieldName, $this->AdditionalErrorFields));
428 return isset($_GET[
"FTFieldErrs"]) || isset($_GET[
"FTAddErrCodes"]);
445 "CA" =>
"California",
447 "CT" =>
"Connecticut",
449 "DC" =>
"District of Columbia",
462 "MA" =>
"Massachusetts",
465 "MS" =>
"Mississippi",
470 "NH" =>
"New Hampshire",
471 "NJ" =>
"New Jersey",
472 "NM" =>
"New Mexico",
474 "NC" =>
"North Carolina",
475 "ND" =>
"North Dakota",
479 "PA" =>
"Pennsylvania",
480 "RI" =>
"Rhode Island",
481 "SC" =>
"South Carolina",
482 "SD" =>
"South Dakota",
488 "WA" =>
"Washington",
489 "WV" =>
"West Virginia",
496 # ---- PRIVATE INTERFACE -------------------------------------------------
512 $Value = isset($_POST[$FieldName]) ? $_POST[$FieldName]
513 : (isset($_GET[$FieldName]) ? $_GET[$FieldName] : NULL);
514 $ErrorCode = $this->Fields[$FieldName]->IsInvalidValue($Value);