need to specify use of onsetDate as member function rather than global
[openemr.git] / patients / add_edit_event_user.php
blobb9a38fa41a4261e7466cb1cef41e016d8bf88f2a
1 <?php
2 // Copyright (C) 2005-2006 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // The event editor looks something like this:
11 //------------------------------------------------------------//
12 // Category __________________V O All day event //
13 // Date _____________ [?] O Time ___:___ __V //
14 // Title ___________________ duration ____ minutes //
15 // Patient _(Click_to_select)_ //
16 // Provider __________________V X Repeats ______V ______V //
17 // Status __________________V until __________ [?] //
18 // Comments ________________________________________________ //
19 // //
20 // [Save] [Find Available] [Delete] [Cancel] //
21 //------------------------------------------------------------//
23 //landing page definition -- where to go if something goes wrong
24 $landingpage = "index.php";
27 // kick out if patient not authenticated
28 session_start();
29 if ( isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite']) ) {
30 $pid = $_SESSION['pid'];
32 else {
33 session_destroy();
34 header('Location: '.$landingpage.'?w');
35 exit;
39 $ignoreAuth = 1;
40 global $ignoreAuth;
42 include_once("../interface/globals.php");
43 include_once("$srcdir/patient.inc");
44 include_once("$srcdir/forms.inc");
46 // Exit if the modify calendar for portal flag is not set
47 if (!($GLOBALS['portal_onsite_appt_modify'])) {
48 echo htmlspecialchars( xl('You are not authorized to schedule appointments.'),ENT_NOQUOTES);
49 exit;
52 // Things that might be passed by our opener.
54 $eid = $_GET['eid']; // only for existing events
55 $date = $_GET['date']; // this and below only for new events
56 $userid = $_GET['userid'];
57 $default_catid = $_GET['catid'] ? $_GET['catid'] : '5';
58 $patientid = $_GET['patid'];
60 if ($date)
61 $date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6);
62 else
63 $date = date("Y-m-d");
65 $starttimem = '00';
66 if (isset($_GET['starttimem']))
67 $starttimem = substr('00' . $_GET['starttimem'], -2);
69 if (isset($_GET['starttimeh'])) {
70 $starttimeh = $_GET['starttimeh'];
71 if (isset($_GET['startampm'])) {
72 if ($_GET['startampm'] == '2' && $starttimeh < 12)
73 $starttimeh += 12;
75 } else {
76 $starttimeh = date("G");
78 $startampm = '';
80 $info_msg = "";
82 // ===========================
83 // EVENTS TO FACILITIES (lemonsoftware)
84 // edit event case - if there is no association made, then insert one with the first facility
85 /*if ( $eid ) {
86 $selfacil = '';
87 $facility = sqlQuery("SELECT pc_facility, pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
88 if ( !$facility['pc_facility'] ) {
89 $qmin = sqlQuery("SELECT MIN(id) as minId FROM facility");
90 $min = $qmin['minId'];
92 // multiple providers case
93 if ( $GLOBALS['select_multi_providers'] ) {
94 $mul = $facility['pc_multiple'];
95 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_multiple = $mul");
97 // EOS multiple
99 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_eid = $eid");
100 $e2f = $minId;
101 } else {
102 $e2f = $facility['pc_facility'];
105 // EOS E2F
106 // ===========================
107 // ===========================
109 // EVENTS TO FACILITIES (lemonsoftware)
110 //(CHEMED) get facility name
111 // edit event case - if there is no association made, then insert one with the first facility
112 if ( $eid ) {
113 $selfacil = '';
114 $facility = sqlQuery("SELECT pc_facility, pc_multiple, pc_aid, facility.name
115 FROM openemr_postcalendar_events
116 LEFT JOIN facility ON (openemr_postcalendar_events.pc_facility = facility.id)
117 WHERE pc_eid = $eid");
118 if ( !$facility['pc_facility'] ) {
119 $qmin = sqlQuery("SELECT facility_id as minId, facility FROM users WHERE id = ".$facility['pc_aid']);
120 $min = $qmin['minId'];
121 $min_name = $qmin['facility'];
123 // multiple providers case
124 if ( $GLOBALS['select_multi_providers'] ) {
125 $mul = $facility['pc_multiple'];
126 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_multiple = $mul");
128 // EOS multiple
130 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_eid = $eid");
131 $e2f = $min;
132 $e2f_name = $min_name;
133 } else {
134 $e2f = $facility['pc_facility'];
135 $e2f_name = $facility['name'];
138 // EOS E2F
139 // ===========================
142 // If we are saving, then save and close the window.
144 if ($_POST['form_action'] == "save") {
145 //print_r($_POST);
146 //exit();
147 $event_date = fixDate($_POST['form_date']);
149 // Compute start and end time strings to be saved.
150 if ($_POST['form_allday']) {
151 $tmph = 0;
152 $tmpm = 0;
153 $duration = 24 * 60;
154 } else {
155 $tmph = $_POST['form_hour'] + 0;
156 $tmpm = $_POST['form_minute'] + 0;
157 if ($_POST['form_ampm'] == '2' && $tmph < 12) $tmph += 12;
158 $duration = $_POST['form_duration'];
160 $starttime = "$tmph:$tmpm:00";
162 $tmpm += $duration;
163 while ($tmpm >= 60) {
164 $tmpm -= 60;
165 ++$tmph;
167 $endtime = "$tmph:$tmpm:00";
169 // Useless garbage that we must save.
170 $locationspec = 'a:6:{s:14:"event_location";N;s:13:"event_street1";N;' .
171 's:13:"event_street2";N;s:10:"event_city";N;s:11:"event_state";N;s:12:"event_postal";N;}';
173 // More garbage, but this time 1 character of it is used to save the
174 // repeat type.
175 if ($_POST['form_repeat']) {
176 $recurrspec = 'a:5:{' .
177 's:17:"event_repeat_freq";s:1:"' . $_POST['form_repeat_freq'] . '";' .
178 's:22:"event_repeat_freq_type";s:1:"' . $_POST['form_repeat_type'] . '";' .
179 's:19:"event_repeat_on_num";s:1:"1";' .
180 's:19:"event_repeat_on_day";s:1:"0";' .
181 's:20:"event_repeat_on_freq";s:1:"0";}';
182 } else {
183 $recurrspec = 'a:5:{' .
184 's:17:"event_repeat_freq";N;' .
185 's:22:"event_repeat_freq_type";s:1:"0";' .
186 's:19:"event_repeat_on_num";s:1:"1";' .
187 's:19:"event_repeat_on_day";s:1:"0";' .
188 's:20:"event_repeat_on_freq";s:1:"1";}';
191 //The modification of the start date for events that take place on one day of the week
192 //for example monday, or thursday. We set the start date on the first day of the week
193 //that the event is scheduled. For example if you set the event to repeat on each monday
194 //the start date of the event will be set on the first monday after the day the event is scheduled
195 if($_POST['form_repeat_type'] == 5)
197 $exploded_date= explode("-",$event_date);
198 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
199 if($edate=="Tue") {
200 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
202 elseif($edate=="Wed") {
203 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
205 elseif($edate=="Thu") {
206 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
208 elseif($edate=="Fri") {
209 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
211 elseif($edate=="Sat") {
212 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
214 elseif($edate=="Sun") {
215 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
217 } elseif($_POST['form_repeat_type'] == 6) {
218 $exploded_date= explode("-",$event_date);
219 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
220 if($edate=="Wed") {
221 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
223 elseif($edate=="Thu") {
224 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
226 elseif($edate=="Fri") {
227 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
229 elseif($edate=="Sat") {
230 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
232 elseif($edate=="Sun") {
233 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
235 elseif($edate=="Mon") {
236 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
238 } elseif($_POST['form_repeat_type'] == 7) {
239 $exploded_date= explode("-",$event_date);
240 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
241 if($edate=="Thu") {
242 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
244 elseif($edate=="Fri") {
245 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
247 elseif($edate=="Sat") {
248 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
250 elseif($edate=="Sun") {
251 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
253 elseif($edate=="Mon") {
254 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
256 elseif($edate=="Tue") {
257 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
259 } elseif($_POST['form_repeat_type'] == 8) {
260 $exploded_date= explode("-",$event_date);
261 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
262 if($edate=="Fri") {
263 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
265 elseif($edate=="Sat") {
266 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
268 elseif($edate=="Sun") {
269 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
271 elseif($edate=="Mon") {
272 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
274 elseif($edate=="Tue") {
275 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
277 elseif($edate=="Wed") {
278 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
280 } elseif($_POST['form_repeat_type'] == 9) {
281 $exploded_date= explode("-",$event_date);
282 $edate = date("D",mktime(0,0,0,$exploded_date[1],$exploded_date[2],$exploded_date[0]));
283 if($edate=="Sat") {
284 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+6,$exploded_date[0]));
286 elseif($edate=="Sun") {
287 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+5,$exploded_date[0]));
289 elseif($edate=="Mon") {
290 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+4,$exploded_date[0]));
292 elseif($edate=="Tue") {
293 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+3,$exploded_date[0]));
295 elseif($edate=="Wed") {
296 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+2,$exploded_date[0]));
298 elseif($edate=="Thu") {
299 $event_date=date("Y-m-d",mktime(0,0,0,$exploded_date[1],$exploded_date[2]+1,$exploded_date[0]));
301 }//if end
302 /* =======================================================
303 // UPDATE EVENTS
304 ========================================================*/
305 if ($eid) {
307 // what is multiple key around this $eid?
308 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
310 if ($GLOBALS['select_multi_providers'] && $row['pc_multiple']) {
311 /* ==========================================
312 // multi providers BOS
313 ==========================================*/
315 // obtain current list of providers regarding the multiple key
316 $up = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_multiple={$row['pc_multiple']}");
317 while ($current = sqlFetchArray($up)) {
318 $providers_current[] = $current['pc_aid'];
321 $providers_new = $_POST['form_provider'];
323 // this difference means that some providers from current was UNCHECKED
324 // so we must delete this event for them
325 $r1 = array_diff ($providers_current, $providers_new);
326 if (count ($r1)) {
327 foreach ($r1 as $to_be_removed) {
328 sqlQuery("DELETE FROM openemr_postcalendar_events WHERE pc_aid='$to_be_removed' AND pc_multiple={$row['pc_multiple']}");
332 // this difference means that some providers was added
333 // so we must insert this event for them
334 $r2 = array_diff ($providers_new, $providers_current);
335 if (count ($r2)) {
336 foreach ($r2 as $to_be_inserted) {
337 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)
338 VALUES ( " .
339 "'" . $_POST['form_category'] . "', " .
340 "'" . $row['pc_multiple'] . "', " .
341 "'" . $to_be_inserted . "', " .
342 "'" . $_POST['form_pid'] . "', " .
343 "'" . $_POST['form_title'] . "', " .
344 "NOW(), " .
345 "'" . $_POST['form_comments'] . "', " .
346 "'" . $_SESSION['authUserID'] . "', " .
347 "'" . $event_date . "', " .
348 "'" . fixDate($_POST['form_enddate']) . "', " .
349 "'" . ($duration * 60) . "', " .
350 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
351 "'$recurrspec', " .
352 "'$starttime', " .
353 "'$endtime', " .
354 "'" . $_POST['form_allday'] . "', " .
355 "'" . $_POST['form_apptstatus'] . "', " .
356 "'" . $_POST['form_prefcat'] . "', " .
357 "'$locationspec', " .
358 "1, " .
359 "1, " .(int)$_POST['facility']. " )"); // FF stuff
360 } // foreach
361 } //if count
364 // after the two diffs above, we must update for remaining providers
365 // those who are intersected in $providers_current and $providers_new
366 foreach ($_POST['form_provider'] as $provider) {
367 sqlStatement("UPDATE openemr_postcalendar_events SET " .
368 "pc_catid = '" . $_POST['form_category'] . "', " .
369 "pc_pid = '" . $_POST['form_pid'] . "', " .
370 "pc_title = '" . $_POST['form_title'] . "', " .
371 "pc_time = NOW(), " .
372 "pc_hometext = '" . $_POST['form_comments'] . "', " .
373 "pc_informant = '" . $_SESSION['authUserID'] . "', " .
374 "pc_eventDate = '" . $event_date . "', " .
375 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
376 "pc_duration = '" . ($duration * 60) . "', " .
377 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
378 "pc_recurrspec = '$recurrspec', " .
379 "pc_startTime = '$starttime', " .
380 "pc_endTime = '$endtime', " .
381 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
382 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
383 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
384 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
385 "WHERE pc_aid = '$provider' AND pc_multiple={$row['pc_multiple']}");
386 } // foreach
388 /* ==========================================
389 // multi providers EOS
390 ==========================================*/
392 } elseif ( !$row['pc_multiple'] ) {
393 if ( $GLOBALS['select_multi_providers'] ) {
394 $prov = $_POST['form_provider'][0];
395 } else {
396 $prov = $_POST['form_provider'];
399 // simple provider case
400 sqlStatement("UPDATE openemr_postcalendar_events SET " .
401 "pc_catid = '" . $_POST['form_category'] . "', " .
402 "pc_aid = '" . $prov . "', " .
403 "pc_pid = '" . $_POST['form_pid'] . "', " .
404 "pc_title = '" . $_POST['form_title'] . "', " .
405 "pc_time = NOW(), " .
406 "pc_hometext = '" . $_POST['form_comments'] . "', " .
407 "pc_informant = '" . $_SESSION['authUserID'] . "', " .
408 "pc_eventDate = '" . $event_date . "', " .
409 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
410 "pc_duration = '" . ($duration * 60) . "', " .
411 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
412 "pc_recurrspec = '$recurrspec', " .
413 "pc_startTime = '$starttime', " .
414 "pc_endTime = '$endtime', " .
415 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
416 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
417 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
418 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
419 "WHERE pc_eid = '$eid'");
423 // =======================================
424 // EOS multi providers case
425 // =======================================
427 // EVENTS TO FACILITIES
429 $e2f = (int)$eid;
431 /* =======================================================
432 // INSERT EVENTS
433 ========================================================*/
434 } else {
436 // =======================================
437 // multi providers case
438 // =======================================
440 if (is_array($_POST['form_provider'])) {
442 // obtain the next available unique key to group multiple providers around some event
443 $q = sqlStatement ("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
444 $max = sqlFetchArray($q);
445 $new_multiple_value = $max['max'] + 1;
447 foreach ($_POST['form_provider'] as $provider) {
448 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
449 "pc_catid, pc_multiple, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
450 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
451 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
452 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
453 ") VALUES ( " .
454 "'" . $_POST['form_category'] . "', " .
455 "'" . $new_multiple_value . "', " .
456 "'" . $provider . "', " .
457 "'" . $_POST['form_pid'] . "', " .
458 "'" . $_POST['form_title'] . "', " .
459 "NOW(), " .
460 "'" . $_POST['form_comments'] . "', " .
461 "'" . $_SESSION['authUserID'] . "', " .
462 "'" . $event_date . "', " .
463 "'" . fixDate($_POST['form_enddate']) . "', " .
464 "'" . ($duration * 60) . "', " .
465 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
466 "'$recurrspec', " .
467 "'$starttime', " .
468 "'$endtime', " .
469 "'" . $_POST['form_allday'] . "', " .
470 "'" . $_POST['form_apptstatus'] . "', " .
471 "'" . $_POST['form_prefcat'] . "', " .
472 "'$locationspec', " .
473 "1, " .
474 "1, " .(int)$_POST['facility']. " )"); // FF stuff
476 } // foreach
478 } else {
479 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
480 "pc_catid, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
481 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
482 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
483 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
484 ") VALUES ( " .
485 "'" . $_POST['form_category'] . "', " .
486 "'" . $_POST['form_provider'] . "', " .
487 "'" . $_POST['form_pid'] . "', " .
488 "'" . $_POST['form_title'] . "', " .
489 "NOW(), " .
490 "'" . $_POST['form_comments'] . "', " .
491 "'" . $_SESSION['authUserID'] . "', " .
492 "'" . $event_date . "', " .
493 "'" . fixDate($_POST['form_enddate']) . "', " .
494 "'" . ($duration * 60) . "', " .
495 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
496 "'$recurrspec', " .
497 "'$starttime', " .
498 "'$endtime', " .
499 "'" . $_POST['form_allday'] . "', " .
500 "'" . $_POST['form_apptstatus'] . "', " .
501 "'" . $_POST['form_prefcat'] . "', " .
502 "'$locationspec', " .
503 "1, " .
504 "1," .(int)$_POST['facility']. ")"); // FF stuff
505 } // INSERT single
506 } // else - insert
508 // Save new DOB if it's there.
509 $patient_dob = trim($_POST['form_dob']);
510 if ($patient_dob && $_POST['form_pid']) {
511 sqlStatement("UPDATE patient_data SET DOB = '$patient_dob' WHERE " .
512 "pid = '" . $_POST['form_pid'] . "'");
515 // Auto-create a new encounter if appropriate.
518 /* if ($GLOBALS['auto_create_new_encounters'] &&
519 $_POST['form_apptstatus'] == '@' && $event_date == date('Y-m-d'))
522 // We decided not to auto-create blank enconter when user arrives. Todd's decision 18 Jun 2010
523 // Applied by Cassian Lup (cassian.lup@clinicdr.com)
525 if (0) {
526 $tmprow = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
527 "pid = '" . $_POST['form_pid'] . "' AND date = '$event_date 00:00:00'");
528 if ($tmprow['count'] == 0) {
529 $tmprow = sqlQuery("SELECT username, facility, facility_id FROM users WHERE id = '" .
530 $_POST['form_provider'] . "'");
531 $username = $tmprow['username'];
532 $facility = $tmprow['facility'];
533 $facility_id = $tmprow['facility_id'];
534 $conn = $GLOBALS['adodb']['db'];
535 $encounter = $conn->GenID("sequences");
536 addForm($encounter, "New Patient Encounter",
537 sqlInsert("INSERT INTO form_encounter SET " .
538 "date = '$event_date', " .
539 "onset_date = '$event_date', " .
540 "reason = '" . $_POST['form_comments'] . "', " .
541 "facility = '$facility', " .
542 "facility_id = '$facility_id', " .
543 "pid = '" . $_POST['form_pid'] . "', " .
544 "encounter = '$encounter'"
546 "newpatient", $_POST['form_pid'], "1", "NOW()", $username
548 $info_msg .= "New encounter $encounter was created. ";
553 else if ($_POST['form_action'] == "delete") {
554 // =======================================
555 // multi providers case
556 // =======================================
557 if ($GLOBALS['select_multi_providers']) {
558 // what is multiple key around this $eid?
559 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
560 if ( $row['pc_multiple'] ) {
561 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_multiple = {$row['pc_multiple']}");
562 } else {
563 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = $eid");
565 // =======================================
566 // EOS multi providers case
567 // =======================================
568 } else {
569 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = '$eid'");
573 if ($_POST['form_action'] != "") {
574 // Close this window and refresh the calendar display.
575 echo "<html>\n<body>\n<script language='JavaScript'>\n";
576 if ($info_msg) echo " alert('$info_msg');\n";
577 // echo " if (!opener.closed && opener.refreshme) opener.refreshme();\n";
578 // echo " if (!opener.closed && opener.refreshme) window.opener.location.reload(true);\n";
579 // echo " opener.refreshme();";
580 // echo " window.location='https://ehr.clinicdr.com/".$GLOBALS['instance_name']."/clinicdr-ehr/interface/main/calendar/index.php?module=PostCalendar&func=view&tplview=default&pc_category=&pc_topic='";
581 echo " parent.jQuery.fn.fancybox.close();\n";
582 echo "</script>\n</body>\n</html>\n";
583 exit();
585 // If we get this far then we are displaying the form.
587 $statuses = array(
588 '-' => '',
589 '*' => xl('* Reminder done'),
590 '+' => xl('+ Chart pulled'),
591 'x' => xl('x Cancelled'), // added Apr 2008 by JRM
592 '?' => xl('? No show'),
593 '@' => xl('@ Arrived'),
594 '~' => xl('~ Arrived late'),
595 '!' => xl('! Left w/o visit'),
596 '#' => xl('# Ins/fin issue'),
597 '<' => xl('< In exam room'),
598 '>' => xl('> Checked out'),
599 '$' => xl('$ Coding done'),
602 $repeats = 0; // if the event repeats
603 $repeattype = '0';
604 $repeatfreq = '0';
605 $patienttitle = "";
606 $hometext = "";
607 $row = array();
609 // If we are editing an existing event, then get its data.
610 if ($eid) {
611 $row = sqlQuery("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = $eid");
612 $date = $row['pc_eventDate'];
613 $userid = $row['pc_aid'];
614 $patientid = $row['pc_pid'];
615 $starttimeh = substr($row['pc_startTime'], 0, 2) + 0;
616 $starttimem = substr($row['pc_startTime'], 3, 2);
617 $repeats = $row['pc_recurrtype'];
618 $multiple_value = $row['pc_multiple'];
620 if (preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
621 $repeattype = $matches[1];
623 if (preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
624 $repeatfreq = $matches[1];
626 $hometext = $row['pc_hometext'];
627 if (substr($hometext, 0, 6) == ':text:') $hometext = substr($hometext, 6);
629 else {
630 $patientid=$_GET['pid'];
633 // If we have a patient ID, get the name and phone numbers to display.
634 if ($patientid) {
635 $prow = sqlQuery("SELECT lname, fname, phone_home, phone_biz, DOB " .
636 "FROM patient_data WHERE pid = '" . $patientid . "'");
637 $patientname = $prow['lname'] . ", " . $prow['fname'];
638 if ($prow['phone_home']) $patienttitle .= " H=" . $prow['phone_home'];
639 if ($prow['phone_biz']) $patienttitle .= " W=" . $prow['phone_biz'];
642 // Get the providers list.
643 $ures = sqlStatement("SELECT id, username, fname, lname FROM users WHERE " .
644 "authorized != 0 AND active = 1 ORDER BY lname, fname");
646 //-------------------------------------
647 //(CHEMED)
648 //Set default facility for a new event based on the given 'userid'
649 if ($userid) {
650 $pref_facility = sqlFetchArray(sqlStatement("SELECT facility_id, facility FROM users WHERE id = $userid"));
651 $e2f = $pref_facility['facility_id'];
652 $e2f_name = $pref_facility['facility'];
654 //END of CHEMED -----------------------
656 // Get event categories.
657 $cres = sqlStatement("SELECT pc_catid, pc_catname, pc_recurrtype, pc_duration, pc_end_all_day " .
658 "FROM openemr_postcalendar_categories ORDER BY pc_catname");
660 // Fix up the time format for AM/PM.
661 $startampm = '1';
662 if ($starttimeh >= 12) { // p.m. starts at noon and not 12:01
663 $startampm = '2';
664 if ($starttimeh > 12) $starttimeh -= 12;
668 <html>
669 <head>
670 <?php html_header_show(); ?>
671 <title><?php echo $eid ? "Edit" : "Add New" ?> <?php xl('Event','e');?></title>
672 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
674 <style>
675 td { font-size:0.8em; }
676 </style>
678 <style type="text/css">@import url(../library/dynarch_calendar.css);</style>
679 <script type="text/javascript" src="../library/topdialog.js"></script>
680 <script type="text/javascript" src="../library/dialog.js"></script>
681 <script type="text/javascript" src="../library/textformat.js"></script>
682 <script type="text/javascript" src="../library/dynarch_calendar.js"></script>
683 <script type="text/javascript" src="../library/dynarch_calendar_en.js"></script>
684 <script type="text/javascript" src="../library/dynarch_calendar_setup.js"></script>
686 <script language="JavaScript">
688 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
690 var durations = new Array();
691 // var rectypes = new Array();
692 <?php
693 // Read the event categories, generate their options list, and get
694 // the default event duration from them if this is a new event.
695 $catoptions = "";
696 $prefcat_options = " <option value='0'>-- None --</option>\n";
697 $thisduration = 0;
698 if ($eid) {
699 $thisduration = $row['pc_alldayevent'] ? 1440 : round($row['pc_duration'] / 60);
701 while ($crow = sqlFetchArray($cres)) {
702 $duration = round($crow['pc_duration'] / 60);
703 if ($crow['pc_end_all_day']) $duration = 1440;
704 echo " durations[" . $crow['pc_catid'] . "] = $duration\n";
705 // echo " rectypes[" . $crow['pc_catid'] . "] = " . $crow['pc_recurrtype'] . "\n";
706 $catoptions .= " <option value='" . $crow['pc_catid'] . "'";
707 if ($eid) {
708 if ($crow['pc_catid'] == $row['pc_catid']) $catoptions .= " selected";
709 } else {
710 if ($crow['pc_catid'] == $default_catid) {
711 $catoptions .= " selected";
712 $thisduration = $duration;
715 $catoptions .= ">" . $crow['pc_catname'] . "</option>\n";
717 // This section is to build the list of preferred categories:
718 if ($duration) {
719 $prefcat_options .= " <option value='" . $crow['pc_catid'] . "'";
720 if ($eid) {
721 if ($crow['pc_catid'] == $row['pc_prefcatid']) $prefcat_options .= " selected";
723 $prefcat_options .= ">" . $crow['pc_catname'] . "</option>\n";
729 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
731 // This is for callback by the find-patient popup.
732 function setpatient(pid, lname, fname, dob) {
733 var f = document.forms[0];
734 f.form_patient.value = lname + ', ' + fname;
735 f.form_pid.value = pid;
736 dobstyle = (dob == '' || dob.substr(5, 10) == '00-00') ? '' : 'none';
737 document.getElementById('dob_row').style.display = dobstyle;
739 function change_provider(){
740 var f = document.forms[0];
741 f.form_date.value='';
742 f.form_hour.value='';
743 f.form_minute.value='';
745 // This is for callback by the find-patient popup.
746 function unsetpatient() {
747 var f = document.forms[0];
748 f.form_patient.value = '';
749 f.form_pid.value = '';
752 // This invokes the find-patient popup.
753 function sel_patient() {
754 dlgopen('find_patient_popup.php', '_blank', 500, 400);
757 // Do whatever is needed when a new event category is selected.
758 // For now this means changing the event title and duration.
759 function set_display() {
760 var f = document.forms[0];
761 var s = f.form_category;
762 if (s.selectedIndex >= 0) {
763 var catid = s.options[s.selectedIndex].value;
764 var style_apptstatus = document.getElementById('title_apptstatus').style;
765 var style_prefcat = document.getElementById('title_prefcat').style;
766 if (catid == '2') { // In Office
767 style_apptstatus.display = 'none';
768 style_prefcat.display = '';
769 f.form_apptstatus.style.display = 'none';
770 f.form_prefcat.style.display = '';
771 } else {
772 style_prefcat.display = 'none';
773 style_apptstatus.display = '';
774 f.form_prefcat.style.display = 'none';
775 f.form_apptstatus.style.display = '';
780 // Gray out certain fields according to selection of Category DDL
781 function categoryChanged() {
782 var value = '5';
784 document.getElementById("form_patient").disabled=false;
785 //document.getElementById("form_apptstatus").disabled=false;
786 //document.getElementById("form_prefcat").disabled=false;
790 // Do whatever is needed when a new event category is selected.
791 // For now this means changing the event title and duration.
792 function set_category() {
793 var f = document.forms[0];
794 var s = f.form_category;
795 if (s.selectedIndex >= 0) {
796 var catid = s.options[s.selectedIndex].value;
797 f.form_title.value = s.options[s.selectedIndex].text;
798 f.form_duration.value = durations[catid];
799 set_display();
803 // Modify some visual attributes when the all-day or timed-event
804 // radio buttons are clicked.
805 function set_allday() {
806 var f = document.forms[0];
807 var color1 = '#777777';
808 var color2 = '#777777';
809 var disabled2 = true;
810 /*if (document.getElementById('rballday1').checked) {
811 color1 = '#000000';
813 if (document.getElementById('rballday2').checked) {
814 color2 = '#000000';
815 disabled2 = false;
817 document.getElementById('tdallday1').style.color = color1;
818 document.getElementById('tdallday2').style.color = color2;
819 document.getElementById('tdallday3').style.color = color2;
820 document.getElementById('tdallday4').style.color = color2;
821 document.getElementById('tdallday5').style.color = color2;
822 f.form_hour.disabled = disabled2;
823 f.form_minute.disabled = disabled2;
824 f.form_ampm.disabled = disabled2;
825 f.form_duration.disabled = disabled2;
828 // Modify some visual attributes when the Repeat checkbox is clicked.
829 function set_repeat() {
830 var f = document.forms[0];
831 var isdisabled = true;
832 var mycolor = '#777777';
833 var myvisibility = 'hidden';
834 /*if (f.form_repeat.checked) {
835 isdisabled = false;
836 mycolor = '#000000';
837 myvisibility = 'visible';
839 //f.form_repeat_type.disabled = isdisabled;
840 //f.form_repeat_freq.disabled = isdisabled;
841 //f.form_enddate.disabled = isdisabled;
842 document.getElementById('tdrepeat1').style.color = mycolor;
843 document.getElementById('tdrepeat2').style.color = mycolor;
844 document.getElementById('img_enddate').style.visibility = myvisibility;
847 // This is for callback by the find-available popup.
848 function setappt(year,mon,mday,hours,minutes) {
849 var f = document.forms[0];
850 f.form_date.value = '' + year + '-' +
851 ('' + (mon + 100)).substring(1) + '-' +
852 ('' + (mday + 100)).substring(1);
853 f.form_ampm.selectedIndex = (hours >= 12) ? 1 : 0;
854 f.form_hour.value = (hours > 12) ? hours - 12 : hours;
855 f.form_minute.value = ('' + (minutes + 100)).substring(1);
858 // Invoke the find-available popup.
859 function find_available() {
860 //top.restoreSession();
861 // (CHEMED) Conditional value selection, because there is no <select> element
862 // when making an appointment for a specific provider
863 var s = document.forms[0].form_provider;
864 <?php if ($userid != 0) { ?>
865 s = document.forms[0].form_provider.value;
866 <?php } else {?>
867 s = document.forms[0].form_provider.options[s.selectedIndex].value;
868 <?php }?>
869 // var fd2=document.forms[0].form_date2.value;
870 // document.forms[0].form_date.value=fd2.substring(6)+'-'+fd2.substring(0,2)+'-'+fd2.substring(3,5);
872 var formDate = document.forms[0].form_date;
873 window.open('find_appt_popup_user.php?bypatient&providerid=' + s +
874 '&catid=5' +
875 '&startdate=' + formDate.value, '_blank', 500, 400);
876 //END (CHEMED) modifications
879 // Check for errors when the form is submitted.
880 function validate() {
881 var f = document.getElementById('theform');
882 if (!f.form_date.value || !f.form_hour.value || !f.form_minute.value) {
883 alert('Please click on "Openings" to select a time.');
884 return false;
887 // in lunch outofoffice reserved vacation
888 f.form_category.value='12';
889 if (f.form_patient.value=='Click to select' && (!(
890 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'
891 || f.form_category.value=='10'))) {
892 alert('Please select a patient.');
893 return false;
894 } else if (f.form_category.value=='10') {
895 unsetpatient();
897 var form_action = document.getElementById('form_action');
898 form_action.value="save";
899 f.submit();
900 //top.restoreSession();
901 // top
902 return true;
905 function deleteEvent() {
906 if (confirm("Deleting this event cannot be undone. It cannot be recovered once it is gone.\nAre you sure you wish to delete this event?")) {
907 var f = document.getElementById('theform');
908 var form_action = document.getElementById('form_action');
909 form_action.value="delete";
910 f.submit();
911 return true;
913 return false;
916 </script>
918 </head>
920 <body class="body_top" onunload='imclosing()' onload='categoryChanged()'>
922 <form method='post' name='theform' id='theform' action='add_edit_event_user.php?eid=<?php echo $eid ?>' />
923 <input type="hidden" name="form_action" id="form_action" value="">
924 <center>
926 <table border='0' width='100%'>
928 <tr>
929 <td width='1%' nowrap>
930 <b><?php xl('Category','e'); ?>:</b>
931 </td>
932 <td nowrap>
933 <input type="text" id='form_category' name='form_category' value='Office Visit' readonly='readonly' style='width:100%'/>
934 </td>
935 <td></td>
936 <td width='1%' nowrap>
937 <b><?php xl('Date','e'); ?>:</b>
938 </td>
939 <td colspan='2' nowrap id='tdallday1'>
940 <input type='text' size='10' name='form_date' readonly id='form_date' <?php echo $disabled ?>
941 value='<?php if (isset($eid)) { echo $eid ? $row['pc_eventDate'] : $date; } ?>'
942 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
943 </td>
944 </tr>
946 <tr>
947 <td nowrap>
948 <b><?php xl('Title','e'); ?>:</b>
949 </td>
950 <td nowrap>
951 <input type='text' size='10' name='form_title' value='<?php echo addslashes($row['pc_title']) ?>'
952 style='width:100%'
953 title='<?php xl('Event title','e'); ?>' />
954 </td>
955 <td nowrap>
956 </td>
957 <td width='1%' nowrap id='tdallday2'>
958 <?php xl('Time','e'); ?>
959 </td>
960 <td width='1%' nowrap id='tdallday3'>
961 <input type='text' size='2' name='form_hour' value='<?php if(isset($eid)) { echo $starttimeh; } ?>'
962 title='<?php xl('Event start time','e'); ?>' readonly/> :
963 <input type='text' size='2' name='form_minute' value='<?php if(isset($eid)) { echo $starttimem; } ?>'
964 title='<?php xl('Event start time','e'); ?>' readonly/>&nbsp;
965 <select name='form_ampm' title='Note: 12:00 noon is PM, not AM' disabled="disabled">
966 <option value='1'><?php xl('AM','e'); ?></option>
967 <option value='2'<?php if ($startampm == '2') echo " selected" ?>><?php xl('PM','e'); ?></option>
968 </select>
969 </td>
970 </tr>
971 <tr>
972 <td nowrap>
973 <b><?php xl('Patient','e'); ?>:</b>
974 </td>
975 <td nowrap>
976 <input type='text' size='10' id='form_patient' name='form_patient' style='width:100%;' value='<?php echo $patientname ?>' title='Patient' readonly />
977 <input type='hidden' name='form_pid' value='<?php echo $patientid ?>' />
978 </td>
979 <td nowrap>
980 &nbsp;
981 </td>
982 <td nowrap id='tdallday4'><?php xl('duration','e'); ?>
983 </td>
984 <td nowrap id='tdallday5'>
985 <input type='text' size='4' name='form_duration' readonly value='<?php echo $thisduration ?>' title='<?php xl('Event duration in minutes','e'); ?>' />
986 <?php xl('minutes','e'); ?>
988 </td>
989 </tr>
991 <tr>
993 </tr>
996 <tr>
997 <td nowrap>
998 <b><?php xl('Provider','e'); ?>:</b>
999 </td>
1000 <td nowrap>
1001 <?php
1003 // present a list of providers to choose from
1004 // default to the currently logged-in user
1005 echo "<select name='form_provider' onchange='change_provider();' style='width:100%' />";
1006 while ($urow = sqlFetchArray($ures)) {
1007 echo " <option value='" . $urow['id'] . "'";
1008 // if ($urow['id'] == $_SESSION['authUserID']) echo " selected";
1009 if (($urow['id'] == $_GET['userid'])||($urow['id']== $userid)) echo " selected";
1010 echo ">" . $urow['lname'];
1011 if ($urow['fname']) echo ", " . $urow['fname'];
1012 echo "</option>\n";
1014 echo "</select>";
1016 // } //END (CHEMED) IF
1019 </td>
1020 <td nowrap style='font-size:8pt'>
1022 </td>
1023 <td><input type='button' value='<?php xl('Openings','e');?>' onclick='find_available()' /></td>
1024 <td></td>
1025 </tr>
1027 <tr>
1028 <td nowrap>
1029 <b><?php xl('Comments','e'); ?>:</b>
1030 </td>
1031 <td colspan='4' nowrap>
1032 <input type='text' size='40' name='form_comments' style='width:100%' value='<?php echo $hometext ?>' title='<?php xl('Optional information about this event','e');?>' />
1033 </td>
1034 </tr>
1036 <?php
1037 // DOB is important for the clinic, so if it's missing give them a chance
1038 // to enter it right here. We must display or hide this row dynamically
1039 // in case the patient-select popup is used.
1040 $patient_dob = trim($prow['DOB']);
1041 $dobstyle = ($prow && (!$patient_dob || substr($patient_dob, 5) == '00-00')) ?
1042 '' : 'none';
1044 <tr id='dob_row' style='display:none<?php //echo $dobstyle
1045 ?>'>
1046 <td colspan='4' nowrap style='display:none'>
1047 <font color='white'><?php xl('DOB is missing, please enter if possible','e'); ?>:</font></b>
1048 </td>
1049 <td nowrap>
1050 <input type='text' size='10' name='form_dob' id='form_dob' style='display:none' title='<?php xl('yyyy-mm-dd date of birth','e');?>' onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
1051 <img src='../../pic/show_calendar.gif' align='absbottom' width='24' height='22'
1052 id='img_dob' border='0' alt='[?]' style='cursor:pointer;cursor:hand;display:none'
1053 title='<?php xl('Click here to choose a date','e');?>'>
1054 </td>
1055 </tr>
1057 </table>
1060 <input type='button' name='form_save' value='<?php xl('Save','e');?>' onclick="validate()" />
1061 &nbsp;
1062 <input type='button' value='<?php xl('Cancel','e');?>' onclick='parent.$.fn.fancybox.close()' />
1063 </p>
1064 </center>
1065 </form>
1067 <script language='JavaScript'>
1068 <?php if ($eid) { ?>
1069 set_display();
1070 <?php } else { ?>
1071 //set_category();
1072 <?php } ?>
1073 //set_allday();
1074 //set_repeat();
1076 //Calendar.setup({inputField:"form_dob", ifFormat:"%Y-%m-%d", button:"img_dob"});
1077 </script>
1079 </body>
1080 </html>