Highway to PSR2
[openemr.git] / portal / add_edit_event_user.php
blob52d346a5052c0dbd2097bbbdd3bef183c1e5b288
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'];
26 } else {
27 session_destroy();
28 header('Location: '.$landingpage.'&w');
29 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");
63 $starttimem = '00';
64 if (isset($_GET['starttimem'])) {
65 $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;
76 } else {
77 $starttimeh = date("G");
80 $startampm = '';
82 $info_msg = "";
84 // ===========================
85 // EVENTS TO FACILITIES (lemonsoftware)
86 // edit event case - if there is no association made, then insert one with the first facility
87 /*if ( $eid ) {
88 $selfacil = '';
89 $facility = sqlQuery("SELECT pc_facility, pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
90 if ( !$facility['pc_facility'] ) {
91 $qmin = sqlQuery("SELECT MIN(id) as minId FROM facility");
92 $min = $qmin['minId'];
94 // multiple providers case
95 if ( $GLOBALS['select_multi_providers'] ) {
96 $mul = $facility['pc_multiple'];
97 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_multiple = $mul");
99 // EOS multiple
101 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_eid = $eid");
102 $e2f = $minId;
103 } else {
104 $e2f = $facility['pc_facility'];
107 // EOS E2F
108 // ===========================
109 // ===========================
111 // EVENTS TO FACILITIES (lemonsoftware)
112 //(CHEMED) get facility name
113 // edit event case - if there is no association made, then insert one with the first facility
114 if ($eid) {
115 $selfacil = '';
116 $facility = sqlQuery("SELECT pc_facility, pc_multiple, pc_aid, facility.name
117 FROM openemr_postcalendar_events
118 LEFT JOIN facility ON (openemr_postcalendar_events.pc_facility = facility.id)
119 WHERE pc_eid = $eid");
120 if (!$facility['pc_facility']) {
121 $qmin = sqlQuery("SELECT facility_id as minId, facility FROM users WHERE id = ".$facility['pc_aid']);
122 $min = $qmin['minId'];
123 $min_name = $qmin['facility'];
125 // multiple providers case
126 if ($GLOBALS['select_multi_providers']) {
127 $mul = $facility['pc_multiple'];
128 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_multiple = $mul");
131 // EOS multiple
133 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_eid = $eid");
134 $e2f = $min;
135 $e2f_name = $min_name;
136 } else {
137 $e2f = $facility['pc_facility'];
138 $e2f_name = $facility['name'];
142 // EOS E2F
143 // ===========================
146 // If we are saving, then save and close the window.
148 if ($_POST['form_action'] == "save") {
149 //print_r($_POST);
150 //exit();
151 $event_date = fixDate($_POST['form_date']);
153 // Compute start and end time strings to be saved.
154 if ($_POST['form_allday']) {
155 $tmph = 0;
156 $tmpm = 0;
157 $duration = 24 * 60;
158 } else {
159 $tmph = $_POST['form_hour'] + 0;
160 $tmpm = $_POST['form_minute'] + 0;
161 if ($_POST['form_ampm'] == '2' && $tmph < 12) {
162 $tmph += 12;
165 $duration = $_POST['form_duration'];
168 $starttime = "$tmph:$tmpm:00";
170 $tmpm += $duration;
171 while ($tmpm >= 60) {
172 $tmpm -= 60;
173 ++$tmph;
176 $endtime = "$tmph:$tmpm:00";
178 // Useless garbage that we must save.
179 $locationspec = 'a:6:{s:14:"event_location";N;s:13:"event_street1";N;' .
180 's:13:"event_street2";N;s:10:"event_city";N;s:11:"event_state";N;s:12:"event_postal";N;}';
182 // More garbage, but this time 1 character of it is used to save the
183 // repeat type.
184 if ($_POST['form_repeat']) {
185 $recurrspec = 'a:5:{' .
186 's:17:"event_repeat_freq";s:1:"' . $_POST['form_repeat_freq'] . '";' .
187 's:22:"event_repeat_freq_type";s:1:"' . $_POST['form_repeat_type'] . '";' .
188 's:19:"event_repeat_on_num";s:1:"1";' .
189 's:19:"event_repeat_on_day";s:1:"0";' .
190 's:20:"event_repeat_on_freq";s:1:"0";}';
191 } else {
192 $recurrspec = 'a:5:{' .
193 's:17:"event_repeat_freq";N;' .
194 's:22:"event_repeat_freq_type";s:1:"0";' .
195 's:19:"event_repeat_on_num";s:1:"1";' .
196 's:19:"event_repeat_on_day";s:1:"0";' .
197 's:20:"event_repeat_on_freq";s:1:"1";}';
200 //The modification of the start date for events that take place on one day of the week
201 //for example monday, or thursday. We set the start date on the first day of the week
202 //that the event is scheduled. For example if you set the event to repeat on each monday
203 //the start date of the event will be set on the first monday after the day the event is scheduled
204 if ($_POST['form_repeat_type'] == 5) {
205 $exploded_date= explode("-", $event_date);
206 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
207 if ($edate=="Tue") {
208 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
209 } elseif ($edate=="Wed") {
210 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
211 } elseif ($edate=="Thu") {
212 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
213 } elseif ($edate=="Fri") {
214 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
215 } elseif ($edate=="Sat") {
216 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
217 } elseif ($edate=="Sun") {
218 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
220 } elseif ($_POST['form_repeat_type'] == 6) {
221 $exploded_date= explode("-", $event_date);
222 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
223 if ($edate=="Wed") {
224 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
225 } elseif ($edate=="Thu") {
226 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
227 } elseif ($edate=="Fri") {
228 $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]));
231 } elseif ($edate=="Sun") {
232 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
233 } elseif ($edate=="Mon") {
234 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
236 } elseif ($_POST['form_repeat_type'] == 7) {
237 $exploded_date= explode("-", $event_date);
238 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
239 if ($edate=="Thu") {
240 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
241 } elseif ($edate=="Fri") {
242 $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]));
245 } elseif ($edate=="Sun") {
246 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
247 } elseif ($edate=="Mon") {
248 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
249 } elseif ($edate=="Tue") {
250 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
252 } elseif ($_POST['form_repeat_type'] == 8) {
253 $exploded_date= explode("-", $event_date);
254 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
255 if ($edate=="Fri") {
256 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
257 } elseif ($edate=="Sat") {
258 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
259 } elseif ($edate=="Sun") {
260 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
261 } elseif ($edate=="Mon") {
262 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
263 } elseif ($edate=="Tue") {
264 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
265 } elseif ($edate=="Wed") {
266 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
268 } elseif ($_POST['form_repeat_type'] == 9) {
269 $exploded_date= explode("-", $event_date);
270 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
271 if ($edate=="Sat") {
272 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
273 } elseif ($edate=="Sun") {
274 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
275 } elseif ($edate=="Mon") {
276 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
277 } elseif ($edate=="Tue") {
278 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
279 } elseif ($edate=="Wed") {
280 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
281 } elseif ($edate=="Thu") {
282 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
284 }//if end
285 /* =======================================================
286 // UPDATE EVENTS
287 ========================================================*/
288 if ($eid) {
289 // what is multiple key around this $eid?
290 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
292 if ($GLOBALS['select_multi_providers'] && $row['pc_multiple']) {
293 /* ==========================================
294 // multi providers BOS
295 ==========================================*/
297 // obtain current list of providers regarding the multiple key
298 $up = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_multiple={$row['pc_multiple']}");
299 while ($current = sqlFetchArray($up)) {
300 $providers_current[] = $current['pc_aid'];
303 $providers_new = $_POST['form_provider_ae'];
305 // this difference means that some providers from current was UNCHECKED
306 // so we must delete this event for them
307 $r1 = array_diff($providers_current, $providers_new);
308 if (count($r1)) {
309 foreach ($r1 as $to_be_removed) {
310 sqlQuery("DELETE FROM openemr_postcalendar_events WHERE pc_aid='$to_be_removed' AND pc_multiple={$row['pc_multiple']}");
314 // this difference means that some providers was added
315 // so we must insert this event for them
316 $r2 = array_diff($providers_new, $providers_current);
317 if (count($r2)) {
318 foreach ($r2 as $to_be_inserted) {
319 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)
320 VALUES ( " .
321 "'" . $_POST['form_category'] . "', " .
322 "'" . $row['pc_multiple'] . "', " .
323 "'" . $to_be_inserted . "', " .
324 "'" . $_POST['form_pid'] . "', " .
325 "'" . add_escape_custom($_POST['form_title']) . "', " .
326 "NOW(), " .
327 "'" . add_escape_custom($_POST['form_comments']) . "', " .
328 "'" . $_SESSION['providerId'] . "', " .
329 "'" . $event_date . "', " .
330 "'" . fixDate($_POST['form_enddate']) . "', " .
331 "'" . ($duration * 60) . "', " .
332 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
333 "'$recurrspec', " .
334 "'$starttime', " .
335 "'$endtime', " .
336 "'" . $_POST['form_allday'] . "', " .
337 "'" . $_POST['form_apptstatus'] . "', " .
338 "'" . $_POST['form_prefcat'] . "', " .
339 "'$locationspec', " .
340 "1, " .
341 "1, " .(int)$_POST['facility']. " )"); // FF stuff
342 } // foreach
343 } //if count
346 // after the two diffs above, we must update for remaining providers
347 // those who are intersected in $providers_current and $providers_new
348 foreach ($_POST['form_provider_ae'] as $provider) {
349 sqlStatement("UPDATE openemr_postcalendar_events SET " .
350 "pc_catid = '" . $_POST['form_category'] . "', " .
351 "pc_pid = '" . $_POST['form_pid'] . "', " .
352 "pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
353 "pc_time = NOW(), " .
354 "pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
355 "pc_informant = '" . $_SESSION['providerId'] . "', " .
356 "pc_eventDate = '" . $event_date . "', " .
357 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
358 "pc_duration = '" . ($duration * 60) . "', " .
359 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
360 "pc_recurrspec = '$recurrspec', " .
361 "pc_startTime = '$starttime', " .
362 "pc_endTime = '$endtime', " .
363 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
364 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
365 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
366 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
367 "WHERE pc_aid = '$provider' AND pc_multiple={$row['pc_multiple']}");
368 } // foreach
370 /* ==========================================
371 // multi providers EOS
372 ==========================================*/
373 } elseif (!$row['pc_multiple']) {
374 if ($GLOBALS['select_multi_providers']) {
375 $prov = $_POST['form_provider_ae'][0];
376 } else {
377 $prov = $_POST['form_provider_ae'];
380 // simple provider case
381 sqlStatement("UPDATE openemr_postcalendar_events SET " .
382 "pc_catid = '" . $_POST['form_category'] . "', " .
383 "pc_aid = '" . $prov . "', " .
384 "pc_pid = '" . $_POST['form_pid'] . "', " .
385 "pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
386 "pc_time = NOW(), " .
387 "pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
388 "pc_informant = '" . $_SESSION['providerId'] . "', " .
389 "pc_eventDate = '" . $event_date . "', " .
390 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
391 "pc_duration = '" . ($duration * 60) . "', " .
392 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
393 "pc_recurrspec = '$recurrspec', " .
394 "pc_startTime = '$starttime', " .
395 "pc_endTime = '$endtime', " .
396 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
397 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
398 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
399 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
400 "WHERE pc_eid = '$eid'");
403 // =======================================
404 // EOS multi providers case
405 // =======================================
407 // EVENTS TO FACILITIES
409 $e2f = (int)$eid;
411 /* =======================================================
412 // INSERT EVENTS
413 ========================================================*/
414 } else {
415 // =======================================
416 // multi providers case
417 // =======================================
419 if (is_array($_POST['form_provider_ae'])) {
420 // obtain the next available unique key to group multiple providers around some event
421 $q = sqlStatement("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
422 $max = sqlFetchArray($q);
423 $new_multiple_value = $max['max'] + 1;
425 foreach ($_POST['form_provider_ae'] as $provider) {
426 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
427 "pc_catid, pc_multiple, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
428 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
429 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
430 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
431 ") VALUES ( " .
432 "'" . $_POST['form_category'] . "', " .
433 "'" . $new_multiple_value . "', " .
434 "'" . $provider . "', " .
435 "'" . $_POST['form_pid'] . "', " .
436 "'" . add_escape_custom($_POST['form_title']) . "', " .
437 "NOW(), " .
438 "'" . add_escape_custom($_POST['form_comments']) . "', " .
439 "'" . $_SESSION['providerId'] . "', " .
440 "'" . $event_date . "', " .
441 "'" . fixDate($_POST['form_enddate']) . "', " .
442 "'" . ($duration * 60) . "', " .
443 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
444 "'$recurrspec', " .
445 "'$starttime', " .
446 "'$endtime', " .
447 "'" . $_POST['form_allday'] . "', " .
448 "'" . $_POST['form_apptstatus'] . "', " .
449 "'" . $_POST['form_prefcat'] . "', " .
450 "'$locationspec', " .
451 "1, " .
452 "1, " .(int)$_POST['facility']. " )"); // FF stuff
453 } // foreach
454 } else {
455 $_POST['form_apptstatus'] = '^';
456 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
457 "pc_catid, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
458 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
459 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
460 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
461 ") VALUES ( " .
462 "'" . $_POST['form_category'] . "', " .
463 "'" . $_POST['form_provider_ae'] . "', " .
464 "'" . $_POST['form_pid'] . "', " .
465 "'" . add_escape_custom($_POST['form_title']) . "', " .
466 "NOW(), " .
467 "'" . add_escape_custom($_POST['form_comments']) . "', " .
468 "'" . $_SESSION['providerId'] . "', " .
469 "'" . $event_date . "', " .
470 "'" . fixDate($_POST['form_enddate']) . "', " .
471 "'" . ($duration * 60) . "', " .
472 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
473 "'$recurrspec', " .
474 "'$starttime', " .
475 "'$endtime', " .
476 "'" . $_POST['form_allday'] . "', " .
477 "'" . $_POST['form_apptstatus'] . "', " .
478 "'" . $_POST['form_prefcat'] . "', " .
479 "'$locationspec', " .
480 "1, " .
481 "1," .(int)$_POST['facility']. ")"); // FF stuff
482 } // INSERT single
483 } // else - insert
485 // Save new DOB if it's there.
486 $patient_dob = trim($_POST['form_dob']);
487 if ($patient_dob && $_POST['form_pid']) {
488 sqlStatement("UPDATE patient_data SET DOB = '$patient_dob' WHERE " .
489 "pid = '" . $_POST['form_pid'] . "'");
492 // Auto-create a new encounter if appropriate.
495 /* if ($GLOBALS['auto_create_new_encounters'] &&
496 $_POST['form_apptstatus'] == '@' && $event_date == date('Y-m-d'))
499 // We decided not to auto-create blank enconter when user arrives. Todd's decision 18 Jun 2010
500 // Applied by Cassian Lup (cassian.lup@clinicdr.com)
502 if (0) {
503 $tmprow = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
504 "pid = '" . $_POST['form_pid'] . "' AND date = '$event_date 00:00:00'");
505 if ($tmprow['count'] == 0) {
506 $tmprow = sqlQuery("SELECT username, facility, facility_id FROM users WHERE id = '" .
507 $_POST['form_provider_ae'] . "'");
508 $username = $tmprow['username'];
509 $facility = $tmprow['facility'];
510 $facility_id = $tmprow['facility_id'];
511 $conn = $GLOBALS['adodb']['db'];
512 $encounter = $conn->GenID("sequences");
513 addForm(
514 $encounter,
515 "New Patient Encounter",
516 sqlInsert("INSERT INTO form_encounter SET " .
517 "date = '$event_date', " .
518 "onset_date = '$event_date', " .
519 "reason = '" . add_escape_custom($_POST['form_comments']) . "', " .
520 "facility = '$facility', " .
521 "facility_id = '$facility_id', " .
522 "pid = '" . $_POST['form_pid'] . "', " .
523 "encounter = '$encounter'"),
524 "newpatient",
525 $_POST['form_pid'],
526 "1",
527 "NOW()",
528 $username
530 $info_msg .= "New encounter $encounter was created. ";
533 } else if ($_POST['form_action'] == "delete") {
534 // =======================================
535 // multi providers case
536 // =======================================
537 if ($GLOBALS['select_multi_providers']) {
538 // what is multiple key around this $eid?
539 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
540 if ($row['pc_multiple']) {
541 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_multiple = {$row['pc_multiple']}");
542 } else {
543 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = $eid");
546 // =======================================
547 // EOS multi providers case
548 // =======================================
549 } else {
550 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = '$eid'");
554 if ($_POST['form_action'] != "") {
555 // Leave
556 $_SESSION['whereto'] = 'appointmentpanel';
557 header('Location:./home.php');
558 exit();
561 // If we get this far then we are displaying the form.
563 $statuses = array(
564 '-' => '',
565 '*' => xl('* Reminder done'),
566 '+' => xl('+ Chart pulled'),
567 'x' => xl('x Cancelled'), // added Apr 2008 by JRM
568 '?' => xl('? No show'),
569 '@' => xl('@ Arrived'),
570 '~' => xl('~ Arrived late'),
571 '!' => xl('! Left w/o visit'),
572 '#' => xl('# Ins/fin issue'),
573 '<' => xl('< In exam room'),
574 '>' => xl('> Checked out'),
575 '$' => xl('$ Coding done'),
576 '^' => xl('^ Pending'),
579 $repeats = 0; // if the event repeats
580 $repeattype = '0';
581 $repeatfreq = '0';
582 $patienttitle = "";
583 $hometext = "";
584 $row = array();
586 // If we are editing an existing event, then get its data.
587 if ($eid) {
588 $row = sqlQuery("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = $eid");
589 $date = $row['pc_eventDate'];
590 $userid = $row['pc_aid'];
591 $patientid = $row['pc_pid'];
592 $starttimeh = substr($row['pc_startTime'], 0, 2) + 0;
593 $starttimem = substr($row['pc_startTime'], 3, 2);
594 $repeats = $row['pc_recurrtype'];
595 $multiple_value = $row['pc_multiple'];
597 if (preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
598 $repeattype = $matches[1];
601 if (preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
602 $repeatfreq = $matches[1];
605 $hometext = $row['pc_hometext'];
606 if (substr($hometext, 0, 6) == ':text:') {
607 $hometext = substr($hometext, 6);
609 } else {
610 $patientid=$_GET['pid'];
613 // If we have a patient ID, get the name and phone numbers to display.
614 if ($patientid) {
615 $prow = sqlQuery("SELECT lname, fname, phone_home, phone_biz, DOB " .
616 "FROM patient_data WHERE pid = '" . $patientid . "'");
617 $patientname = $prow['lname'] . ", " . $prow['fname'];
618 if ($prow['phone_home']) {
619 $patienttitle .= " H=" . $prow['phone_home'];
622 if ($prow['phone_biz']) {
623 $patienttitle .= " W=" . $prow['phone_biz'];
627 // Get the providers list.
628 $ures = sqlStatement("SELECT id, username, fname, lname FROM users WHERE " .
629 "authorized != 0 AND active = 1 ORDER BY lname, fname");
631 //-------------------------------------
632 //(CHEMED)
633 //Set default facility for a new event based on the given 'userid'
634 if ($userid) {
635 $pref_facility = sqlFetchArray(sqlStatement("SELECT facility_id, facility FROM users WHERE id = $userid"));
636 $e2f = $pref_facility['facility_id'];
637 $e2f_name = $pref_facility['facility'];
640 //END of CHEMED -----------------------
642 // Get event categories.
643 $cres = sqlStatement("SELECT pc_catid, pc_catname, pc_recurrtype, pc_duration, pc_end_all_day " .
644 "FROM openemr_postcalendar_categories ORDER BY pc_catname");
646 // Fix up the time format for AM/PM.
647 $startampm = '1';
648 if ($starttimeh >= 12) { // p.m. starts at noon and not 12:01
649 $startampm = '2';
650 if ($starttimeh > 12) {
651 $starttimeh -= 12;
656 <html>
657 <head>
658 <?php //html_header_show(); ?>
659 <title><?php echo $eid ? "Edit" : "Add New" ?> <?php xl('Event', 'e');?></title>
660 <link href="assets/css/style.css?v=<?php echo $v_js_includes; ?>" rel="stylesheet" type="text/css" />
661 <style type="text/css">@import url(../library/dynarch_calendar.css);</style>
662 <script type="text/javascript" src="../library/topdialog.js?v=<?php echo $v_js_includes; ?>"></script>
663 <script type="text/javascript" src="../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
664 <script type="text/javascript" src="../library/textformat.js?v=<?php echo $v_js_includes; ?>"></script>
665 <script type="text/javascript" src="../library/dynarch_calendar.js"></script>
666 <script type="text/javascript" src="../library/dynarch_calendar_en.js"></script>
667 <script type="text/javascript" src="../library/dynarch_calendar_setup.js"></script>
669 </head>
671 <body class="body_top" >
673 <form method='post' name='theaddform' id='theaddform' action='add_edit_event_user.php?eid=<?php echo $eid ?>'>
674 <input type="hidden" name="form_action" id="form_action" value="">
675 <input type='hidden' name='form_category' id='form_category' value='<?php echo $row['pc_catid'] ? $row['pc_catid'] : '5'; ?>' />
676 <input type='hidden' name='form_apptstatus' id='form_apptstatus' value='<?php echo $row['pc_apptstatus'] ? $row['pc_apptstatus'] : "^" ?>' />
677 <table border='0' width='100%'>
678 <tr>
679 <td width='1%' nowrap>
680 <b><?php xl('Visit', 'e'); ?>: </b>
681 </td>
682 <td nowrap style='padding:0px 5px 5px 0'>
683 <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'/>
684 </td>
685 <td></td>
686 <td width='1%' nowrap>
687 <b><?php xl('Date', 'e'); ?>:</b>
688 </td>
689 <td colspan='2' nowrap id='tdallday1'>
690 <input class="form-control input-md" type='text' size='10' name='form_date' readonly id='form_date'
691 value='<?php if (isset($eid)) {
692 echo $eid ? $row['pc_eventDate'] : $date;
693 } ?>'
694 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
695 </td>
696 </tr>
697 <tr>
698 <td nowrap>
699 <b><?php //xl('Title','e'); ?></b>
700 </td>
701 <td style='padding:0px 5px 5px 0' nowrap>
702 <!-- <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'); ?>' /> -->
703 </td>
704 <td nowrap>
705 </td>
706 <td width='1%' nowrap id='tdallday2'>
707 <b><?php xl('Time', 'e');?>:</b>
708 </td>
709 <td width='1%' nowrap id='tdallday3'>
710 <input class="form-control inline" type='text' size='2' name='form_hour' value='<?php if (isset($eid)) {
711 echo $starttimeh;
712 } ?>'
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)) {
715 echo $starttimem;
716 } ?>'
717 title='<?php xl('Event start time', 'e'); ?>' readonly/>&nbsp; <!-- -->
718 <select class="form-control" name='form_ampm' title='Note: 12:00 noon is PM, not AM' readonly >
719 <option value='1'><?php xl('AM', 'e'); ?></option>
720 <option value='2'<?php if ($startampm == '2') {
721 echo " selected";
722 } ?>><?php xl('PM', 'e'); ?></option>
723 </select>
724 </td>
725 </tr>
726 <tr>
727 <td nowrap>
728 <b><?php xl('Patient', 'e'); ?>:</b>
729 </td>
730 <td style='padding:0px 5px 5px 0' nowrap>
731 <input class="form-control input-md" type='text' size='10' id='form_patient' name='form_patient' value='<?php echo $patientname ?>' title='Patient' readonly />
732 <input type='hidden' name='form_pid' value='<?php echo $patientid ?>' />
733 </td>
734 <td nowrap>
735 &nbsp;
736 </td>
737 <td nowrap id='tdallday4'><?php xl('Duration', 'e'); ?></td>
738 <td nowrap id='tdallday5'>
739 <!-- --> <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'); ?>
740 </td>
741 </tr>
742 <tr>
743 </tr>
744 <tr>
745 <td nowrap>
746 <b><?php xl('Provider', 'e'); ?>:</b>
747 </td>
748 <td style='padding:0px 5px 5px 0' nowrap>
749 <select class="form-control input-md" name='form_provider_ae' id='form_provider_ae' onchange='change_provider();'>
750 <?php
751 // present a list of providers to choose from
752 // default to the currently logged-in user
753 while ($urow = sqlFetchArray($ures)) {
754 echo " <option value='" . $urow['id'] . "'";
755 if (($urow['id'] == $_GET['userid'])||($urow['id']== $userid)) {
756 echo " selected";
759 echo ">" . $urow['lname'];
760 if ($urow['fname']) {
761 echo ", " . $urow['fname'];
764 echo "</option>\n";
767 </select>
768 </td>
769 <td nowrap style='font-size:8pt'>
770 </td>
771 <td><input type='button' class='btn btn-danger btn-sm' value='<?php xl('Openings', 'e');?>' onclick='find_available()' /></td>
772 <td></td>
773 </tr>
774 <tr>
775 <td nowrap>
776 <b><?php xl('Reason', 'e'); ?>:</b>
777 </td>
778 <td style='padding:0px 5px 5px 0' colspan='4' nowrap>
779 <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');?>' />
780 </td>
781 </tr>
782 </table>
784 <input type='button' name='form_save' class='btn btn-success btn-md' onsubmit='return false' value='<?php xl('Save', 'e');?>' onclick="validate()" />
785 &nbsp;
786 </p>
787 </form>
788 <script>
789 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
791 var durations = new Array();
792 // var rectypes = new Array();
793 <?php
794 // Read the event categories, generate their options list, and get
795 // the default event duration from them if this is a new event.
796 $catoptions = "";
797 $prefcat_options = " <option value='0'>-- None --</option>\n";
798 $thisduration = 0;
799 if ($eid) {
800 $thisduration = $row['pc_alldayevent'] ? 1440 : round($row['pc_duration'] / 60);
803 while ($crow = sqlFetchArray($cres)) {
804 $duration = round($crow['pc_duration'] / 60);
805 if ($crow['pc_end_all_day']) {
806 $duration = 1440;
809 echo " durations[" . $crow['pc_catid'] . "] = $duration\n";
810 // echo " rectypes[" . $crow['pc_catid'] . "] = " . $crow['pc_recurrtype'] . "\n";
811 $catoptions .= " <option value='" . $crow['pc_catid'] . "'";
812 if ($eid) {
813 if ($crow['pc_catid'] == $row['pc_catid']) {
814 $catoptions .= " selected";
816 } else {
817 if ($crow['pc_catid'] == $default_catid) {
818 $catoptions .= " selected";
819 $thisduration = $duration;
823 $catoptions .= ">" . $crow['pc_catname'] . "</option>\n";
825 // This section is to build the list of preferred categories:
826 if ($duration) {
827 $prefcat_options .= " <option value='" . $crow['pc_catid'] . "'";
828 if ($eid) {
829 if ($crow['pc_catid'] == $row['pc_prefcatid']) {
830 $prefcat_options .= " selected";
834 $prefcat_options .= ">" . $crow['pc_catname'] . "</option>\n";
839 <?php // require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
841 // This is for callback by the find-patient popup.
842 function setpatient(pid, lname, fname, dob) {
843 var f = document.forms.namedItem("theaddform");
844 f.form_patient.value = lname + ', ' + fname;
845 f.form_pid.value = pid;
846 dobstyle = (dob == '' || dob.substr(5, 10) == '00-00') ? '' : 'none';
847 document.getElementById('dob_row').style.display = dobstyle;
849 function change_provider(){
850 var f = document.forms.namedItem("theaddform");
851 f.form_date.value='';
852 f.form_hour.value='';
853 f.form_minute.value='';
855 // This is for callback by the find-patient popup.
856 function unsetpatient() {
857 var f = document.forms.namedItem("theaddform");
858 f.form_patient.value = '';
859 f.form_pid.value = '';
862 // This invokes the find-patient popup.
863 function sel_patient() {
864 dlgopen('find_patient_popup.php', '_blank', 500, 400);
867 // Do whatever is needed when a new event category is selected.
868 // For now this means changing the event title and duration.
869 function set_display() {
870 var f = document.forms.namedItem("theaddform");
871 var si = document.getElementById('form_category');
872 if (si.selectedIndex >= 0) {
873 var catid = si.options[si.selectedIndex].value;
874 var style_apptstatus = document.getElementById('title_apptstatus').style;
875 var style_prefcat = document.getElementById('title_prefcat').style;
876 if (catid == '2') { // In Office
877 style_apptstatus.display = 'none';
878 style_prefcat.display = '';
879 f.form_apptstatus.style.display = 'none';
880 f.form_prefcat.style.display = '';
881 } else {
882 style_prefcat.display = 'none';
883 style_apptstatus.display = '';
884 f.form_prefcat.style.display = 'none';
885 f.form_apptstatus.style.display = '';
890 // Gray out certain fields according to selection of Category DDL
891 function categoryChanged() {
892 var value = '5';
894 document.getElementById("form_patient").disabled=false;
895 //document.getElementById("form_apptstatus").disabled=false;
896 //document.getElementById("form_prefcat").disabled=false;
900 // Do whatever is needed when a new event category is selected.
901 // For now this means changing the event title and duration.
902 function set_category() {
903 var f = document.forms.namedItem("theaddform");
904 var s = f.form_category;
905 if (s.selectedIndex >= 0) {
906 var catid = s.options[s.selectedIndex].value;
907 f.form_title.value = s.options[s.selectedIndex].text;
908 f.form_duration.value = durations[catid];
909 set_display();
913 // Modify some visual attributes when the all-day or timed-event
914 // radio buttons are clicked.
915 function set_allday() {
916 var f = document.forms.namedItem("theaddform");
917 var color1 = '#777777';
918 var color2 = '#777777';
919 var disabled2 = true;
920 /*if (document.getElementById('rballday1').checked) {
921 color1 = '#000000';
923 if (document.getElementById('rballday2').checked) {
924 color2 = '#000000';
925 disabled2 = false;
927 document.getElementById('tdallday1').style.color = color1;
928 document.getElementById('tdallday2').style.color = color2;
929 document.getElementById('tdallday3').style.color = color2;
930 document.getElementById('tdallday4').style.color = color2;
931 document.getElementById('tdallday5').style.color = color2;
932 f.form_hour.disabled = disabled2;
933 f.form_minute.disabled = disabled2;
934 f.form_ampm.disabled = disabled2;
935 f.form_duration.disabled = disabled2;
938 // Modify some visual attributes when the Repeat checkbox is clicked.
939 function set_repeat() {
940 var f = document.forms.namedItem("theaddform");
941 var isdisabled = true;
942 var mycolor = '#777777';
943 var myvisibility = 'hidden';
944 /*if (f.form_repeat.checked) {
945 isdisabled = false;
946 mycolor = '#000000';
947 myvisibility = 'visible';
949 //f.form_repeat_type.disabled = isdisabled;
950 //f.form_repeat_freq.disabled = isdisabled;
951 //f.form_enddate.disabled = isdisabled;
952 document.getElementById('tdrepeat1').style.color = mycolor;
953 document.getElementById('tdrepeat2').style.color = mycolor;
954 document.getElementById('img_enddate').style.visibility = myvisibility;
957 // This is for callback by the find-available popup.
958 function setappt(year,mon,mday,hours,minutes) {
959 var f = document.forms.namedItem("theaddform");
960 f.form_date.value = '' + year + '-' +
961 ('' + (mon + 100)).substring(1) + '-' +
962 ('' + (mday + 100)).substring(1);
963 f.form_ampm.selectedIndex = (hours >= 12) ? 1 : 0;
964 f.form_hour.value = (hours > 12) ? hours - 12 : hours;
965 f.form_minute.value = ('' + (minutes + 100)).substring(1);
968 // Invoke the find-available popup.
969 function find_available() {
971 // (CHEMED) Conditional value selection, because there is no <select> element
972 // when making an appointment for a specific provider
973 var se = document.getElementById('form_provider_ae');
974 <?php if ($userid != 0) { ?>
975 s = se.value;
976 <?php } else {?>
977 s = se.options[se.selectedIndex].value;
978 <?php }?>
979 var formDate = document.getElementById('form_date');
980 window.open('find_appt_popup_user.php?bypatient&providerid=' + s +
981 '&catid=5' +
982 '&startdate=' + formDate.value, '_blank', "width=900,height=800");
985 // Check for errors when the form is submitted.
986 function validate() {
987 var f = document.getElementById('theaddform');
988 if (!f.form_date.value || !f.form_hour.value || !f.form_minute.value) {
989 alert('Please click on "Openings" to select a time.');
990 return false;
993 // in lunch outofoffice reserved vacation
994 // f.form_category.value='2';
995 if (f.form_patient.value=='Click to select' && (!(
996 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'
997 || f.form_category.value=='10'))) {
998 alert('Please select a patient.');
999 return false;
1000 } else if (f.form_category.value=='10') {
1001 unsetpatient();
1003 var form_action = document.getElementById('form_action');
1004 form_action.value="save";
1005 f.submit();
1006 return false;
1009 function deleteEvent() {
1010 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?")) {
1011 var f = document.getElementById('theaddform');
1012 var form_action = document.getElementById('form_action');
1013 form_action.value="delete";
1014 f.submit();
1015 return true;
1017 return false;
1020 </script>
1022 <script>
1023 <?php if ($eid) { ?>
1024 set_display();
1025 <?php } else { ?>
1026 //set_category();
1027 <?php } ?>
1028 //set_allday();
1029 //set_repeat();
1031 //Calendar.setup({inputField:"form_dob", ifFormat:"%Y-%m-%d", button:"img_dob"});
1032 </script>
1034 </body>
1035 </html>