Merge pull request #940 for adding access controls for encounter categories
[openemr.git] / portal / add_edit_event_user.php
bloba0ea880a175be839863d55d577768cabd067d13f
1 <?php
2 /**
4 * Modified from interface/main/calendar/add_edit_event.php for
5 * the patient portal.
7 * @package OpenEMR
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Jerry Padgett <sjpadgett@gmail.com>
10 * @copyright Copyright (C) 2005-2006 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
12 * @link http://www.open-emr.org
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 // continue session
17 session_start();
19 //landing page definition -- where to go if something goes wrong
20 $landingpage = "index.php?site=".$_SESSION['site_id'];
23 // kick out if patient not authenticated
24 if ( isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite_two']) ) {
25 $pid = $_SESSION['pid'];
27 else {
28 session_destroy();
29 header('Location: '.$landingpage.'&w');
30 exit;
34 $ignoreAuth = 1;
35 global $ignoreAuth;
37 include_once("../interface/globals.php");
38 include_once("$srcdir/patient.inc");
39 include_once("$srcdir/forms.inc");
41 // Exit if the modify calendar for portal flag is not set-pulled for v5
42 /* if (!($GLOBALS['portal_onsite_appt_modify'])) {
43 echo add_escape_custom( xl('You are not authorized to schedule appointments.'),ENT_NOQUOTES);
44 exit;
45 } */
47 // Things that might be passed by our opener.
49 $eid = $_GET['eid']; // only for existing events
50 $date = $_GET['date']; // this and below only for new events
51 $userid = $_GET['userid'];
52 $default_catid = $_GET['catid'] ? $_GET['catid'] : '5';
53 $patientid = $_GET['patid'];
56 if ($date)
57 $date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6);
58 else
59 $date = date("Y-m-d");
61 $starttimem = '00';
62 if (isset($_GET['starttimem']))
63 $starttimem = substr('00' . $_GET['starttimem'], -2);
65 if (isset($_GET['starttimeh'])) {
66 $starttimeh = $_GET['starttimeh'];
67 if (isset($_GET['startampm'])) {
68 if ($_GET['startampm'] == '2' && $starttimeh < 12)
69 $starttimeh += 12;
71 } else {
72 $starttimeh = date("G");
74 $startampm = '';
76 $info_msg = "";
78 // ===========================
79 // EVENTS TO FACILITIES (lemonsoftware)
80 // edit event case - if there is no association made, then insert one with the first facility
81 /*if ( $eid ) {
82 $selfacil = '';
83 $facility = sqlQuery("SELECT pc_facility, pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
84 if ( !$facility['pc_facility'] ) {
85 $qmin = sqlQuery("SELECT MIN(id) as minId FROM facility");
86 $min = $qmin['minId'];
88 // multiple providers case
89 if ( $GLOBALS['select_multi_providers'] ) {
90 $mul = $facility['pc_multiple'];
91 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_multiple = $mul");
93 // EOS multiple
95 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_eid = $eid");
96 $e2f = $minId;
97 } else {
98 $e2f = $facility['pc_facility'];
101 // EOS E2F
102 // ===========================
103 // ===========================
105 // EVENTS TO FACILITIES (lemonsoftware)
106 //(CHEMED) get facility name
107 // edit event case - if there is no association made, then insert one with the first facility
108 if ( $eid ) {
109 $selfacil = '';
110 $facility = sqlQuery("SELECT pc_facility, pc_multiple, pc_aid, facility.name
111 FROM openemr_postcalendar_events
112 LEFT JOIN facility ON (openemr_postcalendar_events.pc_facility = facility.id)
113 WHERE pc_eid = $eid");
114 if ( !$facility['pc_facility'] ) {
115 $qmin = sqlQuery("SELECT facility_id as minId, facility FROM users WHERE id = ".$facility['pc_aid']);
116 $min = $qmin['minId'];
117 $min_name = $qmin['facility'];
119 // multiple providers case
120 if ( $GLOBALS['select_multi_providers'] ) {
121 $mul = $facility['pc_multiple'];
122 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_multiple = $mul");
124 // EOS multiple
126 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_eid = $eid");
127 $e2f = $min;
128 $e2f_name = $min_name;
129 } else {
130 $e2f = $facility['pc_facility'];
131 $e2f_name = $facility['name'];
134 // EOS E2F
135 // ===========================
138 // If we are saving, then save and close the window.
140 if ($_POST['form_action'] == "save") {
141 //print_r($_POST);
142 //exit();
143 $event_date = fixDate($_POST['form_date']);
145 // Compute start and end time strings to be saved.
146 if ($_POST['form_allday']) {
147 $tmph = 0;
148 $tmpm = 0;
149 $duration = 24 * 60;
150 } else {
151 $tmph = $_POST['form_hour'] + 0;
152 $tmpm = $_POST['form_minute'] + 0;
153 if ($_POST['form_ampm'] == '2' && $tmph < 12) $tmph += 12;
154 $duration = $_POST['form_duration'];
156 $starttime = "$tmph:$tmpm:00";
158 $tmpm += $duration;
159 while ($tmpm >= 60) {
160 $tmpm -= 60;
161 ++$tmph;
163 $endtime = "$tmph:$tmpm:00";
165 // Useless garbage that we must save.
166 $locationspec = 'a:6:{s:14:"event_location";N;s:13:"event_street1";N;' .
167 's:13:"event_street2";N;s:10:"event_city";N;s:11:"event_state";N;s:12:"event_postal";N;}';
169 // More garbage, but this time 1 character of it is used to save the
170 // repeat type.
171 if ($_POST['form_repeat']) {
172 $recurrspec = 'a:5:{' .
173 's:17:"event_repeat_freq";s:1:"' . $_POST['form_repeat_freq'] . '";' .
174 's:22:"event_repeat_freq_type";s:1:"' . $_POST['form_repeat_type'] . '";' .
175 's:19:"event_repeat_on_num";s:1:"1";' .
176 's:19:"event_repeat_on_day";s:1:"0";' .
177 's:20:"event_repeat_on_freq";s:1:"0";}';
178 } else {
179 $recurrspec = 'a:5:{' .
180 's:17:"event_repeat_freq";N;' .
181 's:22:"event_repeat_freq_type";s:1:"0";' .
182 's:19:"event_repeat_on_num";s:1:"1";' .
183 's:19:"event_repeat_on_day";s:1:"0";' .
184 's:20:"event_repeat_on_freq";s:1:"1";}';
187 //The modification of the start date for events that take place on one day of the week
188 //for example monday, or thursday. We set the start date on the first day of the week
189 //that the event is scheduled. For example if you set the event to repeat on each monday
190 //the start date of the event will be set on the first monday after the day the event is scheduled
191 if($_POST['form_repeat_type'] == 5)
193 $exploded_date= explode("-",$event_date);
194 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
195 if($edate=="Tue") {
196 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
198 elseif($edate=="Wed") {
199 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
201 elseif($edate=="Thu") {
202 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
204 elseif($edate=="Fri") {
205 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
207 elseif($edate=="Sat") {
208 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
210 elseif($edate=="Sun") {
211 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
213 } elseif($_POST['form_repeat_type'] == 6) {
214 $exploded_date= explode("-",$event_date);
215 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
216 if($edate=="Wed") {
217 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
219 elseif($edate=="Thu") {
220 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
222 elseif($edate=="Fri") {
223 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
225 elseif($edate=="Sat") {
226 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
228 elseif($edate=="Sun") {
229 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
231 elseif($edate=="Mon") {
232 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
234 } elseif($_POST['form_repeat_type'] == 7) {
235 $exploded_date= explode("-",$event_date);
236 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
237 if($edate=="Thu") {
238 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
240 elseif($edate=="Fri") {
241 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
243 elseif($edate=="Sat") {
244 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
246 elseif($edate=="Sun") {
247 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
249 elseif($edate=="Mon") {
250 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
252 elseif($edate=="Tue") {
253 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
255 } elseif($_POST['form_repeat_type'] == 8) {
256 $exploded_date= explode("-",$event_date);
257 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
258 if($edate=="Fri") {
259 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
261 elseif($edate=="Sat") {
262 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
264 elseif($edate=="Sun") {
265 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
267 elseif($edate=="Mon") {
268 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
270 elseif($edate=="Tue") {
271 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
273 elseif($edate=="Wed") {
274 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
276 } elseif($_POST['form_repeat_type'] == 9) {
277 $exploded_date= explode("-",$event_date);
278 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
279 if($edate=="Sat") {
280 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
282 elseif($edate=="Sun") {
283 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
285 elseif($edate=="Mon") {
286 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
288 elseif($edate=="Tue") {
289 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
291 elseif($edate=="Wed") {
292 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
294 elseif($edate=="Thu") {
295 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
297 }//if end
298 /* =======================================================
299 // UPDATE EVENTS
300 ========================================================*/
301 if ($eid) {
303 // what is multiple key around this $eid?
304 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
306 if ($GLOBALS['select_multi_providers'] && $row['pc_multiple']) {
307 /* ==========================================
308 // multi providers BOS
309 ==========================================*/
311 // obtain current list of providers regarding the multiple key
312 $up = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_multiple={$row['pc_multiple']}");
313 while ($current = sqlFetchArray($up)) {
314 $providers_current[] = $current['pc_aid'];
317 $providers_new = $_POST['form_provider_ae'];
319 // this difference means that some providers from current was UNCHECKED
320 // so we must delete this event for them
321 $r1 = array_diff ($providers_current, $providers_new);
322 if (count ($r1)) {
323 foreach ($r1 as $to_be_removed) {
324 sqlQuery("DELETE FROM openemr_postcalendar_events WHERE pc_aid='$to_be_removed' AND pc_multiple={$row['pc_multiple']}");
328 // this difference means that some providers was added
329 // so we must insert this event for them
330 $r2 = array_diff ($providers_new, $providers_current);
331 if (count ($r2)) {
332 foreach ($r2 as $to_be_inserted) {
333 sqlInsert("INSERT INTO openemr_postcalendar_events ( pc_catid, pc_multiple, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility)
334 VALUES ( " .
335 "'" . $_POST['form_category'] . "', " .
336 "'" . $row['pc_multiple'] . "', " .
337 "'" . $to_be_inserted . "', " .
338 "'" . $_POST['form_pid'] . "', " .
339 "'" . add_escape_custom($_POST['form_title']) . "', " .
340 "NOW(), " .
341 "'" . add_escape_custom($_POST['form_comments']) . "', " .
342 "'" . $_SESSION['providerId'] . "', " .
343 "'" . $event_date . "', " .
344 "'" . fixDate($_POST['form_enddate']) . "', " .
345 "'" . ($duration * 60) . "', " .
346 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
347 "'$recurrspec', " .
348 "'$starttime', " .
349 "'$endtime', " .
350 "'" . $_POST['form_allday'] . "', " .
351 "'" . $_POST['form_apptstatus'] . "', " .
352 "'" . $_POST['form_prefcat'] . "', " .
353 "'$locationspec', " .
354 "1, " .
355 "1, " .(int)$_POST['facility']. " )"); // FF stuff
356 } // foreach
357 } //if count
360 // after the two diffs above, we must update for remaining providers
361 // those who are intersected in $providers_current and $providers_new
362 foreach ($_POST['form_provider_ae'] as $provider) {
363 sqlStatement("UPDATE openemr_postcalendar_events SET " .
364 "pc_catid = '" . $_POST['form_category'] . "', " .
365 "pc_pid = '" . $_POST['form_pid'] . "', " .
366 "pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
367 "pc_time = NOW(), " .
368 "pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
369 "pc_informant = '" . $_SESSION['providerId'] . "', " .
370 "pc_eventDate = '" . $event_date . "', " .
371 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
372 "pc_duration = '" . ($duration * 60) . "', " .
373 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
374 "pc_recurrspec = '$recurrspec', " .
375 "pc_startTime = '$starttime', " .
376 "pc_endTime = '$endtime', " .
377 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
378 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
379 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
380 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
381 "WHERE pc_aid = '$provider' AND pc_multiple={$row['pc_multiple']}");
382 } // foreach
384 /* ==========================================
385 // multi providers EOS
386 ==========================================*/
388 } elseif ( !$row['pc_multiple'] ) {
389 if ( $GLOBALS['select_multi_providers'] ) {
390 $prov = $_POST['form_provider_ae'][0];
391 } else {
392 $prov = $_POST['form_provider_ae'];
395 // simple provider case
396 sqlStatement("UPDATE openemr_postcalendar_events SET " .
397 "pc_catid = '" . $_POST['form_category'] . "', " .
398 "pc_aid = '" . $prov . "', " .
399 "pc_pid = '" . $_POST['form_pid'] . "', " .
400 "pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
401 "pc_time = NOW(), " .
402 "pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
403 "pc_informant = '" . $_SESSION['providerId'] . "', " .
404 "pc_eventDate = '" . $event_date . "', " .
405 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
406 "pc_duration = '" . ($duration * 60) . "', " .
407 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
408 "pc_recurrspec = '$recurrspec', " .
409 "pc_startTime = '$starttime', " .
410 "pc_endTime = '$endtime', " .
411 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
412 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
413 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
414 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
415 "WHERE pc_eid = '$eid'");
419 // =======================================
420 // EOS multi providers case
421 // =======================================
423 // EVENTS TO FACILITIES
425 $e2f = (int)$eid;
427 /* =======================================================
428 // INSERT EVENTS
429 ========================================================*/
430 } else {
432 // =======================================
433 // multi providers case
434 // =======================================
436 if (is_array($_POST['form_provider_ae'])) {
438 // obtain the next available unique key to group multiple providers around some event
439 $q = sqlStatement ("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
440 $max = sqlFetchArray($q);
441 $new_multiple_value = $max['max'] + 1;
443 foreach ($_POST['form_provider_ae'] as $provider) {
444 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
445 "pc_catid, pc_multiple, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
446 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
447 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
448 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
449 ") VALUES ( " .
450 "'" . $_POST['form_category'] . "', " .
451 "'" . $new_multiple_value . "', " .
452 "'" . $provider . "', " .
453 "'" . $_POST['form_pid'] . "', " .
454 "'" . add_escape_custom($_POST['form_title']) . "', " .
455 "NOW(), " .
456 "'" . add_escape_custom($_POST['form_comments']) . "', " .
457 "'" . $_SESSION['providerId'] . "', " .
458 "'" . $event_date . "', " .
459 "'" . fixDate($_POST['form_enddate']) . "', " .
460 "'" . ($duration * 60) . "', " .
461 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
462 "'$recurrspec', " .
463 "'$starttime', " .
464 "'$endtime', " .
465 "'" . $_POST['form_allday'] . "', " .
466 "'" . $_POST['form_apptstatus'] . "', " .
467 "'" . $_POST['form_prefcat'] . "', " .
468 "'$locationspec', " .
469 "1, " .
470 "1, " .(int)$_POST['facility']. " )"); // FF stuff
472 } // foreach
474 } else {
475 $_POST['form_apptstatus'] = '^';
476 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
477 "pc_catid, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
478 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
479 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
480 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
481 ") VALUES ( " .
482 "'" . $_POST['form_category'] . "', " .
483 "'" . $_POST['form_provider_ae'] . "', " .
484 "'" . $_POST['form_pid'] . "', " .
485 "'" . add_escape_custom($_POST['form_title']) . "', " .
486 "NOW(), " .
487 "'" . add_escape_custom($_POST['form_comments']) . "', " .
488 "'" . $_SESSION['providerId'] . "', " .
489 "'" . $event_date . "', " .
490 "'" . fixDate($_POST['form_enddate']) . "', " .
491 "'" . ($duration * 60) . "', " .
492 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
493 "'$recurrspec', " .
494 "'$starttime', " .
495 "'$endtime', " .
496 "'" . $_POST['form_allday'] . "', " .
497 "'" . $_POST['form_apptstatus'] . "', " .
498 "'" . $_POST['form_prefcat'] . "', " .
499 "'$locationspec', " .
500 "1, " .
501 "1," .(int)$_POST['facility']. ")"); // FF stuff
502 } // INSERT single
503 } // else - insert
505 // Save new DOB if it's there.
506 $patient_dob = trim($_POST['form_dob']);
507 if ($patient_dob && $_POST['form_pid']) {
508 sqlStatement("UPDATE patient_data SET DOB = '$patient_dob' WHERE " .
509 "pid = '" . $_POST['form_pid'] . "'");
512 // Auto-create a new encounter if appropriate.
515 /* if ($GLOBALS['auto_create_new_encounters'] &&
516 $_POST['form_apptstatus'] == '@' && $event_date == date('Y-m-d'))
519 // We decided not to auto-create blank enconter when user arrives. Todd's decision 18 Jun 2010
520 // Applied by Cassian Lup (cassian.lup@clinicdr.com)
522 if (0) {
523 $tmprow = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
524 "pid = '" . $_POST['form_pid'] . "' AND date = '$event_date 00:00:00'");
525 if ($tmprow['count'] == 0) {
526 $tmprow = sqlQuery("SELECT username, facility, facility_id FROM users WHERE id = '" .
527 $_POST['form_provider_ae'] . "'");
528 $username = $tmprow['username'];
529 $facility = $tmprow['facility'];
530 $facility_id = $tmprow['facility_id'];
531 $conn = $GLOBALS['adodb']['db'];
532 $encounter = $conn->GenID("sequences");
533 addForm($encounter, "New Patient Encounter",
534 sqlInsert("INSERT INTO form_encounter SET " .
535 "date = '$event_date', " .
536 "onset_date = '$event_date', " .
537 "reason = '" . add_escape_custom($_POST['form_comments']) . "', " .
538 "facility = '$facility', " .
539 "facility_id = '$facility_id', " .
540 "pid = '" . $_POST['form_pid'] . "', " .
541 "encounter = '$encounter'"
543 "newpatient", $_POST['form_pid'], "1", "NOW()", $username
545 $info_msg .= "New encounter $encounter was created. ";
550 else if ($_POST['form_action'] == "delete") {
551 // =======================================
552 // multi providers case
553 // =======================================
554 if ($GLOBALS['select_multi_providers']) {
555 // what is multiple key around this $eid?
556 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
557 if ( $row['pc_multiple'] ) {
558 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_multiple = {$row['pc_multiple']}");
559 } else {
560 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = $eid");
562 // =======================================
563 // EOS multi providers case
564 // =======================================
565 } else {
566 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = '$eid'");
570 if ($_POST['form_action'] != "") {
571 // Leave
572 $_SESSION['whereto'] = 'appointmentpanel';
573 header('Location:./home.php');
574 exit();
576 // If we get this far then we are displaying the form.
578 $statuses = array(
579 '-' => '',
580 '*' => xl('* Reminder done'),
581 '+' => xl('+ Chart pulled'),
582 'x' => xl('x Cancelled'), // added Apr 2008 by JRM
583 '?' => xl('? No show'),
584 '@' => xl('@ Arrived'),
585 '~' => xl('~ Arrived late'),
586 '!' => xl('! Left w/o visit'),
587 '#' => xl('# Ins/fin issue'),
588 '<' => xl('< In exam room'),
589 '>' => xl('> Checked out'),
590 '$' => xl('$ Coding done'),
591 '^' => xl('^ Pending'),
594 $repeats = 0; // if the event repeats
595 $repeattype = '0';
596 $repeatfreq = '0';
597 $patienttitle = "";
598 $hometext = "";
599 $row = array();
601 // If we are editing an existing event, then get its data.
602 if ($eid) {
603 $row = sqlQuery("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = $eid");
604 $date = $row['pc_eventDate'];
605 $userid = $row['pc_aid'];
606 $patientid = $row['pc_pid'];
607 $starttimeh = substr($row['pc_startTime'], 0, 2) + 0;
608 $starttimem = substr($row['pc_startTime'], 3, 2);
609 $repeats = $row['pc_recurrtype'];
610 $multiple_value = $row['pc_multiple'];
612 if (preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
613 $repeattype = $matches[1];
615 if (preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
616 $repeatfreq = $matches[1];
618 $hometext = $row['pc_hometext'];
619 if (substr($hometext, 0, 6) == ':text:') $hometext = substr($hometext, 6);
621 else {
622 $patientid=$_GET['pid'];
625 // If we have a patient ID, get the name and phone numbers to display.
626 if ($patientid) {
627 $prow = sqlQuery("SELECT lname, fname, phone_home, phone_biz, DOB " .
628 "FROM patient_data WHERE pid = '" . $patientid . "'");
629 $patientname = $prow['lname'] . ", " . $prow['fname'];
630 if ($prow['phone_home']) $patienttitle .= " H=" . $prow['phone_home'];
631 if ($prow['phone_biz']) $patienttitle .= " W=" . $prow['phone_biz'];
634 // Get the providers list.
635 $ures = sqlStatement("SELECT id, username, fname, lname FROM users WHERE " .
636 "authorized != 0 AND active = 1 ORDER BY lname, fname");
638 //-------------------------------------
639 //(CHEMED)
640 //Set default facility for a new event based on the given 'userid'
641 if ($userid) {
642 $pref_facility = sqlFetchArray(sqlStatement("SELECT facility_id, facility FROM users WHERE id = $userid"));
643 $e2f = $pref_facility['facility_id'];
644 $e2f_name = $pref_facility['facility'];
646 //END of CHEMED -----------------------
648 // Get event categories.
649 $cres = sqlStatement("SELECT pc_catid, pc_catname, pc_recurrtype, pc_duration, pc_end_all_day " .
650 "FROM openemr_postcalendar_categories ORDER BY pc_catname");
652 // Fix up the time format for AM/PM.
653 $startampm = '1';
654 if ($starttimeh >= 12) { // p.m. starts at noon and not 12:01
655 $startampm = '2';
656 if ($starttimeh > 12) $starttimeh -= 12;
660 <html>
661 <head>
662 <?php //html_header_show(); ?>
663 <title><?php echo $eid ? "Edit" : "Add New" ?> <?php xl('Event','e');?></title>
664 <link href="assets/css/style.css?v=<?php echo $v_js_includes; ?>" rel="stylesheet" type="text/css" />
665 <style type="text/css">@import url(../library/dynarch_calendar.css);</style>
666 <script type="text/javascript" src="../library/topdialog.js?v=<?php echo $v_js_includes; ?>"></script>
667 <script type="text/javascript" src="../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
668 <script type="text/javascript" src="../library/textformat.js?v=<?php echo $v_js_includes; ?>"></script>
669 <script type="text/javascript" src="../library/dynarch_calendar.js"></script>
670 <script type="text/javascript" src="../library/dynarch_calendar_en.js"></script>
671 <script type="text/javascript" src="../library/dynarch_calendar_setup.js"></script>
673 </head>
675 <body class="body_top" >
677 <form method='post' name='theaddform' id='theaddform' action='add_edit_event_user.php?eid=<?php echo $eid ?>'>
678 <input type="hidden" name="form_action" id="form_action" value="">
679 <input type='hidden' name='form_category' id='form_category' value='<?php echo $row['pc_catid'] ? $row['pc_catid'] : '5'; ?>' />
680 <input type='hidden' name='form_apptstatus' id='form_apptstatus' value='<?php echo $row['pc_apptstatus'] ? $row['pc_apptstatus'] : "^" ?>' />
681 <table border='0' width='100%'>
682 <tr>
683 <td width='1%' nowrap>
684 <b><?php xl('Visit','e'); ?>: </b>
685 </td>
686 <td nowrap style='padding:0px 5px 5px 0'>
687 <input class="form-control input-md" type="text" id='form_title' name='form_title' value='<?php echo htmlspecialchars($row['pc_title'],ENT_QUOTES) ? htmlspecialchars(['pc_title'],ENT_QUOTES) : 'Office Visit'; ?>' readonly='readonly'/>
688 </td>
689 <td></td>
690 <td width='1%' nowrap>
691 <b><?php xl('Date','e'); ?>:</b>
692 </td>
693 <td colspan='2' nowrap id='tdallday1'>
694 <input class="form-control input-md" type='text' size='10' name='form_date' readonly id='form_date'
695 value='<?php if (isset($eid)) { echo $eid ? $row['pc_eventDate'] : $date; } ?>'
696 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
697 </td>
698 </tr>
699 <tr>
700 <td nowrap>
701 <b><?php //xl('Title','e'); ?></b>
702 </td>
703 <td style='padding:0px 5px 5px 0' nowrap>
704 <!-- <input class="form-control input-md" type='text' size='10' name='form_title' readonly value='<?php //echo htmlspecialchars($row['pc_title'],ENT_QUOTES) ?>' title='<?php //xl('Event title','e'); ?>' /> -->
705 </td>
706 <td nowrap>
707 </td>
708 <td width='1%' nowrap id='tdallday2'>
709 <b><?php xl('Time','e');?>:</b>
710 </td>
711 <td width='1%' nowrap id='tdallday3'>
712 <input class="form-control inline" type='text' size='2' name='form_hour' value='<?php if(isset($eid)) { echo $starttimeh; } ?>'
713 title='<?php xl('Event start time','e'); ?>' readonly/> :
714 <input class="form-control inline" type='text' size='2' name='form_minute' value='<?php if(isset($eid)) { echo $starttimem; } ?>'
715 title='<?php xl('Event start time','e'); ?>' readonly/>&nbsp; <!-- -->
716 <select class="form-control" name='form_ampm' title='Note: 12:00 noon is PM, not AM' readonly >
717 <option value='1'><?php xl('AM','e'); ?></option>
718 <option value='2'<?php if ($startampm == '2') echo " selected" ?>><?php xl('PM','e'); ?></option>
719 </select>
720 </td>
721 </tr>
722 <tr>
723 <td nowrap>
724 <b><?php xl('Patient','e'); ?>:</b>
725 </td>
726 <td style='padding:0px 5px 5px 0' nowrap>
727 <input class="form-control input-md" type='text' size='10' id='form_patient' name='form_patient' value='<?php echo $patientname ?>' title='Patient' readonly />
728 <input type='hidden' name='form_pid' value='<?php echo $patientid ?>' />
729 </td>
730 <td nowrap>
731 &nbsp;
732 </td>
733 <td nowrap id='tdallday4'><?php xl('Duration','e'); ?></td>
734 <td nowrap id='tdallday5'>
735 <!-- --> <input class="form-control input-md" type='text' size='1' name='form_duration' value='<?php echo $row['pc_duration'] ? ($row['pc_duration']*1/60) : "0" ?>' readonly /><?php echo xl('minutes'); ?>
736 </td>
737 </tr>
738 <tr>
739 </tr>
740 <tr>
741 <td nowrap>
742 <b><?php xl('Provider','e'); ?>:</b>
743 </td>
744 <td style='padding:0px 5px 5px 0' nowrap>
745 <select class="form-control input-md" name='form_provider_ae' id='form_provider_ae' onchange='change_provider();'>
746 <?php
747 // present a list of providers to choose from
748 // default to the currently logged-in user
749 while ($urow = sqlFetchArray($ures)) {
750 echo " <option value='" . $urow['id'] . "'";
751 if (($urow['id'] == $_GET['userid'])||($urow['id']== $userid)) echo " selected";
752 echo ">" . $urow['lname'];
753 if ($urow['fname']) echo ", " . $urow['fname'];
754 echo "</option>\n";
757 </select>
758 </td>
759 <td nowrap style='font-size:8pt'>
760 </td>
761 <td><input type='button' class='btn btn-danger btn-sm' value='<?php xl('Openings','e');?>' onclick='find_available()' /></td>
762 <td></td>
763 </tr>
764 <tr>
765 <td nowrap>
766 <b><?php xl('Reason','e'); ?>:</b>
767 </td>
768 <td style='padding:0px 5px 5px 0' colspan='4' nowrap>
769 <input class="form-control input-md" type='text' size='40' name='form_comments' style='width:100%' value='<?php echo htmlspecialchars($hometext,ENT_QUOTES) ?>' title='<?php xl('Optional information about this event','e');?>' />
770 </td>
771 </tr>
772 </table>
774 <input type='button' name='form_save' class='btn btn-success btn-md' onsubmit='return false' value='<?php xl('Save','e');?>' onclick="validate()" />
775 &nbsp;
776 </p>
777 </form>
778 <script>
779 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
781 var durations = new Array();
782 // var rectypes = new Array();
783 <?php
784 // Read the event categories, generate their options list, and get
785 // the default event duration from them if this is a new event.
786 $catoptions = "";
787 $prefcat_options = " <option value='0'>-- None --</option>\n";
788 $thisduration = 0;
789 if ($eid) {
790 $thisduration = $row['pc_alldayevent'] ? 1440 : round($row['pc_duration'] / 60);
792 while ($crow = sqlFetchArray($cres)) {
793 $duration = round($crow['pc_duration'] / 60);
794 if ($crow['pc_end_all_day']) $duration = 1440;
795 echo " durations[" . $crow['pc_catid'] . "] = $duration\n";
796 // echo " rectypes[" . $crow['pc_catid'] . "] = " . $crow['pc_recurrtype'] . "\n";
797 $catoptions .= " <option value='" . $crow['pc_catid'] . "'";
798 if ($eid) {
799 if ($crow['pc_catid'] == $row['pc_catid']) $catoptions .= " selected";
800 } else {
801 if ($crow['pc_catid'] == $default_catid) {
802 $catoptions .= " selected";
803 $thisduration = $duration;
806 $catoptions .= ">" . $crow['pc_catname'] . "</option>\n";
808 // This section is to build the list of preferred categories:
809 if ($duration) {
810 $prefcat_options .= " <option value='" . $crow['pc_catid'] . "'";
811 if ($eid) {
812 if ($crow['pc_catid'] == $row['pc_prefcatid']) $prefcat_options .= " selected";
814 $prefcat_options .= ">" . $crow['pc_catname'] . "</option>\n";
820 <?php // require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
822 // This is for callback by the find-patient popup.
823 function setpatient(pid, lname, fname, dob) {
824 var f = document.forms.namedItem("theaddform");
825 f.form_patient.value = lname + ', ' + fname;
826 f.form_pid.value = pid;
827 dobstyle = (dob == '' || dob.substr(5, 10) == '00-00') ? '' : 'none';
828 document.getElementById('dob_row').style.display = dobstyle;
830 function change_provider(){
831 var f = document.forms.namedItem("theaddform");
832 f.form_date.value='';
833 f.form_hour.value='';
834 f.form_minute.value='';
836 // This is for callback by the find-patient popup.
837 function unsetpatient() {
838 var f = document.forms.namedItem("theaddform");
839 f.form_patient.value = '';
840 f.form_pid.value = '';
843 // This invokes the find-patient popup.
844 function sel_patient() {
845 dlgopen('find_patient_popup.php', '_blank', 500, 400);
848 // Do whatever is needed when a new event category is selected.
849 // For now this means changing the event title and duration.
850 function set_display() {
851 var f = document.forms.namedItem("theaddform");
852 var si = document.getElementById('form_category');
853 if (si.selectedIndex >= 0) {
854 var catid = si.options[si.selectedIndex].value;
855 var style_apptstatus = document.getElementById('title_apptstatus').style;
856 var style_prefcat = document.getElementById('title_prefcat').style;
857 if (catid == '2') { // In Office
858 style_apptstatus.display = 'none';
859 style_prefcat.display = '';
860 f.form_apptstatus.style.display = 'none';
861 f.form_prefcat.style.display = '';
862 } else {
863 style_prefcat.display = 'none';
864 style_apptstatus.display = '';
865 f.form_prefcat.style.display = 'none';
866 f.form_apptstatus.style.display = '';
871 // Gray out certain fields according to selection of Category DDL
872 function categoryChanged() {
873 var value = '5';
875 document.getElementById("form_patient").disabled=false;
876 //document.getElementById("form_apptstatus").disabled=false;
877 //document.getElementById("form_prefcat").disabled=false;
881 // Do whatever is needed when a new event category is selected.
882 // For now this means changing the event title and duration.
883 function set_category() {
884 var f = document.forms.namedItem("theaddform");
885 var s = f.form_category;
886 if (s.selectedIndex >= 0) {
887 var catid = s.options[s.selectedIndex].value;
888 f.form_title.value = s.options[s.selectedIndex].text;
889 f.form_duration.value = durations[catid];
890 set_display();
894 // Modify some visual attributes when the all-day or timed-event
895 // radio buttons are clicked.
896 function set_allday() {
897 var f = document.forms.namedItem("theaddform");
898 var color1 = '#777777';
899 var color2 = '#777777';
900 var disabled2 = true;
901 /*if (document.getElementById('rballday1').checked) {
902 color1 = '#000000';
904 if (document.getElementById('rballday2').checked) {
905 color2 = '#000000';
906 disabled2 = false;
908 document.getElementById('tdallday1').style.color = color1;
909 document.getElementById('tdallday2').style.color = color2;
910 document.getElementById('tdallday3').style.color = color2;
911 document.getElementById('tdallday4').style.color = color2;
912 document.getElementById('tdallday5').style.color = color2;
913 f.form_hour.disabled = disabled2;
914 f.form_minute.disabled = disabled2;
915 f.form_ampm.disabled = disabled2;
916 f.form_duration.disabled = disabled2;
919 // Modify some visual attributes when the Repeat checkbox is clicked.
920 function set_repeat() {
921 var f = document.forms.namedItem("theaddform");
922 var isdisabled = true;
923 var mycolor = '#777777';
924 var myvisibility = 'hidden';
925 /*if (f.form_repeat.checked) {
926 isdisabled = false;
927 mycolor = '#000000';
928 myvisibility = 'visible';
930 //f.form_repeat_type.disabled = isdisabled;
931 //f.form_repeat_freq.disabled = isdisabled;
932 //f.form_enddate.disabled = isdisabled;
933 document.getElementById('tdrepeat1').style.color = mycolor;
934 document.getElementById('tdrepeat2').style.color = mycolor;
935 document.getElementById('img_enddate').style.visibility = myvisibility;
938 // This is for callback by the find-available popup.
939 function setappt(year,mon,mday,hours,minutes) {
940 var f = document.forms.namedItem("theaddform");
941 f.form_date.value = '' + year + '-' +
942 ('' + (mon + 100)).substring(1) + '-' +
943 ('' + (mday + 100)).substring(1);
944 f.form_ampm.selectedIndex = (hours >= 12) ? 1 : 0;
945 f.form_hour.value = (hours > 12) ? hours - 12 : hours;
946 f.form_minute.value = ('' + (minutes + 100)).substring(1);
949 // Invoke the find-available popup.
950 function find_available() {
952 // (CHEMED) Conditional value selection, because there is no <select> element
953 // when making an appointment for a specific provider
954 var se = document.getElementById('form_provider_ae');
955 <?php if ($userid != 0) { ?>
956 s = se.value;
957 <?php } else {?>
958 s = se.options[se.selectedIndex].value;
959 <?php }?>
960 var formDate = document.getElementById('form_date');
961 window.open('find_appt_popup_user.php?bypatient&providerid=' + s +
962 '&catid=5' +
963 '&startdate=' + formDate.value, '_blank', "width=900,height=800");
966 // Check for errors when the form is submitted.
967 function validate() {
968 var f = document.getElementById('theaddform');
969 if (!f.form_date.value || !f.form_hour.value || !f.form_minute.value) {
970 alert('Please click on "Openings" to select a time.');
971 return false;
974 // in lunch outofoffice reserved vacation
975 // f.form_category.value='2';
976 if (f.form_patient.value=='Click to select' && (!(
977 f.form_category.value=='2' || f.form_category.value=='8' || f.form_category.value=='3' || f.form_category.value=='4' || f.form_category.value=='11'
978 || f.form_category.value=='10'))) {
979 alert('Please select a patient.');
980 return false;
981 } else if (f.form_category.value=='10') {
982 unsetpatient();
984 var form_action = document.getElementById('form_action');
985 form_action.value="save";
986 f.submit();
987 return false;
990 function deleteEvent() {
991 if (confirm("Deleting this event cannot be undone. It cannot be recovered once it is gone. Are you sure you wish to delete this event?")) {
992 var f = document.getElementById('theaddform');
993 var form_action = document.getElementById('form_action');
994 form_action.value="delete";
995 f.submit();
996 return true;
998 return false;
1001 </script>
1003 <script>
1004 <?php if ($eid) { ?>
1005 set_display();
1006 <?php } else { ?>
1007 //set_category();
1008 <?php } ?>
1009 //set_allday();
1010 //set_repeat();
1012 //Calendar.setup({inputField:"form_dob", ifFormat:"%Y-%m-%d", button:"img_dob"});
1013 </script>
1015 </body>
1016 </html>