-
Notifications
You must be signed in to change notification settings - Fork 3
/
pgsql_dao.php
569 lines (508 loc) · 11.9 KB
/
pgsql_dao.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<?
function dbo_Search_Open_Appt($ou, $role, $start_day, $end_day, $by_id = null, $by_ou = null, $by_role = null){
$sql = "
select b.bid, b.title, p.role, p.key, cast(p.value as int),
b.start_time, b.end_time,
count(s.id)
from blocks b
left join properties p on (b.bid = p.bid)
left join participants s on (b.bid = s.bid and s.role = '$role')
left join participants me on (b.bid = me.bid)
where p.ou_code = '$ou'
and TO_CHAR(b.start_time,'YYYY-MM-DD') >= '".$start_day."'
and TO_CHAR(b.start_time,'YYYY-MM-DD') <= '".$end_day."'
";
if($by_id){
$sql .= "and me.id = '$by_id'";
}
if($by_ou){
$sql .= "and me.ou_code = '$by_ou'";
}
if($by_role){
$sql .= "and me.role = '$by_role'";
}
$sql .= "
and p.role = '$role'
and p.key = 'max'
group by b.bid, b.title, p.role, p.key, p.value, b.start_time, b.end_time
having count(s.id) < cast(p.value as int)
order by b.start_time
";
$results = db_query($sql);
return $results;
}
function dbo_Appt_General($id, $bid){
$sql = "
select * from blocks b
WHERE b.bid = '$bid'
";
$results = db_query($sql);
return $results;
}
function dbo_Appt_Details($id, $bid){
$sql = "
select * from blocks b
left join participants p on (b.bid= p.bid)
left join person n on (p.id = n.id)
left join ous o on (p.ou_code = o.ou_code)
WHERE b.bid = '$bid'
order by role
";
$results = db_query($sql);
return $results;
}
function dbo_Block_Properties($id, $bid){
$sql = "
select * from properties p
WHERE p.bid = '$bid'
";
$results = db_query($sql);
return $results;
}
function dbo_insertSample($id, $bid){
$sql = "
DELETE FROM participants
WHERE id = '$id' and bid = '$bid'
";
db_query($sql);
$sql = "
INSERT INTO participants
(bid, id, ou_code, role, created_by, attending)
VALUES
('$bid', '$id', 'think', 'counselor', '00776161', 'y')
";
//echo $sql;
db_query($sql);
}
function dbo_getDayOfAppointments($id, $day, $ou_code, $role){
if(($ou_code == null) and ($role == null)){
$sql = "
SELECT b.bid, p.ou_code, p.role,
p.attending, p.created_by,
b.title,
b.start_time,
b.end_time
FROM participants p
left join blocks b on (p.bid = b.bid)
where b.start_time > '$day'
ORDER BY b.start_time
";
}
//TODO Review
if(($ou_code != null) and ($role != null)){
$sql = "
SELECT b.bid, p.ou_code, p.role,
p.attending, p.created_by,
b.title,
b.start_time,
b.end_time
FROM participants p
left join blocks b on (p.bid = b.bid)
where b.start_time > '$day'
and p.ou_code = '$ou_code'
and p.role = '$role'
and b.title = 'Counseling'
ORDER BY b.start_time
";
echo $sql;
}
$results = db_query($sql);
return $results;
}
function dbo_getBid($bid, $id = null){
//Notice the (MM - 1)
//For some reason javascript starts counting months at zero. Crazy huh?
$sql = "
SELECT b.bid, p.ou_code, p.role,
p.attending, p.created_by,
b.title,
b.start_time,
TO_CHAR(b.start_time,'YYYY, (MM - 1), DD, HH24, MI') AS start_time,
TO_CHAR(b.end_time,'YYYY, (MM - 1), DD, HH24, MI') AS end_time,
case when u.sub_value is null then '333399' else u.sub_value end as color
FROM participants p
left join blocks b on (p.bid = b.bid)
left join user_settings u on (
p.id = u.id
and u.key = 'calendar_color'
and u.value = p.ou_code || '/' || p.role )
where b.bid = '$bid'
";
if($id){
$sql.="and p.id = '$id'";
}
$sql.="
ORDER BY b.start_time
";
$results = db_query($sql);
return $results;
}
function dbo_getMonthOfAppointments($id, $day, $ou_code = null, $role = null){
//Notice the (MM - 1)
//For some reason javascript starts counting months at zero. Crazy huh?
$sql = "
SELECT b.bid, p.ou_code, p.role,
p.attending, p.created_by,
b.title,
b.start_time,
TO_CHAR(b.start_time,'YYYY, (MM - 1), DD, HH24, MI') AS start_time,
TO_CHAR(b.end_time,'YYYY, (MM - 1), DD, HH24, MI') AS end_time,
case when u.sub_value is null then '#333399' else u.sub_value end as color
FROM participants p
left join blocks b on (p.bid = b.bid)
left join user_settings u on (
p.id = u.id
and u.key = 'calendar_color'
and u.value = p.ou_code || '/' || p.role )
where p.id = '$id'
--and TO_CHAR(b.start_time,'YYYY-MM') = '".substr($day, 0, 7)."'
ORDER BY b.start_time
";
//TODO review
if(($ou_code != null) and ($role != null)){
$sql = "
SELECT b.bid, p.ou_code, p.role,
p.attending, p.created_by,
b.title,
b.start_time,
TO_CHAR(b.start_time,'YYYY, (MM - 1), DD, HH24, MI') AS start_time,
TO_CHAR(b.end_time,'YYYY, (MM - 1), DD, HH24, MI') AS end_time
FROM participants p
left join blocks b on (p.bid = b.bid)
where p.id = '$id'
and p.ou_code = '$ou_code'
and p.role = '$role'
and b.title = 'Counseling'
ORDER BY b.start_time
";
}
//TODO Filter month
$results = db_query($sql);
return $results;
}
function dbo_person($id){
$sql = "
SELECT firstname, middlename, lastname, email
FROM person
WHERE id = '$id'
";
//TODO Filter month
$results = db_query($sql);
$return = $results[0];
return $return;
}
function dbo_addRole($id, $ou, $role){
$sql = "
INSERT INTO ou_roles
(id, ou_code, role)
VALUES
('$id', '$ou', '$role')
";
$results = db_query($sql);
return $results;
}
function getRoles($id, $ou){
$sql = "
select o.role, r.long_name, r.description
from ou_roles o
left join roles r on (o.ou_code = r.ou_code and o.role = r.role)
where o.ou_code = '$ou'
and o.id = '$id'
";
$results = db_query($sql);
return $results;
}
function getOus($id){
$sql = "
SELECT ou_roles.ou_code,
ous.long_name
FROM ou_roles
LEFT JOIN ous on (ou_roles.ou_code = ous.ou_code)
where id = '$id'
group by ou_roles.ou_code, ous.long_name
";
$results = db_query($sql);
return $results;
}
function dbo_getPassword($username){
$sql = "
SELECT content
FROM login
WHERE method = 'password'
AND id = '$username'
";
$results = db_query($sql);
return $results;
}
function dbo_insertOrUpdateLocalPassword($id, $password){
include("common.php");
$sql = "
SELECT count(*) as c
FROM login
WHERE id = '$id'
AND method = 'password'
";
$results = db_query($sql);
if($results[0]['c'] > 0){
//update
$sql = "
UPDATE login
SET content = '".md5($password)."'
WHERE id = '$id'
AND method = 'password'
";
}else{
//Insert
$sql = "
INSERT INTO login
(id, method, content)
VALUES
('$id', 'password', '".md5($password)."')
";
}
$results = db_query($sql);
return $results;
}
function dbo_insertOrUpdateLocalPerson($id, $firstname = null, $middlename = null, $lastname = null, $dob = null, $email = null){
if(strlen($dob) < 10){
$dob = 'null';
}
$sql = "
SELECT count(*) as c
FROM person
WHERE id = '$id'
";
$results = db_query($sql);
if($results[0]['c'] > 0){
//update
$sql = "
UPDATE person
SET firstname = '$firstname',
middlename = '$middlename',
lastname = '$lastname',
email = '$email',
dob = $dob
WHERE id = '$id'
";
}else{
//Insert
$sql = "
INSERT INTO person
(id, firstname, middlename, lastname, dob, email)
VALUES
('$id', '$firstname', '$middlename', '$lastname', $dob, '$email')
";
}
$results = db_query($sql);
return $results;
}
function dboUpdateUserSettings($id, $key, $value, $sub_value = null){
if(!$sub_value) {
$sql = "
UPDATE user_settings
SET active = '0',
deleted_by = '$id',
deleted_ts = ('now'::text)::timestamp with time zone
WHERE key = '$key'
AND id = '$id'
AND active = '1'
";
db_query($sql);
$sql = "
INSERT INTO user_settings
(id, key, value)
VALUES
('$id', '$key', '$value')
";
$return = db_query($sql);
}else{
$sql = "
UPDATE user_settings
SET active = '0',
deleted_by = '$id',
deleted_ts = ('now'::text)::timestamp with time zone
WHERE key = '$key'
AND id = '$id'
AND value = '$value'
AND active = '1'
";
//Try it with just a delete. Why do we need to save a history of user settings?
$sql = "
delete from user_settings
WHERE key = '$key'
AND id = '$id'
AND value = '$value'
";
db_query($sql);
$sql = "
INSERT INTO user_settings
(id, key, value, sub_value)
VALUES
('$id', '$key', '$value', '$sub_value')
";
$return = db_query($sql);
}
return $return;
}
function dboUpdateRolePermissions($ou, $role, $key, $value){
$return = "unknown_error";
$sql = "
SELECT *
FROM role_permissions
WHERE ou = '$ou'
AND role = '$role'
AND permissions = '$key'
AND affected = '$value'
";
$results = db_query($sql);
if(strlen($results[0]['permissions']) > 0){
$return = "already_set";
}else{
$sql = "
INSERT INTO
role_permissions
(ou, role, permissions, affected, allow)
VALUES
('$ou', '$role', '$key', '$value', '1')
";
db_query($sql);
$return = "set";
}
return $return;
}
function dbo_CurrentUserValue($id, $key, $value = null){
if(!$value){
$sql = "
SELECT value
FROM user_settings
WHERE key = '$key'
and id = '$id'
and active = '1'
";
$return = db_query($sql);
return $return[0]['value'];
}else{
$sql = "
SELECT sub_value as value
FROM user_settings
WHERE key = '$key'
and id = '$id'
and value = '$value'
and active = '1'
";
$return = db_query($sql);
return $return[0]['value'];
}
}
function dbo_getRangeOfAppointments($id, $start_day, $end_day, $ou_code = null, $role = null){
//TODO Maybe make the $ou_code and $role an array so we can display blocks from mutiple roles in one call
//Notice the (MM - 1)
//For some reason javascript starts counting months at zero. Crazy huh?
$sql = "
SELECT b.bid, p.ou_code, p.role,
p.attending, p.created_by,
b.title,
b.start_time,
TO_CHAR(b.start_time,'YYYY, (MM - 1), DD, HH24, MI') AS start_time,
TO_CHAR(b.end_time,'YYYY, (MM - 1), DD, HH24, MI') AS end_time,
case when u.sub_value is null then '#333399' else u.sub_value end as color
FROM blocks b
--FROM participants p
left join participants p on (b.bid = p.bid)
--left join blocks b on (p.bid = b.bid)
left join user_settings u on (
p.id = u.id
and u.key = 'calendar_color'
and u.value = p.ou_code || '/' || p.role )
where p.id = '$id'
";
if($ou_code){
$sql.= "and p.ou_code = '$ou_code'";
}
if($role){
$sql.= "and p.role = '$role'";
}
$sql .= "
and TO_CHAR(b.start_time,'YYYY-MM-DD') >= '".$start_day."'
and TO_CHAR(b.start_time,'YYYY-MM-DD') <= '".$end_day."'
ORDER BY b.start_time
";
$results = db_query($sql);
return $results;
}
function dbo_newBlock($bid, $start_time, $end_time, $title, $created_by){
$sql = "
INSERT INTO blocks
(bid, title, start_time, end_time, created_by, created)
VALUES
('$bid', '$title', '$start_time', '$end_time', '$created_by', now())
";
db_query($sql);
//Should we return somenthing?
}
function dbo_addParticipant($bid, $id, $ou_code, $role, $created_by, $attending){
//quote attending character if not null
if($attending){
$attending = "'$attending'";
}
$sql = "
INSERT INTO participants
(bid, id, ou_code, role, created, created_by, attending)
VALUES
('$bid', '$id', '$ou_code', '$role', now(), '$created_by', $attending)
";
db_query($sql);
//Should we return somenthing?
}
function dbo_addProperty($bid, $id, $ou_code, $role, $key, $value, $created_by){
//TODO Check for duplicates, or overlapping properties
$sql = "
INSERT INTO properties
(bid, id, ou_code, role, key, value, created_by)
VALUES
('$bid', '$id', '$ou_code', '$role', '$key', '$value', '$created_by')
";
db_query($sql);
}
function dbo_isBidFull($bid){
$sql = "
select count(s.id) as count
from properties p
left join participants s on (
p.bid = s.bid
and p.ou_code = s.ou_code
and p.role = s.role
)
where p.bid = '$bid'
and s.id is not null
";
$results = db_query($sql);
return $results[0]['count'];
}
function dbo_ouLongName($ou){
$sql = "
select long_name
from ous
where ou_code = '$ou'
";
$results = db_query($sql);
return $results[0]['long_name'];
}
function dbo_miscLog($log){
$sql = "
INSERT INTO log (log) VALUES ('$log')
";
$results = db_query($sql);
return $results;
}
function dbo_getUserRole($bid, $id){
$sql = "
select ou_code, role, id
from participants
where bid = '$bid'
and id = '$id'
";
$result = db_query($sql);
return $result[0];
}
?>