equilibrium /common.php

Language PHP Lines 686
MD5 Hash 00a96263cb24b226c8d22e6ba9eb2096 Estimated Cost $13,268 (why?)
Repository https://github.com/obenauer/equilibrium.git View Raw File View Project SPDX
  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
<?php
// Copyright 2008, St. Jude Children's Research Hospital.
// Written by Dr. John Obenauer, john.obenauer@stjude.org.

// This file is part of Equilibrium.  Equilibrium is free software:
// you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software
// Foundation, either version 2 of the License, or (at your option)
// any later version.

// Equilibrium is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Equilibrium.  If not, see <http://www.gnu.org/licenses/>.

require("check_login.php");
require("config.php");

// Check for passed arguments
if (isset($_GET['action'])) {
    $action = $_GET['action'];
} else {
    $action = "";
}

if (isset($_GET['cmd'])) {
    $cmd = $_GET['cmd'];
} else {
    $cmd = "";
}

if (isset($_REQUEST['staff'])) {
    $staff = $_REQUEST['staff'];
} else {
    if ($_SESSION['SESSION_STAFF'] == "Y") {
        $staff = $_SESSION['SESSION_USERID'];
    } else {
        $staff = 0;
    }
}

if (isset($_REQUEST['status'])) {
    $status = $_REQUEST['status'];
} else {
    $status = "Active";
}

if (isset($_REQUEST['todostatus'])) {
    $todostatus = $_REQUEST['todostatus'];
} else {
    $todostatus = "Completed";
}

if (isset($_REQUEST['priority'])) {
    $priority = $_REQUEST['priority'];
} else {
    $priority = "High";
}

if (isset($_REQUEST['calmode'])) {
    $calmode = $_REQUEST['calmode'];
} else {
    $calmode = 0;
}

if (isset($_REQUEST['edit_priv'])) {
    $edit_priv = $_REQUEST['edit_priv'];
} else {
    $edit_priv = "N";
}

if (isset($_REQUEST['content'])) {
    $content = $_REQUEST['content'];
} else {
    $content = "";
}

if (isset($_REQUEST['order'])) {
    $order = $_REQUEST['order'];
} else {
    $order = "";
}

if (isset($_REQUEST['page'])) {
    $page = $_REQUEST['page'];
} else {
    $page = 1;
}

if (isset($_REQUEST['pageflag'])) {
    $pageflag = $_REQUEST['pageflag'];
} else {
    $pageflag = 0;
}

if (isset($_REQUEST['dragonly'])) {
    $dragonly = $_REQUEST['dragonly'];
} else {
    $dragonly = 0;
}

if ((isset($_REQUEST['project'])) && (is_numeric($_REQUEST['project']))) {
    $project = $_REQUEST['project'];
} else {
    $project = 0;
}

if ((isset($_REQUEST['duty'])) && (is_numeric($_REQUEST['duty']))) {
    $duty = $_REQUEST['duty'];
} else {
    $duty = 0;
}

if (isset($_REQUEST['maxresults'])) {
    $maxresults = $_REQUEST['maxresults'];
    if ($maxresults == 0) {
        $maxresults = 20;
    }
} else {
    $maxresults = 20;
}

if (isset($_REQUEST['newtext'])) {
    $newtext = $_REQUEST['newtext'];
} else {
    $newtext = 0;
}

if (isset($_REQUEST['pdflag'])) {
    $pdflag = $_REQUEST['pdflag'];
} else {
    $pdflag = "";
}

if (isset($_REQUEST['pdchange'])) {
    $pdchange = $_REQUEST['pdchange'];
} else {
    $pdchange = 0;
}

if (isset($_REQUEST['staffchange'])) {
    $staffchange = $_REQUEST['staffchange'];
} else {
    $staffchange = 0;
}

if (isset($_REQUEST['visibility'])) {
    $visibility = $_REQUEST['visibility'];
} else {
    $visibility = "Public";
}

if (isset($_REQUEST['scheduledate'])) {
    $scheduledate = $_REQUEST['scheduledate'];
} else {
    $scheduledate = "";
}

if (isset($_REQUEST['fromdate'])) {
    $fromdate = $_REQUEST['fromdate'];
} else {
    // Default: one month ago
    $prevmonth = mktime(0, 0, 0, date("m") - 1, date("d"), date("Y"));
    $fromdate = date("Y", $prevmonth) . "-" . date("m", $prevmonth) . "-" .    
        date("d", $prevmonth);
}

if (isset($_REQUEST['todate'])) {
    $todate = $_REQUEST['todate'];
} else {
    // Default: today's date
    $todate = date('Y') . "-" . date('m') . "-" . date('d');
}

// Declare PHP functions
require("equilibrium.php");

// Commands that don't generate HTML output
switch($cmd) {
    case "updatelist";
        if ($order) {
            $todo_order = explode(",", $order);
            
            if ($project) {
                $order_var = "project_order";
            } else if ($duty) {
                $order_var = "duty_order";
            } else {
                $order_var = "order_number";
            }
            
            if ($calmode) {

                // Remove alphabetic div's from to-do order
                $modlist = array();
                $modcount = 0;
                for ($i = 0; $i < count($todo_order); $i++) {
                    if (is_numeric($todo_order[$i])) {
                        $modlist[$modcount] = $todo_order[$i];
                        $modcount++;
                    }
                }
                $numeric_todos = implode(",", $modlist);

                // Retrieve existing order of to-do's
                $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
                    or die ("Cannot connect to database. " . mysql_error() . "\n<br>\n");
                mysql_select_db(DB_DATABASE);
                $get_orig_order = mysql_query("select todo_id from todos " .
                    "where todo_id in ($numeric_todos) order by $order_var asc ");
                $oldorder = array();
                $oldcount = 0;
                while ($row = mysql_fetch_array($get_orig_order, MYSQL_ASSOC)) {
                    $oldorder[$oldcount] = $row['todo_id'];
                    $oldcount++;
                }
                mysql_free_result($get_orig_order);

                // Get schedule dates of to-do's near the moved one
                $mover = 0;
                $datepartner = 0;
                find_mover($oldorder, $todo_order, $mover, $datepartner);

                // Re-assign scheduled date of moved to-do item
                $get_copy_date = mysql_query("select schedule_date from todos " .
                    "where todo_id = \"" . $datepartner . "\" ");
                if ($row = mysql_fetch_array($get_copy_date, MYSQL_ASSOC)) {
                    $copied_date = $row['schedule_date'];
                }
                mysql_free_result($get_copy_date);
                if (($copied_date == "0000-00-00") || ($copied_date == "")) {
                    $copied_date = "NULL";
                } else {
                    // Don't allow scheduling a to-do to a past date
                    $parts = explode("-", $copied_date);
                    $calday = mktime(0, 0, 0, $parts[1], $parts[2], $parts[0]);
                    $today = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
                    if ($calday < $today) {
                        $copied_date = "NULL";
                    }
                }
                $query = "update todos set schedule_date = \"" . $copied_date . "\" " .
                    "where todo_id = \"" . $mover . "\" ";
                $update = mysql_query($query);
                if ($update != 1) {
                    $errorflag = 1;
                }
            }

            // Re-order to-do items in response to drag and drop
            $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
            $errorflag = 0;
            $offset = ($page - 1) * $maxresults;
            for ($i = 0; $i < count($todo_order); $i++) {
                
                $query = "update todos set $order_var = \"" . ($offset + $i + 1) . "\" " .
                    "where todo_id = \"" . $todo_order[$i] . "\" ";
                //printf("query = $query (page = $page)<br>\n");
                $update = $conn->query($query);
                if ($update != 1) {
                    $errorflag = 1;
                }
            }
            // Close the database connection
            $conn->close();

            $new_todolist = build_todolist($staff, $status, $todostatus, $priority, 
                $calmode, $project, $duty, $pageflag, $dragonly, $page, $maxresults);
            printf($new_todolist);
            exit;
        } else {
            printf("<p>Error updating to-do list: Order is not specified.</p>\n");
            require("footer.php");
            exit;
        }

        break;

    // Delete to-do item
    case "deleteitem";

        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) 
            or die ("Cannot connect to database. " . mysql_error() . "\n<br>");
        mysql_select_db(DB_DATABASE);
        $content = trim(mysql_real_escape_string($content));
        $result = mysql_query("DELETE FROM todos WHERE todo_id = '$content'");
        mysql_close($conn);
        $new_todolist = build_todolist($staff, $status, $todostatus, $priority, 
            $calmode, $project, $duty, $pageflag, $dragonly, $page, $maxresults);
        printf($new_todolist);
        exit;
        break;

    case "edititem";

        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) 
            or die ("Cannot connect to database. " . mysql_error() . "\n<br>");
        mysql_select_db(DB_DATABASE);
        //$newtext = trim(mysql_real_escape_string($newtext));
        if (($scheduledate) && ($scheduledate != "NULL") && ($scheduledate != "000-00-00")) {
            $scheduleclause = "schedule_date = \"$scheduledate\", ";
        } else {
            $scheduleclause = "schedule_date = NULL, ";
        }
        if ($pdflag == "Project") {
            $edit_item = mysql_query("UPDATE todos set description = \"" . $newtext . 
                "\", project_id = \"$pdchange\", duty_id = \"0\", " .
                "staff_assigned = \"$staffchange\", $scheduleclause " . 
                "visibility = \"$visibility\" WHERE todo_id = '$content' ");
        
        } else if ($pdflag == "Duty") {
            $edit_item = mysql_query("UPDATE todos set description = \"" . $newtext . 
                "\", duty_id = \"$pdchange\", project_id = \"0\", " .
                "staff_assigned = \"$staffchange\", $scheduleclause " . 
                "visibility = \"$visibility\" WHERE todo_id = '$content' ");
                
        } else {
            $edit_item = mysql_query("UPDATE todos set description = \"" . $newtext . 
                "\", staff_assigned = \"$staffchange\", $scheduleclause " . 
                "visibility = \"$visibility\" WHERE todo_id = '$content' ");
        }
        mysql_close($conn);

        $new_todolist = build_todolist($staff, $status, $todostatus, $priority, 
            $calmode, $project, $duty, $pageflag, $dragonly, $page, $maxresults);
        //$new_todolist .= "UPDATE todos set description = \"" . $newtext . 
        //        "\", duty_id = \"$pdchange\", project_id = \"0\", " .
        //        "staff_assigned = \"$staffchange\", " . 
        //        "visibility = \"$visibility\" WHERE todo_id = '$content' ";
        printf($new_todolist);
        exit;

        break;

    case "additem";

        // Add new item
        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) 
            or die ("Cannot connect to database. " . mysql_error() . "\n<br>");
        mysql_select_db(DB_DATABASE);
        //$newtext = trim(mysql_real_escape_string($content));
        $newtext = trim($content);
        if ($newtext) {
            if ($pdflag == "Project") {
                
                // Move existing to-do items up in order
                $start_trans = mysql_query("start transaction ");
                $get_order = mysql_query("SELECT todo_id from todos " .
                    "where project_id = \"$pdchange\" " .
                    "and completed = 'N' " .
                    "order by project_order asc ");
                $count = 2;
                while ($row = mysql_fetch_array($get_order, MYSQL_ASSOC)) {
                    $set_order = mysql_query("update todos set project_order = \"$count\" " .
                        "where todo_id = \"" . $row['todo_id'] . "\" ");
                    $count++;
                }
                mysql_free_result($get_order);
                    
                // Assign this to-do the same visibility as its project
                $get_vis = mysql_query("SELECT visibility from projects " .
                    "where project_id = \"$pdchange\" ");
                if ($row = mysql_fetch_array($get_vis, MYSQL_ASSOC)) {
                    $visibility = $row['visibility'];
                } else {
                    $visibility = 'Public';
                }
                mysql_free_result($get_vis);
                
                // Add new item at top of list
                $add_item = mysql_query("INSERT into todos (description, " .
                    "project_id, duty_id, staff_assigned, project_order, " .
                    "visibility) " .
                    "values (\"$newtext\", \"$pdchange\", \"0\", \"$staff\", " .
                    "\"1\", \"$visibility\" ) ");
                $stop_trans = mysql_query("commit ");
                
            } else if ($pdflag == "Duty") {
                
                // Move existing to-do items up in order
                $start_trans = mysql_query("start transaction ");
                $get_order = mysql_query("SELECT todo_id from todos " .
                    "where duty_id = \"$pdchange\" " .
                    "and completed = 'N' " .
                    "order by duty_order asc ");
                $count = 2;
                while ($row = mysql_fetch_array($get_order, MYSQL_ASSOC)) {
                    $set_order = mysql_query("update todos set duty_order = \"$count\" " .
                        "where todo_id = \"" . $row['todo_id'] . "\" ");
                    $count++;
                }
                mysql_free_result($get_order);
                
                // Assign this to-do the same visibility as its duty
                $get_vis = mysql_query("SELECT visibility from duties " .
                    "where duty_id = \"$pdchange\" ");
                if ($row = mysql_fetch_array($get_vis, MYSQL_ASSOC)) {
                    $visibility = $row['visibility'];
                } else {
                    $visibility = 'Public';
                }
                mysql_free_result($get_vis);
                
                // Add new item at top of list
                $add_item = mysql_query("INSERT into todos (description, " .
                    "project_id, duty_id, staff_assigned, duty_order, " .
                    "visibility) " .
                    "values (\"$newtext\", \"0\", \"$pdchange\", \"$staff\", " .
                    "\"1\", \"$visibility\" ) ");
                $stop_trans = mysql_query("commit ");
            
            } else {
                
                // Query conditions for To Do page
                if ($status == "All") {
                    $statusclause = "";
                } else if ($status == "Open") {
                    $statusclause = "and ((p.status in ('Pending', 'Active', 'Suspended') " .
                        "or t.project_id = 0) " .
                        "and (d.status = \"Active\"  or t.duty_id = 0)) ";
                } else {
                    $statusclause = "and ((p.status = \"$status\" " .
                        "or t.project_id = 0) " .
                        "and (d.status = \"Active\"  or t.duty_id = 0)) ";
                }
                
                // Only let people add items as themselves, but can reassign later
                $staffclause = "and t.staff_assigned = \"" . 
                    $_SESSION['SESSION_USERID'] . "\" ";
                
                // Move existing to-do items up in order
                $start_trans = mysql_query("start transaction ");
                $get_order = mysql_query("SELECT t.todo_id from todos as t " .
                    "left join projects as p on t.project_id = p.project_id " .
                    "left join duties as d on t.duty_id = d.duty_id " .
                    "where project_id = \"$pdchange\" " .
                    "and completed = 'N' " .
                    $statusclause . $staffclause .
                    "order by order_number asc ");
                $count = 2;
                while ($row = mysql_fetch_array($get_order, MYSQL_ASSOC)) {
                    $set_order = mysql_query("update todos set order_number = \"$count\" " .
                        "where todo_id = \"" . $row['todo_id'] . "\" ");
                    $count++;
                }
                mysql_free_result($get_order);
                
                // Add new item at top of list
                $add_item = mysql_query("INSERT into todos (description, project_id, " .
                    "duty_id, staff_assigned, order_number) values (\"" . $newtext . "\", \"0\", " .
                    "\"0\", \"$staff\", \"1\" ) ");
                $stop_trans = mysql_query("commit ");
            
            }
        }
        mysql_close($conn);

        $new_todolist = build_todolist($staff, $status, $todostatus, $priority, 
            $calmode, $project, $duty, $pageflag, $dragonly, $page, $maxresults);
        printf($new_todolist);
        exit;

        break;

      // Toggle task priority
      case 'togglepriority':

        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
            or die ("Cannot connect to database. " . mysql_error() . "\n<br>");
        mysql_select_db(DB_DATABASE);
        // escape input data
        $content = trim(mysql_real_escape_string($content));

        // Check whether task is high or low priority already
        $result = mysql_query('SELECT priority FROM todos ' .
            'where todo_id = "' . $content . '"');
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        if ($row['priority'] == "High") {

            // mark task low priority
            $result = mysql_query('UPDATE todos set priority = "Low" WHERE todo_id="'
                . $content . '"');

        } else {

            // mark task completed
            $result = mysql_query('UPDATE todos set priority = "High" WHERE todo_id="'
                . $content . '"');
        }
        mysql_close($conn);

        $new_todolist = build_todolist($staff, $status, $todostatus, $priority, 
            $calmode, $project, $duty, $pageflag, $dragonly, $page, $maxresults);
        printf($new_todolist);
        exit;

        break;
    //}

    case "togglecomplete";

        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) 
            or die ("Cannot connect to database. " . mysql_error() . "\n<br>");
        mysql_select_db(DB_DATABASE);
        $content = trim(mysql_real_escape_string($content));
        
        // Check whether task is already marked completed
        $result = mysql_query('SELECT completed FROM todos ' .
            'where todo_id = "' . $content . '"');
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        if ($row['completed'] == "Y") {
        
            // mark task NOT completed
            $result = mysql_query('UPDATE todos set completed = "N", ' .
                'completed_date = NULL, completed_time = NULL ' .
                'WHERE todo_id="' . $content . '"');

        } else {
        
            // mark task completed
            $result = mysql_query('UPDATE todos set completed = "Y", ' .
                'completed_date = CURDATE(), completed_time = NOW() ' .
                'WHERE todo_id="' . $content . '"');
        }          
        mysql_close($conn);
        
        $new_todolist = build_todolist($staff, $status, $todostatus, $priority, 
            $calmode, $project, $duty, $pageflag, $dragonly, $page, $maxresults);
        printf($new_todolist);
        exit;
        break;

    case "togglecomplete_twolists";

        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) 
            or die ("Cannot connect to database. " . mysql_error() . "\n<br>");
        mysql_select_db(DB_DATABASE);
        $content = trim(mysql_real_escape_string($content));
        
        // Check whether task is already marked completed
        $result = mysql_query('SELECT completed FROM todos ' .
            'where todo_id = "' . $content . '"');
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        if ($row['completed'] == "Y") {
        
            // mark task NOT completed
            $result = mysql_query('UPDATE todos set completed = "N", ' .
                'completed_date = NULL WHERE todo_id="' . $content . '"');

        } else {
        
            // mark task completed
            $result = mysql_query('UPDATE todos set completed = "Y", ' .
                'completed_date = CURDATE() WHERE todo_id="' . $content . '"');
        }          
        mysql_close($conn);
        
        $new_twolists = build_two_lists($staff, $status, $priority, $calmode,
            $project, $duty, $pageflag, $dragonly, $page, $maxresults);
        printf($new_twolists);
        exit;
        break;

    // Delete comment
    case "deletecomment";

        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) 
            or die ("Cannot connect to database. " . mysql_error() . "\n<br>");
        mysql_select_db(DB_DATABASE);
        $content = trim(mysql_real_escape_string($content));
        $result = mysql_query("DELETE FROM comments WHERE comment_id = '$content' ");
        mysql_close($conn);
        $new_commentlist = build_commentlist($staff, $project, $duty, 
            $fromdate, $todate, $pageflag, $page, $maxresults);
        printf($new_commentlist);
        break;

    // Edit comment
    case "editcomment";

        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) 
            or die ("Cannot connect to database. " . mysql_error() . "\n<br>");
        mysql_select_db(DB_DATABASE);
        $newtext = trim(mysql_real_escape_string($newtext));
        if ($pdflag == "Project") {
            $edit_comment = mysql_query("UPDATE comments set comment_text = \"" . $newtext . 
                "\", project_id = \"$pdchange\", duty_id = \"0\", " .
                "submitter_id = \"" . $_SESSION['SESSION_USERID'] . "\", " . 
                "visibility = \"$visibility\" WHERE comment_id = '$content' ");
        
        } else if ($pdflag == "Duty") {
            $edit_comment = mysql_query("UPDATE comments set comment_text = \"" . $newtext . 
                "\", duty_id = \"$pdchange\", project_id = \"0\", " .
                "submitter_id = \"" . $_SESSION['SESSION_USERID'] . "\", " . 
                "visibility = \"$visibility\" WHERE comment_id = '$content' ");
                
        } else {
            $edit_comment = mysql_query("UPDATE comments set comment_text = \"" . $newtext . 
                "\", submitter_id = \"" . $_SESSION['SESSION_USERID'] . "\", " . 
                "visibility = \"$visibility\" WHERE comment_id = '$content' ");
        }
        mysql_close($conn);

        $new_commentlist = build_commentlist($staff, $project, $duty, 
            $fromdate, $todate, $pageflag, $page, $maxresults);
        printf($new_commentlist);
        exit;

        break;

    // Add comment
    case "addcomment";

        // Add new comment
        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) 
            or die ("Cannot connect to database. " . mysql_error() . "\n<br>");
        mysql_select_db(DB_DATABASE);
        $newtext = trim(mysql_real_escape_string($content));
        if ($newtext) {
            if ($pdflag == "Project") {
                
                // Assign this comment the same visibility as its project
                $get_vis = mysql_query("SELECT visibility from projects " .
                    "where project_id = \"$pdchange\" ");
                if ($row = mysql_fetch_array($get_vis, MYSQL_ASSOC)) {
                    $visibility = $row['visibility'];
                } else {
                    $visibility = 'Public';
                }
                mysql_free_result($get_vis);
                
                // Add new comment
                $add_comment = mysql_query("INSERT into comments (comment_text, " .
                    "project_id, duty_id, submitter_id, submit_date, submit_time, " .
                    "visibility) " .
                    "values (\"$newtext\", \"$pdchange\", \"0\", \"" . 
                    $_SESSION['SESSION_USERID'] . "\", curdate(), now(), " .
                    "\"$visibility\" ) ");
                
            } else if ($pdflag == "Duty") {
                
                // Assign this comment the same visibility as its duty
                $get_vis = mysql_query("SELECT visibility from duties " .
                    "where duty_id = \"$pdchange\" ");
                if ($row = mysql_fetch_array($get_vis, MYSQL_ASSOC)) {
                    $visibility = $row['visibility'];
                } else {
                    $visibility = 'Public';
                }
                mysql_free_result($get_vis);
                
                // Add new comment
                $add_comment = mysql_query("INSERT into comments (comment_text, " .
                    "project_id, duty_id, submitter_id, submit_date, submit_time, " .
                    "visibility) " .
                    "values (\"$newtext\", \"0\", \"$pdchange\", \"" . 
                    $_SESSION['SESSION_USERID'] . "\", curdate(), now(), " .
                    "\"$visibility\" ) ");
            
            } else {
                
                // Add new comment
                $add_comment = mysql_query("INSERT into comments (comment_text, " .
                    "project_id, duty_id, submitter_id, submit_date, submit_time) " .
                    "values (\"$newtext\", \"0\", \"$pdchange\", \"" . 
                    $_SESSION['SESSION_USERID'] . "\", curdate(), now() ) ");
                // Add new item at top of list
            
            }
        }
        mysql_close($conn);

        $new_commentlist = build_commentlist($staff, $project, $duty, 
            $fromdate, $todate, $pageflag, $page, $maxresults);
        printf($new_commentlist);
        exit;
        break;

}

?>
Back to Top