This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathajax.php
1548 lines (1398 loc) · 45.6 KB
/
ajax.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once('include/Database.inc.php');
require_once('include/Mail.inc.php');
require_once('include/auth.inc.php');
require_once('include/autoresponse.inc.php');
header('Content-type: application/json');
if (!isLoggedIn(false)) { // if we're not logged in, tell user
echo json_encode(array(
'status'=>401, // unauthorized
'message'=>'Unauthorized. Please login to complete your request.'
)
);
exit();
}
if (isExpired()) { // if session expired, tell user
echo json_encode(array(
'status'=>401, // unauthorized
'message'=>'Session expired. Please login to complete your request.'
)
);
exit();
}
updateLastReq(); // ajax req means user is active
$cmd = $_REQUEST['cmd']; // get command to perform
$data = array('status'=>200); // default to OK
// try to get current user permissions
$r = $db->q("SELECT p.*
FROM volunteers v
LEFT JOIN privileges p
ON v.privilege_id = p.id
WHERE v.id=$_SESSION[id]"
);
$priv_error = json_encode(array(
'status'=>500, // db error
'message'=>"An error occurred while checking your privileges.\nI cannot allow you to proceed."
)
);
if (!$r->isValid())
die($priv_error);
// global containing all this user's privileges
$PRIV = $r->buildArray();
$PRIV = array_key_exists(0, $PRIV) ? $PRIV[0] : null;
if ($PRIV == null)
die($priv_error);
function forbidden() {
global $data;
$data['status'] = 403; // forbidden
$data['message']='Oops! You do not have permission to perform this operation.';
return $data;
}
function contains($haystack, $needle) {
return stripos($haystack, $needle) !== false;
}
function updatePassword($volunteer_id) {
global $db;
global $PRIV;
if (!$PRIV['change_priv']) { // make sure this user can modify other users
return false;
}
$pass = generatePassword(); // so generate
$sql = "UPDATE volunteers SET password = SHA1('$pass') WHERE id='$volunteer_id';";
$r = $db->q($sql);
if (!$r->isValid())
return false;
return array(
'subject'=>PAGE_TITLE . ' - Password Updated',
'message'=>"The following password has been generated for you:\r\n$pass\r\nThis password is required to administrate ".PAGE_TITLE.". Please keep it in a safe place."
);
}
function updateVolunteer($exists) {
global $db;
global $data;
global $mail;
global $PRIV;
$id = $_REQUEST['id'];
$firstName = $_REQUEST['firstname'];
$middleName = $_REQUEST['middlename'];
$lastName = $_REQUEST['lastname'];
$organization = $_REQUEST['organization'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$active_id = $_REQUEST['active_id'];
$street = $_REQUEST['street'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip = $_REQUEST['zip'];
$notes = $_REQUEST['note'];
/* forget about source id
if(!isset($_REQUEST['source_id']))
$source_id = 1; // Default to "Others";
else
$source_id = $_REQUEST['source_id'];
*/
if (isset($_REQUEST['privilege_id']))
$priv_id = $_REQUEST['privilege_id'];
else
$priv_id = null;
if ($exists) { // volunteer exists so just update
$sql = "Update volunteers Set first_name='$firstName', middle_name='$middleName',last_name='$lastName', organization='$organization', phone='$phone', email='$email', active_id='$active_id', street='$street', city='$city', state='$state',zip='$zip', notes='$notes' where id=$id";
$r = $db->q($sql);
getError($r);
for ($i=1; $i<6 ; $i++) {
if (isset($_REQUEST["volunteerRole$i"])) { //it is checked
$sql = "Insert into volunteer_roles(volunteer_id, volunteer_type_id) Values($id,$i)";
$r = $db->q($sql);
} else { // it is unchecked
$sql = "Delete From volunteer_roles Where volunteer_type_id=$i And volunteer_id=$id";
$r = $db->q($sql);
}
}
for ($i=1; $i<8 ; $i++) {
if (isset($_REQUEST["volunteerDay$i"])) { //it is checked
$sql = "Insert into volunteer_prefers(volunteer_id, day_id) Values($id,$i)";
$r = $db->q($sql);
} else { // it is unchecked
$sql = "Delete From volunteer_prefers Where day_id=$i And volunteer_id=$id";
$r = $db->q($sql);
}
}
// Check if priv changed
$sql = "SELECT p.id, p.name, v.password, v.first_name, v.last_name FROM volunteers v LEFT JOIN privileges p ON v.privilege_id = p.id WHERE v.id = $id";
$r = $db->q($sql);
$row = $r->getRow();
$old_priv_id = $row[0];
$old_priv_name = $row[1];
$old_pass = $row[2];
$old_first = $row[3];
$old_last = $row[4];
if ($priv_id != null && $priv_id != $old_priv_id) { // privs have changed
if (!$PRIV['change_priv']) { // make sure this user can modify other users
$data['status']=403;
$data['message']='Are you trying to hack us? You cannot change user privileges!';
return;
}
$sql = "UPDATE volunteers SET privilege_id=$priv_id WHERE id=$id"; // new priv
$r = $db->q($sql); // execute
getError($r);
$message = "$firstName $lastName,\r\nYour privileges have changed. If you feel this is an error, please contact us.";
/*
if (empty($old_pass)) { // no password
$pass = generatePassword(); // so generate
$sql .= ", password = SHA1('$pass') "; // and add to update
$message .= "\n\rThe following password has been generated for you:\r\n$pass\n\rThis password is required to administrate The Harvest Club.";
}*/
// always update password
$a = updatePassword($id);
if ($a) // if password updated, append message
$message .= "\r\n" . $a['message'];
// Send an email
$subject = PAGE_TITLE . ' - Privileges Changed';
$mail->send($subject, $message, $email); // use default from/replyto
}
} else { // adding new volunteer
if (!$PRIV['change_priv']) {
$priv_id = '2'; // if no change_priv, set as volunteer
}
$sql = "INSERT INTO volunteers (first_name, middle_name, last_name, organization, phone, email, active_id, street, city, state, zip, privilege_id, notes, signed_up) VALUES
('$firstName', '$middleName', '$lastName', '$organization', '$phone', '$email', '$active_id', '$street', '$city', '$state', '$zip', '$priv_id', '$notes', CURDATE())";
$r = $db->q($sql);
getError($r);
$id = mysql_insert_id();
for ($i=1; $i<6 ; $i++) {
if (isset($_REQUEST["volunteerRole$i"])) { //it is checked
$sql = "Insert into volunteer_roles(volunteer_id, volunteer_type_id) Values($id,$i)";
$r = $db->q($sql);
} else { // it is unchecked
$sql = "Delete From volunteer_roles Where volunteer_type_id=$i And volunteer_id=$id";
$r = $db->q($sql);
}
}
for ($i=1; $i<8 ; $i++) {
if (isset($_REQUEST["volunteerDay$i"])) { //it is checked
$sql = "Insert into volunteer_prefers(volunteer_id, day_id) Values($id,$i)";
$r = $db->q($sql);
} else { // it is unchecked
$sql = "Delete From volunteer_prefers Where day_id=$i And volunteer_id=$id";
$r = $db->q($sql);
}
}
if ($priv_id > 2) { // if the new can login, generate password
$a = updatePassword($id);
if ($a) { // if password updated, send email
$subject = PAGE_TITLE . ' - Welcome New Administrator';
$mail->send($subject, $a['message'], $email); // use default from/replyto
} else {
$data['status'] = 500;
$data['message'] = 'New volunteer added but password could not be generated :(';
}
}
}
}
function updateTree($exist){
global $db;
global $data;
$id = $_REQUEST['id'];
$grower_id = $_REQUEST['grower_id'];
$tree_type_id = $_REQUEST['tree_type_id'];
$varietal = $_REQUEST['varietal'];
$number = $_REQUEST['number'];
$avgHeight_id = $_REQUEST['avgHeight_id'];
$chemicaled_id = $_REQUEST['chemicaled_id'];
if($exist){
$sql = "Update grower_trees Set grower_id='$grower_id', tree_type='$tree_type_id', varietal='$varietal', number='$number', avgHeight_id='$avgHeight_id', chemicaled='$chemicaled_id' where id=$id";
$r = $db->q($sql);
if (getError($r))
return;
for ($i=1; $i<13 ; $i++) {
if (isset($_GET["tree_month$i"])) { //it is checked
$sql = "Insert into month_harvests(tree_id, month_id) Values($id,$i)";
$r = $db->q($sql);
} else { // it is unchecked
$sql = "Delete From month_harvests Where month_id=$i And tree_id=$id";
$r = $db->q($sql);
}
}
getError($r);
}
else{
$sql = "INSERT INTO grower_trees(grower_id, tree_type, varietal, number, avgHeight_id, chemicaled)
VALUES ('$grower_id', '$tree_type_id', '$varietal', '$number', '$avgHeight_id', '$chemicaled_id')";
$r = $db->q($sql);
if (getError($r))
return;
$treeID = mysql_insert_id();
for ($i=1; $i<13 ; $i++) {
if (isset($_GET["tree_month$i"])) { //it is checked
$sql = "INSERT INTO month_harvests(tree_id,month_id) VALUES('$treeID', '$i')";
$r = $db->q($sql);
} else { // it is unchecked
$sql = "Delete From month_harvests Where month_id=$i And tree_id=$treeID";
$r = $db->q($sql);
}
}
getError($r);
}
}
function getTree_Months($sql){
global $db;
global $data;
$r = $db->q($sql);
if (getError($r))
return $data;
// get result as giant array
$a = $r->buildArray();
foreach ($a as $v) {
// add a checkbox to each row (might need unique names)
$record = array();
foreach ($v as $name=>$value) {
$record[] = $value;
}
$data['datatable']['aaData'][] = $record;
}
}
function updateGrower($exist){
global $db;
global $data;
$id = $_REQUEST['id'];
$firstname = $_REQUEST['firstname'];
$middlename = $_REQUEST['middlename'];
$lastname = $_REQUEST['lastname'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$preferred = $_REQUEST['preferred'];
$street = $_REQUEST['street'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip = $_REQUEST['zip'];
$tools = $_REQUEST['tools'];
$notes = $_REQUEST['notes'];
$property_type= $_REQUEST['property_type'];
$property_relationship = $_REQUEST['property_relationship'];
if($exist){
$sql = "Update growers Set first_name='".$firstname."', middle_name ='".$middlename."', last_name='".$lastname."', phone='".$phone."', email='".$email."', street='".$street."', city='".$city."', state='".$state."',zip='".$zip."', tools='".$tools."', notes='".$notes."', property_type_id ='".$property_type."', property_relationship_id ='".$property_relationship."' where id=".$id;
$r = $db->q($sql);
}
else{
$sql = "INSERT INTO growers(first_name, middle_name, last_name, phone, email, preferred, street, city, state, zip, tools, notes, pending, property_type_id, property_relationship_id)
VALUES ('$firstname', '$middlename', '$lastname', '$phone', '$email', '$preferred', '$street', '$city', '$state', '$zip', '$tools', '$notes', 0, '$property_type', '$property_relationship')";
$r = $db->q($sql);
getError($r);
}
}
function getError($r) {
global $data;
global $db;
if ($r->isValid())
return 0; // 0 is success
$data['status'] = 999;
$data['message'] = 'DB: ' . $db->error(); // show error message
return $data;
}
function getTable($sql, $checkbox=true) {
global $db;
global $data;
//$data['datatable'] = array('aoColumns', 'aaData');
$r = $db->q($sql);
if (getError($r))
return $data;
// get result as giant array
$a = $r->buildArray();
// if empty return empty result
if (!$a) {
$data['datatable']['aoColumns'][] = array('sTitle'=>'Empty Set');
$data['datatable']['aaData'][] = array('No results found. Maybe you should add something?');
return;
}
// first column is select-all checkbox
if ($checkbox) {
$data['datatable']['aoColumns'][] = array(
'sTitle' => '<input type="checkbox" name="select-all" />',
'sWidth' => '30px',
'bSortable' => false
);
}
// add column data
foreach ($a[0] as $k => $v) {
$column = array();
$column['sTitle'] = $k;
if ($k == 'id' || $k == 'password' || contains($k, '_id')) {
$column['bSearchable'] = false;
$column['bVisible'] = false;
} else if ($k == 'middle_name' || $k == 'street' || $k == 'state' || $k == 'zip' || contains($k, '_tag') || contains($k, 'property_')) {
$column['bVisible'] = false; // hide but still searchable
} else if (contains($k,'notes') || contains($k,'phone') || contains($k,'email') || contains($k,'signed') || contains($k, 'days')) {
$column['sClass'] = 'small';
}
$data['datatable']['aoColumns'][] = $column;
}
// add row data
foreach ($a as $v) {
// add a checkbox to each row (might need unique names)
if(!$checkbox)
$record = array();
else
$record = array('<input type="checkbox" name="select-row" />');
foreach ($v as $name=>$value) {
$record[] = $value;
}
$data['datatable']['aaData'][] = $record;
}
}
function getName($sql) {
global $db;
global $data;
$r = $db->q($sql);
if (getError($r))
return $data;
// get result as giant array
$a = $r->buildArray();
foreach ($a as $v) {
$record = array();
foreach ($v as $name=>$value) {
$record[] = $value;
}
$data['datatable']['aaData'][] = $record;
}
}
function generatePassword($length=8) {
$password = "";
$possible = "12346789abcdfghjkmnpqrtvwxyzABCDFGHJKLMNPQRTVWXYZ";
$maxlength = strlen($possible);
if ($length > $maxlength)
$length = $maxlength;
// add random characters to $password until $length is reached
for ($i=0; $i<$length; $i++) {
// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, $maxlength-1), 1);
// only add unique chars
if (!strstr($password, $char)) {
$password .= $char;
}
}
return $password;
}
function dateToStr($dateStr) {
date_default_timezone_set('America/Los_Angeles');
$a = explode('-', $dateStr); // split
return date("l F j, Y", mktime(0, 0, 0, $a[1], $a[2], $a[0]));
}
switch ($cmd)
{
case 'get_notifications':
// no privs needed, just check user type
$data['id'] = 0;
$data['title'] = 'Notifications';
$sql = "SELECT 'Pending volunteers' AS 'Table', count(*) AS 'Updates'
FROM volunteers WHERE privilege_id=1
UNION
SELECT 'Active volunteers' AS 'Table', count(*) AS 'Updates'
FROM volunteers WHERE active_id=1 AND (privilege_id = 2 OR privilege_id = 3)
UNION
SELECT 'Pending growers' AS 'Table', count(*) AS 'Updates'
FROM growers WHERE pending=1
UNION
SELECT 'Upcoming events' AS 'Table', count(*) AS 'Updates'
FROM events WHERE date>=CURDATE();";
getTable($sql);
break;
case 'get_volunteers':
if (!$PRIV['view_volunteer']) {
forbidden();
break;
}
$data['id'] = 1;
$data['title'] = 'Volunteers';
$sql = "SELECT v.id,
v.first_name as First,
v.last_name as Last,
v.city as City,
v.email as Email,
v.phone as Phone,
p.name as 'User Type',
v.signed_up as 'Signed Up',
IF((v.active_id=1),'Active','Inactive') as Active,
v.notes as Notes,
v.middle_name as middle_tag,
v.organization as organization_tag,
v.street as street_tag,
v.state as state_tag,
v.zip as zip_tag,
v.password as password_id,
v.source_id,
v.privilege_id
FROM volunteers v
LEFT JOIN privileges p ON v.privilege_id = p.id;";
getTable($sql);
break;
case 'get_volunteer_stats':
if (!$PRIV['view_volunteer']) {
forbidden();
break;
}
$data['title'] = 'Volunteer-Events';
$volunteer_id = $_REQUEST['volunteer_id'];
$sql = "SELECT ve.event_id AS event_id, ve.volunteer_id, CONCAT(g.first_name,' ', g.last_name) AS Grower, e.date AS Date, ve.hour AS Hours, '0' AS surplus_tag
FROM volunteer_events ve, events e, growers g
WHERE ve.volunteer_id = $volunteer_id AND ve.event_id=e.id AND e.grower_id=g.id
UNION
SELECT '-1' AS event_id,
$volunteer_id AS volunteer_id,
'TOTAL' AS Grower,
CONCAT('(+ ',
v.surplus_hours,
' surplus):') AS Date,
(v.surplus_hours + IF(temp.hour is null,0,temp.hour)) as Hours,
v.surplus_hours AS surplus_tag
FROM volunteers v LEFT JOIN ( SELECT v2.id AS id, SUM(ve.hour ) AS hour
FROM volunteers v2, volunteer_events ve WHERE v2.id = ve.volunteer_id
GROUP BY v2.id
) temp ON temp.id = v.id
WHERE v.id=$volunteer_id;";
getTable($sql,false);
break;
case 'get_grower_stats':
if (!$PRIV['view_grower']) {
forbidden();
break;
}
$data['title'] = 'Grower-Stats';
$grower_id = $_REQUEST['grower_id'];
$sql = "SELECT tt.name AS Tree, SUM(h.pound) AS Pounds
FROM grower_trees gt, harvests h, events e, tree_types tt
WHERE gt.grower_id = $grower_id AND e.id = h.event_id AND
gt.id = h.tree_id AND tt.id=gt.tree_type
GROUP BY gt.tree_type
UNION
SELECT 'TOTAL:' AS Tree, SUM(h1.pound) AS Pounds
FROM harvests h1, events e1
WHERE e1.grower_id=$grower_id AND h1.event_id = e1.id;";
getTable($sql,false);
break;
case 'get_grower_event_stats':
if (!$PRIV['view_grower']) {
forbidden();
break;
}
$data['title'] = 'Grower-Stats';
$grower_id = $_REQUEST['grower_id'];
$sql = "SELECT e.id AS id, e.date AS Date, SUM(h.pound) AS Pounds
FROM harvests h, events e
WHERE e.grower_id = $grower_id AND e.id = h.event_id
GROUP BY e.id
UNION
SELECT '' AS id, 'TOTAL:' AS Date, SUM(h1.pound) AS Pounds
FROM `harvests` h1, `events` e1
WHERE e1.grower_id=$grower_id AND h1.event_id = e1.id";
getTable($sql,false);
break;
case 'get_growers':
if (!$PRIV['view_grower']) {
forbidden();
break;
}
$data['id'] = 2;
$data['title'] = 'Growers';
$sql = "SELECT g.id, g.first_name AS 'First Name', g.middle_name, g.last_name AS 'Last Name', g.phone AS 'Phone', g.email AS 'Email', g.preferred AS 'Preferred', g.street, g.city AS 'City', g.state, g.zip, g.tools AS tools_id, g.source_id, g.notes AS Notes, g.pending AS pending_id, IF((g.pending=1),'Pending','Approved') AS Pending, g.property_type_id, g.property_relationship_id, pt.name AS property_type, pr.name AS property_relationship
FROM growers g LEFT JOIN property_types pt ON g.property_type_id = pt.id
LEFT JOIN property_relationships pr ON g.property_relationship_id = pr.id;";
getTable($sql);
break;
case 'get_grower':
if (!$PRIV['view_grower']) {
forbidden();
break;
}
$data['id'] = 2;
$data['title'] = 'Grower';
$growerID = $_REQUEST['growerID'];
$sql = "SELECT g.id, g.first_name AS 'First Name', g.middle_name, g.last_name AS 'Last Name', g.phone AS 'Phone', g.email AS 'Email', g.preferred AS 'Preferred', g.street, g.city AS 'City', g.state, g.zip, g.tools AS tools_id, g.source_id, g.notes AS Notes, g.pending AS pending_id, IF((g.pending=1),'Pending','Approved') AS Pending, g.property_type_id, g.property_relationship_id, pt.name AS property_type, pr.name AS property_relationship
FROM growers g LEFT JOIN property_types pt ON g.property_type_id = pt.id
LEFT JOIN property_relationships pr ON g.property_relationship_id = pr.id
WHERE g.id = $growerID;";
getTable($sql);
break;
case 'get_trees': // same priv as grower
if (!$PRIV['view_grower']) {
forbidden();
break;
}
$data['id'] = 3;
$data['title'] = 'Trees';
$sql = "SELECT gt.id AS tree_id, Concat(g.first_name,' ', g.last_name) AS Owner, g.id AS grower_id , tt.id AS 'tree_type_id', tt.name AS 'Tree type', gt.varietal AS Varietal, gt.number AS Number, gt.chemicaled AS Chemicaled_id, IF((gt.chemicaled=0),'No','Yes') AS 'Chemicals Used', th.id AS avgHeight_id, th.name AS Height,
( SELECT group_concat(m.name)
FROM month_harvests mh, months m
WHERE mh.tree_id = gt.id AND mh.month_id = m.id) month_tag
FROM grower_trees gt, tree_types tt, growers g, tree_heights th
WHERE g.id = gt.grower_id AND gt.tree_type=tt.id AND gt.avgHeight_id = th.id;";
getTable($sql);
break;
case 'get_trees_from':
if (!$PRIV['view_grower']) {
forbidden();
break;
}
$data['id'] = 3;
$data['title'] = 'Trees';
$growerID = $_REQUEST['growerID'];
$sql = "SELECT gt.id AS tree_id, Concat(g.first_name,' ', g.last_name) AS Owner, g.id AS grower_id , tt.id AS 'tree_type_id', tt.name AS 'Tree type', gt.varietal AS Varietal, gt.number AS Number, gt.chemicaled AS Chemicaled_id, IF((gt.chemicaled=0),'No','Yes') AS 'Chemicals Used', th.id AS avgHeight_id, th.name AS Height,
( SELECT group_concat(m.name)
FROM month_harvests mh, months m
WHERE mh.tree_id = gt.id AND mh.month_id = m.id) month_tag
FROM grower_trees gt, tree_types tt, growers g, tree_heights th
WHERE g.id = gt.grower_id AND g.id = $growerID AND gt.tree_type=tt.id AND gt.avgHeight_id = th.id;";
getTable($sql);
break;
case 'get_active_volunteers':
if (!$PRIV['view_volunteer']) {
forbidden();
break;
}
$data['id'] = 1;
$data['title'] = 'Volunteers';
$sql = "SELECT v.id,
v.first_name as First,
v.last_name as Last,
v.city as City,
v.email as Email,
v.phone as Phone,
p.name as 'User Type',
v.signed_up as 'Signed Up',
IF((v.active_id=1),'Active','Inactive') as Active,
v.notes as Notes,
v.middle_name as middle_tag,
v.organization as organization_tag,
v.street as street_tag,
v.state as state_tag,
v.zip as zip_tag,
v.password as password_id,
v.source_id,
v.privilege_id
FROM volunteers v
LEFT JOIN privileges p ON v.privilege_id = p.id
WHERE v.active_id = 1 AND (privilege_id = 2 OR privilege_id = 3);";
getTable($sql);
break;
case 'get_pending_volunteers':
if (!$PRIV['view_volunteer']) {
forbidden();
break;
}
$data['id'] = 1;
$data['title'] = 'Volunteers';
$sql = "SELECT v.id,
v.first_name as First,
v.last_name as Last,
v.city as City,
v.email as Email,
v.phone as Phone,
p.name as 'User Type',
v.signed_up as 'Signed Up',
IF((v.active_id=1),'Active','Inactive') as Active,
v.notes as Notes,
v.middle_name as middle_tag,
v.organization as organization_tag,
v.street as street_tag,
v.state as state_tag,
v.zip as zip_tag,
v.password as password_id,
v.source_id,
v.privilege_id
FROM volunteers v
LEFT JOIN privileges p ON v.privilege_id = p.id
WHERE v.privilege_id = 1;";
getTable($sql);
break;
case 'get_pending_growers':
if (!$PRIV['view_grower']) {
forbidden();
break;
}
$data['id'] = 2;
$data['title'] = 'Growers';
$sql = "SELECT g.id, g.first_name AS 'First Name', g.middle_name, g.last_name AS 'Last Name', g.phone AS 'Phone', g.email AS 'Email', g.preferred AS 'Preferred', g.street, g.city AS 'City', g.state, g.zip, g.tools AS tools_id, g.source_id, g.notes AS Notes, g.pending AS pending_id, IF((g.pending=1),'Pending','Approved') AS Pending, g.property_type_id, g.property_relationship_id, pt.name AS property_type, pr.name AS property_relationship
FROM growers g LEFT JOIN property_types pt ON g.property_type_id = pt.id
LEFT JOIN property_relationships pr ON g.property_relationship_id = pr.id
WHERE g.pending = 1;";
getTable($sql);
break;
break;
case 'get_distribs':
if (!$PRIV['view_distrib']) {
forbidden();
break;
}
$data['id'] = 4;
$data['title'] = 'Distributions';
$sql = "SELECT id,
name as 'Agency Name',
street as 'Street Address',
city as City,
zip as 'Zip Code',
contact as 'Agency Contact',
phone as Phone,
email as email_tag,
state as state_tag,
contact2 as contact2_tag,
phone2 as phone2_tag,
daytime as 'Days/Hours',
notes as Notes
FROM distributions dis;";
getTable($sql);
break;
case 'get_distribution_times':
if (!$PRIV['view_distrib']) {
forbidden();
break;
}
$id = $_REQUEST['id'];
$data['title'] = 'Hours';
$sql = "SELECT h.* FROM distributions d, distribution_hours h WHERE h.distribution_id = d.id AND d.id=$id";
getName($sql);
break;
case 'update_distribution':
if (!$PRIV['edit_distrib']) {
forbidden();
break;
}
global $db;
global $data;
$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$contact = $_REQUEST['contact'];
$phone = $_REQUEST['phone'];
$contact2 = $_REQUEST['contact2'];
$phone2 = $_REQUEST['phone2'];
$email = $_REQUEST['email'];
$street = $_REQUEST['street'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip = $_REQUEST['zip'];
$notes = $_REQUEST['notes'];
$daytime = $_REQUEST['daytime'];
$sql = "Update distributions Set name='$name', contact='$contact', phone='$phone', contact2='$contact2', phone2='$phone2', email='$email', street='$street', city='$city', state='$state',zip='$zip', notes='$notes', daytime='$daytime' where id=$id";
$r = $db->q($sql);
for ($i=1; $i<8 ; $i++) {
$openHour = $_REQUEST['distributionHour'.$i.'-OpenHour'];
$openMin = $_REQUEST['distributionHour'.$i.'-OpenMin'];
$closeHour = $_REQUEST['distributionHour'.$i.'-CloseHour'];
$closeMin= $_REQUEST['distributionHour'.$i.'-CloseMin'];
$sql = "Select * From distribution_hours where distribution_id= ".$id." And day_id = ".$i;
$r = $db->q($sql);
if($r->hasRows()) {
if (($openHour!='') && ($openMin!='') && ($closeHour!='') && ($closeMin!='')) {
$sql = "Update distribution_hours Set open='".$openHour.":".$openMin."', close='".$closeHour.":".$closeMin."' Where distribution_id=".$id." And day_id=".$i;
$db->q($sql);
}
if (($openHour=='') && ($openMin=='') && ($closeHour=='') && ($closeMin=='')) {
$sql = "Delete From distribution_hours Where distribution_id=".$id." And day_id=".$i;
$db->q($sql);
}
} else if (($openHour!='') && ($openMin!='') && ($closeHour!='') && ($closeMin!='')) {
$sql = "Insert into distribution_hours(distribution_id, day_id, open, close) values (".$id.",".$i.",'".$openHour.":".$openMin."','".$closeHour.":".$closeMin."')";
$db->q($sql);
}
}
break;
case 'add_distribution':
if (!$PRIV['edit_distrib']) {
forbidden();
break;
}
global $db;
global $data;
if (isset($_REQUEST['name']))
$name = $_REQUEST['name'];
else $name ="";
if (isset($_REQUEST['contact']))
$contact = $_REQUEST['contact'];
else $contact ="";
if (isset($_REQUEST['phone']))
$phone = $_REQUEST['phone'];
else $phone ="";
if (isset($_REQUEST['contact2']))
$contact2 = $_REQUEST['contact2'];
else $contact2 ="";
if (isset($_REQUEST['phone2']))
$phone2 = $_REQUEST['phone2'];
else $phone2 ="";
if (isset($_REQUEST['email']))
$email = $_REQUEST['email'];
else $email ="";
if (isset($_REQUEST['street']))
$street = $_REQUEST['street'];
else $street ="";
if (isset($_REQUEST['city']))
$city = $_REQUEST['city'];
else $city ="";
if (isset($_REQUEST['state']))
$state = $_REQUEST['state'];
else $state ="";
if (isset($_REQUEST['zip']))
$zip = $_REQUEST['zip'];
else $zip ="";
if (isset($_REQUEST['notes']))
$notes = $_REQUEST['notes'];
else $notes ="";
if (isset($_REQUEST['daytime']))
$daytime = $_REQUEST['daytime'];
else $daytime ="";
$sql = "Insert into distributions(name, contact, phone, contact2, phone2, email, street, city, state, zip, notes, daytime) Values ('$name', '$contact', '$phone', '$contact2', '$phone2', '$email','$street','$city', '$state','$zip','$notes','$daytime')";
$r = $db->q($sql);
if (!$r->isValid())
$data = getError();
else
$id = $db->getInsertId();
for ($i=1; $i<8 ; $i++) {
$openHour = $_REQUEST['distributionHour'.$i.'-OpenHour'];
$openMin = $_REQUEST['distributionHour'.$i.'-OpenMin'];
$closeHour = $_REQUEST['distributionHour'.$i.'-CloseHour'];
$closeMin= $_REQUEST['distributionHour'.$i.'-CloseMin'];
if (($openHour!='') && ($openMin!='') && ($closeHour!='') && ($closeMin!='')) {
$sql = "Insert into distribution_hours(distribution_id, day_id, open, close) values (".$id.",".$i.",'".$openHour.":".$openMin."','".$closeHour.":".$closeMin."')";
$db->q($sql);
}
}
break;
case 'remove_distribution':
if (!$PRIV['del_distrib']) {
forbidden();
break;
}
global $db;
global $data;
$id = $_REQUEST['id'];
$sql = "DELETE FROM distribution_hours WHERE distribution_id=$id";
$r = $db->q($sql);
$sql = "DELETE FROM distributions WHERE id=$id";
$r = $db->q($sql);
getError($r);
break;
case 'get_volunteer_role':
if (!$PRIV['view_volunteer']) {
forbidden();
break;
}
$id = $_REQUEST['id'];
$data['title'] = 'Roles';
$sql = "SELECT t.id FROM volunteers v, volunteer_roles r , volunteer_types t Where v.id = r.volunteer_id And r.volunteer_type_id = t.id And v.id=$id";
getName($sql);
break;
case 'get_volunteer_prefer':
if (!$PRIV['view_volunteer']) {
forbidden();
break;
}
$id = $_REQUEST['id'];
$data['title'] = 'Prefer';
$sql = "SELECT d.id FROM volunteers v, volunteer_prefers p , days d Where v.id = p.volunteer_id And p.day_id = d.id And v.id=$id";
getName($sql);
break;
case 'update_grower':
if (!$PRIV['edit_grower']) {
forbidden();
break;
}
updateGrower(true);
break;
case 'add_grower':
if (!$PRIV['edit_grower']) {
forbidden();
break;
}
updateGrower(false);
break;
case 'approve_grower':
if (!$PRIV['edit_grower']) { //TODO find out if this needs a separate priv
forbidden();
break;
}
$growerID = $_REQUEST['growerID'];
$sql = "UPDATE growers SET pending = 0
WHERE id=".$growerID;
$r = $db->q($sql);
getError($r);
break;
case 'approve_volunteer':
if (!$PRIV['edit_volunteer']) { //TODO find out if this needs a separate priv
forbidden();
break;
}
$volunteerID = $_REQUEST['volunteerID'];
$sql = "UPDATE volunteers SET privilege_id = 2
WHERE id=".$volunteerID;
$r = $db->q($sql);
getError($r);
break;
case 'update_tree':
if (!$PRIV['edit_grower']) {
forbidden();
break;
}
updateTree(true);
break;
case 'add_tree':
if (!$PRIV['edit_grower']) {
forbidden();
break;
}
updateTree(false);
break;
case 'get_tree_month':
if (!$PRIV['view_grower']) {
forbidden();
break;
}
$id = $_REQUEST['id'];
$data['title'] = 'Months';
$sql = "SELECT month_id FROM month_harvests mh Where mh.tree_id=".$id;
getTree_Months($sql);
break;
case 'update_surplus_hours':
if (!$PRIV['edit_volunteer']) {
forbidden();
break;
}
$surplus_hours = $_REQUEST['surplus_hours'];
$volunteer_id = $_REQUEST['volunteer_id'];
$data['title'] = 'Surplus Hours';
$sql = "Update volunteers Set surplus_hours = $surplus_hours where id=$volunteer_id";
$r = $db->q($sql);
break;
case 'update_volunteer':
if (!$PRIV['edit_volunteer']) {
forbidden();
break;
}
updateVolunteer(true);
break;
case 'add_volunteer':
if (!$PRIV['edit_volunteer']) {
forbidden();
break;
}
updateVolunteer(false);
break;
case 'remove_volunteer':
if (!$PRIV['del_volunteer']) {
forbidden();
break;
}
global $db;
global $data;
$id = $_REQUEST['id'];
$sql = "DELETE FROM volunteers
WHERE id=$id";
$r = $db->q($sql);
getError($r);
break;
case 'remove_grower':
if (!$PRIV['del_grower']) {
forbidden();
break;
}
global $db;
global $data;
$id = $_REQUEST['id'];
$sql = "DELETE FROM growers
WHERE id=$id";
$r = $db->q($sql);
getError($r);
break;
case 'remove_tree':
if (!$PRIV['del_grower']) {
forbidden();
break;
}
global $db;
global $data;
$id = $_REQUEST['id'];
$sql = "DELETE FROM grower_trees
WHERE id=$id";
$r = $db->q($sql);