27 header(
'Content-Type: application/json');
29 require_once(dirname(__FILE__) .
'/../../shared/library/dms_imp_val.i');
33 "action" => array(
'filter' => FILTER_SANITIZE_STRING),
34 "pId" => array(
'filter' => FILTER_VALIDATE_INT),
35 "pCode" => array(
'filter' => FILTER_SANITIZE_STRING),
36 "pDesc" => array(
'filter' => FILTER_SANITIZE_STRING),
37 "fList" => array(
'filter' => FILTER_SANITIZE_STRING),
38 "fType" => array(
'filter' => FILTER_SANITIZE_STRING),
39 "fCode" => array(
'filter' => FILTER_SANITIZE_STRING),
40 "fApt" => array(
'filter' => FILTER_VALIDATE_FLOAT),
41 "fApa" => array(
'filter' => FILTER_VALIDATE_FLOAT),
42 "fApd" => array(
'filter' => FILTER_VALIDATE_FLOAT),
43 "fApm" => array(
'filter' => FILTER_VALIDATE_FLOAT),
44 "fCpa" => array(
'filter' => FILTER_VALIDATE_INT),
45 "fCpd" => array(
'filter' => FILTER_VALIDATE_INT),
46 "fCpm" => array(
'filter' => FILTER_VALIDATE_INT),
47 "fCfm" => array(
'filter' => FILTER_VALIDATE_BOOLEAN),
50 HCU_ImportVars($admVars,
"", $varOk);
52 $action = isset($admVars[
'action']) ? $admVars[
'action'] :
null;
53 $pId = isset($admVars[
'pId']) ? $admVars[
'pId'] :
null;
54 $pCode = isset($admVars[
'pCode']) ? $admVars[
'pCode'] :
null;
55 $pDesc = isset($admVars[
'pDesc']) ? $admVars[
'pDesc'] :
null;
56 $fList = isset($admVars[
'fList']) ? $admVars[
'fList'] :
null;
57 $fCode = isset($admVars[
'fCode']) ? $admVars[
'fCode'] :
null;
58 $fType = isset($admVars[
'fType']) ? $admVars[
'fType'] :
null;
59 $fApt = isset($admVars[
'fApt']) ? $admVars[
'fApt'] :
null;
60 $fApd = isset($admVars[
'fApd']) ? $admVars[
'fApd'] :
null;
61 $fApm = isset($admVars[
'fApm']) ? $admVars[
'fApm'] :
null;
62 $fApa = isset($admVars[
'fApa']) ? $admVars[
'fApa'] :
null;
63 $fCpd = isset($admVars[
'fCpd']) ? $admVars[
'fCpd'] :
null;
64 $fCpm = isset($admVars[
'fCpm']) ? $admVars[
'fCpm'] :
null;
65 $fCpa = isset($admVars[
'fCpa']) ? $admVars[
'fCpa'] :
null;
66 $fCfm = isset($admVars[
'fCfm']) ? $admVars[
'fCfm'] :
null;
74 $aryResult[
'data'][
'profiles'] = ReadProfiles($SYSENV, $dbh, $Cu);
75 $aryResult[
'data'][
'limits'] = ReadProfileFeatures($SYSENV, $dbh, $Cu);
76 $aryResult[
'data'][
'features'] = ReadAvailableFeatures($SYSENV, $dbh, $Cu);
79 case "profile_create":
81 $aryResult = CreateProfile($SYSENV, $dbh, $pCode, $pDesc, $Cu);
84 case "profile_update":
86 $aryResult = UpdateProfile($SYSENV, $dbh, $pId, $pCode, $pDesc, $Cu);
89 case "profile_delete":
91 $aryResult = DeleteProfile($SYSENV, $dbh, $pId, $pCode, $Cu);
96 $fAmount = array(
"apt" => $fApt,
"apd" => $fApd,
"apm" => $fApm,
"apa" => $fApa);
97 $fCount = array(
"cpd" => $fCpd,
"cpm" => $fCpm,
"cpa" => $fCpa,
"cfm" => $fCfm);
98 $fFeature = array(
"code" => $fCode,
"type" => $fType);
100 $aryResult = CreateLimit($SYSENV, $dbh, $pId, $fFeature, $fAmount, $fCount, $Cu);
105 $fAmount = array(
"apt" => $fApt,
"apd" => $fApd,
"apm" => $fApm,
"apa" => $fApa);
106 $fCount = array(
"cpd" => $fCpd,
"cpm" => $fCpm,
"cpa" => $fCpa,
"cfm" => $fCfm);
107 $fFeature = array(
"code" => $fCode,
"type" => $fType);
109 $aryResult = UpdateLimit($SYSENV, $dbh, $pId, $fFeature, $fAmount, $fCount);
114 $aryResult = DeleteLimit($SYSENV, $dbh, $pId, $fList, $Cu);
118 throw new Exception(
"Feature Action: ( $action ) unknown");
122 }
catch (Exception $e) {
123 $aryReply[
'error'][] = $e->getMessage();
124 $aryResult[
'data'] = array();
125 $aryResult[
'info'] = array();
128 $aryReply[
'action'] = $action;
129 if (isset($aryResult[
'data']) && count($aryResult[
'data'])) $aryReply[
'data'] = $aryResult[
'data'];
130 if (isset($aryResult[
'info']) && count($aryResult[
'info'])) $aryReply[
'info'] = $aryResult[
'info'];
131 if (isset($aryResult[
'error']) && count($aryResult[
'error'])) $aryReply[
'error'] = $aryResult[
'error'];
133 print json_encode(array(
"Results" => $aryReply));
147 function LimitInUse($pEnv, $pDbh, $pId, $fCode, $pCu) {
151 SELECT COUNT(*) FROM {$pCu}grouprights gr 152 INNER JOIN {$pCu}group g 153 ON gr.group_id = g.group_id 154 WHERE g.profile_id = $pId 155 AND gr.feature_code = $fCode";
158 SELECT COUNT(*) FROM {$pCu}userrights ur 159 INNER JOIN {$pCu}user u 160 ON ur.user_id = u.user_id 161 INNER JOIN {$pCu}group g 162 ON u.group_id = g.group_id 163 WHERE g.profile_id = $pId 164 AND ur.feature_code = $fCode";
166 $sqlInUseGroupRs = db_query($sqlInUseGroup, $pDbh);
167 $sqlInUseUserRs = db_query($sqlInUseUser, $pDbh);
168 if (!$sqlInUseGroupRs || !$sqlInUseUserRs) {
169 $pEnv[
'logger']->error(db_last_error());
170 throw new Exception(
"Failed to read limit uses");
173 $sqlDataGroup = db_fetch_all($sqlInUseGroupRs);
174 $sqlDataUser = db_fetch_all($sqlInUseUserRs);
175 $sqlDataGroupCount = $sqlDataGroup[0][
'count'];
176 $sqlDataUserCount = $sqlDataUser[0][
'count'];
177 if ($sqlDataGroupCount > 0 || $sqlDataUserCount > 0) {
195 function DeleteLimit($pEnv, $pDbh, $pId, $fList, $pCu) {
197 $sqlReturn = array();
200 $featuresAry = array();
201 $featuresLst = explode(
",", $fList);
202 $featuresInUse =
false;
204 foreach ($featuresLst as $key => $value) {
206 $feature = explode(
":", $value);
207 $code =
"'" . prep_save($feature[1], 10) .
"'";
208 $desc = prep_save($feature[0], 255);
210 $inUse = LimitInUse($pEnv, $pDbh, $pId, $code, $pCu);
213 $featuresInUse =
true;
214 $sqlReturn[
'error'][] =
"$desc ( Feature has custom access rights for one or more users. )";
216 array_push($featuresAry, $code);
217 $sqlReturn[
'info'][] =
"$desc ( Deleted )";
221 if ($featuresInUse) {
222 $sqlReturn[
'error'][] =
"Contact HomeCU if you wish to delete these features from the profile.";
224 $features = implode(
",", $featuresAry);
226 $sqlColumnsLimit =
"profile_id AS pid, 227 feature_code AS fcode, 228 amount_per_transaction AS fapt, 229 amount_per_day AS fapd, 230 amount_per_month AS fapm, 231 amount_per_account_per_day AS fapa, 232 count_per_day AS fcpd, 233 count_per_month AS fcpm, 234 count_per_account_per_day AS fcpa, 235 confirm_required AS fcfm";
237 $sqlDeleteLimit =
"DELETE FROM cu_profilerights 238 WHERE feature_code IN ($features) 239 AND profile_id = $pId 240 RETURNING $sqlColumnsLimit";
242 $sqlDeleteRs = db_query($sqlDeleteLimit, $pDbh);
244 $pEnv[
'logger']->error(db_last_error());
245 throw new Exception(
"Failed to delete limits");
248 $sqlReturn[
'data'] = db_fetch_all($sqlDeleteRs);
268 function UpdateLimit($pEnv, $pDbh, $pId, $fFeature, $fAmount, $fCount) {
271 $code = prep_save($fFeature[
'code'], 10);
272 $type = prep_save($fFeature[
'type'], 1);
273 $apt = ($type ==
"B" || $type ==
"D") ? $fAmount[
'apt'] :
"NULL";
274 $apd = ($type ==
"B" || $type ==
"D") ? $fAmount[
'apd'] :
"NULL";
275 $apm = ($type ==
"B" || $type ==
"D") ? $fAmount[
'apm'] :
"NULL";
276 $apa = ($type ==
"B" || $type ==
"D") ? $fAmount[
'apa'] :
"NULL";
277 $cpd = ($type ==
"B" || $type ==
"Q") ? $fCount[
'cpd'] :
"NULL";
278 $cpm = ($type ==
"B" || $type ==
"Q") ? $fCount[
'cpm'] :
"NULL";
279 $cpa = ($type ==
"B" || $type ==
"Q") ? $fCount[
'cpa'] :
"NULL";
280 $cfm = $fCount[
'cfm'] == 1 ?
"true" :
"false";
282 $sqlReturn = array();
283 $sqlColumnsLimit =
"profile_id AS pid, 284 feature_code AS fcode, 285 amount_per_transaction AS fapt, 286 amount_per_day AS fapd, 287 amount_per_month AS fapm, 288 amount_per_account_per_day AS fapa, 289 count_per_day AS fcpd, 290 count_per_month AS fcpm, 291 count_per_account_per_day AS fcpa, 292 confirm_required AS fcfm";
294 $sqlUpdateLimit =
"UPDATE cu_profilerights 295 SET (amount_per_transaction, amount_per_day, amount_per_month, amount_per_account_per_day, 296 count_per_day, count_per_month, count_per_account_per_day, confirm_required) 297 = ($apt, $apd, $apm, $apa, $cpd, $cpm, $cpa, $cfm) 298 WHERE profile_id = $pId 299 AND feature_code = '$code' 300 RETURNING $sqlColumnsLimit";
302 $sqlUpdateLimitRs = db_query($sqlUpdateLimit, $pDbh);
303 if (!$sqlUpdateLimitRs) {
304 $pEnv[
'logger']->error(db_last_error());
305 throw new Exception(
"Failed to update limit");
308 $sqlReturn[
'data'] = db_fetch_all($sqlUpdateLimitRs);
309 $sqlReturn[
'info'][] =
"Limit has been updated successfully";
329 function CreateLimit($pEnv, $pDbh, $pId, $fFeature, $fAmount, $fCount, $pCu) {
332 $code = prep_save($fFeature[
'code'], 10);
333 $type = prep_save($fFeature[
'type'], 1);
334 $apt = ($type ==
"B" || $type ==
"D") ? $fAmount[
'apt'] :
"NULL";
335 $apd = ($type ==
"B" || $type ==
"D") ? $fAmount[
'apd'] :
"NULL";
336 $apm = ($type ==
"B" || $type ==
"D") ? $fAmount[
'apm'] :
"NULL";
337 $apa = ($type ==
"B" || $type ==
"D") ? $fAmount[
'apa'] :
"NULL";
338 $cpd = ($type ==
"B" || $type ==
"Q") ? $fCount[
'cpd'] :
"NULL";
339 $cpm = ($type ==
"B" || $type ==
"Q") ? $fCount[
'cpm'] :
"NULL";
340 $cpa = ($type ==
"B" || $type ==
"Q") ? $fCount[
'cpa'] :
"NULL";
341 $cfm = $fCount[
'cfm'] == 1 ?
"true" :
"false";
343 $sql =
"select 'FOUND' from cu_featuremenu fm inner join cu_feature f on fm.feature_code = f.feature_code and fm.cu = '" . prep_save($pCu, 10) .
"' and f.enabled = true";
344 $sth = db_query($sql, $pDbh);
345 if (!$sth || db_num_rows($sth) == 0) {
346 throw new exception(
"Failed to create limit");
349 $sqlReturn = array();
350 $sqlColumnsLimit =
"profile_id AS pid, 351 feature_code AS fcode, 352 amount_per_transaction AS fapt, 353 amount_per_day AS fapd, 354 amount_per_month AS fapm, 355 amount_per_account_per_day AS fapa, 356 count_per_day AS fcpd, 357 count_per_month AS fcpm, 358 count_per_account_per_day AS fcpa, 359 confirm_required AS fcfm";
361 $sqlInsertLimit =
"INSERT INTO cu_profilerights 362 (profile_id, feature_code, 363 amount_per_transaction, amount_per_day, amount_per_month, amount_per_account_per_day, 364 count_per_day, count_per_month, count_per_account_per_day, confirm_required) 366 ($id, '$code', $apt, $apd, $apm, $apa, $cpd, $cpm, $cpa, $cfm) 367 RETURNING $sqlColumnsLimit";
369 $sqlInsertLimitRs = db_query($sqlInsertLimit, $pDbh);
370 if (!$sqlInsertLimitRs) {
371 $pEnv[
'logger']->error(db_last_error());
372 throw new Exception(
"Failed to create limit");
375 $sqlReturn[
'data'] = db_fetch_all($sqlInsertLimitRs);
376 $sqlReturn[
'info'][] =
"Limit has been created successfully";
392 function ProfileInUse($pEnv, $pDbh, $pId, $pCu) {
395 $sqlSelectProfileGroup =
"SELECT COUNT(*) FROM {$pCu}group 396 WHERE profile_id = $pId";
398 $sqlSelectProfileRights =
"SELECT COUNT(*) FROM cu_profilerights 399 WHERE profile_id = $pId";
401 $sqlSelectProfileGroupRs = db_query($sqlSelectProfileGroup, $pDbh);
402 $sqlSelectProfileRightsRs = db_query($sqlSelectProfileRights, $pDbh);
403 if (!$sqlSelectProfileGroupRs || !$sqlSelectProfileRightsRs) {
404 $pEnv[
'logger']->error(db_last_error());
405 throw new Exception(
"Failed to read profile uses");
408 $sqlDataProfileGroup = db_fetch_all($sqlSelectProfileGroupRs);
409 $sqlDataProfileRights = db_fetch_all($sqlSelectProfileRightsRs);
410 $sqlDataProfileGroupCount = $sqlDataProfileGroup[0][
'count'];
411 $sqlDataProfileRightsCount = $sqlDataProfileRights[0][
'count'];
412 if ($sqlDataProfileGroupCount > 0 || $sqlDataProfileRightsCount > 0) {
431 function DeleteProfile($pEnv, $pDbh, $pId, $pCode, $pCu) {
433 $sqlReturn = array();
434 if (ProfileInUse($pEnv, $pDbh, $pId, $pCu)) {
435 throw new Exception(
"Failed to delete profile. It is currently in use");
438 $sqlDeleteProfile =
"DELETE FROM cu_profile 439 WHERE profile_id = $pId 440 AND profile_code = '$pCode' 442 RETURNING profile_id AS pid, profile_code AS pcode, description AS pdesc";
444 $sqlDeleteProfileRs = db_query($sqlDeleteProfile, $pDbh);
445 if (!$sqlDeleteProfileRs) {
446 $pEnv[
'logger']->error(db_last_error());
447 throw new Exception(
"Failed to delete profile");
450 $sqlReturn[
'data'] = db_fetch_all($sqlDeleteProfileRs);
451 $sqlReturn[
'info'][] =
"Profile has been deleted successfully";
469 function UpdateProfile($pEnv, $pDbh, $pId, $pCode, $pDesc, $pCu) {
471 $code = strtoupper($pCode);
472 $code = prep_save($code, 10);
474 $desc = html_entity_decode($pDesc, ENT_QUOTES);
475 $desc = prep_save($desc, 255);
477 $sqlReturn = array();
478 $sqlUpdateProfile =
"UPDATE cu_profile 479 SET (profile_code, description) = ('$code', '$desc') 480 WHERE profile_id = $pId 482 RETURNING profile_id AS pid, profile_code AS pcode, description AS pdesc";
484 $sqlUpdateRs = db_query($sqlUpdateProfile, $pDbh);
486 $pEnv[
'logger']->error(db_last_error());
487 throw new Exception(
"Failed to update profile");
490 $sqlReturn[
'data'] = db_fetch_all($sqlUpdateRs);
491 $sqlReturn[
'info'][] =
"Profile was successfully updated";
507 function CreateProfile($pEnv, $pDbh, $pCode, $pDesc, $pCu) {
509 $code = strtoupper($pCode);
510 $code = prep_save($code, 20);
512 $desc = html_entity_decode($pDesc, ENT_QUOTES);
513 $desc = prep_save($desc, 255);
515 $cu = prep_save($pCu, 10);
517 $sqlReturn = array();
518 $sqlInsertProfile =
"INSERT INTO cu_profile 519 (profile_code, description, cu) 521 ('$code', '$desc', '$cu') 522 RETURNING profile_id AS pid, profile_code AS pcode, description AS pdesc";
524 $sqlInsertRs = db_query($sqlInsertProfile, $pDbh);
526 $pEnv[
'logger']->error(db_last_error());
527 throw new Exception(
"Failed to create profile");
530 $sqlReturn[
'data'] = db_fetch_all($sqlInsertRs);
531 $sqlReturn[
'info'][] =
"Profile was successfully created";
545 function ReadProfiles($pEnv, $pDbh, $pCu) {
546 $sqlReturn = array();
547 $sqlColumnsProfiles =
"cup.profile_id AS pid, 548 cup.profile_code AS pcode, 549 cup.description AS pdesc";
551 $sqlSelectProfiles =
"SELECT $sqlColumnsProfiles 553 WHERE cup.cu = '$pCu' 554 ORDER BY cup.description ASC";
556 $sqlQueryProfiles = db_query($sqlSelectProfiles, $pDbh);
557 if (!$sqlQueryProfiles) {
558 $pEnv[
'logger']->error(db_last_error());
559 throw new Exception(
"Failed to read profiles list");
562 $sqlReturn = db_fetch_all($sqlQueryProfiles);
576 function ReadProfileFeatures($pEnv, $pDbh, $pCu) {
577 $sqlReturn = array();
579 $sqlColumnsRights =
"cupr.profile_id AS pid, 580 cupr.feature_code AS fcode, 581 cupr.amount_per_transaction AS fapt, 582 cupr.amount_per_day AS fapd, 583 cupr.amount_per_month AS fapm, 584 cupr.amount_per_account_per_day AS fapa, 585 cupr.count_per_day AS fcpd, 586 cupr.count_per_month AS fcpm, 587 cupr.count_per_account_per_day AS fcpa, 588 cupr.confirm_required AS fcfm, 589 cuf.description AS fdesc, 590 cuf.limit_type AS ftype";
592 $sqlSelectRights =
"SELECT $sqlColumnsRights 593 FROM cu_profilerights cupr 594 INNER JOIN cu_feature cuf ON cupr.feature_code = cuf.feature_code 595 INNER JOIN cu_profile cup ON cupr.profile_id = cup.profile_id 596 WHERE cuf.enabled = TRUE 598 ORDER BY cuf.description ASC";
600 $sqlQueryRights = db_query($sqlSelectRights, $pDbh);
601 if (!$sqlQueryRights) {
602 $pEnv[
'logger']->error(db_last_error());
603 throw new Exception(
"Failed to read profile rights");
606 $sqlReturn = db_fetch_all($sqlQueryRights);
619 function ReadAvailableFeatures($pEnv, $pDbh, $pCu) {
620 $sqlReturn = array();
622 $sqlColumnsFeatures =
"cuf.feature_code AS fcode, 623 cuf.description AS fdesc, 624 cuf.limit_type AS ftype, 625 cuf.enabled AS fenabled";
628 $sqlSelectFeatures =
"SELECT $sqlColumnsFeatures, 629 EXISTS (SELECT 'FOUND' FROM cu_featuremenu fm WHERE cuf.enabled = TRUE AND cuf.feature_code = fm.feature_code AND fm.cu = '" . prep_save($pCu, 10) .
"') as cuexists 631 ORDER BY cuf.description ASC";
633 $sqlQueryFeatures = db_query($sqlSelectFeatures, $pDbh);
634 if (!$sqlQueryFeatures) {
635 $pEnv[
'logger']->error(db_last_error());
636 throw new Exception(
"Failed to read feature list");
639 $sqlReturn = db_fetch_all($sqlQueryFeatures);