Odyssey
lnapp.data
1 <?php
2 // File: lnapp.data.v2
3 // SPB: 10/6/2015 version 2 with kendo
4 
5 $monLibrary= dirname(__FILE__) . "/../library";
6 $sharedLibrary= dirname(__FILE__) . "/../../shared/library";
7 require_once("$monLibrary/cu_top.i");
8 require_once("$monLibrary/ck_hticket.i");
9 require_once("$sharedLibrary/commonPhpFunctions.i");
10 
11 HCU_ImportVars($DATA_PARAMETERS, "TOP_LEVEL", array('operation' => 'string', "action" => "string"));
12 $operation= "";
13 if (HCU_array_key_exists("operation", $DATA_PARAMETERS['TOP_LEVEL'])) {
14  $operation = $DATA_PARAMETERS['TOP_LEVEL']['operation'];
15 } else {
16  $operation = $DATA_PARAMETERS['TOP_LEVEL']['action'];
17 }
18 
19 $showSQL= false;
20 
21  if (!CheckPerm($link, $Hu, 'loanAppConfig', $_SERVER['REMOTE_ADDR'])) {
22  // ** Permissions failed
23  // ** redirect to new page
24  header("Location: /hcuadm/hcu_noperm.prg");
25  exit;
26  }
27 
28 switch ($operation)
29 {
30  case "readOptions":
31  $returnArray= readOrModifyOptions($dbh, "read");
32  break;
33  case "createOption":
34  $returnArray= readOrModifyOptions($dbh, "add");
35  break;
36  case "updateOption":
37  $returnArray= readOrModifyOptions($dbh, "update");
38  break;
39  case "deleteOption":
40  $returnArray= readOrModifyOptions($dbh, "delete");
41  break;
42  case "readLoanApplications":
43  $returnArray= readLoanApplications($dbh);
44  break;
45  case "createLoanApplication":
46  $returnArray= createOrUpdateLoanApplication($dbh, true);
47  break;
48  case "updateLoanApplication":
49  $returnArray= createOrUpdateLoanApplication($dbh, false);
50  break;
51  case "deleteLoanApplication":
52  $returnArray= deleteLoanApplication($dbh);
53  break;
54  case "app":
55  $returnArray= editSave($dbh);
56  break;
57  default: $returnArray= array("sql" => array(), "error" => array("Operation not specified: '$operation'"), "record" => "", "operation" => "");
58 }
59 
60 if (!$showSQL)
61  unset($returnArray["sql"]);
62 header('Content-type: application/json');
63 print HCU_JsonEncode($returnArray);
64 
65 function getConfigOptions($isLive)
66 {
67  return array("configHomeLogin" => array("text" => "Allow Home Banking Login", "type" => "boolean"),
68  "configDisableEmail" => array("text" => "Disable Email Login", "type" => "boolean"),
69  "configSSOOnly" => array("text" => "SSO Only", "type" => "boolean"),
70  "configLoanSubmit" => array("text" => "Post Loan Applications to Core", "type" => "boolean", "disabled" => !$isLive, "defaultValue" => true),
71  "configLoanSubmitMember" => array("text" => "Post Primary Member With Loan App", "type" => "boolean"),
72  "configLoanFormat" => array("text" => "Post Loan Applications Format", "type" => "list"),
73  "configLoanMIR" => array("text" => "Allow Member Info to Populate From Core", "type" => "boolean"),
74  "configLoanCSS" => array("text" => "Loan App Stylesheet", "type" => "url"),
75  "configLoanIntroMbr" => array("text" => "Loan Login Message for Members", "type" => "string"),
76  "configLoanIntroNonHBMbr" => array("text" => "Loan Login Message for Non-Home Banking Members", "type" => "string"),
77  "configLoanIntroNon" => array("text" => "Loan Login Message for non-Members", "type" => "string"),
78  "configLoanDataOnly" => array("text" => "Submit Fields With Data Only", "type" => "boolean"),
79  "configPassword" => array("text" => "Password Requirements", "type" => "string")
80  );
81 }
82 
83 function getConfigLists()
84 {
85  return array("configLoanFormat" => array("XML", "JSON", "SERIAL"));
86 }
87 
88 function readOrModifyOptions($dbh, $mode)
89 {
90  $parameters= array();
91  $allowed= array("cu" => "string");
92  if ($mode != "read")
93  {
94  $allowed["configId"]= "string";
95  $allowed["value"]= "string";
96  }
97  HCU_ImportVars($parameters, "BOTTOM_LEVEL", $allowed);
98 
99  // Since it is in a json array, encode it and THEN prep for the database.
100  if ($mode != "read") {
101  $configId= $parameters["BOTTOM_LEVEL"]["configId"];
102  }
103  if ($mode != "read" && $mode != "delete") {
104  $value= $parameters["BOTTOM_LEVEL"]["value"];
105  }
106 
107  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
108  array("cu" => array("type" => "string", "required" => true, "maxlength" => 12),
109  "configId" => array("type" => "string", "required" => true),
110  "value" => array("type" => "string", "required" => false)));
111 
112  $cu= strtoupper($parameters["BOTTOM_LEVEL"]["cu"]);
113 
114  $findAppConfigSQL= "select appconfig, cu from lnappconfig where upper(cu) = '$cu'";
115  $findLiveBatchSQL= "select livebatch from cuadmin where upper(user_name) = '$cu'";
116  $sqls= array($findAppConfigSQL, $findLiveBatchSQL);
117 
118  $appResults= runSelectStatement($findAppConfigSQL, $dbh, array("appconfig" => "appconfig", "cu" => "cu"));
119  $liveBatchResults= runSelectStatement($findLiveBatchSQL, $dbh, array("livebatch" => "livebatch"));
120  $errors= array_merge($errors, $appResults["error"], $liveBatchResults["error"]);
121 
122  $appConfig = "";
123  $isLive = false;
124  $hasAppConfigRecord = false;
125  if (HCU_array_key_exists(0, $appResults["record"])) {
126  $appConfig= strval($appResults["record"][0]["appconfig"]);
127  $isLive= trim($liveBatchResults["record"][0]["livebatch"]) == "L";
128  $hasAppConfigRecord= strval($appResults["record"][0]["cu"]) != "";
129  }
130 
131  $configOptions= getConfigOptions($isLive);
132 
133  $options= $appConfig == "" ? array() : HCU_JsonDecode($appConfig, true);
134  $gridData= array();
135  if (is_array($options))
136  {
137  if ($mode != "read")
138  {
139  switch($mode)
140  {
141  case "delete":
142  unset($options[$configId]);
143  break;
144  case "add":
145  case "update":
146  $options[$configId]= $value;
147  break;
148  default:
149  $errors[]= "$mode is not a valid mode.";
150  }
151  $appConfig= prepSave(HCU_JsonEncode($options));
152  $updateSQL= $hasAppConfigRecord ? "update lnappconfig set appconfig='$appConfig' where upper(cu) = '$cu'"
153  : "insert into lnappconfig (cu, appconfig) values ('$cu', '$appConfig')";
154  $sqls[]= $updateSQL;
155  if (count($errors) == 0)
156  {
157  $results= runExecStatement($updateSQL, $dbh);
158  $errors= array_merge($errors, $results["error"]);
159  }
160  }
161 
162  if ($mode == "read")
163  {
164  foreach($options as $key => $value)
165  {
166  $config= $configOptions[$key];
167  $configText= isset($config["text"]) ? $config["text"] : $key;
168  $type= isset($config["type"]) ? $config["type"] : "string";
169  $disabled= isset($config["disabled"]) ? $config["disabled"] : false;
170  $gridData[]= array("configId" => $key, "configText" => $configText, "value" => $value, "type" => $type, "disabled" => $disabled);
171  }
172  }
173  else if ($mode != "delete")
174  {
175  $config= $configOptions[$configId];
176  $configText= isset($config["text"]) ? $config["text"] : $configId;
177  $type= isset($config["type"]) ? $config["type"] : "string";
178  $disabled= isset($config["disabled"]) ? $config["disabled"] : false;
179  $gridData[]= array("configId" => $configId, "configText" => $configText, "value" => $value, "type" => $type, "disabled" => $disabled);
180  }
181  }
182  else
183  $errors[]= "Options not coded correctly for $cu.";
184 
185  $returnArray= array("error" => $errors, "sql" => $sqls, "operation" => $mode);
186 
187  if ($mode != "delete")
188  $returnArray["record"]= $gridData;
189 
190  if ($mode == "read")
191  {
192  $configDDL= array();
193  $configLists= getConfigLists();
194  foreach($configOptions as $key => $record)
195  {
196  $record["value"]= $key;
197  $configDDL[]= $record;
198  }
199  $returnArray["configDDL"]= $configDDL;
200  $returnArray["configLists"]= $configLists;
201  }
202 
203  return $returnArray;
204 }
205 
206 function readLoanApplications($dbh)
207 {
208  $parameters= array();
209  HCU_ImportVars($parameters, "BOTTOM_LEVEL", array("cu" => "string"));
210 
211  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
212  array("cu" => array("type" => "string", "required" => true, "maxlength" => 12)));
213 
214  $cu= strtolower($parameters["BOTTOM_LEVEL"]["cu"]);
215 
216  // ** Order the results with the template firt followed in CU alphanumeric order
217  $sql= "select asm.loanid, asm.cu, asm.loantitle, asm.loandisclosure_fragment, asm.lastmodified from lnappschemamaster asm order by case when cu = 'DMSTEMPL' then 0 else 1 end, cu, loanid";
218  $sqls= array($sql);
219 
220  $records= array();
221  $templateDDL= array(array("value" => 0, "text" => "(None)"));
222  if (count($errors) == 0)
223  {
224  $results= runSelectStatement($sql, $dbh, array("loanid" => "loanId", "cu" => "cu", "loantitle" => "title", "loandisclosure_fragment" => "fragment", "lastmodified" => "lastModified"));
225  $errors= array_merge($errors, $results["error"]);
226 
227  foreach($results["record"] as $record)
228  {
229  $templateDDL[]= array("value" => $record["loanId"], "text" => $record['cu'] . " - " . $record["title"]);
230  if (((strtoupper($cu) == 'DMSTEMPL') && $record['cu'] == 'DMSTEMPL') || $record["cu"] == strtoupper($cu))
231  {
232  unset($record["cu"]);
233  $records[]= $record;
234  }
235  }
236 
237  $templateDDL[]= array("value" => "", "text" => "Blank");
238  }
239 
240  return array("sql" => $sqls, "error" => $errors, "record" => $records, "operation" => "read", "templateDDL" => $templateDDL);
241 }
242 
243 function createOrUpdateLoanApplication($dbh, $isCreate)
244 {
245  $parameters= array();
246  $allowed= array("title" => "string", "fragment" => "string");
247  if ($isCreate)
248  {
249  $allowed["cu"]= "string";
250  $allowed["templateId"]= "string";
251  }
252  else
253  {
254  $allowed["loanId"]= "string";
255  }
256  HCU_ImportVars($parameters, "BOTTOM_LEVEL", $allowed);
257 
258 
259  $title= $parameters["BOTTOM_LEVEL"]["title"];
260  $fragment= $parameters["BOTTOM_LEVEL"]["fragment"];
261 
262  $sqls= array();
263  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
264  array("cu" => array("type" => "string", "required" => true, "maxlength" => 12),
265  "title" => array("type" => "string", "required" => true, "maxlength" => 50),
266  "fragment" => array("type" => "string", "required" => false, "maxlength" => 50),
267  "templateId" => array("type" => "int", "required" => false),
268  "loanId" => array("type" => "int", "required" => true)));
269 
270  $titleClean= $parameters["BOTTOM_LEVEL"]["title"];
271  $fragmentClean= $parameters["BOTTOM_LEVEL"]["fragment"];
272 
273  if (count($errors) == 0)
274  {
275  if ($isCreate)
276  {
277  $cu= $parameters["BOTTOM_LEVEL"]["cu"];
278  $cuClean= $parameters["BOTTOM_LEVEL"]["cu"];
279  $templateId= intval($parameters["BOTTOM_LEVEL"]["templateId"]);
280 
281  $sql= "insert into lnappschemamaster (cu, loantitle, loandisclosure_fragment, lastmodified) values ('$cuClean', '$titleClean', '$fragmentClean', now());
282  select currval('lnappschemamaster_loanid_seq'::regclass) as next";
283  $sqls[]= $sql;
284  $results= runSelectStatement($sql, $dbh, array("next" => "next"));
285  $errors= array_merge($errors, $results["error"]);
286  $loanId= intval($results["record"][0]["next"]);
287 
288  if (count($errors) == 0 && $templateId != 0)
289  {
290  $sql= "insert into lnappschemadetail(loanid, pageid, groupid, lineid, fieldtype, fieldvalue, fieldattr) select $loanId, pageid, groupid, lineid, fieldtype, fieldvalue, fieldattr
291  from lnappschemadetail where loanid = $templateId";
292  $sqls[]= $sql;
293  $results= runExecStatement($sql, $dbh);
294  $errors= array_merge($errors, $results["error"]);
295  }
296  }
297  else
298  {
299  $loanId= $parameters["BOTTOM_LEVEL"]["loanId"];
300 
301  $sql= "update lnappschemamaster set loantitle= '$titleClean', loandisclosure_fragment= '$fragmentClean', lastmodified= now() where loanid= $loanId";
302  $sqls[]= $sql;
303  $results= runExecStatement($sql, $dbh);
304  $errors= array_merge($errors, $results["error"]);
305  }
306  }
307  return array("error" => $errors, "sql" => $sqls, "operation" => $isCreate ? "create" : "update", "record" => array(array("loanId" => $loanId, "title" => $title, "fragment" => $fragment)));
308 }
309 
310 function deleteLoanApplication($dbh)
311 {
312  $parameters= array();
313  HCU_ImportVars($parameters, "BOTTOM_LEVEL", array("loanId" => "string", "cu" => "string"));
314 
315  $sqls= array();
316  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
317  array("loanId" => array("type" => "int", "required" => true),
318  "cu" => array("type" => "string", "required" => true, "maxlength" => 12)));
319 
320  $loanId= $parameters["BOTTOM_LEVEL"]["loanId"];
321  $cu= strtolower($parameters["BOTTOM_LEVEL"]["cu"]);
322 
323  $sql= "delete from lnappschemadetail where loanid = $loanId and exists (select 'FOUND' from lnappschemamaster where loanid = $loanId and lower(cu) = '$cu');
324  delete from lnappschemamaster where loanid = $loanId and lower(cu) = '$cu'";
325  $sqls= array($sql);
326  if (count($errors) == 0)
327  {
328  $results= runExecStatement($sql, $dbh);
329  $errors= array_merge($errors, $results["error"]);
330  }
331 
332  return array("sql" => $sqls, "error" => $errors, "operation" => "delete");
333 }
334 
335 function editSave($dbh)
336 {
337  $parameters= array();
338  HCU_ImportVars($parameters, "BOTTOM_LEVEL", array("loanid" => "string"));
339  $loanid= $parameters["BOTTOM_LEVEL"]["loanid"];
340 
341  // ** I messed up on my naming, I mixed the - sign in the field names
342  // ** and by the time I noticed I had written almost all the form for
343  // ** editing. So at this time, I will instead use the _POST variable
344  // ** For accessing data here
345  // ** LOOP THROUGH THE lnapp-item values
346  $sql = "";
347 
348  // ** FIRST -- DELETE ALL ROWS
349  // ** Deleting rows ensures removal of any deletes
350  $sql = "DELETE FROM lnappschemadetail
351  WHERE loanid = " . intval($loanid) . "; ";
352 
353  foreach ($_POST['lnapp-item'] as $item_value) {
354  // ** We have either an insert or an update
355  // ** ALL RECORDS ARE INSERTS -- SOME ARE KNOWN detailid records -
356  // ** in which case we use the value from the POST
357  if ($item_value < 0) {
358  // ** NEW RECORD -- CREATE INSERT
359  $sql .= "INSERT INTO lnappschemadetail (loanid, pageid, groupid, lineid, fieldtype, fieldvalue, fieldattr)
360  VALUES
361  ( " . intval($loanid) . ",
362  " . intval($_POST['lnapp-item-page-' . $item_value]) . ",
363  " . intval($_POST['lnapp-item-group-' . $item_value]) . ",
364  " . intval($_POST['lnapp-item-line-' . $item_value]) . ",
365  '" . prep_save($_POST['lnapp-item-fieldtype-' . $item_value], 10) . "',
366  '" . prep_save($_POST['lnapp-item-fieldvalue-' . $item_value], 2000) . "',
367  '" . prep_save($_POST['lnapp-item-fieldattr-' . $item_value], 3000) . "'
368  ); ";
369 
370  } else {
371  $sql .= "INSERT INTO lnappschemadetail (detailid, loanid, pageid, groupid, lineid, fieldtype, fieldvalue, fieldattr)
372  VALUES
373  ( " . intval($item_value) . ",
374  " . intval($loanid) . ",
375  " . intval($_POST['lnapp-item-page-' . $item_value]) . ",
376  " . intval($_POST['lnapp-item-group-' . $item_value]) . ",
377  " . intval($_POST['lnapp-item-line-' . $item_value]) . ",
378  '" . prep_save($_POST['lnapp-item-fieldtype-' . $item_value], 10) . "',
379  '" . prep_save($_POST['lnapp-item-fieldvalue-' . $item_value], 2000) . "',
380  '" . prep_save($_POST['lnapp-item-fieldattr-' . $item_value], 3000) . "'
381  ); ";
382  }
383  }
384  if ($sql != '') {
385  // ** ALSO Update the modified date on the lnappschemamaster record
386  $sql .= "UPDATE lnappschemamaster
387  SET lastmodified = current_timestamp
388  WHERE loanid = " . intval($loanid) . " ";
389  $sqls= array($sql);
390  $results= runExecStatement($sql, $dbh);
391  if (count($results["error"]) > 0)
392  return array("Result" => "ERROR", "sql" => array($sql), "Message" => $results["error"][0]);
393  else
394  return array("Result" => "OK", "sql" => array($sql));
395  }
396 
397 }