Odyssey
aGroupSupport.i
1 <?php
2 require_once("$admLibrary/userSearch.i");
3 
4 /**
5  * Reply:
6  * @uses return data to client view print to json
7  * @param assoc $pResult: array contining results of search
8  * @param assoc $pReply: empty array to return to client
9  * @param assoc $pOperation: requested search operation
10  */
11 function GroupReply($pResult, $pReply, $pOperation) {
12  $pReply['operation'] = $pOperation;
13  if (isset($pResult['data']) && count($pResult['data'])) $pReply['data'] = $pResult['data'];
14  if (isset($pResult['info']) && count($pResult['info'])) $pReply['info'] = $pResult['info'];
15 
16  print json_encode(array("Results" => $pReply));
17 }
18 
19 /**
20  * GroupContext:
21  * @uses validate the context for group hub cards, this will generate an array
22  * including the Cu code in ways for use as table identifiers and as co code variable.
23  * this function will also add neccessary group data to the array for common use on
24  * the server.
25  *
26  * @param #pEnv array: environment variable for debugging
27  * @param $pCu string: credit union code to be used as variable or table in sql
28  * @param $pGroup array: group data needed by grouphub cards
29  *
30  * @return $vContext array: common data used by group hub cards
31  */
32 
33 function GroupContext($pEnv, $pCu, $pGroup = null) {
34  $vCu = prep_save($pCu, 20);
35 
36  $vContext = array(
37  "cu_table" => strtolower($pCu),
38  "cu_code" => strtoupper($pCu),
39  "g_id" => $pGroup ? ( isset($pGroup['g_id']) ? intval($pGroup['g_id']) : null ) : null,
40  "p_id" => $pGroup ? ( isset($pGroup['p_id']) ? intval($pGroup['p_id']) : null ) : null
41  );
42 
43  return $vContext;
44 }
45 
46 /**
47  * GroupSelect:
48  * @uses select a group by specific parameters, for now select by group_name.
49  * this function will select all display information for the group hub.
50  * also the contact information has been removed due to no forseeable use.
51  *
52  * @param $pEnv array: evironment object for debugging
53  * @param $pDbh object: database access variable
54  * @param $pContext array: array of data needed for multiple group hub functions:
55  * in this case the group_name.
56  * @param $fromGroupHub -- it assumes that it is from the group hub which also needs the payload per primary user. For other uses, that is a wasted calculation.
57  *
58  * @return $sqlReturn array: single data row, the selected member
59  */
60 function GroupSelect($pEnv, $pDbh, $pContext) {
61  $cuTable = $pContext['cu_table'];
62  $cuCode = $pContext['cu_code'];
63  $gId = prep_save(intval($pContext["g_id"]));
64 
65  $sqlReturn = array();
66  $sqlColumns = "
67  g.group_id AS g_id,
68  g.group_name AS g_name,
69  g.tax_id AS g_taxid,
70  p.profile_id AS p_id,
71  p.description AS g_profile";
72 
73  // select primary user_name
74  $sqlSub = "
75  SELECT user_name
76  FROM {$cuTable}user
77  WHERE group_id = g.group_id
78  AND is_group_primary = 'TRUE'";
79 
80  // select number of users in this group
81  $sqlCount = "
82  SELECT COUNT(user_id)
83  FROM {$cuTable}user
84  WHERE group_id = g.group_id";
85 
86  // select group information
87  $sqlSelect = "
88  SELECT $sqlColumns, (SELECT array($sqlSub)) AS g_primary, ($sqlCount) AS g_count
89  FROM {$cuTable}group g
90  LEFT JOIN {$cuTable}user u ON u.group_id = g.group_id
91  LEFT JOIN cu_profile p ON p.profile_id = g.profile_id
92  WHERE g.group_id = '$gId'";
93 
94  $sqlSelectRs = db_query($sqlSelect, $pDbh);
95  if (!$sqlSelectRs) {
96  if (isset($pEnv))
97  $pEnv['logger']->error(db_last_error());
98  throw new Exception("Failed to select group.");
99  }
100 
101  // get payload for primary users
102  // strip brackets
103  // split into array
104  $sqlGroup = db_fetch_all($sqlSelectRs)[0];
105  $sqlPrimaryString = $sqlGroup['g_primary'];
106  $sqlPrimaryString = str_replace(array("{", "}"), "", $sqlPrimaryString);
107  $sqlPrimary = explode(",", $sqlPrimaryString);
108 
109  // get user hub payload for each primary user
110  // calling script must include userSearch.i
111  // for call to readUserSearch function
112  $aryPrimary = array();
113  if ($sqlPrimaryString != "") {
114  foreach ($sqlPrimary as $key => $value) {
115  $aryPayload = readUserSearch($pDbh, $cuCode, array(
116  "a" => array ("username"=>$value)
117  ));
118  $aryPrimary[$key] = array(
119  "p_name" => $value,
120  "p_payload" => urlencode($aryPayload['encryption'])
121  );
122  }
123  }
124 
125  // replace sql array with payload array
126  $sqlGroup['g_primary'] = $aryPrimary;
127  $sqlReturn['group'] = $sqlGroup;
128  return $sqlReturn;
129 }
130 
131 /**
132  * GroupValidate:
133  * @uses validate the json string of parameters submitted by the user for search purposes.
134  * this function will prepare the data for submission to the database (if needed).
135  * also the functioncan be used to get the correct group name value for group selection
136  * purposes.
137  *
138  * @param $pEnv array: evironment object for debugging
139  * @param $pParameters string: json string of field/value pairs from user submitted
140  * form.
141  * @param $pJson boolean: tell the function if the incoming data needs to be decoded
142  * into array form.
143  *
144  * @return $vParameters array: array of data valid for use in the database
145  */
146 function GroupValidate($pEnv, $pParameters, $pJson = false) {
147  $gParameters = $pParameters;
148  $gValidate = array();
149 
150  // if parameters are in json string form
151  // decode into array
152  if ($pJson) {
153  $gParameters = html_entity_decode($pParameters, ENT_QUOTES);
154  $gParameters = HCU_JsonDecode($gParameters);
155  }
156 
157  // validate group name parameter if available
158  if (isset($gParameters['g_name'])) {
159  $gName = $gParameters['g_name'];
160  $gName = strtolower($gName);
161  $gName = prep_save($gName, 50);
162  $gName = trim($gName);
163 
164  $gValidate['g_name'] = $gName;
165  }
166 
167  return $gValidate;
168 }
169 
170 /**
171  * GroupEncrypt:
172  * @uses encode group data from anywhere given at least the group_id and profile_id. This function
173  * will also encode the string for url purposes.
174  *
175  * @param $pEnv array: evironment object for debugging
176  * @param $pCu string: cu code used for accessing variables and database tables
177  * @param $pMember array: group data to encrypt
178  * @param $pJson boolean: allows user to pass array or json string to be encoded (optional)
179  *
180  * @return $sMemberEncrypt string: encoded data string
181  */
182 function GroupEncrypt($pEnv, $pCu, $pGroup, $pJson = false) {
183  $sGroupEncode = $pGroup;
184 
185  if ($pJson) {
186  $sGroupEncode = html_entity_decode($sGroupEncode, ENT_QUOTES);
187  $sGroupEncode = HCU_JsonDecode($sGroupEncode);
188  }
189 
190  $sGroupEncrypt = HCU_PayloadEncode($pCu, $sGroupEncode);
191  return $sGroupEncrypt;
192 }
193 
194 /**
195  * GroupDecrypt:
196  * @uses decrypt group data from anywhere given the encrypted string, this function will
197  * decode any url characters if neccessary.
198  *
199  * @param $pEnv array: evironment object for debugging
200  * @param $pCu string: cu code used for accessing variables and database tables
201  * @param $pEncode string: encrypted data string
202  * @param $pJson boolean: allows user to return an array or json string (optional)
203  *
204  * @return $sMemberDecode string | array: decrypted member data as string or array
205  */
206 function GroupDecrypt($pEnv, $pCu, $pEncrypt, $rJson = false) {
207  $sGroupDecrypt = HCU_PayloadDecode($pCu, $pEncrypt, $rJson);
208  return $sGroupDecrypt;
209 }
210 
211 ?>