Odyssey
userSupportAlerts.prg
1 <?php
2 /**
3  * @package UserSupport (Subpackage alert)
4  * @author SPB
5  *
6  * This script is run when the user opens up the alert card in the user hub. It cannot be run independently of that.
7  */
8 $string = array("filter" => HCUFILTER_INPUT_STRING);
9 $parameters = array("a" => array("operation" => "", "payload" => "", "alerts" => ""));
10 HCU_ImportVars($parameters, "a", array("operation" => $string, "payload" => $string, "alerts" => $string));
11 extract($parameters["a"]);
12 
13 $operation = !isset($operation) ? "" : trim($operation);
14 $payload = isset($payload) ? trim($payload) : "";
15 $alerts = isset($alerts) ? trim($alerts) : "";
16 $userId = null;
17 try { $userId = HCU_PayloadDecode($Cu, $payload); } catch(exception $e) {}
18 $userId = isset($userId) ? intval($userId["user_id"]) : null;
19 
20 if ($operation != "") {
21  if (isset($userId)) {
22  switch($operation) {
23  case "saveAlert":
24  $returnArray = saveAlert($dbh, $Cu, $userId, $Cn, $alerts);
25  break;
26  default: // Won't get here
27  $returnArray = array("error" => array("Operation not specified: '$operation'"), "record" => array());
28  }
29  } else {
30  $returnArray = array("error" => array("No User Found"), "record" => array());
31  }
32 
33  header('Content-type: application/json');
34  print HCU_JsonEncode($returnArray);
35 } else {
36  if (isset($userId)) {
37  printPage("$menu_link?ft=$ft", $userId, readAlerts($dbh, $Cu, $userId), $payload);
38  } else { ?>
39  <div class='noUserFound'><div>No User Found</div></div>
40  <?php }
41 }
42 
43 /**
44  * function alert($dbh, $Cu, $showSQL)
45  *
46  * Gets data for the alert tab.
47  *
48  * @param integer $dbh Database handle
49  * @param string $Cu The current credit union
50  * @param integer $userId The banking user id
51  *
52  * @return array("sql" => array(), "error" => array(), "record" => array())
53  */
54 function readAlerts($dbh, $Cu, $userId) {
55  $errors = array();
56  $data = Get_AlertsDetailed($dbh, $Cu, $userId, GetFlagsetValue("CU3_PREPEND_MBR_DESC")); // For admin, account number is always shown and unmasked.
57  $errors = array_merge($errors, $data["errors"]);
58 
59  $records = array();
60 
61  // Get what I need
62  foreach($data["data"] as $record) {
63  $isCell = $record["emailtype"] == "W";
64  $newRecord = array("id" => $record["id"], "alertDescription" => $record["type_name"], "accntDescription" => $record["description"],
65  "alerted" => $record["lastalert"], "isCell" => $isCell, "destinationEmail" => "", "destinationPhone" => 0, "checked" => $record["alertstatus"] == 1,
66  "accountNumber" => trim($record["accountnumber"]));
67 
68  if ($isCell) {
69  $newRecord["destinationPhone"] = intval($record["notifyto"]);
70  } else {
71  $newRecord["destinationEmail"] = $record["notifyto"];
72  }
73  $records[] = $newRecord;
74  }
75  $returnArray = array("error" => $errors, "record" => $records);
76 
77  return $returnArray;
78 }
79 
80 /**
81  * function alertSave($dbh, $Cu, $showSQL, $loggedInUser)
82  *
83  * Save database call that happens when user changes a row checkbox on the alert screen.
84  *
85  * @param integer $dbh Database handle
86  * @param string $Cu The current credit union
87  * @param integer $userId The banking user id
88  * @param string $Cn The Logged in User.
89  * @param string $alerts The alerts to save (JSON)
90  *
91  * @return array("sql" => array(), "error" => array(), "record" => array())
92  */
93 function saveAlert($dbh, $Cu, $userId, $Cn, $alerts) {
94  $parameters = array();
95  $data = Update_AlertStatuses($dbh, $Cu, $userId, $alerts, $Cn, true);
96 
97  $errors = $data["errors"];
98  return array("error" => $errors);
99 }
100 
101 /**
102  * function printPage($self, $userId, $readData)
103  * This function will print out the popup for the audit.
104  *
105  * @param string $self -- the URL of this script
106  * @param integer $userId -- the user id banking side
107  * @param array $readData -- the data to read
108  * @param string $payload -- the payload for the data calls to use if from the userhub.
109  */
110 function printPage($self, $userId, $readData, $payload) { ?>
111  <script type="text/javascript">
112  //# sourceURL=alerts.js
113 
114  <?php
115  /**
116  * function dirtify(dirty, container)
117  * This function sets the dirty flag after the cell is changed.
118  *
119  * @param Boolean dirty -- if there are changes or not.
120  * @param HTMLElement container -- this is the cell. (TD tag)
121  */
122  ?>
123  function dirtify(dirty, container) {
124  if (dirty) {
125  $(container).addClass("k-dirty-cell");
126  $(container).prepend("<span class='k-dirty'></span>");
127  } else {
128  $(container).removeClass("k-dirty-cell");
129  $(container).find(".k-dirty").remove();
130  }
131  }
132 
133  <?php
134  /**
135  * function init()
136  * This initializes the whole shebang.
137  */
138  ?>
139  function init() {
140  $.homecuValidator.setup({formValidate: "userAlertsDiv", formStatusField: "formValidateAlertsDiv"});
141  $("#externalTabWindow").data("preferredHeight", 450);
142  var alertData = [];
143  var alertGrid = $("#alertGrid").kendoGrid({
144  dataSource: {
145  transport: {
146  read: function(options) {
147  options.success(alertData);
148  }
149  },
150  schema: {
151  model: {
152  id: "id",
153  fields: {
154  id: {type: "number"},
155  alertDescription: {type: "string"},
156  accntDescription: {type: "string"},
157  alerted: {type: "date"},
158  isCell: {type: "boolean"},
159  destinationEmail: {type: "string"},
160  destinationPhone: {type: "number"},
161  // destinationCarrier: {type: "string"},
162  checked: {type: "boolean"},
163  originalChecked: {type: "boolean"},
164  accountNumber: {type: "string"}
165  }
166  }
167  }
168  },
169  autoBind: false,
170  scrollable: false,
171  columns: [
172  {headerTemplate: "<center>Enabled</center>", width: 45, field: "checked"},
173  {title: "Sub-Account"},
174  {title: "Description", field: "alertDescription"},
175  {title: "Last Alert", field: "alerted"},
176  {title: "Destination"}
177  ],
178  rowTemplate: "# var alertDate= kendo.toString(alerted, 'MM/dd/yyyy'); var alertTime= kendo.toString(alerted, 'HH:mm:ss'); #"
179  + "<tr data-uid='#: uid #'>"
180  + "<td><center> # if (checked) { # <div class='restriction allow hcu-all-100'><i class='fa fa-check'></i></div> # } else { #"
181  + "<div class='restriction ban hcu-all-100'><i class='fa fa-ban'></i></div> # } # </center></td>"
182  + "<td>#: accntDescription #</td><td>#: alertDescription #<span class='hidden-xs hidden-sm'> Alert</span></td>"
183  + "<td># if (alerted == null) { # &nbsp; # } else { # #: alertDate #<span class='hidden-xs hidden-sm'> #: alertTime #</span># } #</td>"
184  + "<td># if (isCell) { #<i class='fa fa-phone-square'></i>&nbsp;&nbsp;&nbsp;#: destinationPhone # # } else { #"
185  + "<i class='fa fa-envelope'></i>&nbsp;&nbsp;&nbsp;#: destinationEmail # # } #</td></tr>",
186  noRecords: {
187  template: "<tr><td colspan='5'><span class='hcu-secondary'><span class='vsgSecondary'>No Records Found</span></span></td></tr>"
188  }
189  // @ #: destinationCarrier #
190  }).data("kendoGrid");
191 
192  $(".userAlertsDiv .cancelBtn").click(function() {
193  userSupportDoOnClose();
194  return false;
195  });
196 
197  $(".userAlertsDiv .updateBtn").click(function() {
198  var gridData = $("#alertGrid").data("kendoGrid").dataSource.data();
199  var changedAlerts = [];
200  for(var i = 0; i != gridData.length; i++) {
201  var value = gridData[i].checked;
202  var id = gridData[i].id;
203  if (value == gridData[i].originalChecked) {
204  continue;
205  }
206  changedAlerts.push({id: id, alertstatus: value ? "1" : "0"});
207  }
208 
209  showWaitWindow();
210  $.post("<?php echo $self; ?>&operation=saveAlert", {alerts: kendo.stringify(changedAlerts), payload: "<?php echo $payload; ?>"}, function(data) {
211  hideWaitWindow();
212  if (data.error.length > 0) {
213  $.homecuValidator.displayMessage(data.error, $.homecuValidator.settings.statusError );
214  } else {
215  for(var i = 0; i != gridData.length; i++) {
216  gridData[i].dirty = false;
217  gridData[i].originalChecked = gridData[i].checked;
218  }
219  postPostPostPost();
220  }
221  });
222  return false;
223  });
224 
225  $("#alertGrid").on("click", ".restriction.allow", function() {
226  var td = $(this).closest("td");
227  $(td).html("<center><div class='restriction ban hcu-all-100'><i class='fa fa-ban'></i></div></center>");
228  var tr = $(td).closest("tr");
229  var dataItem = alertGrid.dataItem(tr);
230  dataItem.dirty = true;
231  dataItem.checked = false;
232  dirtify(dataItem.checked != dataItem.originalChecked, td);
233  return false;
234  });
235 
236  $("#alertGrid").on("click", ".restriction.ban", function() {
237  var td = $(this).closest("td");
238  $(td).html("<center><div class='restriction allow hcu-all-100'><i class='fa fa-check'></i></div></center>");
239  var tr = $(td).closest("tr");
240  var dataItem = alertGrid.dataItem(tr);
241  dataItem.dirty = true;
242  dataItem.checked = true;
243 
244  dirtify(dataItem.checked != dataItem.originalChecked, td);
245  return false;
246  });
247 
248  $("#alertGrid").on("click", "tbody tr", function(e) {
249  $(this).find(".restriction").click();
250  });
251 
252  var data= <?php echo HCU_JsonEncode($readData); ?>;
253  if (data.error.length > 0) {
254  $.homecuValidator.displayMessage(data.error, $.homecuValidator.settings.statusError );
255  } else {
256  for(var i = 0; i != data.record.length; i++) {
257  var record = data.record[i];
258  record.kendoIndex = i + 1;
259  record.originalChecked = record.checked;
260  }
261  alertData = new kendo.data.ObservableArray(data.record);
262  alertGrid.dataSource.read();
263  $("#alertGrid").css({maxHeight: 300, overflowY: "auto"});
264  }
265  }
266 
267  <?php
268  /**
269  * function postPostPostPost()
270  * This obviously happens after everything else.
271  */
272  ?>
273  function postPostPostPost() {
274  $("#externalTabWindow").data("isClosing", true);
275  $("#externalTabWindow").data("kendoWindow").close();
276  $("#externalTabWindow").data("isClosing", false);
277  }
278 
279  <?php
280  /**
281  * function userSupportDoOnClose()
282  * Whew! I saved a couple of lines. No, this is needed as it is the hook function from the userSupportHead script.
283  */
284  ?>
285  function userSupportDoOnClose() {
286  potentiallyCancelChanges();
287  $("#externalTabWindow").data("shouldClose", false);
288  }
289 
290  <?php
291  /**
292  * function potentiallyCancelChanges()
293  * This displays the discard changes dialog on close if there are changes made.
294  */
295  ?>
296  function potentiallyCancelChanges() {
297  if ($.grep($("#alertGrid").data("kendoGrid").dataSource.data(), function(n,i) { return n.originalChecked != n.checked; }).length == 0) {
298  postPostPostPost();
299  } else {
300  var discardChangesDialog= $("#discardChangesDialog").data("kendoDialog");
301  if (discardChangesDialog == null) {
302  discardChangesDialog = $("<div id='discardChangesDialog'></div>").appendTo("body").kendoDialog({
303  title: "Discard Changes",
304  content: "<p>Changes have been made to this user's alerts.</p><p>Do you wish to discard your changes?</p>",
305  actions: [
306  {text: "No"},
307  {text: "Yes", primary: true, action: function() {
308  postPostPostPost();
309  }}
310  ],
311  visible: false,
312  open: function() {
313  if (window.activeWindows != null)
314  window.activeWindows.push(this);
315  },
316  close: function() {
317  if (window.activeWindows != null)
318  window.activeWindows.pop();
319  }
320  }).data("kendoDialog");
321  }
322  discardChangesDialog.open();
323  }
324  }
325 
326  init();
327  </script>
328 
329  <div class="container userAlertsDiv vsgPrimary hcu-template hcu-all-100" id="userAlertsDiv">
330  <div class="row">
331  <div id="formValidateAlertsDiv" class="k-block k-error-colored formValidateDiv" style="display:none;"></div>
332  </div>
333  <div class="hcu-secondary">
334  <div class="row small vsgSecondary hcu-note">
335  Click on a row to change enabled status.
336  </div>
337  </div>
338  <div class="row">
339  <div id="alertGrid" class="hcu-all-100"></div>
340  </div>
341  <div class="hcu-edit-buttons k-state-default row">
342  <a class="cancelBtn" href="#">Cancel</a>
343  &nbsp;&nbsp;&nbsp;
344  <a class="updateBtn k-button k-primary" href="#"><i class="fa fa-check"></i>Update</a>
345  </div>
346  </div>
347 <?php }
Definition: User.php:7