Odyssey
mUserList.data
1 <?php
2 /**
3  * File: mUserList.data
4  * This file works is for the data operations of the file mUserList.prg. Currently, the only operation is "read" but that could change. As its .prg counterpart, it is only for Odyssey.
5  */
6 
7 // ********************************************************************************************************************************************************************************
8 // Top portion: includes and extracting parameters from commandline and/or post.
9 // ********************************************************************************************************************************************************************************
10 
11 $monLibrary = dirname(__FILE__) . "/../library";
12 $sharedLibrary = dirname(__FILE__) . "../../shared/library";
13 require_once("$monLibrary/cu_top.i");
14 
15 $string = array("filter" => FILTER_SANITIZE_STRING);
16 $params = array("operation" => $string, "memberNumber" => $string, "numRows" => $string, "userName" => $string, "email" => $string, "cu" => $string);
17 $vars = array("operation" => "", "memberNumber" => "", "numRows" => "", "userName" => "", "email" => "", "cu" => "");
18 HCU_ImportVars($vars, "", $params);
19 
20 $operation = HCU_array_key_value("operation", $vars);
21 $operation = $operation === false ? "" : trim($operation);
22 $showSQL = $SYSENV["devmode"];
23 
24 switch($operation) {
25  case "read":
26  case "":
27  $returnArray = ReadUserList($dbh, $SYSENV, $vars);
28  break;
29  default:
30  $returnArray = array("error" => "Operation is not known.", "data" => array());
31  break;
32 }
33 
34 header('Content-type: application/json');
35 if (!$showSQL) {
36  unset($returnArray["sql"]);
37 }
38 print HCU_JsonEncode($returnArray);
39 
40 // ********************************************************************************************************************************************************************************
41 // Common to .prg and .data (if there is enough lines of code, put it in an include file.)
42 // ********************************************************************************************************************************************************************************
43 
44 /**
45  * function GetMaxRowsList()
46  * This function returns the specified options of the max rows kendoDropDownList. It is used in the prg to set the kendo definition and in the data to verify that it is a correct option.
47  */
48 function GetMaxRowsList() {
49  return array("50", "100", "ALL");
50 }
51 
52 // ********************************************************************************************************************************************************************************
53 // Start of functions
54 // ********************************************************************************************************************************************************************************
55 
56 /**
57  * function ReadUserList($dbh, $SYSENV, $params)
58  * This function takes in the parameters from the search and returns a list of user/account combinations (Cartesian product)
59  *
60  * @param $dbh -- the database connection
61  * @param $SYSENV -- the system environment variables
62  * @param $params -- the parameters from the POST. These come from the search controls in the search DIV.
63  *
64  * @return $returnArray
65  * If successful: $returnArray contains the data for the grid and an empty error array.
66  * If not successful: $returnArray contains an error array with one error and an empty data array.
67  */
68 function ReadUserList($dbh, $SYSENV, $params) {
69  $logger = $SYSENV["logger"];
70  unset($SYSENV);
71  $sqls = array();
72 
73  try {
74  // Get parameters and see if there are any errors in retrieval.
75  $cu = HCU_array_key_value("cu", $params);
76  $cu = $cu === false ? "" : trim($cu);
77 
78  $memberNumber = HCU_array_key_value("memberNumber", $params);
79  $memberNumber = $memberNumber === false ? "" : trim($memberNumber);
80 
81  $numRows = HCU_array_key_value("numRows", $params);
82  $numRows = $numRows === false ? "" : trim($numRows);
83 
84  $userName = HCU_array_key_value("userName", $params);
85  $userName = $userName === false ? "" : trim($userName);
86 
87  $email = HCU_array_key_value("email", $params);
88  $email = $email === false ? "" : trim($email);
89 
90  $where = array();
91 
92  if ($cu == "") {
93  throw new exception("Cu must be defined.", 2);
94  }
95 
96  if ($memberNumber != "") {
97  if (preg_match('/\D/', trim($memberNumber)) !== 0) { // If there is a non-digit character then throw error message. 1: there is a non-digit + false: reg expression errors out.
98  throw new exception("Member must be a number.", 3);
99  }
100  $where[] = "maa.accountnumber = '" . prep_save(intval($memberNumber), 12) . "'"; // Even though this is a number, it is a character in the database.
101  }
102 
103  if (!in_array($numRows, GetMaxRowsList())) {
104  throw new exception("Max rows must be either 50, 100, or ALL.", 4);
105  }
106  $limit = $numRows == "ALL" ? "" : "limit $numRows";
107 
108  if ($userName != "") {
109  $where[] = "u.user_name = '" . prep_save($userName, 50) . "'";
110  }
111 
112  if ($email != "") {
113  if (!validateEmail($email)) {
114  throw new exception("Email is not valid.", 5);
115  }
116  $where[] = "u.email = '" . prep_save($email, 255) . "'";
117  }
118 
119  $where = count($where) > 0 ? "where " . implode(" and ", $where) : "";
120 
121  $preppedCu = prep_save($cu, 12);
122  $sql = "select case when u.lastlogin = '' then 1 else 0 end as sort, u.lastlogin, u.user_id as login, u.user_name as user, u.email, maa.accountnumber as member, maa.estmnt_flag
123  from (select user_id, user_name, email, coalesce(trim(lastlogin), '') as lastlogin from ${preppedCu}user) u
124  inner join (select ma.accountnumber, ma.estmnt_flag, ua.user_id from ${preppedCu}memberacct ma
125  inner join (select row_number() over(partition by user_id, accountnumber) as rown, accountnumber, user_id from ${preppedCu}useraccounts) ua
126  on ma.accountnumber = ua.accountnumber and ua.rown = 1) maa on u.user_id = maa.user_id
127  $where order by 1, 2 desc $limit";
128  // Using row_number is less expensive than using the "distinct" keyword.
129  $sqls[] = $sql;
130 
131  $sth = db_query($sql, $dbh);
132  if (!$sth) {
133  throw new exception("Select query failed.", 1);
134  }
135 
136  $list = array();
137  for($i = 0; $row = db_fetch_assoc($sth, $i); $i++) {
138  $row["lastLogin"] = isset($row["lastlogin"]) ? $row["lastlogin"] : "";
139  $row["hasEstmnt"] = isset($row["estmnt_flag"]) ? trim($row["estmnt_flag"]) == "Y" : false;
140  $row["member"] = isset($row["member"]) ? trim($row["member"]) : "";
141  unset($row["estmnt_flag"]);
142  unset($row["lastlogin"]);
143  $list[] = $row;
144  }
145 
146  $returnArray = array("error" => "", "data" => $list, "sql" => $sqls);
147  } catch (exception $e) {
148  $logger->error("Error: ". $e->getMessage());
149  $code = "HCU-" . str_pad(strval($e->getCode()), 4, STR_PAD_LEFT, "0"); // E.g. 0003.
150  $returnArray = array("error" => "A problem occurred. $code", "data" => array(), "sql" => $sqls);
151  }
152  return $returnArray;
153 }