Odyssey
aGroupDelete.prg
1 <?php
2 /**
3  * @package GroupHub
4  * @author MGHandy
5  *
6  * @uses admin group delete
7  * * delete orphaned groups
8  * * delete users from group by going to user hub
9  *
10  * @var string $sOperation: requested search action
11  */
12 
13 require_once("$admLibrary/aGroupSupport.i");
14 
15 try {
16  $admVars = array();
17  $admOk = array(
18  "operation" => array("filter" => FILTER_SANITIZE_STRING),
19  "payload" => array("filter" => FILTER_SANITIZE_STRING)
20  );
21  HCU_ImportVars($admVars, "GROUP_DELETE", $admOk);
22 
23  $dOperation = isset($admVars["GROUP_DELETE"]["operation"]) ? $admVars["GROUP_DELETE"]["operation"] : null;
24  $dPayload = isset($admVars["GROUP_DELETE"]["payload"]) ? $admVars["GROUP_DELETE"]["payload"] : null;
25 
26  // decrypt payload only if there is one
27  // payload won't exist if operation is empty
28  // operation empty means just load card content
29  $dGroup = $dPayload ?
30  GroupDecrypt($SYSENV, $Cu, $dPayload) :
31  null;
32 
33  // load context depending on payload existence.
34  $dContext = $dPayload ?
35  GroupContext($SYSENV, $Cu, $dGroup['group']) :
36  GroupContext($SYSENV, $Cu);
37 
38  $aryResult = array();
39  $aryReply = array();
40 
41  switch ($dOperation) {
42  case "":
43  PrintGroupDelete();
44  break;
45  case "groupDelete":
46  header('Content-type: application/json');
47 
48  $gDelete = GroupDelete($SYSENV, $dbh, $dContext);
49  $aryResult['data']['group'] = $gDelete['group'];
50  $aryResult['info'] = $gDelete['message'];
51  GroupReply($aryResult, $aryReply, $dOperation);
52  break;
53  default:
54  throw new Exception("Unknown server request: " . $dOperation);
55  break;
56  }
57 
58 } catch (Exception $e) {
59  $aryReply['errors'][] = $e->getMessage();
60  $aryResult['data'] = array();
61  $aryResult['info'] = array();
62 
63  GroupReply($aryResult, $aryReply, $dOperation);
64 }
65 
66 function GroupDelete($pEnv, $pDbh, $pContext) {
67  $gId = $pContext['g_id'];
68  $pId = $pContext['p_id'];
69  $cuTable = $pContext['cu_table'];
70  $cuCode = $pContext['cu_code'];
71 
72  $sqlReturn = array();
73  $sqlDelete = "
74  DELETE FROM {$cuTable}group
75  WHERE group_id = $gId
76  AND profile_id = $pId
77  RETURNING
78  group_id AS g_id,
79  profile_id AS p_id,
80  group_name AS g_name";
81 
82  $sqlDeleteRs = db_query($sqlDelete, $pDbh);
83  if (!$sqlDeleteRs) {
84  $pEnv['logger']->error(db_last_error());
85  throw new Exception("Failed to delete group.");
86  }
87 
88  $sqlReturn['group'] = db_fetch_all($sqlDeleteRs)[0];
89  $sqlReturn['message'] = "The following group has been deleted successfully:";
90  return $sqlReturn;
91 }
92 ?>
93 
94 <?php function PrintGroupDelete() { ?>
95 <div id="gdDelete">
96  <div class="hcu-template">
97  <div class="hcu-edit-buttons k-state-default">
98  <span class="hcu-icon-delete">
99  <!--<a href="##" id="lnkDelete">
100  <i class="fa fa-trash fa-lg"></i>
101  </a>-->
102  </span>
103  <a href="##" id="lnkCancel">Cancel</a>
104  &ensp;
105  <a href="##" id="btnUpdate" class="k-button k-primary">
106  <i class="fa fa-trash fa-lg"></i>
107  Delete
108  </a>
109  </div>
110  </div>
111 </div>
112 
113 <div id="gdInfo"></div>
114 
115 <script type="text/x-kendo-template" id="gdOrphaned">
116  <div class="col-sm-12">
117  <p>You are about to remove this group from the system.</p>
118  <p>Do you wish to continue?</p>
119  </div>
120 </script>
121 
122 <script type="text/x-kendo-template" id="gdFull">
123  <div class="col-sm-12">
124  <p>There are existing users in this group.</p>
125  <p>Groups cannot be deleted if there are users.
126  Use the Group Users card to view which users
127  are still using this group.</p>
128  </div>
129 </script>
130 
131 <script type="text/javascript">
132  var GroupDelete = function() {
133  var gdCardContainer = null;
134  var gdCardWindows = null;
135 
136  var gdCall = null;
137  var gdGroup = null;
138  var gdPayload = null;
139  var gdAction = null;
140 
141  var gdDataSource = null;
142  var gdDelete = null;
143  var gdInfo = null;
144 
145  var EventOpenWindow = function(e) {
146  var windowElement = this.element[0];
147  var windowId = windowElement.id;
148 
149  switch (windowId) {
150 
151  }
152 
153  gdCardWindows.push(this);
154  }
155 
156  var EventCloseWindow = function(e) {
157  var windowElement = this.element[0];
158  var windowId = windowElement.id;
159 
160  switch (gdAction) {
161  case "deleteConfirm":
162  EventPopWindow(windowId);
163  var groupRequest = {
164  operation: "groupDelete",
165  payload: gdPayload
166  };
167 
168  gdDataSource.transport.options.read.type = "POST";
169  gdDataSource.read(groupRequest);
170  break;
171  default:
172  EventPopWindow(windowId);
173  break;
174  }
175 
176  gdAction = null;
177  }
178 
179  var EventPopWindow = function(windowId) {
180  var popIndex = -1;
181  for (var i = 0; i < gdCardWindows.length; i++) {
182  var openWindow = gdCardWindows[i].element[0];
183  var openId = openWindow.id;
184 
185  if (openId == windowId) {
186  popIndex = i;
187  break;
188  }
189  }
190 
191  if (popIndex > -1) {
192  gdCardWindows.splice(popIndex, 1);
193  }
194  }
195 
196  var InitDataSources = function() {
197  gdDataSource = new kendo.data.DataSource({
198  transport: {
199  read: {
200  url: "main.prg",
201  dataType: "json",
202  contentType: "application/x-www-form-urlencoded",
203  type: "GET",
204  data: {
205  ft: "102106"
206  },
207  cache: false
208  }
209  },
210  schema: {
211  parse: function(response) {
212  var results = null;
213  var resultData = null;
214  var resultOperation = null;
215 
216  // check for valid response
217  if (response.hasOwnProperty("Results")) {
218  results = response.Results;
219  resultData = results.data;
220  } else {
221  gdInfo.content("Error Parsing Server");
222  gdInfo.open();
223  return [];
224  }
225 
226  // check for errors
227  if (results.hasOwnProperty("errors")) {
228  gdInfo.content(results.errors);
229  gdInfo.open();
230  return [];
231  }
232 
233  // valid response and no errors, clear group hub and show message
234  var content = "";
235  content += "<p>" + results.info + "</p>";
236  content += "<p></p>";
237  content += "<p><strong>" + gdGroup.g_name + "</strong></p>";
238 
239  gdInfo.content(content);
240  gdInfo.open();
241 
242  gdCall("removeGroupInfo");
243  gdCall("updateGroupResults", []);
244 
245  return [];
246  }
247  }
248  });
249  }
250 
251  var InitDataViews = function() {
252  gdDelete = $("#gdDelete").kendoWindow({
253  title: "Delete Group",
254  modal: true,
255  visible: false,
256  resizable: false,
257  minWidth: 300,
258  maxWidth: 500,
259  activate: EventOpenWindow,
260  close: EventCloseWindow
261  }).data("kendoWindow");
262 
263  gdInfo = $("#gdInfo").kendoDialog({
264  title: "Delete Group",
265  modal: true,
266  visible: false,
267  resizable: false,
268  minWidth: 300,
269  maxWidth: 300,
270  show: EventOpenWindow,
271  close: EventCloseWindow,
272  actions: [
273  { text: "Ok", primary: true }
274  ]
275  }).data("kendoDialog");
276  }
277 
278  var InitDataActions = function() {
279  $("#btnUpdate").off();
280  $("#btnUpdate").on("click", function(e) {
281  e.preventDefault();
282  if (gdGroup.g_count == 0) {
283  gdAction = "deleteConfirm";
284  gdDelete.close();
285  }
286  });
287 
288  $("#lnkCancel").off();
289  $("#lnkCancel").on("click", function(e) {
290  e.preventDefault();
291  gdAction = "deleteDeny";
292  gdDelete.close();
293  })
294  }
295 
296  this.Open = function(windowStack) {
297  gdCardWindows = windowStack;
298 
299  if (gdGroup.g_count == 0) {
300  $("#gdDelete").prepend(
301  $("#gdOrphaned").html()
302  );
303  } else {
304  $("#gdDelete").prepend(
305  $("#gdFull").html()
306  );
307  }
308 
309  var template= kendo.template($("#titleTemplate").html());
310  gdGroup.cardTitle= "Delete Group";
311  gdDelete.title(template(gdGroup));
312  gdDelete.center();
313  gdDelete.open();
314  }
315 
316  this.Close = function() {
317  gdDelete.destroy();
318  }
319 
320  this.Data = function(payload, group) {
321  gdPayload = payload;
322  gdGroup = group;
323  }
324 
325  this.Init = function(hubCall, carContainer) {
326  gdCall = hubCall;
327  gdCardContainer = carContainer;
328 
329  InitDataSources();
330  InitDataViews();
331  InitDataActions();
332 
333  gdCall("GroupDelete", this);
334  }
335  }
336 </script>
337 <?php }