forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_edit_event.php
2176 lines (1948 loc) · 88.8 KB
/
add_edit_event.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
/*
* Add or edit an event in the calendar.
*
* Can be displayed as a popup window, or as an iframe via
* fancybox.
*
*
* The event editor looks something like this:
*
* //------------------------------------------------------------//
* // Category __________________V O All day event //
* // Date _____________ [?] O Time ___:___ __V //
* // Title ___________________ duration ____ minutes //
* // Patient _(Click_to_select)_ //
* // Provider __________________V X Repeats ______V ______V //
* // Status __________________V until __________ [?] //
* // Comments ________________________________________________ //
* // //
* // [Save] [Find Available] [Delete] [Cancel] //
* //------------------------------------------------------------//
*
*
* Copyright (C) 2005-2013 Rod Roark <[email protected]>
* Copyright (C) 2017 Brady Miller <[email protected]>
*
* LICENSE: This program 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 3
* of the License, or (at your option) any later version.
* This program 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 this program. If not, see <https://opensource.org/licenses/gpl-license.php>;.
*
* @package OpenEMR
* @author Rod Roark <[email protected]>
* @author Brady Miller <[email protected]>
* @link https://www.open-emr.org
*/
require_once('../../globals.php');
require_once($GLOBALS['srcdir'].'/patient.inc');
require_once($GLOBALS['srcdir'].'/forms.inc');
require_once($GLOBALS['srcdir'].'/calendar.inc');
require_once($GLOBALS['srcdir'].'/options.inc.php');
require_once($GLOBALS['srcdir'].'/encounter_events.inc.php');
require_once($GLOBALS['srcdir'].'/acl.inc');
require_once($GLOBALS['srcdir'].'/patient_tracker.inc.php');
require_once($GLOBALS['incdir']."/main/holidays/Holidays_Controller.php");
require_once($GLOBALS['srcdir'].'/group.inc');
//Check access control
if (!acl_check('patients', 'appt', '', array('write','wsome'))) {
die(xl('Access not allowed'));
}
use OpenEMR\Core\Header;
/* Things that might be passed by our opener. */
$eid = $_GET['eid']; // only for existing events
$date = $_GET['date']; // this and below only for new events
$userid = $_GET['userid'];
$default_catid = $_GET['catid'] ? $_GET['catid'] : '5';
//
if ($date) {
$date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6);
} else {
$date = date("Y-m-d");
}
//
$starttimem = '00';
if (isset($_GET['starttimem'])) {
$starttimem = substr('00' . $_GET['starttimem'], -2);
}
//
if (isset($_GET['starttimeh'])) {
$starttimeh = $_GET['starttimeh'];
if (isset($_GET['startampm'])) {
if ($_GET['startampm'] == '2' && $starttimeh < 12) {
$starttimeh += 12;
}
}
} else {
$starttimeh = date("G");
}
$startampm = '';
$info_msg = "";
?>
<?php $g_edit = acl_check("groups", "gcalendar", false, 'write');?>
<?php $g_view = acl_check("groups", "gcalendar", false, 'view');?>
<?php Header::setupHeader(['common', 'datetime-picker', 'opener']); ?>
<!-- validation library -->
<!--//Not lbf forms use the new validation, please make sure you have the corresponding values in the list Page validation-->
<?php $use_validate_js = 1;?>
<?php require_once($GLOBALS['srcdir'] . "/validation/validation_script.js.php"); ?>
<?php
//Gets validation rules from Page Validation list.
//Note that for technical reasons, we are bypassing the standard validateUsingPageRules() call.
$have_group_global_enabled = true;
if ((!$g_edit && !$g_view) || (!$GLOBALS['enable_group_therapy'])) {
$_GET['group'] = false;
$have_group_global_enabled = false;
}
if ($_GET['group'] == true) {
//groups tab
$collectthis = collectValidationPageRules("/interface/main/calendar/add_edit_event.php?group=true");
} elseif ($_GET['prov']) {
//providers tab
$collectthis = collectValidationPageRules("/interface/main/calendar/add_edit_event.php?prov=true");
} else { //patient tab
$collectthis = collectValidationPageRules("/interface/main/calendar/add_edit_event.php");
}
if (empty($collectthis)) {
$collectthis = "undefined";
} else {
$collectthis = $collectthis[array_keys($collectthis)[0]]["rules"];
}
?>
<?php $disabled = (!$g_edit && $have_group_global_enabled )?' disabled=true; ':'';?>
<?php if ($disabled) {
echo '<script>$( document ).ready(function(){
$("input").prop("disabled", true);
$("select").prop("disabled", true);
}) </script>';
}?>
<?php
function InsertEventFull()
{
global $new_multiple_value,$provider,$event_date,$duration,$recurrspec,$starttime,$endtime,$locationspec;
// =======================================
// multi providers case
// =======================================
if (is_array($_POST['form_provider'])) {
// obtain the next available unique key to group multiple providers around some event
$q = sqlStatement("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
$max = sqlFetchArray($q);
$new_multiple_value = $max['max'] + 1;
$pc_eid = null;
foreach ($_POST['form_provider'] as $provider) {
$args = $_POST;
// specify some special variables needed for the INSERT
$args['new_multiple_value'] = $new_multiple_value;
$args['form_provider'] = $provider;
$args['event_date'] = $event_date;
$args['duration'] = $duration * 60;
$args['recurrspec'] = $recurrspec;
$args['starttime'] = $starttime;
$args['endtime'] = $endtime;
$args['locationspec'] = $locationspec;
$pc_eid_temp = InsertEvent($args);
if ($pc_eid == null) {
$pc_eid = $pc_eid_temp;
}
}
return $pc_eid;
// ====================================
// single provider
// ====================================
} else {
$args = $_POST;
// specify some special variables needed for the INSERT
$args['new_multiple_value'] = "";
$args['event_date'] = $event_date;
$args['duration'] = $duration * 60;
$args['recurrspec'] = $recurrspec;
$args['starttime'] = $starttime;
$args['endtime'] = $endtime;
$args['locationspec'] = $locationspec;
$pc_eid = InsertEvent($args);
return $pc_eid;
}
}
function DOBandEncounter($pc_eid)
{
global $event_date,$info_msg;
// Save new DOB if it's there.
$patient_dob = trim($_POST['form_dob']);
$tmph = $_POST['form_hour'] + 0;
$tmpm = $_POST['form_minute'] + 0;
if ($_POST['form_ampm'] == '2' && $tmph < 12) {
$tmph += 12;
}
$appttime = "$tmph:$tmpm:00";
if ($patient_dob && $_POST['form_pid']) {
sqlStatement("UPDATE patient_data SET DOB = ? WHERE " .
"pid = ?", array($patient_dob,$_POST['form_pid']));
}
// Manage tracker status.
// And auto-create a new encounter if appropriate.
if (!empty($_POST['form_pid'])) {
if ($GLOBALS['auto_create_new_encounters'] && $event_date == date('Y-m-d') && (is_checkin($_POST['form_apptstatus']) == '1') && !is_tracker_encounter_exist($event_date, $appttime, $_POST['form_pid'], $_GET['eid'])) {
$encounter = todaysEncounterCheck($_POST['form_pid'], $event_date, $_POST['form_comments'], $_POST['facility'], $_POST['billing_facility'], $_POST['form_provider'], $_POST['form_category'], false);
if ($encounter) {
$info_msg .= xl("New encounter created with id");
$info_msg .= " $encounter";
}
# Capture the appt status and room number for patient tracker. This will map the encounter to it also.
if (isset($GLOBALS['temporary-eid-for-manage-tracker']) || !empty($_GET['eid'])) {
// Note that the temporary-eid-for-manage-tracker is used to capture the eid for new appointments and when separate a recurring
// appointment. It is set in the InsertEvent() function. Note that in the case of spearating a recurrent appointment, the get eid
// parameter is actually erroneous(is eid of the recurrent appt and not the new separated appt), so need to use the
// temporary-eid-for-manage-tracker global instead.
$temp_eid = (isset($GLOBALS['temporary-eid-for-manage-tracker'])) ? $GLOBALS['temporary-eid-for-manage-tracker'] : $_GET['eid'];
manage_tracker_status($event_date, $appttime, $temp_eid, $_POST['form_pid'], $_SESSION["authUser"], $_POST['form_apptstatus'], $_POST['form_room'], $encounter);
}
} else {
# Capture the appt status and room number for patient tracker.
if (!empty($_GET['eid'])) {
manage_tracker_status($event_date, $appttime, $_GET['eid'], $_POST['form_pid'], $_SESSION["authUser"], $_POST['form_apptstatus'], $_POST['form_room']);
}
}
}
// auto create encounter for therapy group
if (!empty($_POST['form_gid'])) {
// status Took Place is the check in of therapy group
if ($GLOBALS['auto_create_new_encounters'] && $event_date == date('Y-m-d') && $_POST['form_apptstatus'] == '=') {
$encounter = todaysTherapyGroupEncounterCheck($_POST['form_gid'], $event_date, $_POST['form_comments'], $_POST['facility'], $_POST['billing_facility'], $_POST['form_provider'], $_POST['form_category'], false, $pc_eid);
if ($encounter) {
$info_msg .= xl("New group encounter created with id");
$info_msg .= " $encounter";
}
}
}
}
/*This function is used for setting the date of the first event when using the "day_every_week" repetition mechanism.
When the 'start date' is not one of the days chosen for the repetition, the start date needs to be changed to the first
occurrence of one of these set days. */
function setEventDate($start_date, $recurrence)
{
$timestamp = strtotime($start_date);
$day = date('w', $timestamp);
//If the 'start date' is one of the set days
if (in_array(($day+1), explode(',', $recurrence))) {
return $start_date;
}
//else: (we need to change start date to first occurrence of one of the set days)
$new_date = getTheNextAppointment($start_date, $recurrence);
return $new_date;
}
//================================================================================================================
// EVENTS TO FACILITIES (lemonsoftware)
//(CHEMED) get facility name
// edit event case - if there is no association made, then insert one with the first facility
if ($eid) {
$selfacil = '';
$facility = sqlQuery("SELECT pc_facility, pc_multiple, pc_aid, facility.name
FROM openemr_postcalendar_events
LEFT JOIN facility ON (openemr_postcalendar_events.pc_facility = facility.id)
WHERE pc_eid = ?", array($eid));
// if ( !$facility['pc_facility'] ) {
if (is_array($facility) && !$facility['pc_facility']) {
$qmin = sqlQuery("SELECT facility_id as minId, facility FROM users WHERE id = ?", array($facility['pc_aid']));
$min = $qmin['minId'];
$min_name = $qmin['facility'];
// multiple providers case
if ($GLOBALS['select_multi_providers']) {
$mul = $facility['pc_multiple'];
sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = ? WHERE pc_multiple = ?", array($min,$mul));
}
// EOS multiple
sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = ? WHERE pc_eid = ?", array($min,$eid));
$e2f = $min;
$e2f_name = $min_name;
} else {
// not edit event
if (!$facility['pc_facility'] && $_SESSION['pc_facility']) {
$e2f = $_SESSION['pc_facility'];
} elseif (!$facility['pc_facility'] && $_COOKIE['pc_facility'] && $GLOBALS['set_facility_cookie']) {
$e2f = $_COOKIE['pc_facility'];
} else {
$e2f = $facility['pc_facility'];
$e2f_name = $facility['name'];
}
}
}
// EOS E2F
// ===========================
//=============================================================================================================================
if ($_POST['form_action'] == "duplicate" || $_POST['form_action'] == "save") {
// Compute start and end time strings to be saved.
if ($_POST['form_allday']) {
$tmph = 0;
$tmpm = 0;
$duration = 24 * 60;
} else {
$tmph = $_POST['form_hour'] + 0;
$tmpm = $_POST['form_minute'] + 0;
if ($_POST['form_ampm'] == '2' && $tmph < 12) {
$tmph += 12;
}
$duration = abs($_POST['form_duration']); // fixes #395
}
$starttime = "$tmph:$tmpm:00";
//
$tmpm += $duration;
while ($tmpm >= 60) {
$tmpm -= 60;
++$tmph;
}
$endtime = "$tmph:$tmpm:00";
// Set up working variables related to repeated events.
$my_recurrtype = 0;
$my_repeat_freq = 0 + $_POST['form_repeat_freq'];
$my_repeat_type = 0 + $_POST['form_repeat_type'];
$my_repeat_on_num = 1;
$my_repeat_on_day = 0;
$my_repeat_on_freq = 0;
// the starting date of the event, pay attention with this value
// when editing recurring events -- JRM Oct-08
$event_date = fixDate($_POST['form_date']);
//If used new recurrence mechanism of set days every week
if (!empty($_POST['days_every_week'])) {
$my_recurrtype = 3;
//loop through checkboxes and insert encounter days into array
$days_every_week_arr = array();
for ($i=1; $i<=7; $i++) {
if (!empty($_POST['day_' . $i])) {
array_push($days_every_week_arr, $i);
}
}
$my_repeat_freq = implode(",", $days_every_week_arr);
$my_repeat_type = 6;
$event_date = fixDate(setEventDate($_POST['form_date'], $my_repeat_freq));
} elseif (!empty($_POST['form_repeat'])) {
$my_recurrtype = 1;
if ($my_repeat_type > 4) {
$my_recurrtype = 2;
$time = strtotime($event_date);
$my_repeat_on_day = 0 + date('w', $time);
$my_repeat_on_freq = $my_repeat_freq;
if ($my_repeat_type == 5) {
$my_repeat_on_num = intval((date('j', $time) - 1) / 7) + 1;
} else {
// Last occurence of this weekday on the month
$my_repeat_on_num = 5;
}
// Maybe not needed, but for consistency with postcalendar:
$my_repeat_freq = 0;
$my_repeat_type = 0;
}
}
// Useless garbage that we must save.
$locationspecs = array("event_location" => "",
"event_street1" => "",
"event_street2" => "",
"event_city" => "",
"event_state" => "",
"event_postal" => ""
);
$locationspec = serialize($locationspecs);
// capture the recurring specifications
$recurrspec = array("event_repeat_freq" => "$my_repeat_freq",
"event_repeat_freq_type" => "$my_repeat_type",
"event_repeat_on_num" => "$my_repeat_on_num",
"event_repeat_on_day" => "$my_repeat_on_day",
"event_repeat_on_freq" => "$my_repeat_on_freq",
"exdate" => $_POST['form_repeat_exdate']
);
//
if ($my_recurrtype == 2) { // Added by epsdky 2016 (details in commit)
if ($_POST['old_repeats'] == 2) {
if ($_POST['rt2_flag2']) {
$recurrspec['rt2_pf_flag'] = "1";
}
} else {
$recurrspec['rt2_pf_flag'] = "1";
}
} // End of addition by epsdky
//
// no recurr specs, this is used for adding a new non-recurring event
$noRecurrspec = array("event_repeat_freq" => "",
"event_repeat_freq_type" => "",
"event_repeat_on_num" => "1",
"event_repeat_on_day" => "0",
"event_repeat_on_freq" => "0",
"exdate" => ""
);
}//if ($_POST['form_action'] == "duplicate" || $_POST['form_action'] == "save")
//=============================================================================================================================
if ($_POST['form_action'] == "duplicate") {
$eid = InsertEventFull();
DOBandEncounter($eid);
}
// If we are saving, then save and close the window.
//
if ($_POST['form_action'] == "save") {
/* =======================================================
* UPDATE EVENTS
* =====================================================*/
if ($eid) {
// what is multiple key around this $eid?
$row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
// ====================================
// multiple providers
// ====================================
if ($GLOBALS['select_multi_providers'] && $row['pc_multiple']) {
// obtain current list of providers regarding the multiple key
$up = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_multiple=?", array($row['pc_multiple']));
while ($current = sqlFetchArray($up)) {
$providers_current[] = $current['pc_aid'];
}
// get the new list of providers from the submitted form
$providers_new = $_POST['form_provider'];
// ===== Only current event of repeating series =====
if ($_POST['recurr_affect'] == 'current') {
// update all existing event records to exlude the current date
foreach ($providers_current as $provider) {
// update the provider's original event
// get the original event's repeat specs
$origEvent = sqlQuery("SELECT pc_recurrspec FROM openemr_postcalendar_events ".
" WHERE pc_aid = ? AND pc_multiple=?", array($provider,$row['pc_multiple']));
$oldRecurrspec = unserialize($origEvent['pc_recurrspec']);
$selected_date = date("Ymd", strtotime($_POST['selected_date']));
if ($oldRecurrspec['exdate'] != "") {
$oldRecurrspec['exdate'] .= ",".$selected_date;
} else {
$oldRecurrspec['exdate'] .= $selected_date;
}
// mod original event recur specs to exclude this date
sqlStatement("UPDATE openemr_postcalendar_events SET " .
" pc_recurrspec = ? ".
" WHERE pc_aid = ? AND pc_multiple=?", array(serialize($oldRecurrspec),$provider,$row['pc_multiple']));
}
// obtain the next available unique key to group multiple providers around some event
$q = sqlStatement("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
$max = sqlFetchArray($q);
$new_multiple_value = $max['max'] + 1;
// insert a new event record for each provider selected on the form
foreach ($providers_new as $provider) {
// insert a new event on this date with POST form data
$args = $_POST;
// specify some special variables needed for the INSERT
$args['new_multiple_value'] = $new_multiple_value;
$args['form_provider'] = $provider;
$args['event_date'] = $event_date;
$args['duration'] = $duration * 60;
// this event is forced to NOT REPEAT
$args['form_repeat'] = "0";
$args['days_every_week'] = "0";
$args['recurrspec'] = $noRecurrspec;
$args['form_enddate'] = "0000-00-00";
$args['starttime'] = $starttime;
$args['endtime'] = $endtime;
$args['locationspec'] = $locationspec;
InsertEvent($args);
}
} // ===== Future Recurring events of a repeating series =====
else if ($_POST['recurr_affect'] == 'future') {
// update all existing event records to
// stop recurring on this date-1
$selected_date = date("Y-m-d", (strtotime($_POST['selected_date'])-24*60*60));
foreach ($providers_current as $provider) {
// In case of a change in the middle of the event
if (strcmp($_POST['event_start_date'], $_POST['selected_date'])!=0) {
// mod original event recur specs to end on this date
sqlStatement("UPDATE openemr_postcalendar_events SET " .
" pc_enddate = ? " .
" WHERE pc_aid = ? AND pc_multiple=?", array($selected_date, $provider, $row['pc_multiple']));
} // In case of a change in the event head
else {
sqlStatement("DELETE FROM openemr_postcalendar_events " .
" WHERE pc_aid = ? AND pc_multiple=?", array($provider, $row['pc_multiple']));
}
}
// obtain the next available unique key to group multiple providers around some event
$q = sqlStatement("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
$max = sqlFetchArray($q);
$new_multiple_value = $max['max'] + 1;
// insert a new event record for each provider selected on the form
foreach ($providers_new as $provider) {
// insert a new event on this date with POST form data
$args = $_POST;
// specify some special variables needed for the INSERT
$args['new_multiple_value'] = $new_multiple_value;
$args['form_provider'] = $provider;
$args['event_date'] = $event_date;
$args['duration'] = $duration * 60;
$args['recurrspec'] = $recurrspec;
$args['starttime'] = $starttime;
$args['endtime'] = $endtime;
$args['locationspec'] = $locationspec;
InsertEvent($args);
}
} else {
/* =================================================================== */
// ===== a Single event or All events in a repeating series ==========
/* =================================================================== */
// this difference means that some providers from current was UNCHECKED
// so we must delete this event for them
$r1 = array_diff($providers_current, $providers_new);
$old_eid = null;
if (count($r1)) {
foreach ($r1 as $to_be_removed) {
if (!empty($_POST['form_gid'])) {
$old_eid = sqlQuery("SELECT pc_eid FROM openemr_postcalendar_events WHERE pc_aid=? AND pc_multiple=?", array($to_be_removed,$row['pc_multiple']));
}
sqlQuery("DELETE FROM openemr_postcalendar_events WHERE pc_aid=? AND pc_multiple=?", array($to_be_removed,$row['pc_multiple']));
}
}
// perform a check to see if user changed event date
// this is important when editing an existing recurring event
// oct-08 JRM
if ($_POST['form_date'] == $_POST['selected_date']) {
// user has NOT changed the start date of the event (and not recurrtype 3)
if ($my_recurrtype != 3) {
$event_date = fixDate($_POST['event_start_date']);
}
}
// this difference means that some providers were added
// so we must insert this event for them
$r2 = array_diff($providers_new, $providers_current);
if (count($r2)) {
foreach ($r2 as $to_be_inserted) {
$args = $_POST;
// specify some special variables needed for the INSERT
$args['new_multiple_value'] = $row['pc_multiple'];
$args['form_provider'] = $to_be_inserted;
$args['event_date'] = $event_date;
$args['duration'] = $duration * 60;
$args['recurrspec'] = $recurrspec;
$args['starttime'] = $starttime;
$args['endtime'] = $endtime;
$args['locationspec'] = $locationspec;
$new_eid = InsertEvent($args);
}
}
if (!is_null($old_eid) && !empty($old_eid)) {
// update group encounter is connected to event.
sqlQuery("UPDATE form_groups_encounter SET appt_id = ? WHERE appt_id = ?", array($new_eid, $old_eid['pc_eid']));
}
// after the two diffs above, we must update for remaining providers
// those who are intersected in $providers_current and $providers_new
foreach ($_POST['form_provider'] as $provider) {
sqlStatement("UPDATE openemr_postcalendar_events SET " .
"pc_catid = '" . add_escape_custom($_POST['form_category']) . "', " .
"pc_pid = '" . add_escape_custom($_POST['form_pid']) . "', " .
"pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
"pc_time = NOW(), " .
"pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
"pc_room = '" . add_escape_custom($_POST['form_room']) . "', " .
"pc_informant = '" . add_escape_custom($_SESSION['authUserID']) . "', " .
"pc_eventDate = '" . add_escape_custom($event_date) . "', " .
"pc_endDate = '" . add_escape_custom(fixDate($_POST['form_enddate'])) . "', " .
"pc_duration = '" . add_escape_custom(($duration * 60)) . "', " .
"pc_recurrtype = '" . add_escape_custom($my_recurrtype) . "', " .
"pc_recurrspec = '" . add_escape_custom(serialize($recurrspec)) . "', " .
"pc_startTime = '" . add_escape_custom($starttime) . "', " .
"pc_endTime = '" . add_escape_custom($endtime) . "', " .
"pc_alldayevent = '" . add_escape_custom($_POST['form_allday']) . "', " .
"pc_apptstatus = '" . add_escape_custom($_POST['form_apptstatus']) . "', " .
"pc_prefcatid = '" . add_escape_custom($_POST['form_prefcat']) . "' ," .
"pc_facility = '" . add_escape_custom((int)$_POST['facility']) ."' ," . // FF stuff
"pc_billing_location = '" . add_escape_custom((int)$_POST['billing_facility']) ."' " .
"WHERE pc_aid = '" . add_escape_custom($provider) . "' AND pc_multiple = '" . add_escape_custom($row['pc_multiple']) . "'");
} // foreach
}
// ====================================
// single provider
// ====================================
} elseif (!$row['pc_multiple']) {
if ($GLOBALS['select_multi_providers']) {
$prov = $_POST['form_provider'][0];
} else {
$prov = $_POST['form_provider'];
}
if ($_POST['recurr_affect'] == 'current') {
// get the original event's repeat specs
$origEvent = sqlQuery("SELECT pc_recurrspec FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
$oldRecurrspec = unserialize($origEvent['pc_recurrspec']);
$selected_date = date("Ymd", strtotime($_POST['selected_date']));
if ($oldRecurrspec['exdate'] != "") {
$oldRecurrspec['exdate'] .= ",".$selected_date;
} else {
$oldRecurrspec['exdate'] .= $selected_date;
}
// mod original event recur specs to exclude this date
sqlStatement("UPDATE openemr_postcalendar_events SET " .
" pc_recurrspec = ? ".
" WHERE pc_eid = ?", array(serialize($oldRecurrspec),$eid));
// insert a new event on this date with POST form data
$args = $_POST;
// specify some special variables needed for the INSERT
$args['event_date'] = $event_date;
$args['duration'] = $duration * 60;
// this event is forced to NOT REPEAT
$args['form_repeat'] = "0";
$args['days_every_week'] = "0";
$args['recurrspec'] = $noRecurrspec;
$args['form_enddate'] = "0000-00-00";
$args['starttime'] = $starttime;
$args['endtime'] = $endtime;
$args['locationspec'] = $locationspec;
InsertEvent($args);
} else if ($_POST['recurr_affect'] == 'future') {
// mod original event to stop recurring on this date-1
$selected_date = date("Ymd", (strtotime($_POST['selected_date'])-24*60*60));
sqlStatement("UPDATE openemr_postcalendar_events SET " .
" pc_enddate = ? ".
" WHERE pc_eid = ?", array($selected_date,$eid));
// insert a new event starting on this date with POST form data
$args = $_POST;
// specify some special variables needed for the INSERT
$args['event_date'] = $event_date;
$args['duration'] = $duration * 60;
$args['recurrspec'] = $recurrspec;
$args['starttime'] = $starttime;
$args['endtime'] = $endtime;
$args['locationspec'] = $locationspec;
InsertEvent($args);
} else {
// perform a check to see if user changed event date
// this is important when editing an existing recurring event
// oct-08 JRM
if ($_POST['form_date'] == $_POST['selected_date']) {
// user has NOT changed the start date of the event (and not recurrtype 3)
if ($my_recurrtype != 3) {
$event_date = fixDate($_POST['event_start_date']);
}
}
// mod the SINGLE event or ALL EVENTS in a repeating series
// simple provider case
sqlStatement("UPDATE openemr_postcalendar_events SET " .
"pc_catid = '" . add_escape_custom($_POST['form_category']) . "', " .
"pc_aid = '" . add_escape_custom($prov) . "', " .
"pc_pid = '" . add_escape_custom($_POST['form_pid']) . "', " .
"pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
"pc_time = NOW(), " .
"pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
"pc_room = '" . add_escape_custom($_POST['form_room']) . "', " .
"pc_informant = '" . add_escape_custom($_SESSION['authUserID']) . "', " .
"pc_eventDate = '" . add_escape_custom($event_date) . "', " .
"pc_endDate = '" . add_escape_custom(fixDate($_POST['form_enddate'])) . "', " .
"pc_duration = '" . add_escape_custom(($duration * 60)) . "', " .
"pc_recurrtype = '" . add_escape_custom($my_recurrtype) . "', " .
"pc_recurrspec = '" . add_escape_custom(serialize($recurrspec)) . "', " .
"pc_startTime = '" . add_escape_custom($starttime) . "', " .
"pc_endTime = '" . add_escape_custom($endtime) . "', " .
"pc_alldayevent = '" . add_escape_custom($_POST['form_allday']) . "', " .
"pc_apptstatus = '" . add_escape_custom($_POST['form_apptstatus']) . "', " .
"pc_prefcatid = '" . add_escape_custom($_POST['form_prefcat']) . "' ," .
"pc_facility = '" . add_escape_custom((int)$_POST['facility']) ."' ," . // FF stuff
"pc_billing_location = '" . add_escape_custom((int)$_POST['billing_facility']) ."' " .
"WHERE pc_eid = '" . add_escape_custom($eid) . "'");
}
}
// =======================================
// end Update Multi providers case
// =======================================
// EVENTS TO FACILITIES
$e2f = (int)$eid;
} else {
/* =======================================================
* INSERT NEW EVENT(S)
* ======================================================*/
$eid = InsertEventFull();
}
// done with EVENT insert/update statements
DOBandEncounter(isset($eid) ? $eid : null);
} // =======================================
// DELETE EVENT(s)
// =======================================
else if ($_POST['form_action'] == "delete") {
// =======================================
// multi providers event
// =======================================
if ($GLOBALS['select_multi_providers']) {
// what is multiple key around this $eid?
$row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
// obtain current list of providers regarding the multiple key
$providers_current = array();
$up = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_multiple=?", array($row['pc_multiple']));
while ($current = sqlFetchArray($up)) {
$providers_current[] = $current['pc_aid'];
}
// establish a WHERE clause
if ($row['pc_multiple']) {
$whereClause = "pc_multiple = '{$row['pc_multiple']}'";
} else {
$whereClause = "pc_eid = '$eid'";
}
if ($_POST['recurr_affect'] == 'current') {
// update all existing event records to exlude the current date
foreach ($providers_current as $provider) {
// update the provider's original event
// get the original event's repeat specs
$origEvent = sqlQuery("SELECT pc_recurrspec FROM openemr_postcalendar_events ".
" WHERE pc_aid <=> ? AND pc_multiple=?", array($provider,$row['pc_multiple']));
$oldRecurrspec = unserialize($origEvent['pc_recurrspec']);
$selected_date = date("Y-m-d", strtotime($_POST['selected_date']));
if ($oldRecurrspec['exdate'] != "") {
$oldRecurrspec['exdate'] .= ",".$selected_date;
} else {
$oldRecurrspec['exdate'] .= $selected_date;
}
// mod original event recur specs to exclude this date
sqlStatement("UPDATE openemr_postcalendar_events SET " .
" pc_recurrspec = ? ".
" WHERE ". $whereClause, array(serialize($oldRecurrspec)));
}
} else if ($_POST['recurr_affect'] == 'future') {
// update all existing event records to stop recurring on this date-1
$selected_date = date("Y-m-d", (strtotime($_POST['selected_date'])-24*60*60));
foreach ($providers_current as $provider) {
// In case of a change in the middle of the event
if (strcmp($_POST['event_start_date'], $_POST['selected_date'])!=0) {
// update the provider's original event
sqlStatement("UPDATE openemr_postcalendar_events SET " .
" pc_enddate = ? " .
" WHERE " . $whereClause, array($selected_date));
} // In case of a change in the event head
else {
sqlStatement("DELETE FROM openemr_postcalendar_events WHERE ".$whereClause);
}
}
} else {
// really delete the event from the database
sqlStatement("DELETE FROM openemr_postcalendar_events WHERE ".$whereClause);
}
} // =======================================
// single provider event
// =======================================
else {
if ($_POST['recurr_affect'] == 'current') {
// mod original event recur specs to exclude this date
// get the original event's repeat specs
$origEvent = sqlQuery("SELECT pc_recurrspec FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
$oldRecurrspec = unserialize($origEvent['pc_recurrspec']);
$selected_date = date("Ymd", strtotime($_POST['selected_date']));
if ($oldRecurrspec['exdate'] != "") {
$oldRecurrspec['exdate'] .= ",".$selected_date;
} else {
$oldRecurrspec['exdate'] .= $selected_date;
}
sqlStatement("UPDATE openemr_postcalendar_events SET " .
" pc_recurrspec = ? ".
" WHERE pc_eid = ?", array(serialize($oldRecurrspec),$eid));
} else if ($_POST['recurr_affect'] == 'future') {
// mod original event to stop recurring on this date-1
$selected_date = date("Ymd", (strtotime($_POST['selected_date'])-24*60*60));
sqlStatement("UPDATE openemr_postcalendar_events SET " .
" pc_enddate = ? ".
" WHERE pc_eid = ?", array($selected_date,$eid));
} else {
// fully delete the event from the database
sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
}
}
}
if ($_POST['form_action'] != "") {
// Close this window and refresh the calendar (or the patient_tracker) display.
echo "<html>\n<body>\n<script language='JavaScript'>\n";
if ($info_msg) {
echo " alert('" . addslashes($info_msg) . "');\n";
}
echo " if (opener && !opener.closed && opener.refreshme) {\n " .
" opener.refreshme();\n " . // This is for standard calendar page refresh
" } else {\n " .
" if(window.opener.pattrk){" .
" window.opener.pattrk.submit()\n " . // This is for patient flow board page refresh}
" }};\n";
echo " dlgclose();\n";
echo "</script>\n</body>\n</html>\n";
exit();
}
//*********************************
// If we get this far then we are displaying the form.
//*********************************
/*********************************************************************
This has been migrate to the administration->lists
$statuses = array(
'-' => '',
'*' => xl('* Reminder done'),
'+' => xl('+ Chart pulled'),
'x' => xl('x Cancelled'), // added Apr 2008 by JRM
'?' => xl('? No show'),
'@' => xl('@ Arrived'),
'~' => xl('~ Arrived late'),
'!' => xl('! Left w/o visit'),
'#' => xl('# Ins/fin issue'),
'<' => xl('< In exam room'),
'>' => xl('> Checked out'),
'$' => xl('$ Coding done'),
'%' => xl('% Cancelled < 24h ')
);
*********************************************************************/
$repeats = 0; // if the event repeats
$repeattype = '0';
$repeatfreq = '0';
$patientid = '';
if ($_REQUEST['patientid']) {
$patientid = $_REQUEST['patientid'];
}
$patientname = null;
$patienttitle = array();
$pcroom = "";
$hometext = "";
$row = array();
$informant = "";
$groupid = '';
if ($_REQUEST['groupid']) {
$groupid = $_REQUEST['groupid'];
}
$groupname = null;
$group_statuses = getGroupStatuses();
// If we are editing an existing event, then get its data.
if ($eid) {
// $row = sqlQuery("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = $eid");
$row = sqlQuery("SELECT e.*, u.fname, u.mname, u.lname " .
"FROM openemr_postcalendar_events AS e " .
"LEFT OUTER JOIN users AS u ON u.id = e.pc_informant " .
"WHERE pc_eid = ?", array($eid));
$informant = $row['fname'] . ' ' . $row['mname'] . ' ' . $row['lname'];
// instead of using the event's starting date, keep what has been provided
// via the GET array, see the top of this file
if (empty($_GET['date'])) {
$date = $row['pc_eventDate'];
}
$eventstartdate = $row['pc_eventDate']; // for repeating event stuff - JRM Oct-08
$userid = $row['pc_aid'];
$patientid = $row['pc_pid'];
$groupid = $row['pc_gid'];
$starttimeh = substr($row['pc_startTime'], 0, 2) + 0;
$starttimem = substr($row['pc_startTime'], 3, 2);
$repeats = $row['pc_recurrtype'];
$multiple_value = $row['pc_multiple'];
// parse out the repeating data, if any
$rspecs = unserialize($row['pc_recurrspec']); // extract recurring data
$repeattype = $rspecs['event_repeat_freq_type'];
$repeatfreq = $rspecs['event_repeat_freq'];
$repeatexdate = $rspecs['exdate']; // repeating date exceptions
// Adjustments for repeat type 2, a particular weekday of the month.
if ($repeats == 2) {
$repeatfreq = $rspecs['event_repeat_on_freq'];
if ($rspecs['event_repeat_on_num'] < 5) {
$repeattype = 5;
} else {
$repeattype = 6;
}
}
$recurrence_end_date = ($row['pc_endDate'] && $row['pc_endDate'] != '0000-00-00') ? $row['pc_endDate'] : null;
$pcroom = $row['pc_room'];
$hometext = $row['pc_hometext'];
if (substr($hometext, 0, 6) == ':text:') {
$hometext = substr($hometext, 6);
}
} else {
// a NEW event
$eventstartdate = $date; // for repeating event stuff - JRM Oct-08
//-------------------------------------
//(CHEMED)
//Set default facility for a new event based on the given 'userid'
if ($userid) {
/*************************************************************
$pref_facility = sqlFetchArray(sqlStatement("SELECT facility_id, facility FROM users WHERE id = $userid"));
*************************************************************/
if ($_SESSION['pc_facility']) {
$pref_facility = sqlFetchArray(sqlStatement(
"
SELECT f.id as facility_id,
f.name as facility
FROM facility f
WHERE f.id = ?
",
array($_SESSION['pc_facility'])
));
} else {
$pref_facility = sqlFetchArray(sqlStatement("
SELECT u.facility_id,
f.name as facility
FROM users u
LEFT JOIN facility f on (u.facility_id = f.id)
WHERE u.id = ?
", array($userid)));
}
/************************************************************/
$e2f = $pref_facility['facility_id'];
$e2f_name = $pref_facility['facility'];
}
//END of CHEMED -----------------------
}
// If we have a patient ID, get the name and phone numbers to display.
if ($patientid) {
$prow = sqlQuery("SELECT lname, fname, phone_home, phone_biz, DOB " .
"FROM patient_data WHERE pid = ?", array($patientid));
$patientname = $prow['lname'] . ", " . $prow['fname'];
if ($prow['phone_home']) {
$patienttitle['phone_home'] = xl("Home Phone").": " . $prow['phone_home'];
}
if ($prow['phone_biz']) {
$patienttitle['phone_biz'] = xl("Work Phone").": " . $prow['phone_biz'];
}
}
// If we have a group id, get group data
if ($groupid) {
$group_data = getGroup($groupid);
$groupname = $group_data['group_name'];
$group_end_date = $group_data['group_end_date'];
if (!$recurrence_end_date && $group_end_date && $group_end_date != '0000-00-00') {
$recurrence_end_date = $group_end_date;// If there is no recurr end date get group's end date as default (only if group has an end date)