Portal Two appointments bug fix
[openemr.git] / portal / add_edit_event_user.php
blobb3bb7add1d0d1b1b38c3db27bfbe0cc6b35ff8f0
1 <?php
2 /**
4 * Modified from interface/main/calendar/add_edit_event.php for
5 * the patient portal.
7 * @package OpenEMR
8 * @link http://www.open-emr.org
9 * @author Rod Roark <rod@sunsetsystems.com>
10 * @author Jerry Padgett <sjpadgett@gmail.com>
11 * @author Brady Miller <brady.g.miller@gmail.com>
12 * @copyright Copyright (C) 2005-2006 Rod Roark <rod@sunsetsystems.com>
13 * @copyright Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
14 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
15 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
18 // continue session
19 session_start();
21 //landing page definition -- where to go if something goes wrong
22 $landingpage = "index.php?site=".$_SESSION['site_id'];
25 // kick out if patient not authenticated
26 if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite_two'])) {
27 $pid = $_SESSION['pid'];
28 } else {
29 session_destroy();
30 header('Location: '.$landingpage.'&w');
31 exit;
36 $ignoreAuth = 1;
37 global $ignoreAuth;
39 require_once("../interface/globals.php");
40 require_once("$srcdir/patient.inc");
41 require_once("$srcdir/forms.inc");
43 // Exit if the modify calendar for portal flag is not set-pulled for v5
44 /* if (!($GLOBALS['portal_onsite_appt_modify'])) {
45 echo add_escape_custom( xl('You are not authorized to schedule appointments.'),ENT_NOQUOTES);
46 exit;
47 } */
49 // Things that might be passed by our opener.
51 $eid = $_GET['eid']; // only for existing events
52 $date = $_GET['date']; // this and below only for new events
53 $userid = $_GET['userid'];
54 $default_catid = $_GET['catid'] ? $_GET['catid'] : '5';
55 $patientid = $_GET['patid'];
58 if ($date) {
59 $date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6);
60 } else {
61 $date = date("Y-m-d");
65 $starttimem = '00';
66 if (isset($_GET['starttimem'])) {
67 $starttimem = substr('00' . $_GET['starttimem'], -2);
71 if (isset($_GET['starttimeh'])) {
72 $starttimeh = $_GET['starttimeh'];
73 if (isset($_GET['startampm'])) {
74 if ($_GET['startampm'] == '2' && $starttimeh < 12) {
75 $starttimeh += 12;
78 } else {
79 $starttimeh = date("G");
82 $startampm = '';
84 $info_msg = "";
86 // EVENTS TO FACILITIES (lemonsoftware)
87 //(CHEMED) get facility name
88 // edit event case - if there is no association made, then insert one with the first facility
89 if ($eid) {
90 $selfacil = '';
91 $facility = sqlQuery("SELECT pc_facility, pc_multiple, pc_aid, facility.name
92 FROM openemr_postcalendar_events
93 LEFT JOIN facility ON (openemr_postcalendar_events.pc_facility = facility.id)
94 WHERE pc_eid = ?", array($eid));
95 if (!$facility['pc_facility']) {
96 $qmin = sqlQuery("SELECT facility_id as minId, facility FROM users WHERE id = ?", array($facility['pc_aid']));
97 $min = $qmin['minId'];
98 $min_name = $qmin['facility'];
100 // multiple providers case
101 if ($GLOBALS['select_multi_providers']) {
102 $mul = $facility['pc_multiple'];
103 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = ? WHERE pc_multiple = ?", array($min, $mul));
106 // EOS multiple
108 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = ? WHERE pc_eid = ?", array($min, $eid));
109 $e2f = $min;
110 $e2f_name = $min_name;
111 } else {
112 $e2f = $facility['pc_facility'];
113 $e2f_name = $facility['name'];
117 // EOS E2F
118 // ===========================
121 // If we are saving, then save and close the window.
123 if ($_POST['form_action'] == "save") {
124 //print_r($_POST);
125 //exit();
126 $event_date = fixDate($_POST['form_date']);
128 // Compute start and end time strings to be saved.
129 if ($_POST['form_allday']) {
130 $tmph = 0;
131 $tmpm = 0;
132 $duration = 24 * 60;
133 } else {
134 $tmph = $_POST['form_hour'] + 0;
135 $tmpm = $_POST['form_minute'] + 0;
136 if ($_POST['form_ampm'] == '2' && $tmph < 12) {
137 $tmph += 12;
140 $duration = $_POST['form_duration'];
143 $starttime = "$tmph:$tmpm:00";
145 $tmpm += $duration;
146 while ($tmpm >= 60) {
147 $tmpm -= 60;
148 ++$tmph;
151 $endtime = "$tmph:$tmpm:00";
153 // Useless garbage that we must save.
154 $locationspec = 'a:6:{s:14:"event_location";N;s:13:"event_street1";N;' .
155 's:13:"event_street2";N;s:10:"event_city";N;s:11:"event_state";N;s:12:"event_postal";N;}';
157 // More garbage, but this time 1 character of it is used to save the
158 // repeat type.
159 if ($_POST['form_repeat']) {
160 $recurrspec = 'a:5:{' .
161 's:17:"event_repeat_freq";s:1:"' . $_POST['form_repeat_freq'] . '";' .
162 's:22:"event_repeat_freq_type";s:1:"' . $_POST['form_repeat_type'] . '";' .
163 's:19:"event_repeat_on_num";s:1:"1";' .
164 's:19:"event_repeat_on_day";s:1:"0";' .
165 's:20:"event_repeat_on_freq";s:1:"0";}';
166 } else {
167 $recurrspec = 'a:5:{' .
168 's:17:"event_repeat_freq";N;' .
169 's:22:"event_repeat_freq_type";s:1:"0";' .
170 's:19:"event_repeat_on_num";s:1:"1";' .
171 's:19:"event_repeat_on_day";s:1:"0";' .
172 's:20:"event_repeat_on_freq";s:1:"1";}';
175 //The modification of the start date for events that take place on one day of the week
176 //for example monday, or thursday. We set the start date on the first day of the week
177 //that the event is scheduled. For example if you set the event to repeat on each monday
178 //the start date of the event will be set on the first monday after the day the event is scheduled
179 if ($_POST['form_repeat_type'] == 5) {
180 $exploded_date= explode("-", $event_date);
181 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
182 if ($edate=="Tue") {
183 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
184 } elseif ($edate=="Wed") {
185 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
186 } elseif ($edate=="Thu") {
187 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
188 } elseif ($edate=="Fri") {
189 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
190 } elseif ($edate=="Sat") {
191 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
192 } elseif ($edate=="Sun") {
193 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
195 } elseif ($_POST['form_repeat_type'] == 6) {
196 $exploded_date= explode("-", $event_date);
197 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
198 if ($edate=="Wed") {
199 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
200 } elseif ($edate=="Thu") {
201 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
202 } elseif ($edate=="Fri") {
203 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
204 } elseif ($edate=="Sat") {
205 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
206 } elseif ($edate=="Sun") {
207 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
208 } elseif ($edate=="Mon") {
209 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
211 } elseif ($_POST['form_repeat_type'] == 7) {
212 $exploded_date= explode("-", $event_date);
213 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
214 if ($edate=="Thu") {
215 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
216 } elseif ($edate=="Fri") {
217 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
218 } elseif ($edate=="Sat") {
219 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
220 } elseif ($edate=="Sun") {
221 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
222 } elseif ($edate=="Mon") {
223 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
224 } elseif ($edate=="Tue") {
225 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
227 } elseif ($_POST['form_repeat_type'] == 8) {
228 $exploded_date= explode("-", $event_date);
229 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
230 if ($edate=="Fri") {
231 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
232 } elseif ($edate=="Sat") {
233 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
234 } elseif ($edate=="Sun") {
235 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
236 } elseif ($edate=="Mon") {
237 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
238 } elseif ($edate=="Tue") {
239 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
240 } elseif ($edate=="Wed") {
241 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
243 } elseif ($_POST['form_repeat_type'] == 9) {
244 $exploded_date= explode("-", $event_date);
245 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
246 if ($edate=="Sat") {
247 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
248 } elseif ($edate=="Sun") {
249 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
250 } elseif ($edate=="Mon") {
251 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
252 } elseif ($edate=="Tue") {
253 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
254 } elseif ($edate=="Wed") {
255 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
256 } elseif ($edate=="Thu") {
257 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
259 }//if end
260 /* =======================================================
261 // UPDATE EVENTS
262 ========================================================*/
263 if ($eid) {
264 // what is multiple key around this $eid?
265 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
267 if ($GLOBALS['select_multi_providers'] && $row['pc_multiple']) {
268 /* ==========================================
269 // multi providers BOS
270 ==========================================*/
272 // obtain current list of providers regarding the multiple key
273 $up = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_multiple={$row['pc_multiple']}");
274 while ($current = sqlFetchArray($up)) {
275 $providers_current[] = $current['pc_aid'];
278 $providers_new = $_POST['form_provider_ae'];
280 // this difference means that some providers from current was UNCHECKED
281 // so we must delete this event for them
282 $r1 = array_diff($providers_current, $providers_new);
283 if (count($r1)) {
284 foreach ($r1 as $to_be_removed) {
285 sqlQuery("DELETE FROM openemr_postcalendar_events WHERE pc_aid='$to_be_removed' AND pc_multiple={$row['pc_multiple']}");
289 // this difference means that some providers was added
290 // so we must insert this event for them
291 $r2 = array_diff($providers_new, $providers_current);
292 if (count($r2)) {
293 foreach ($r2 as $to_be_inserted) {
294 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)
295 VALUES ( " .
296 "'" . $_POST['form_category'] . "', " .
297 "'" . $row['pc_multiple'] . "', " .
298 "'" . $to_be_inserted . "', " .
299 "'" . $_POST['form_pid'] . "', " .
300 "'" . add_escape_custom($_POST['form_title']) . "', " .
301 "NOW(), " .
302 "'" . add_escape_custom($_POST['form_comments']) . "', " .
303 "'" . $_SESSION['providerId'] . "', " .
304 "'" . $event_date . "', " .
305 "'" . fixDate($_POST['form_enddate']) . "', " .
306 "'" . ($duration * 60) . "', " .
307 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
308 "'$recurrspec', " .
309 "'$starttime', " .
310 "'$endtime', " .
311 "'" . $_POST['form_allday'] . "', " .
312 "'" . $_POST['form_apptstatus'] . "', " .
313 "'" . $_POST['form_prefcat'] . "', " .
314 "'$locationspec', " .
315 "1, " .
316 "1, " .(int)$_POST['facility']. " )"); // FF stuff
317 } // foreach
318 } //if count
321 // after the two diffs above, we must update for remaining providers
322 // those who are intersected in $providers_current and $providers_new
323 foreach ($_POST['form_provider_ae'] as $provider) {
324 sqlStatement("UPDATE openemr_postcalendar_events SET " .
325 "pc_catid = '" . $_POST['form_category'] . "', " .
326 "pc_pid = '" . $_POST['form_pid'] . "', " .
327 "pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
328 "pc_time = NOW(), " .
329 "pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
330 "pc_informant = '" . $_SESSION['providerId'] . "', " .
331 "pc_eventDate = '" . $event_date . "', " .
332 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
333 "pc_duration = '" . ($duration * 60) . "', " .
334 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
335 "pc_recurrspec = '$recurrspec', " .
336 "pc_startTime = '$starttime', " .
337 "pc_endTime = '$endtime', " .
338 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
339 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
340 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
341 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
342 "WHERE pc_aid = '$provider' AND pc_multiple={$row['pc_multiple']}");
343 } // foreach
345 /* ==========================================
346 // multi providers EOS
347 ==========================================*/
348 } elseif (!$row['pc_multiple']) {
349 if ($GLOBALS['select_multi_providers']) {
350 $prov = $_POST['form_provider_ae'][0];
351 } else {
352 $prov = $_POST['form_provider_ae'];
355 // simple provider case
356 sqlStatement("UPDATE openemr_postcalendar_events SET " .
357 "pc_catid = '" . $_POST['form_category'] . "', " .
358 "pc_aid = '" . $prov . "', " .
359 "pc_pid = '" . $_POST['form_pid'] . "', " .
360 "pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
361 "pc_time = NOW(), " .
362 "pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
363 "pc_informant = '" . $_SESSION['providerId'] . "', " .
364 "pc_eventDate = '" . $event_date . "', " .
365 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
366 "pc_duration = '" . ($duration * 60) . "', " .
367 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
368 "pc_recurrspec = '$recurrspec', " .
369 "pc_startTime = '$starttime', " .
370 "pc_endTime = '$endtime', " .
371 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
372 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
373 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
374 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
375 "WHERE pc_eid = '$eid'");
378 // =======================================
379 // EOS multi providers case
380 // =======================================
382 // EVENTS TO FACILITIES
384 $e2f = (int)$eid;
386 /* =======================================================
387 // INSERT EVENTS
388 ========================================================*/
389 } else {
390 // =======================================
391 // multi providers case
392 // =======================================
394 if (is_array($_POST['form_provider_ae'])) {
395 // obtain the next available unique key to group multiple providers around some event
396 $q = sqlStatement("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
397 $max = sqlFetchArray($q);
398 $new_multiple_value = $max['max'] + 1;
400 foreach ($_POST['form_provider_ae'] as $provider) {
401 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
402 "pc_catid, pc_multiple, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
403 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
404 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
405 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
406 ") VALUES ( " .
407 "'" . $_POST['form_category'] . "', " .
408 "'" . $new_multiple_value . "', " .
409 "'" . $provider . "', " .
410 "'" . $_POST['form_pid'] . "', " .
411 "'" . add_escape_custom($_POST['form_title']) . "', " .
412 "NOW(), " .
413 "'" . add_escape_custom($_POST['form_comments']) . "', " .
414 "'" . $_SESSION['providerId'] . "', " .
415 "'" . $event_date . "', " .
416 "'" . fixDate($_POST['form_enddate']) . "', " .
417 "'" . ($duration * 60) . "', " .
418 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
419 "'$recurrspec', " .
420 "'$starttime', " .
421 "'$endtime', " .
422 "'" . $_POST['form_allday'] . "', " .
423 "'" . $_POST['form_apptstatus'] . "', " .
424 "'" . $_POST['form_prefcat'] . "', " .
425 "'$locationspec', " .
426 "1, " .
427 "1, " .(int)$_POST['facility']. " )"); // FF stuff
428 } // foreach
429 } else {
430 $_POST['form_apptstatus'] = '^';
431 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
432 "pc_catid, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
433 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
434 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
435 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
436 ") VALUES ( " .
437 "'" . $_POST['form_category'] . "', " .
438 "'" . $_POST['form_provider_ae'] . "', " .
439 "'" . $_POST['form_pid'] . "', " .
440 "'" . add_escape_custom($_POST['form_title']) . "', " .
441 "NOW(), " .
442 "'" . add_escape_custom($_POST['form_comments']) . "', " .
443 "'" . $_SESSION['providerId'] . "', " .
444 "'" . $event_date . "', " .
445 "'" . fixDate($_POST['form_enddate']) . "', " .
446 "'" . ($duration * 60) . "', " .
447 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
448 "'$recurrspec', " .
449 "'$starttime', " .
450 "'$endtime', " .
451 "'" . $_POST['form_allday'] . "', " .
452 "'" . $_POST['form_apptstatus'] . "', " .
453 "'" . $_POST['form_prefcat'] . "', " .
454 "'$locationspec', " .
455 "1, " .
456 "1," . (int)$_POST['facility'] . ")"); // FF stuff
457 } // INSERT single
458 } // else - insert
460 // Save new DOB if it's there.
461 $patient_dob = trim($_POST['form_dob']);
462 if ($patient_dob && $_POST['form_pid']) {
463 sqlStatement("UPDATE patient_data SET DOB = '$patient_dob' WHERE " .
464 "pid = '" . $_POST['form_pid'] . "'");
467 // Auto-create a new encounter if appropriate.
470 /* if ($GLOBALS['auto_create_new_encounters'] &&
471 $_POST['form_apptstatus'] == '@' && $event_date == date('Y-m-d'))
474 // We decided not to auto-create blank enconter when user arrives. Todd's decision 18 Jun 2010
475 // Applied by Cassian Lup (cassian.lup@clinicdr.com)
477 if (0) {
478 $tmprow = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
479 "pid = '" . $_POST['form_pid'] . "' AND date = '$event_date 00:00:00'");
480 if ($tmprow['count'] == 0) {
481 $tmprow = sqlQuery("SELECT username, facility, facility_id FROM users WHERE id = '" .
482 $_POST['form_provider_ae'] . "'");
483 $username = $tmprow['username'];
484 $facility = $tmprow['facility'];
485 $facility_id = $tmprow['facility_id'];
486 $conn = $GLOBALS['adodb']['db'];
487 $encounter = $conn->GenID("sequences");
488 addForm(
489 $encounter,
490 "New Patient Encounter",
491 sqlInsert("INSERT INTO form_encounter SET " .
492 "date = '$event_date', " .
493 "onset_date = '$event_date', " .
494 "reason = '" . add_escape_custom($_POST['form_comments']) . "', " .
495 "facility = '$facility', " .
496 "facility_id = '$facility_id', " .
497 "pid = '" . $_POST['form_pid'] . "', " .
498 "encounter = '$encounter'"),
499 "newpatient",
500 $_POST['form_pid'],
501 "1",
502 "NOW()",
503 $username
505 $info_msg .= "New encounter $encounter was created. ";
508 } else if ($_POST['form_action'] == "delete") {
509 // =======================================
510 // multi providers case
511 // =======================================
512 if ($GLOBALS['select_multi_providers']) {
513 // what is multiple key around this $eid?
514 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
515 if ($row['pc_multiple']) {
516 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_multiple = {$row['pc_multiple']}");
517 } else {
518 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = $eid");
521 // =======================================
522 // EOS multi providers case
523 // =======================================
524 } else {
525 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = '$eid'");
529 if ($_POST['form_action'] != "") {
530 // Leave
531 $_SESSION['whereto'] = 'appointmentpanel';
532 header('Location:./home.php#appointmentpanel');
533 exit();
536 // If we get this far then we are displaying the form.
538 $statuses = array(
539 '-' => '',
540 '*' => xl('* Reminder done'),
541 '+' => xl('+ Chart pulled'),
542 'x' => xl('x Cancelled'), // added Apr 2008 by JRM
543 '?' => xl('? No show'),
544 '@' => xl('@ Arrived'),
545 '~' => xl('~ Arrived late'),
546 '!' => xl('! Left w/o visit'),
547 '#' => xl('# Ins/fin issue'),
548 '<' => xl('< In exam room'),
549 '>' => xl('> Checked out'),
550 '$' => xl('$ Coding done'),
551 '^' => xl('^ Pending'),
554 $repeats = 0; // if the event repeats
555 $repeattype = '0';
556 $repeatfreq = '0';
557 $patienttitle = "";
558 $hometext = "";
559 $row = array();
561 // If we are editing an existing event, then get its data.
562 if ($eid) {
563 $row = sqlQuery("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
564 $date = $row['pc_eventDate'];
565 $userid = $row['pc_aid'];
566 $patientid = $row['pc_pid'];
567 $starttimeh = substr($row['pc_startTime'], 0, 2) + 0;
568 $starttimem = substr($row['pc_startTime'], 3, 2);
569 $repeats = $row['pc_recurrtype'];
570 $multiple_value = $row['pc_multiple'];
572 if (preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
573 $repeattype = $matches[1];
576 if (preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
577 $repeatfreq = $matches[1];
580 $hometext = $row['pc_hometext'];
581 if (substr($hometext, 0, 6) == ':text:') {
582 $hometext = substr($hometext, 6);
584 } else {
585 $patientid=$_GET['pid'];
588 // If we have a patient ID, get the name and phone numbers to display.
589 if ($patientid) {
590 $prow = sqlQuery("SELECT lname, fname, phone_home, phone_biz, DOB " .
591 "FROM patient_data WHERE pid = ?", array($patientid));
592 $patientname = $prow['lname'] . ", " . $prow['fname'];
593 if ($prow['phone_home']) {
594 $patienttitle .= " H=" . $prow['phone_home'];
597 if ($prow['phone_biz']) {
598 $patienttitle .= " W=" . $prow['phone_biz'];
602 // Get the providers list.
603 $ures = sqlStatement("SELECT id, username, fname, lname FROM users WHERE " .
604 "authorized != 0 AND active = 1 ORDER BY lname, fname");
606 //-------------------------------------
607 //(CHEMED)
608 //Set default facility for a new event based on the given 'userid'
609 if ($userid) {
610 $pref_facility = sqlFetchArray(sqlStatement("SELECT facility_id, facility FROM users WHERE id = ?", array($userid)));
611 $e2f = $pref_facility['facility_id'];
612 $e2f_name = $pref_facility['facility'];
615 //END of CHEMED -----------------------
617 // Get event categories.
618 $cres = sqlStatement("SELECT pc_catid, pc_catname, pc_recurrtype, pc_duration, pc_end_all_day " .
619 "FROM openemr_postcalendar_categories ORDER BY pc_catname");
621 // Fix up the time format for AM/PM.
622 $startampm = '1';
623 if ($starttimeh >= 12) { // p.m. starts at noon and not 12:01
624 $startampm = '2';
625 if ($starttimeh > 12) {
626 $starttimeh -= 12;
631 <html>
632 <head>
634 <title><?php echo $eid ? "Edit" : "Add New" ?> <?php xl('Event', 'e');?></title>
636 <link href="assets/css/style.css?v=<?php echo $v_js_includes; ?>" rel="stylesheet" type="text/css" />
637 <script type="text/javascript" src="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script>
638 </head>
640 <body class="skin-blue" >
641 <div class="well">
642 <form class="form-inline" method='post' name='theaddform' id='theaddform' action='add_edit_event_user.php?eid=<?php echo $eid ?>'>
643 <input type="hidden" name="form_action" id="form_action" value="">
644 <input type='hidden' name='form_category' id='form_category' value='<?php echo $row['pc_catid'] ? $row['pc_catid'] : '5'; ?>' />
645 <input type='hidden' name='form_apptstatus' id='form_apptstatus' value='<?php echo $row['pc_apptstatus'] ? $row['pc_apptstatus'] : "^" ?>' />
646 <table border='0' width='100%'>
647 <tr>
648 <td width='1%' nowrap>
649 <b><?php xl('Visit', 'e'); ?>: </b>
650 </td>
651 <td nowrap style='padding:0px 5px 5px 0'>
652 <input class="form-control" type="text" id='form_title' name='form_title' value='<?php echo ($row['pc_title'] > "") ? htmlspecialchars($row['pc_title'], ENT_QUOTES) : xlt('Office Visit'); ?>' readonly='readonly'/>
653 </td>
654 <td></td>
655 <td width='1%' nowrap>
656 <b><?php xl('Date', 'e'); ?>:</b>
657 </td>
658 <td colspan='2' nowrap id='tdallday1'>
659 <input class="form-control" type='text' size='10' name='form_date' readonly id='form_date'
660 value='<?php echo (isset($eid) && $eid) ? $row['pc_eventDate'] : $date; ?>' />
661 </td>
662 </tr>
663 <tr>
664 <td nowrap>
665 <b><?php //xl('Title','e'); ?></b>
666 </td>
667 <td style='padding:0px 5px 5px 0' nowrap>
668 <!-- <input class="form-control input-sm" type='text' size='10' name='form_title' readonly value='<?php //echo htmlspecialchars($row['pc_title'],ENT_QUOTES) ?>' title='<?php //xl('Event title','e'); ?>' /> -->
669 </td>
670 <td nowrap>
671 </td>
672 <td width='1%' nowrap id='tdallday2'>
673 <b><?php xl('Time', 'e');?>:</b>
674 </td>
675 <td width='1%' nowrap id='tdallday3'>
676 <input class="form-control inline" type='text' size='2' name='form_hour' value='<?php echo (isset($eid)) ? $starttimeh : ''; ?>'
677 title='<?php xl('Event start time', 'e'); ?>' readonly/> :
678 <input class="form-control inline" type='text' size='2' name='form_minute' value='<?php echo (isset($eid)) ? $starttimem : ''; ?>'
679 title='<?php xl('Event start time', 'e'); ?>' readonly/>&nbsp; <!-- -->
680 <select class="form-control" name='form_ampm' title='Note: 12:00 noon is PM, not AM' readonly >
681 <option value='1'><?php xl('AM', 'e'); ?></option>
682 <option value='2'<?php echo ($startampm == '2') ? " selected" : ""; ?>><?php xl('PM', 'e'); ?></option>
683 </select>
684 </td>
685 </tr>
686 <tr>
687 <td nowrap>
688 <b><?php xl('Patient', 'e'); ?>:</b>
689 </td>
690 <td style='padding:0px 5px 5px 0' nowrap>
691 <input class="form-control" type='text' id='form_patient' name='form_patient' value='<?php echo $patientname ?>' title='Patient' readonly />
692 <input type='hidden' name='form_pid' value='<?php echo $patientid ?>' />
693 </td>
694 <td nowrap>
695 &nbsp;
696 </td>
697 <td nowrap id='tdallday4'><?php xl('Duration', 'e'); ?></td>
698 <td nowrap id='tdallday5'>
699 <input class="form-control input-sm" type='text' size='1' name='form_duration' value='<?php echo $row['pc_duration'] ? ($row['pc_duration']*1/60) : "15" ?>' readonly /><?php echo "&nbsp" . xlt('minutes'); ?>
700 </td>
701 </tr>
702 <tr>
703 </tr>
704 <tr>
705 <td nowrap>
706 <b><?php xl('Provider', 'e'); ?>:</b>
707 </td>
708 <td style='padding:0px 5px 5px 0' nowrap>
709 <select class="form-control" name='form_provider_ae' id='form_provider_ae' onchange='change_provider();'>
710 <?php
711 // present a list of providers to choose from
712 // default to the currently logged-in user
713 while ($urow = sqlFetchArray($ures)) {
714 echo " <option value='" . $urow['id'] . "'";
715 if (($urow['id'] == $_GET['userid'])||($urow['id']== $userid)) {
716 echo " selected";
719 echo ">" . $urow['lname'];
720 if ($urow['fname']) {
721 echo ", " . $urow['fname'];
724 echo "</option>\n";
727 </select>
728 </td>
729 <td nowrap style='font-size:8pt'>
730 </td>
731 <td><input type='button' class='btn btn-danger btn-sm' value='<?php xl('Openings', 'e');?>' onclick='find_available()' /></td>
732 <td></td>
733 </tr>
734 <tr>
735 <td nowrap>
736 <b><?php xl('Reason', 'e'); ?>:</b>
737 </td>
738 <td style='padding:0px 5px 5px 0' colspan='4' nowrap>
739 <input class="form-control" 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');?>' />
740 </td>
741 </tr>
742 </table>
744 <input type='button' name='form_save' class='btn btn-success btn-md' onsubmit='return false' value='<?php xl('Save', 'e');?>' onclick="validate()" />
745 &nbsp;
746 </p>
747 </form>
748 <script>
750 var durations = new Array();
751 <?php
752 // Read the event categories, generate their options list, and get
753 // the default event duration from them if this is a new event.
754 $catoptions = "";
755 $prefcat_options = " <option value='0'>-- None --</option>\n";
756 $thisduration = 0;
757 if ($eid) {
758 $thisduration = $row['pc_alldayevent'] ? 1440 : round($row['pc_duration'] / 60);
761 while ($crow = sqlFetchArray($cres)) {
762 $duration = round($crow['pc_duration'] / 60);
763 if ($crow['pc_end_all_day']) {
764 $duration = 1440;
767 echo " durations[" . $crow['pc_catid'] . "] = $duration\n";
768 // echo " rectypes[" . $crow['pc_catid'] . "] = " . $crow['pc_recurrtype'] . "\n";
769 $catoptions .= " <option value='" . $crow['pc_catid'] . "'";
770 if ($eid) {
771 if ($crow['pc_catid'] == $row['pc_catid']) {
772 $catoptions .= " selected";
774 } else {
775 if ($crow['pc_catid'] == $default_catid) {
776 $catoptions .= " selected";
777 $thisduration = $duration;
781 $catoptions .= ">" . $crow['pc_catname'] . "</option>\n";
783 // This section is to build the list of preferred categories:
784 if ($duration) {
785 $prefcat_options .= " <option value='" . $crow['pc_catid'] . "'";
786 if ($eid) {
787 if ($crow['pc_catid'] == $row['pc_prefcatid']) {
788 $prefcat_options .= " selected";
792 $prefcat_options .= ">" . $crow['pc_catname'] . "</option>\n";
797 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
799 // This is for callback by the find-patient popup.
800 function setpatient(pid, lname, fname, dob) {
801 var f = document.forms.namedItem("theaddform");
802 f.form_patient.value = lname + ', ' + fname;
803 f.form_pid.value = pid;
804 dobstyle = (dob == '' || dob.substr(5, 10) == '00-00') ? '' : 'none';
805 document.getElementById('dob_row').style.display = dobstyle;
807 function change_provider(){
808 var f = document.forms.namedItem("theaddform");
809 f.form_date.value='';
810 f.form_hour.value='';
811 f.form_minute.value='';
813 // This is for callback by the find-patient popup.
814 function unsetpatient() {
815 var f = document.forms.namedItem("theaddform");
816 f.form_patient.value = '';
817 f.form_pid.value = '';
820 // This invokes the find-patient popup.
821 function sel_patient() {
822 dlgopen('find_patient_popup.php', '_blank', 500, 400);
825 // Do whatever is needed when a new event category is selected.
826 // For now this means changing the event title and duration.
827 function set_display() {
828 var f = document.forms.namedItem("theaddform");
829 var si = document.getElementById('form_category');
830 if (si.selectedIndex >= 0) {
831 var catid = si.options[si.selectedIndex].value;
832 var style_apptstatus = document.getElementById('title_apptstatus').style;
833 var style_prefcat = document.getElementById('title_prefcat').style;
834 if (catid == '2') { // In Office
835 style_apptstatus.display = 'none';
836 style_prefcat.display = '';
837 f.form_apptstatus.style.display = 'none';
838 f.form_prefcat.style.display = '';
839 } else {
840 style_prefcat.display = 'none';
841 style_apptstatus.display = '';
842 f.form_prefcat.style.display = 'none';
843 f.form_apptstatus.style.display = '';
848 // Gray out certain fields according to selection of Category DDL
849 function categoryChanged() {
850 var value = '5';
852 document.getElementById("form_patient").disabled=false;
853 //document.getElementById("form_apptstatus").disabled=false;
854 //document.getElementById("form_prefcat").disabled=false;
858 // Do whatever is needed when a new event category is selected.
859 // For now this means changing the event title and duration.
860 function set_category() {
861 var f = document.forms.namedItem("theaddform");
862 var s = f.form_category;
863 if (s.selectedIndex >= 0) {
864 var catid = s.options[s.selectedIndex].value;
865 f.form_title.value = s.options[s.selectedIndex].text;
866 f.form_duration.value = durations[catid];
867 set_display();
871 // Modify some visual attributes when the all-day or timed-event
872 // radio buttons are clicked.
873 function set_allday() {
874 var f = document.forms.namedItem("theaddform");
875 var color1 = '#777777';
876 var color2 = '#777777';
877 var disabled2 = true;
878 /*if (document.getElementById('rballday1').checked) {
879 color1 = '#000000';
881 if (document.getElementById('rballday2').checked) {
882 color2 = '#000000';
883 disabled2 = false;
885 document.getElementById('tdallday1').style.color = color1;
886 document.getElementById('tdallday2').style.color = color2;
887 document.getElementById('tdallday3').style.color = color2;
888 document.getElementById('tdallday4').style.color = color2;
889 document.getElementById('tdallday5').style.color = color2;
890 f.form_hour.disabled = disabled2;
891 f.form_minute.disabled = disabled2;
892 f.form_ampm.disabled = disabled2;
893 f.form_duration.disabled = disabled2;
896 // Modify some visual attributes when the Repeat checkbox is clicked.
897 function set_repeat() {
898 var f = document.forms.namedItem("theaddform");
899 var isdisabled = true;
900 var mycolor = '#777777';
901 var myvisibility = 'hidden';
902 /*if (f.form_repeat.checked) {
903 isdisabled = false;
904 mycolor = '#000000';
905 myvisibility = 'visible';
907 //f.form_repeat_type.disabled = isdisabled;
908 //f.form_repeat_freq.disabled = isdisabled;
909 //f.form_enddate.disabled = isdisabled;
910 document.getElementById('tdrepeat1').style.color = mycolor;
911 document.getElementById('tdrepeat2').style.color = mycolor;
912 document.getElementById('img_enddate').style.visibility = myvisibility;
915 // This is for callback by the find-available popup.
916 function setappt(year,mon,mday,hours,minutes) {
917 var f = document.forms.namedItem("theaddform");
918 f.form_date.value = '' + year + '-' +
919 ('' + (mon + 100)).substring(1) + '-' +
920 ('' + (mday + 100)).substring(1);
921 f.form_ampm.selectedIndex = (hours >= 12) ? 1 : 0;
922 f.form_hour.value = (hours > 12) ? hours - 12 : hours;
923 f.form_minute.value = ('' + (minutes + 100)).substring(1);
926 // Invoke the find-available popup.
927 function find_available() {
928 // when making an appointment for a specific provider
929 var se = document.getElementById('form_provider_ae');
930 <?php if ($userid != 0) { ?>
931 s = se.value;
932 <?php } else {?>
933 s = se.options[se.selectedIndex].value;
934 <?php }?>
935 var formDate = document.getElementById('form_date');
936 var url = 'find_appt_popup_user.php?bypatient&providerid=' + s + '&catid=5' + '&startdate=' + formDate.value;
937 var params = {
938 buttons: [
939 {text: '<?php echo xla('Cancel'); ?>', close: true, style: 'danger btn-sm'}
942 allowResize: true,
943 dialogId: 'apptDialog',
944 type: 'iframe'
946 dlgopen(url, 'apptFind', 'modal-md', 300, '', 'Find Date', params);
949 // Check for errors when the form is submitted.
950 function validate() {
951 var f = document.getElementById('theaddform');
952 if (!f.form_date.value || !f.form_hour.value || !f.form_minute.value) {
953 alert('Please click on "Openings" to select a time.');
954 return false;
957 // in lunch outofoffice reserved vacation
958 // f.form_category.value='2';
959 if (f.form_patient.value=='Click to select' && (!(
960 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'
961 || f.form_category.value=='10'))) {
962 alert('Please select a patient.');
963 return false;
964 } else if (f.form_category.value=='10') {
965 unsetpatient();
967 var form_action = document.getElementById('form_action');
968 form_action.value="save";
969 f.submit();
970 return false;
973 function deleteEvent() {
974 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?")) {
975 var f = document.getElementById('theaddform');
976 var form_action = document.getElementById('form_action');
977 form_action.value="delete";
978 f.submit();
979 return true;
981 return false;
984 <?php if ($eid) { ?>
985 set_display();
986 <?php } ?>
988 $(document).ready(function() {
989 $('.datepicker').datetimepicker({
990 <?php $datetimepicker_timepicker = false; ?>
991 <?php $datetimepicker_showseconds = false; ?>
992 <?php $datetimepicker_formatInput = false; ?>
993 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
994 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
997 </script>
998 </div>
999 </body>
1000 </html>