5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2011 Edward Almasy and Internet Scout Project
7 # http://scout.wisc.edu/
12 # ---- PUBLIC INTERFACE --------------------------------------------------
17 # if form field value is not an array
18 if (!is_array($FormFields))
20 # look for form field file to include
21 $PossibleFileNames = array(
22 "local/include/%NAME%",
23 "local/include/%NAME%.php",
24 "local/include/Form--%NAME%.php",
25 "local/include/SPT--Form--%NAME%.php",
28 "include/Form--%NAME%.php",
29 "include/SPT--Form--%NAME%.php",
31 foreach ($PossibleFileNames as $FileName)
33 $FileName = str_replace(
"%NAME%", $FormFields, $FileName);
34 if (file_exists($FileName))
36 $FormFieldFile = $FileName;
41 # if form field file was found
42 if (isset($FormFieldFile))
44 # load form field file (should set $FormFields)
45 include_once($FormFieldFile);
49 # save field info with field name as index
50 foreach ($FormFields as $Field)
52 $this->Fields[$Field->Name()] = $Field;
55 # save additional error messages (if any)
58 # set default error color
59 $this->ErrorColor =
"red";
61 # save any additional fields indicated to be marked for error
62 if (isset($_GET[
"FTAddErrFields"]) && count($_GET[
"FTAddErrFields"]))
64 $this->AdditionalErrorFields = explode(
"!", $_GET[
"FTAddErrFields"]);
68 $this->AdditionalErrorFields = array();
72 # get/set field values
75 return $this->Fields[$FieldName]->Value($NewValue);
78 # checks whether all required form variables are set in $_POST
81 # assume that all required variables will be found
84 # for each required form variable
85 foreach ($this->Fields as $FieldName => $Field)
87 # if variable is not set in $_POST
88 if (!isset($_POST[$FieldName]) || !strlen($_POST[$FieldName]))
90 # set flag indicating we found a missing var and exit loop
96 # report back to caller whether we found on required vars
100 # return URL parameter string with form values that are set (from $_POST)
101 # (returns empty string if none of the form values are set)
102 # (URL parameter string does not include leading separator (? or &))
105 # assume no values will be found
108 # for each form field
109 foreach ($this->Fields as $FieldName => $Field)
111 # if form value is set and contains something and is not excluded
112 if (isset($_POST[$FieldName])
113 && strlen(trim($_POST[$FieldName]))
114 && (!$Field->IsPassword() || $IncludePasswords))
116 # add value to URL param string
117 $UrlParams = strlen($UrlParams)
118 ? $UrlParams.
"&".$FieldName.
"=".urlencode($_POST[$FieldName])
119 : $FieldName.
"=".urlencode($_POST[$FieldName]);
123 # return parameter string to caller
127 # set field values from URL parameters where available
131 foreach ($this->Fields as $FieldName => $Field)
133 # if value is available for field in incoming GET parameters
134 if (isset($_GET[$FieldName]))
137 $Field->Value($_GET[$FieldName]);
142 # check form values for each field and report whether errors found
145 return (count($_GET) || count($_POST)) ?
149 # return URL parameter string with codes for any form value errors
150 # (returns empty string if no errors found)
151 # (URL parameter string does not include leading separator (? or &))
154 # start with empty error code string
155 $ErrorCodeString =
"";
157 # for each field value
158 foreach ($this->Fields as $FieldName => $Field)
160 # if validation function says that value is invalid
164 # add error code for value to error code string
165 $ErrorCodeString .= (strlen($ErrorCodeString) ?
"!" :
"")
166 .$FieldName.
"-".$ErrorCode;
170 # if values were added to error code string
171 if (strlen($ErrorCodeString))
173 # prepend name of GET variable to contain error codes
174 $ErrorCodeString =
"FTFieldErrs=".$ErrorCodeString;
177 # if additional error codes were supplied
178 if (isset($this->AdditionalErrorCodes) && count($this->AdditionalErrorCodes))
180 # for each additional error code
181 foreach ($this->AdditionalErrorCodes as $Code)
183 # append code to string
184 $AddCodeString = isset($AddCodeString) ? $AddCodeString.
"!".$Code
188 # append additional error code string to error code string
189 $ErrorCodeString .= (strlen($ErrorCodeString) ?
"&" :
"")
190 .
"FTAddErrCodes=".$AddCodeString;
193 # if additional fields were supplied to be marked as erroneous
194 if (count($this->AdditionalErrorFields))
196 # for each additional error code
197 foreach ($this->AdditionalErrorFields as $FieldName)
199 # append code to string
200 $AddFieldString = isset($AddFieldString) ? $AddFieldString.
"!".$FieldName
204 # append additional error code string to error code string
205 $ErrorCodeString .= (strlen($ErrorCodeString) ?
"&" :
"")
206 .
"FTAddErrFields=".$AddFieldString;
209 # return error code string to caller
210 return $ErrorCodeString;
213 # save additional fields to be marked as having errors
216 # convert to array if needed
217 if (!is_array($FieldNames))
219 $FieldNames = array($FieldNames);
222 # save fields (if not already present)
223 foreach ($FieldNames as $FieldName)
225 if (!in_array($FieldName, $this->AdditionalErrorFields))
227 $this->AdditionalErrorFields[] = $FieldName;
232 # save additional error codes
235 # convert to array if needed
236 if (!is_array($Codes))
238 $Codes = array($Codes);
241 # save codes (if not already present)
242 foreach ($Codes as $Code)
244 if (!isset($this->AdditionalErrorCodes)
245 || !in_array($Code, $this->AdditionalErrorCodes))
247 $this->AdditionalErrorCodes[] = $Code;
252 # convenience function that adds value and err codes to URL
257 $ParamStart = strpos($BaseUrl,
"?") ?
"&" :
"?";
259 .(strlen($ValParams) ? $ParamStart.$ValParams :
"")
260 .(strlen($ErrParams) ?
261 (strlen($ValParams) ?
"&" : $ParamStart).$ErrParams :
"");
264 # get list of error messages based on codes from URL ($_GET)
267 # start with empty list
268 $ErrorList = array();
270 # if it looks like there are field-specific error messages to be had
271 if (isset($_GET[
"FTFieldErrs"]))
273 # split error data into list of fields
274 $FieldList = explode(
"!", $_GET[
"FTFieldErrs"]);
276 # for each field found
277 foreach ($FieldList as $FieldListEntry)
279 # split field entry into name and code
280 list($FieldName, $ErrorCode) = explode(
"-", $FieldListEntry);
282 # if we know about this field
283 if (isset($this->Fields[$FieldName]))
285 # translate error code into message and add to list
286 $Field = $this->Fields[$FieldName];
287 $Replacements = array(
288 "%N" =>
"<i>".$Field->Name().
"</i>",
289 "%V" =>
"<i>".$Field->Value().
"</i>",
290 "%L" =>
"<i>".preg_replace(
"/:$/",
"", $Field->Label()).
"</i>",
291 "%C" =>
"<i>".$ErrorCode.
"</i>",
293 $Message = $Field->GetInvalidValueMessage($ErrorCode);
294 $ErrorList[$FieldName] = str_replace(
295 array_keys($Replacements), $Replacements, $Message);
300 # if it looks like there are additional general error messages to be had
301 if (isset($_GET[
"FTAddErrCodes"]) && count($this->AdditionalErrorMessages))
303 # split error data into list of codes
304 $CodeList = explode(
"!", $_GET[
"FTAddErrCodes"]);
306 # for each code found
307 foreach ($CodeList as $Code)
309 # if there is a message corresponding to this code
310 if (isset($this->AdditionalErrorMessages[$Code]))
312 # add message to list
313 $ErrorList[$Code] = $this->AdditionalErrorMessages[$Code];
318 # remove duplicate messages (if requested by caller)
319 if ($EliminateDuplicateMessages)
321 $NewErrorList = array();
322 foreach ($ErrorList as $Code => $Message)
324 if (!in_array($Message, $NewErrorList))
326 $NewErrorList[$Code] = $Message;
329 $ErrorList = $NewErrorList;
332 # return list of error messages to caller
336 # print tags for specified field
339 $this->Fields[$FieldName]->PrintField(
341 || in_array($FieldName, $this->AdditionalErrorFields));
345 $this->Fields[$FieldName]->PrintLabel(
347 || in_array($FieldName, $this->AdditionalErrorFields));
351 $this->Fields[$FieldName]->PrintInput(
353 || in_array($FieldName, $this->AdditionalErrorFields));
356 # report whether error codes are available
359 return isset($_GET[
"FTFieldErrs"]) || isset($_GET[
"FTAddErrCodes"]);
362 # return array of US state names with two-letter abbreviations as index
371 "CA" =>
"California",
373 "CT" =>
"Connecticut",
375 "DC" =>
"District of Columbia",
388 "MA" =>
"Massachusetts",
391 "MS" =>
"Mississippi",
396 "NH" =>
"New Hampshire",
397 "NJ" =>
"New Jersey",
398 "NM" =>
"New Mexico",
400 "NC" =>
"North Carolina",
401 "ND" =>
"North Dakota",
405 "PA" =>
"Pennsylvania",
406 "RI" =>
"Rhode Island",
407 "SC" =>
"South Carolina",
408 "SD" =>
"South Dakota",
414 "WA" =>
"Washington",
415 "WV" =>
"West Virginia",
422 # ---- PRIVATE INTERFACE -------------------------------------------------
430 # check form (POST) value for specified field and return error code
433 $Value = isset($_POST[$FieldName]) ? $_POST[$FieldName]
434 : (isset($_GET[$FieldName]) ? $_GET[$FieldName] : NULL);
435 $ErrorCode = $this->Fields[$FieldName]->IsInvalidValue($Value);