Odyssey
lnappExport.prg
1 <?php
2 
3 
4  // ** Load the current database
5 
6  // **
7 
8 require_once(dirname(__FILE__) . "/../../monitor/library/cu_top.i");
9 
10 require_once(dirname(__FILE__) . "/../../eforms/library/lnappfunctions.i"); // TODO: that reference isn't going to work (probably).
11 
12  /*
13  *
14  *
15  *
16  * Export the loan details, exported in the configured format
17  *
18  *
19  *
20  *
21  */
22  $lnappConfigOptions = Array('configHomeLogin' => 'Allow Home Banking Login',
23  'configDisableEmail' => 'Disable Email Login',
24  'configSSOOnly' => 'SSO Only',
25  'configLoanSubmit' => 'Post Loan Applications to Core',
26  'configLoanSubmitMember' => 'Post Primary Member With Loan App',
27  'configLoanFormat' => ' Post Loan Applications Format',
28  'configLoanMIR' => 'Allow Member Info to Populate From Core',
29  'configLoanCSS' => 'Loan App Stylesheet',
30  'configLoanIntroMbr' => 'Loan Login Message for Members',
31  'configLoanIntroNonHBMbr' => 'Loan Login Message for non-Home Banking Members',
32  'configLoanIntroNon' => 'Loan Login Message for non-Members',
33  'configLoanDataOnly' => 'Submit Fields With Data Only');
34 
35 
36  /*
37  * Variables
38  */
39  $DB_TABLE_PREFIX = 'lnapp';
40  $FORM_VALIDATION_ERROR = "";
41 
42 
43 try
44 {
45 
46  $dms_ok = array('action'=>'string', 'cu'=>'string', 'LoanId'=>'string',
47  'Title'=>'string', 'LastModified'=>'string', 'Fragment'=>'string',
48  'loanid'=>'string', 'option' => 'string', 'ConfigField' => 'string',
49  'ConfigValue' => 'string', 'CopyFrom' => 'string');
50 
51  HCU_ImportVars($DATA_PARAMETERS, "TOP_LEVEL", $dms_ok);
52  $cu = $DATA_PARAMETERS['TOP_LEVEL']['cu'];
53  $loanid = $DATA_PARAMETERS['TOP_LEVEL']['loanid'];
54 
55  $dbh = $link;
56 
57 
58  /*
59  * Retrieve the configuration for the credit union
60  *
61  */
62 
63  $currentConfig = Array(); // Declare as an empty array
64  $recordFound = false;
65  // ** This will handle the data options for Configuration Options
66  $sql = "SELECT cu, appconfig
67  FROM lnappconfig
68  WHERE cu = '" . prep_save($cu, 12) . "'";
69  $config_rs = db_query($sql, $dbh);
70  if ($config_row = db_fetch_assoc($config_rs)) {
71  $recordFound = true;
72  // ** Retrieve the text
73  $currentConfigJson = trim($config_row['appconfig']);
74  // ** Validate this object may be json
75  $currentConfig = HCU_JsonDecode($currentConfigJson, true);
76  if (json_last_error() != JSON_ERROR_NONE) {
77  // AN ERROR WAS DETECTED - revert the Value back to an array
78  $currentConfig = Array();
79  }
80  }
81 
82 
83  $sql = "SELECT schemadetail.*
84  FROM {$DB_TABLE_PREFIX}schemadetail as schemadetail
85  JOIN {$DB_TABLE_PREFIX}schemamaster as schemamaster ON
86  schemamaster.loanid = schemadetail.loanid
87  WHERE schemadetail.loanid = " . intval($loanid) . "
88  AND schemamaster.cu = '$cu'
89  ORDER BY schemadetail.loanid, schemadetail.pageid, schemadetail.groupid, schemadetail.lineid ";
90 
91  $loan_rs = db_query($sql, $dbh);
92 
93  $App_SchemaDetail = Array();
94  $App_SchemaFields = Array();
95  $App_AnswerDetail = Array();
96  $App_ConditionalFields = Array(); // ** this is the list of conditional fields on the posted form..
97 
98  // ** IF NO RECORD
99 
100  if ($loan_rs) {
101  $loan_idx = 0;
102  while ($loan_row = db_fetch_assoc($loan_rs, $loan_idx++)) {
103  // ** Need to create the Loan Master Schema Array
104  // ** Make the detailid the key so I can look it up quickly
105  $App_SchemaDetail[$loan_row['detailid']] = $loan_row;
106 
107  // ** The Haystack was in the wrong position for the function
108  if (strpos(":YNR:YNR01:YNS:YNS01:GN:GA:GT:AS:AR:", ":" . trim($loan_row['fieldtype']) . ":") !== false) {
109  // * This appears to be a valid type... This is a field that should be saved
110  // to the database field response
111  // ** All fields save will be the field name "formfield_{detailid}"
112  $fieldname = "formfield_" . $loan_row['detailid'];
113  $App_SchemaFields[$fieldname] = "";
114 
115  // ** If we are posting the form.. There are situations that I want to
116  // ** record something.. This is when
117  if (isset($_POST['btnFormPost'])) {
118  $fieldattr = HCU_JsonDecode($App_SchemaDetail[$loan_row['detailid']]['fieldattr'], true);
119  if ($fieldattr['validation_conditionname'] != '') {
120 
121  // This value is set as a conditional value -- I will set the value in the array here
122  $App_ConditionalFields[$fieldattr['validation_conditionname']] = trim($_POST[$fieldname]);
123  }
124  }
125  }
126 
127  }
128  } elseif ($FORM_VALIDATION_ERROR != '') {
129  // ** PROBLEM LOADING MASTER SCHEMA -- If validation error is not set, then set something
130  $FORM_VALIDATION_ERROR = "<li>A problem was encountered that is preventing the loan application from loading.</li>";
131  }
132 
133 
134  print BuildObject($currentConfig);
135 
136 } catch(Exception $ex) {
137  //Return error message
138  print "An error occurred exporting the schema";
139 }
140 
141 
142 // * App_Response -- this is the applicatn response in an array based on fields..
143 // * The array for CORE WILL BE BUILT HERE
144 function BuildObject($pCurrentConfig) {
145  global $App_SchemaDetail, $DB_TABLE_PREFIX, $FORM_VALIDATION_ERROR;
146 
147  $core_values_array = Array();
148  $local_values_array = Array();
149  $core_values_serial = "";
150 
151  $App_AnswerDetail = $GLOBALS['App_AnswerDetail'];
152  // ** Need to assign
153  foreach ($App_SchemaDetail as $schema_detailid => $schema_detailvalue) {
154 
155  // ** For each postable value -- Look up and assign the value on
156  $fieldattr = HCU_JsonDecode($App_SchemaDetail[$schema_detailid]['fieldattr'], true);
157  $fieldval = $App_SchemaDetail[$schema_detailid]['fieldvalue'];
158  $fieldkey = "formfield_" . $schema_detailid;
159 
160  $sampleData = '';
161  // ** data type
162  switch ($App_SchemaDetail[$schema_detailid]['fieldtype']) {
163  case "YNR":
164  case "YNS":
165  // ** Values may be Y / N or Blank
166  if ($fieldval != "" && !strstr(":Y:N:", $fieldval)) {
167  $FORM_VALIDATION_ERROR .= "<li>" . $App_SchemaDetail[$schema_detailid]['fieldvalue'] . " contains an invalid option.</li>";
168  }
169  $sampleData = '{Y/N}';
170  break;
171  case "YNR01":
172  case "YNS01":
173  // ** Values may be 0 / 1 or Blank
174  if ($fieldval != "" && !strstr(":0:1:", $fieldval)) {
175  $FORM_VALIDATION_ERROR .= "<li>" . $App_SchemaDetail[$schema_detailid]['fieldvalue'] . " contains an invalid option.</li>";
176  }
177  $sampleData = '{1/0}';
178  break;
179  case "AS":
180  case "AR":
181  $ansId = $fieldattr['fieldansid'];
182 
183 
184  if (!isset($GLOBALS['App_AnswerDetail'][$ansId])) {
185  // ** Send to load the answer
186  Load_AnswerList($ansId);
187  }
188 
189  // ** THESE COULD BE NUMERIC OR TEXT -- The ultimate test is to know the type and look it up as a valid option
190  // ** values may be an integer only -- not larger than???
191  if (is_array($GLOBALS['App_AnswerDetail'][$ansId])) {
192  $sampleData = "{" . implode('/', array_keys($GLOBALS['App_AnswerDetail'][$ansId])) . "}";
193  }
194  break;
195  // ** GN AND GA types may be phased out
196  case "GN":
197  // ** value must be numeric
198  $sampleData = '######';
199  break;
200  case "GA":
201  // ** value must be an amount
202  $sampleData = '######.##';
203  break;
204  case "GT":
205  // ** value is a generic text
206  // ** The data type is determined by validation_match under the fieldattr object
207  if (isset($fieldattr['validation_match'])) {
208  switch ($fieldattr['validation_match']) {
209  case "date":
210  $sampleData = 'MM/DD/YYYY';
211  break;
212  case "ssn":
213  $sampleData = '###-##-####';
214  break;
215  case "phone":
216  $sampleData = '###-###-####';
217  break;
218  case "url":
219  $sampleData = 'www.url.com';
220  break;
221  case "email":
222  $sampleData = 'sampleemail@url.com';
223  break;
224  case "usd":
225  $sampleData = '$#####.##';
226  break;
227  case "integer":
228  $sampleData = '#######';
229  break;
230  case "zip":
231  $sampleData = '######-####';
232  break;
233  case "number":
234  $sampleData = '#######';
235  break;
236  default:
237  $sampleData = 'string';
238  }
239  } else {
240  $sampleData = 'string';
241  }
242 
243  // ** Be sure the field is NOT too large
244  if (isset($fieldattr['maxlength'])) {
245  if (intval($fieldattr['maxlength']) && intval($fieldattr['maxlength']) > 0) {
246  $FORM_VALIDATION_ERROR .= "<li>" . $App_SchemaDetail[$schema_detailid]['fieldvalue'] . " has exceeded the max length of " . intval($fieldattr['maxlength']) . ".</li>";
247  $sampleData .= ' max ' . intval($fieldattr['maxlength']);
248  }
249  }
250  }
251 
252  // ** ONLY add this field if the fieldcorename is set and is not a local only field
253  // * localfieldonly will only be available if it is set to 1, otherwise the field won't exist.
254  $hasKey_fieldCoreName = HCU_array_key_exists('fieldcorename', $fieldattr);
255  $hasKey_localFieldOnly = HCU_array_key_exists('localfieldonly', $fieldattr);
256  if ($hasKey_fieldCoreName && !$hasKey_localFieldOnly) {
257  // localfieldonly == 0 / doesn't exist in array
258  if ($fieldattr['fieldcorename'] != '') {
259  if (isset($fieldattr['validation_match'])) {
260  // * Create sample data based on the data type
261  switch ($fieldattr['validation_match']) {
262  case "number":
263  $sampleData = '#######';
264  break;
265  default:
266  $sampleData = 'string';
267  }
268  } else {
269  $sampleData = 'string';
270  }
271 
272  // ** Be sure the field is NOT too large
273  if (isset($fieldattr['maxlength'])) {
274  if (intval($fieldattr['maxlength']) && intval($fieldattr['maxlength']) > 0) {
275  $FORM_VALIDATION_ERROR .= "<li>" . $App_SchemaDetail[$schema_detailid]['fieldvalue'] . " has exceeded the max length of " . intval($fieldattr['maxlength']) . ".</li>";
276  $sampleData .= ' max ' . intval($fieldattr['maxlength']);
277  }
278  }
279 
280  // * Create sample data based on the data type
281  // ** Assign the value -- There may be an Index associated with the row
282  // ** IF so then assign that here
283  // IF fieldcorename contains a ',' then I will explode the list out into an
284  // array and call each each key to add it to the master list
285  if (strpos($fieldattr['fieldcorename'], ",") === FALSE) {
286  if (isset($fieldattr['fieldcorearrayindex'])) {
287  $core_values_array[$fieldattr['fieldcorearrayindex']][$fieldattr['fieldcorename']] = $sampleData;
288  } else {
289  $core_values_array[$fieldattr['fieldcorename']] = $sampleData;
290  }
291  } else {
292  // The fieldcorename will be treated as a list
293  $pieces = explode(",", $fieldattr['fieldcorename']);
294  foreach ($pieces as $fieldcorename_piece) {
295  $fieldcorename_piece = trim($fieldcorename_piece);
296  if (isset($fieldattr['fieldcorearrayindex'])) {
297  $core_values_array[$fieldattr['fieldcorearrayindex']][$fieldcorename_piece] = $sampleData;
298  } else {
299  $core_values_array[$fieldcorename_piece] = $sampleData;
300  }
301  }
302  }
303  }
304  } else if ($hasKey_fieldCoreName && $hasKey_localFieldOnly) {
305  // localfieldonly == 1
306  // * add to local values array
307  $local_values_array[$fieldattr['fieldcorename']] = $sampleData;
308  }
309  }
310 
311  // ** This is sort of hard coded for now..
312  // ** But I do NOT want to send a co-applicant if dmsapp_coapp_status is set to N
313  if (!HCU_array_key_exists('dmsapp_coapp_status', $local_values_array) || $local_values_array['dmsapp_coapp_status'] == "N") {
314  // ** REMOVE the "C" co-applicant from array
315  unset($core_values_array['C']);
316  }
317 
318  // ** Based on the return format, format the code appropriately
319  $formatData = HCU_array_key_exists('configLoanFormat', $pCurrentConfig) ?
320  $pCurrentConfig['configLoanFormat'] :
321  "";
322  switch ($formatData) {
323  case 'XML':
324  $dataExport = assocArrayToXML('NewLoanApplication', $core_values_array);
325  // $dataExport = generate_valid_xml_from_array($core_values_array,"NewLoanApplication");
326  // header ('Content-type: text/xml');
327  break;
328  case 'SERIAL':
329  $dataExport = serialize($core_values_array);
330  break;
331  case 'JSON':
332  // header ('Content-type: application/json');
333  $dataExport = HCU_JsonEncode($core_values_array);
334  break;
335  default:
336  $dataExport = print_r($core_values_array, true);
337 
338  }
339  return $dataExport;
340 }
341 
342 
343 function generate_xml_from_array($array, $node_name) {
344  $xml = '';
345 
346  if (is_array($array) || is_object($array)) {
347  foreach ($array as $key=>$value) {
348  if (is_numeric($key)) {
349  $key = $node_name;
350  }
351 
352  $xml .= '<' . $key . '>' . "\n" . generate_xml_from_array($value, $node_name) . '</' . $key . '>' . "\n";
353  }
354  } else {
355  $xml = htmlspecialchars($array, ENT_QUOTES) . "\n";
356  }
357 
358  return $xml;
359 }
360 function generate_valid_xml_from_array($array, $node_block='nodes', $node_name='node') {
361  $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
362 
363  $xml .= '<' . $node_block . '>' . "\n";
364  $xml .= generate_xml_from_array($array, $node_name);
365  $xml .= '</' . $node_block . '>' . "\n";
366 
367  return $xml;
368 }
369 function assocArrayToXML($root_element_name,$ar)
370 {
371  $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");
372  $f = create_function('$f,$c,$a','
373  foreach($a as $k=>$v) {
374  if(is_array($v)) {
375  $ch=$c->addChild($k);
376  $f($f,$ch,$v);
377  } else {
378  $c->addChild($k,$v);
379  }
380  }');
381  $f($f,$xml,$ar);
382  return $xml->asXML();
383 }