Odyssey
mFeature.data
1 <?php
2 /**
3  * @package mFeature.data
4  * Purpose: Handle the CRUD portion of the Features feature in Monitor. Return any requested data
5  * in a JSON format, for the client to display accordingly.
6  *
7  * Call this script with the following parameters
8  * @param string $action : requested database action
9  * @param string $featureList : comma separated list of features for multiple update
10  * @param string $featureCode : single featureCode for create/update
11  * @param string $featureDescription : single description for create/update
12  * @param string $featureLimit : single limit_type for create/update
13  *
14  *
15  * @return JSON OBJECT.
16  */
17 header('Content-Type: application/json');
18 
19 try {
20  $monLibrary= dirname(__FILE__) . "/../library";
21  require_once("$monLibrary/cu_top.i");
22 
23  $var_ok = array(
24  "action" => array('filter' => FILTER_SANITIZE_STRING),
25  "featureList" => array('filter' => FILTER_SANITIZE_STRING),
26  "featureCode" => array('filter' => FILTER_SANITIZE_STRING),
27  "featureDescription" => array('filter' => FILTER_SANITIZE_STRING),
28  "featureLimit" => array('filter' => FILTER_SANITIZE_STRING),
29  );
30 
31  HCU_ImportVars($MON_VARS, "", $var_ok );
32 
33  $action = isset($MON_VARS['action']) ? $MON_VARS['action'] : null;
34  $featureList = isset($MON_VARS['featureList']) ? $MON_VARS['featureList'] : null;
35  $featureCode = isset($MON_VARS['featureCode']) ? $MON_VARS['featureCode'] : null;
36  $featureDescription = isset($MON_VARS['featureDescription']) ? $MON_VARS['featureDescription'] : null;
37  $featureLimit = isset($MON_VARS['featureLimit']) ? $MON_VARS['featureLimit'] : null;
38 
39  $aryResult = array();
40  $aryReply = array();
41 
42  switch ($action) {
43 
44  case "feature_create":
45 
46  // CREATE NEW FEATURE
47  $aryResult = FeatureCreate($SYSENV, $dbh, $featureCode, $featureDescription, $featureLimit);
48  break;
49 
50  case "feature_read":
51 
52  // GET FEATURE LIST
53  $aryResult = FeatureReadList($SYSENV, $dbh);
54  break;
55 
56  case "feature_edit":
57 
58  // EDIT FEATURE
59  $aryResult = FeatureEdit($SYSENV, $dbh, $featureCode, $featureDescription, $featureLimit);
60  break;
61 
62  case "feature_delete":
63 
64  // ENABLE FEATURES
65  $aryResult = FeatureDelete($SYSENV, $dbh, $featureList);
66  break;
67 
68  case "feature_enable":
69 
70  // ENABLE FEATURES
71  $aryResult = FeatureEnable($SYSENV, $dbh, $featureList);
72  break;
73 
74  case "feature_disable":
75 
76  // DISABLE FEATURES
77  $aryResult = FeatureDisable($SYSENV, $dbh, $featureList);
78  break;
79 
80  default:
81 
82  // ACTION UNKNOWN
83  throw new Exception("Feature Action: ( $action ) unknown");
84  break;
85  }
86 
87 } catch (Exception $e) {
88 
89  // ONLY SUPPLY ERRORS
90  $aryReply['errors'] = $e->getMessage();
91 
92  $aryResult['data'] = array();
93  $aryResult['info'] = array();
94 }
95 
96 $aryReply['action'] = $action;
97 if (isset($aryResult['data']) && count($aryResult['data'])) $aryReply['data'] = $aryResult['data'];
98 if (isset($aryResult['info']) && count($aryResult['info'])) $aryReply['info'] = $aryResult['info'];
99 
100 print json_encode(Array("Results" => $aryReply));
101 
102 /**
103  * FeatureSetup:
104  * - format the data being returned to the client side code
105  *
106  * @param array $pQuery : query result containing rows of data for setup
107  *
108  * @return array $sqlReturn : formatted data
109  */
110 function FeatureSetup($pData) {
111  // BUILD RETURN DATA
112  $dataReturn = array();
113 
114  if ($pData == null) { return $dataReturn; }
115 
116  foreach ($pData as $key => $value) {
117 
118  $feature = array();
119  $feature['featureCode'] = trim($value['feature_code']);
120  $feature['featureDescription'] = trim($value['description']);
121  $feature['featureLimit'] = trim($value['limit_type']);
122  $feature['featureEnabled'] = $value['enabled'] == 't' ? true : false;
123 
124  $dataReturn[] = $feature;
125  }
126 
127  return $dataReturn;
128 }
129 
130 /**
131  * FeatureInUse:
132  * - this function used to determine featureCode dependancy, if a feature is
133  * currently being used by a CU
134  *
135  * @param object $pDbh : database reference for access and manipulation of data
136  * @param string $pFeature : featureCode to be checked
137  *
138  * @return array $numSelectRs : 0: no rows found, not in use, 1: rows have been found, in use
139  */
140 function FeatureInUse($pDbh, $pFeature) {
141 
142  // SQL STRING FOR cu_featuremenu
143  $sqlSelectFm = "SELECT feature_code FROM cu_featuremenu
144  WHERE btrim(feature_code) = $pFeature";
145 
146  // QUERY
147  $sqlSelectRs = db_query($sqlSelectFm, $pDbh);
148  $numSelectRs = pg_num_rows($sqlSelectRs);
149 
150  return $numSelectRs != 0;
151 }
152 
153 /**
154  * FeatureDelete:
155  * - delete a list of user defined global features, these features can only be deleted
156  * if they are not currently in use by a CU. These features will be permanently deleted
157  * from the database table.
158  *
159  * @param object $pDbh : database reference for access and manipulation of data
160  * @param string $pFeatures : comma-separated list of featureCode to be deleted
161  *
162  * @return array $sqlReturn : array containing the list of deleted featureCode and success information
163  */
164 function FeatureDelete($pEnv, $pDbh, $pFeatures) {
165 
166  $sqlReturn = array();
167 
168  // EXPLODE DATA
169  $features = "";
170  $featureAry = array();
171  $featureList = explode(',', $pFeatures);
172 
173  foreach ($featureList as $key => $value) {
174  // PREP_SAVE
175  $feature = "'" . prep_save($value, 10) . "'";
176  $inUse = FeatureInUse($pDbh, $feature);
177 
178  if ($inUse) {
179  // CANNOT DELETE, FEATURE IS IN USE
180  $sqlReturn['info'][] = "( $feature ) cannot be deleted, it is currently in use\n";
181  } else {
182  array_push($featureAry, $feature);
183  $sqlReturn['info'][] = "( $feature ) was deleted successfully\n";
184  }
185  }
186 
187  // IMPLODE FOR DELETE
188  $features = implode(",", $featureAry);
189 
190  // SQL STRING FOR DELETE
191  $sqlDelete = "DELETE FROM cu_feature
192  WHERE feature_code IN ($features)
193  RETURNING feature_code, description, limit_type, enabled";
194 
195  // QUERY
196  $sqlDeleteRs = db_query($sqlDelete, $pDbh);
197  if (!$sqlDeleteRs) {
198  $pEnv['logger']->error(db_last_error());
199  throw new Exception("Feature deleted has failed");
200  }
201 
202  $sqlDataRs = db_fetch_all($sqlDeleteRs);
203  $sqlReturn['data'] = FeatureSetup($sqlDataRs);
204 
205  return $sqlReturn;
206 }
207 
208 /**
209  * FeatureEnable:
210  * - enable a list of user defined features, these features will become available
211  * for use by credit unions.
212  *
213  * @param object $pDbh : database reference for access and manipulation of data
214  * @param string $pFeatures : comma-separated list of featureCode to be enabled
215  *
216  * @return array $sqlReturn : array containing a list of only the features which have been updated
217  * along with success information
218  */
219 function FeatureEnable($pEnv, $pDbh, $pFeatures) {
220 
221  $sqlReturn = array();
222 
223  // EXPLODE DATA
224  $features = "";
225  $featureList = explode(',', $pFeatures);
226 
227  foreach ($featureList as $key => $value) {
228  // PREP_SAVE
229  $featureList[$key] = "'" . prep_save($value, 10) . "'";
230  }
231 
232  // IMPLODE FOR DELETE
233  $features = implode(",", $featureList);
234 
235  // SQL STRING FOR UPDATE
236  $sqlUpdate = "UPDATE cu_feature
237  SET enabled = TRUE
238  WHERE feature_code IN ($features)
239  RETURNING feature_code, description, limit_type, enabled";
240 
241  // QUERY & ERROR CHECK
242  $sqlUpdateRs = db_query($sqlUpdate, $pDbh);
243  if (!$sqlUpdateRs) {
244  $pEnv['logger']->error(db_last_error());
245  throw new Exception("Feature update has failed");
246  }
247 
248  // GATHER DATA ROWS AND INFO
249  $sqlDataRs = db_fetch_all($sqlUpdateRs);
250  $sqlReturn['data'] = FeatureSetup($sqlDataRs);
251  $sqlReturn['info'][] = "Feature update was successful\n";
252 
253  return $sqlReturn;
254 }
255 
256 /**
257  * FeatureDisable:
258  * - disable a list of user defined global features, these features will no
259  * longer be available to creditunions until enabled again
260  *
261  * parameters:
262  * @param object $pDbh : database reference for access and manipulation of data
263  * @param string $pFeatures : comma separated list of featureCode to be disabled
264  *
265  * @return array $sqlReturn : array containing a list of only the features which have been updated
266  * along with success information
267  */
268 function FeatureDisable($pEnv, $pDbh, $pFeatures) {
269 
270  $sqlReturn = array();
271 
272  // EXPLODE DATA
273  $features = "";
274  $featureList = explode(',', $pFeatures);
275 
276  // PREP_SAVE
277  foreach ($featureList as $key => $value) {
278  $featureList[$key] = "'" . prep_save($value, 10) . "'";
279  }
280 
281  // IMPLODE FOR DELETE
282  $features = implode(",", $featureList);
283 
284  // SQL STRING FOR UPDATE
285  $sqlUpdate = "UPDATE cu_feature
286  SET enabled = FALSE
287  WHERE feature_code IN ($features)
288  RETURNING feature_code, description, limit_type, enabled";
289 
290  // QUERY & ERROR CHECK
291  $sqlUpdateRs = db_query($sqlUpdate, $pDbh);
292  if (!$sqlUpdateRs) {
293  $pEnv['logger']->error(db_last_error());
294  throw new Exception("Feature update has failed");
295  }
296 
297  // GATHER DATA ROWS AND INFO
298  $sqlDataRs = db_fetch_all($sqlUpdateRs);
299  $sqlReturn['data'] = FeatureSetup($sqlDataRs);
300  $sqlReturn['info'][] = "Feature update was successful\n";
301 
302  return $sqlReturn;
303 }
304 
305 /**
306  * FeatureEdit:
307  * - edit a current global feature available from homecu
308  *
309  * @param object $pDbh : database reference for access and manipulation of data
310  * @param string $pCode : featureCode, feature name
311  * @param string $pDescription : description, feature description
312  * @param string $pLimit : limit_type, feature limit_type
313  *
314  * @return array $sqlReturn : array containing data and success/failure information
315  */
316 function FeatureEdit($pEnv, $pDbh, $pCode, $pDescription, $pLimit) {
317 
318  // PREP_SAVE
319  $code = strtoupper($pCode);
320  $code = prep_save($code, 10);
321 
322  $desc = html_entity_decode($pDescription, ENT_QUOTES);
323  $desc = prep_save($desc, 255);
324 
325  $limit = strtoupper($pLimit);
326  $limit = prep_save($limit, 1);
327 
328  // SQL STRING FOR UPDATE
329  $sqlUpdate = "UPDATE cu_feature
330  SET (description, limit_type) = ('$desc', '$limit')
331  WHERE feature_code = '$code'
332  RETURNING feature_code, description, limit_type, enabled";
333 
334  // QUERY & ERROR CHECK
335  $sqlUpdateRs = db_query($sqlUpdate, $pDbh);
336  if (!$sqlUpdateRs) {
337  $pEnv['logger']->error(db_last_error());
338  throw new Exception("Feature update has failed");
339  }
340 
341  // GATHER DATA ROWS AND INFO
342  $sqlDataRs = db_fetch_all($sqlUpdateRs);
343  $sqlReturn['data'] = FeatureSetup($sqlDataRs);
344  $sqlReturn['info'][] = "Feature update was successful\n";
345 
346  return $sqlReturn;
347 }
348 
349 /**
350  * FeatureCreate:
351  * - create new global feature to be available from homecu
352  *
353  * @param object $pDbh : database reference for access and manipulation of data
354  * @param string $pCode : featureCode, feature name
355  * @param string $pDescription : description, feature description
356  * @param string $pLimit : limit_type, feature limit_type
357  *
358  * @return array $sqlReturn : array containing data and success/failure information
359  */
360 function FeatureCreate($pEnv, $pDbh, $pCode, $pDescription, $pLimit) {
361 
362  // PREP_SAVE
363  $code = strtoupper($pCode);
364  $code = prep_save($code, 10);
365 
366  $desc = html_entity_decode($pDescription, ENT_QUOTES);
367  $desc = prep_save($desc, 255);
368 
369  $limit = strtoupper($pLimit);
370  $limit = prep_save($limit, 1);
371 
372  // SQL STRING FOR CREATE/INSERT
373  $sqlInsert = "INSERT INTO cu_feature
374  (feature_code, description, limit_type, category, enabled)
375  VALUES
376  ('$code', '$desc', '$limit', null, FALSE)
377  RETURNING feature_code, description, limit_type, enabled";
378 
379  // QUERY & ERROR CHECK
380  $sqlInsertRs = db_query($sqlInsert, $pDbh);
381  if (!$sqlInsertRs) {
382  $pEnv['logger']->error(db_last_error());
383  throw new Exception("Feature add has failed");
384  }
385 
386  // GATHER DATA ROWS AND INFO
387  $sqlDataRs = db_fetch_all($sqlInsertRs);
388  $sqlReturn['data'] = FeatureSetup($sqlDataRs);
389  $sqlReturn['info'][] = "Feature add was successful\n";
390 
391  return $sqlReturn;
392 }
393 
394 /**
395  * FeatureReadList:
396  * - read the list of global features available from homecu
397  *
398  * @param object $pDbh : database reference for access and manipulation of internal data
399  *
400  * @return array $sqlReturn : array containing list of features available from homecu
401  */
402 function FeatureReadList($pEnv, $pDbh) {
403 
404  // SQL STRING
405  $sqlSelect = "SELECT feature_code, description, limit_type, enabled
406  FROM cu_feature";
407 
408  // QUERY & ERROR CHECK
409  $sqlSelectRs = db_query($sqlSelect, $pDbh);
410  if (!$sqlSelectRs) {
411  $pEnv['logger']->error(db_last_error());
412  throw new Exception("Failed to read feature list");
413  }
414 
415  // GATHER DATA ROWS AND INFO
416  $sqlDataRs = db_fetch_all($sqlSelectRs);
417  $sqlReturn['data'] = FeatureSetup($sqlDataRs);
418 
419  return $sqlReturn;
420 }
421 ?>