-
Notifications
You must be signed in to change notification settings - Fork 0
/
tng_sub_notify_list_code.php
378 lines (342 loc) · 9.79 KB
/
tng_sub_notify_list_code.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/*---------------------------------------------------------------
author: alim karim
date: Apr 10, 2007
file: tng_sub_notify_list.php
desc: backend script to allow users to select
which users should be notified about a
submission
---------------------------------------------------------------*/
include_once('classes/class_login.php');
include_once('classes/class_dbconn.php');
include_once('classes/class_app_config.php');
// since php4 does not have built in json support,
// make sure the json extension is loaded
if(phpversion() < 5.0)
dl('json.so');
session_start();
// do not process any requests if the
// session variable is not set
//if(!isset($_SESSION['obj_login']))
// return;
// globals
if(isset($_REQUEST['ajax_req'])){
switch($_REQUEST['ajax_req']){
case "get_user_list":
$searchStr = $_REQUEST['query'];
$userEmailList = getUserEmailList($searchStr);
echo json_encode($userEmailList);
break;
case "get_notification_list":
$subID = $_REQUEST['submission_id'];
// we deal with notification lists at the
// parent level. Check if this submission
// is not a parent.
if(!isParent($subID))
// not parent, get the parent
$subID = getParent($subID);
$notificationList = getSubNotificationList($subID);
echo json_encode($notificationList);
break;
case "append_notification_list":
$subID = $_REQUEST['submission_id'];
// we deal with notification lists at the
// parent level. Check if this submission
// is not a parent.
$parentID = -1;
if(!isParent($subID))
// not parent, get the parent
$parentID = getParent($subID);
$notificationList = json_decode(str_replace("\\", "", $_POST['notification_list']), true);
$sendNotificationEmail = ($_REQUEST['notify'] == "true");
foreach($notificationList as $email)
addToNotificationList($parentID == -1 ? $subID : $parentID, $email);
if($sendNotificationEmail)
sendNotificationEmail($subID, $parentID);
break;
case "delete_from_notification_list":
$subID = $_REQUEST['submission_id'];
// we deal with notification lists at the
// parent level. Check if this submission
// is not a parent.
if(!isParent($subID))
// not parent, get the parent
$subID = getParent($subID);
$email = $_REQUEST['email'];
delFromNotificationList($subID, $email);
break;
}
}
/*---------------------------------------------------------------*/
// functions
/*---------------------------------------------------------------*/
///
/// isParent
/// Check to see if the given submission is a parent
///
function isParent($subID){
$dbconn =& new DBConn();
$isParent = false;
$sqlStr = "SELECT "
. "pid "
. "FROM "
. "tng_form_submission "
. "WHERE "
. "form_submission_id = " . $subID;
$dbconn->connect();
$result = pg_query($dbconn->conn, $sqlStr);
if(!$result){
echo "An error occurred while executing the query - "
. $sqlStr ." - " . pg_last_error($this->dbconn->conn);
$dbconn->disconnect();
return NULL;
}
// valid submission id ?
if(pg_num_rows($result) == 0)
return NULL;
if(pg_fetch_result($result, 0, 'pid') == -1)
$isParent = true;
$dbconn->disconnect();
return $isParent;
}
///
/// getParent
/// get the parent submission for the current
/// submission
///
function getParent($subID){
$dbconn =& new DBConn();
$parentID = -1;
$sqlStr = "SELECT "
. "pid "
. "FROM "
. "tng_form_submission "
. "WHERE "
. "form_submission_id = " . $subID;
$dbconn->connect();
$result = pg_query($dbconn->conn, $sqlStr);
if(!$result){
echo "An error occurred while executing the query - "
. $sqlStr ." - " . pg_last_error($this->dbconn->conn);
$dbconn->disconnect();
return NULL;
}
// valid submission id ?
if(pg_num_rows($result) == 0)
return NULL;
$parentID = pg_fetch_result($result, 0, 'pid');
return $parentID;
}
///
/// getUserEmailList
/// get list of all users in the Portal
///
function getUserEmailList($searchStr){
$userList = array();
$dbconn =& new DBConn();
$groups = array();
$sqlStr = "SELECT "
. "uid, "
. "fname || ' ' || lname AS name, "
. "email "
. "FROM "
. "tng_user "
. "WHERE "
. "active = 'true' "
. "AND ("
. "fname LIKE '" . $searchStr . "%' "
. "OR "
. "email LIKE '" . $searchStr . "%' "
. ") "
. "ORDER BY "
. "email ";
$dbconn->connect();
$result = pg_query($dbconn->conn, $sqlStr);
if(!$result){
echo "An error occurred while executing the query - "
. $sqlStr ." - " . pg_last_error($this->dbconn->conn);
$dbconn->disconnect();
return NULL;
}
$nItems = pg_num_rows($result);
for($i = 0; $i < $nItems; $i++){
$userList[$i] = array(
"id" => pg_fetch_result($result, $i, 'uid'),
"name" => pg_fetch_result($result, $i, 'name'),
"email" => pg_fetch_result($result, $i, 'email')
);
}
$dbconn->disconnect();
return $userList;
}
///
/// getSubNotificationList
/// Given a submission ID, get the list of
/// email addresses associated with notification
///
function getSubNotificationList($subID){
$notificationList = array();
$dbconn =& new DBConn();
$groups = array();
$sqlStr = "SELECT "
. "id, "
. "email "
. "FROM "
. "tng_submission_notification "
. "WHERE "
. "sub_id = " . $subID
. "ORDER BY "
. "email ";
$dbconn->connect();
$result = pg_query($dbconn->conn, $sqlStr);
if(!$result){
echo "An error occurred while executing the query - "
. $sqlStr ." - " . pg_last_error($this->dbconn->conn);
$dbconn->disconnect();
return NULL;
}
$nItems = pg_num_rows($result);
for($i = 0; $i < $nItems; $i++){
$notificationList[$i] = array(
"id" => pg_fetch_result($result, $i, 'id'),
"email" => pg_fetch_result($result, $i, 'email')
);
}
$dbconn->disconnect();
return $notificationList;
}
///
/// addToNotificationList
/// add an email address to the notification
/// list associated with a submission
///
function addToNotificationList($subID, $email){
$dbconn =& new DBConn();
// check if the email address is already
// associated with the submission
$sqlStr = "SELECT "
. "sub_id "
. "FROM "
. "tng_submission_notification "
. "WHERE "
. "sub_id = " . $subID . " "
. "AND email ='" . $email . "' ";
$dbconn->connect();
$result = pg_query($dbconn->conn, $sqlStr);
if(!$result){
echo "An error occurred while executing the query - "
. $sqlStr ." - " . pg_last_error($this->dbconn->conn);
$dbconn->disconnect();
return false;
}
// if a result was found, then there is no need
// to insert this record again.
// otherwise, insert the record.
if(pg_num_rows($result) == 0){
$sqlStr = "INSERT INTO "
. "tng_submission_notification "
. "( "
. "sub_id, "
. "email "
. ") "
. "VALUES "
. "( "
. $subID . ", "
. "'" . $email . "' "
. ") ";
$result = pg_query($dbconn->conn, $sqlStr);
if(!$result){
echo "An error occurred while executing the query - "
. $sqlStr ." - " . pg_last_error($this->dbconn->conn);
$dbconn->disconnect();
return false;
}
}
$dbconn->disconnect();
return true;
}
///
/// delFromNotificationList
/// delete given email address from the notification
/// list associated with a submission.
///
function delFromNotificationList($subID, $email){
$dbconn =& new DBConn();
$sqlStr = "DELETE FROM "
. "tng_submission_notification "
. "WHERE "
. "sub_id = " . $subID . " "
. "AND email ='" . $email . "' ";
$dbconn->connect();
$result = pg_query($dbconn->conn, $sqlStr);
if(!$result){
echo "An error occurred while executing the query - "
. $sqlStr ." - " . pg_last_error($this->dbconn->conn);
$dbconn->disconnect();
return false;
}
$dbconn->connect();
return true;
}
///
/// readMessageFromFile
/// given the file path, read the message into a local
/// variable and return it.
///
function readMessageFromFile($filepath){
$handle = fopen($filepath, "r");
$messageBuffer = "";
if ($handle) {
$messageBuffer = fread($handle, filesize($filepath));
fclose($handle);
}
return $messageBuffer;
}
///
/// sendNotificationEmail
/// send a notification Email to all addresses associated
/// with the given submission.
///
function sendNotificationEmail($subID, $pid){
$notificationList = getSubNotificationList($pid != -1 ? $pid : $subID);
// genrate 'from' list
$fromList = "";
foreach($notificationList as $tuple)
$fromList .= $tuple['email'] . ",";
// generate subject
$subject = "Submission Successful - " . $subID;
if($pid != -1)
$subject .= " - Amendment to " . $pid;
// read template message from file into local array
$messageTemplate = readMessageFromFile($_SESSION['app_config']->confirmation_email_template);
// print list of failed files to be appended to the message
$failed_files = "";
if(isset($_SESSION['failed_files'])){
foreach($_SESSION['failed_files'] as $failed_file)
$failed_files .= "- " . $failed_file . "\n";
}
// print list of successful files to be appended to the message
$successful_files = "";
if(isset($_SESSION['successful_files'])){
foreach($_SESSION['successful_files'] as $successful_file)
$successful_files .= "- " . $successful_file . "\n";
}
// messageTemplate is formatted to be fed into sprintf. it
// requires the following arguments:
// 1. User's name
// 2. Submission ID (parent ID if applicable)
// 3. Submission ID (again)
// 4. List of failed files
// 5. List of successful files
$emailBody = sprintf($messageTemplate,
$_SESSION['obj_login']->fname . " " . $_SESSION['obj_login']->lname,
$pid != -1 ? $pid : $subID,
$pid != -1 ? $pid : $subID,
$failed_files,
$successful_files);
// additional headers
$headers = "From: portaladmin@tsilhqotin.ca\r\n"
. "Cc: portaladmin@tsilhqotin.ca\r\n";
mail($fromList, $subject, wordwrap($emailBody,70), $headers);
}
?>