Merge pull request #956 to fix therapy groups permissions
[openemr.git] / patients / add_edit_event_user.php
blobcd0b9e00ee1c2ce24a86100a442ff6e0940ec3aa
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 // continue session
24 session_start();
26 //landing page definition -- where to go if something goes wrong
27 $landingpage = "index.php?site=".$_SESSION['site_id'];
30 // kick out if patient not authenticated
31 if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
32 $pid = $_SESSION['pid'];
33 } else {
34 session_destroy();
35 header('Location: '.$landingpage.'&w');
36 exit;
41 $ignoreAuth = 1;
42 global $ignoreAuth;
44 include_once("../interface/globals.php");
45 include_once("$srcdir/patient.inc");
46 include_once("$srcdir/forms.inc");
48 // Exit if the modify calendar for portal flag is not set
49 if (!($GLOBALS['portal_onsite_appt_modify'])) {
50 echo htmlspecialchars(xl('You are not authorized to schedule appointments.'), ENT_NOQUOTES);
51 exit;
54 // Things that might be passed by our opener.
56 $eid = $_GET['eid']; // only for existing events
57 $date = $_GET['date']; // this and below only for new events
58 $userid = $_GET['userid'];
59 $default_catid = $_GET['catid'] ? $_GET['catid'] : '5';
60 $patientid = $_GET['patid'];
62 if ($date) {
63 $date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6);
64 } else {
65 $date = date("Y-m-d");
69 $starttimem = '00';
70 if (isset($_GET['starttimem'])) {
71 $starttimem = substr('00' . $_GET['starttimem'], -2);
75 if (isset($_GET['starttimeh'])) {
76 $starttimeh = $_GET['starttimeh'];
77 if (isset($_GET['startampm'])) {
78 if ($_GET['startampm'] == '2' && $starttimeh < 12) {
79 $starttimeh += 12;
82 } else {
83 $starttimeh = date("G");
86 $startampm = '';
88 $info_msg = "";
90 // ===========================
91 // EVENTS TO FACILITIES (lemonsoftware)
92 // edit event case - if there is no association made, then insert one with the first facility
93 /*if ( $eid ) {
94 $selfacil = '';
95 $facility = sqlQuery("SELECT pc_facility, pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
96 if ( !$facility['pc_facility'] ) {
97 $qmin = sqlQuery("SELECT MIN(id) as minId FROM facility");
98 $min = $qmin['minId'];
100 // multiple providers case
101 if ( $GLOBALS['select_multi_providers'] ) {
102 $mul = $facility['pc_multiple'];
103 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_multiple = $mul");
105 // EOS multiple
107 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_eid = $eid");
108 $e2f = $minId;
109 } else {
110 $e2f = $facility['pc_facility'];
113 // EOS E2F
114 // ===========================
115 // ===========================
117 // EVENTS TO FACILITIES (lemonsoftware)
118 //(CHEMED) get facility name
119 // edit event case - if there is no association made, then insert one with the first facility
120 if ($eid) {
121 $selfacil = '';
122 $facility = sqlQuery("SELECT pc_facility, pc_multiple, pc_aid, facility.name
123 FROM openemr_postcalendar_events
124 LEFT JOIN facility ON (openemr_postcalendar_events.pc_facility = facility.id)
125 WHERE pc_eid = $eid");
126 if (!$facility['pc_facility']) {
127 $qmin = sqlQuery("SELECT facility_id as minId, facility FROM users WHERE id = ".$facility['pc_aid']);
128 $min = $qmin['minId'];
129 $min_name = $qmin['facility'];
131 // multiple providers case
132 if ($GLOBALS['select_multi_providers']) {
133 $mul = $facility['pc_multiple'];
134 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_multiple = $mul");
137 // EOS multiple
139 sqlStatement("UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_eid = $eid");
140 $e2f = $min;
141 $e2f_name = $min_name;
142 } else {
143 $e2f = $facility['pc_facility'];
144 $e2f_name = $facility['name'];
148 // EOS E2F
149 // ===========================
152 // If we are saving, then save and close the window.
154 if ($_POST['form_action'] == "save") {
155 //print_r($_POST);
156 //exit();
157 $event_date = fixDate($_POST['form_date']);
159 // Compute start and end time strings to be saved.
160 if ($_POST['form_allday']) {
161 $tmph = 0;
162 $tmpm = 0;
163 $duration = 24 * 60;
164 } else {
165 $tmph = $_POST['form_hour'] + 0;
166 $tmpm = $_POST['form_minute'] + 0;
167 if ($_POST['form_ampm'] == '2' && $tmph < 12) {
168 $tmph += 12;
171 $duration = $_POST['form_duration'];
174 $starttime = "$tmph:$tmpm:00";
176 $tmpm += $duration;
177 while ($tmpm >= 60) {
178 $tmpm -= 60;
179 ++$tmph;
182 $endtime = "$tmph:$tmpm:00";
184 // Useless garbage that we must save.
185 $locationspec = 'a:6:{s:14:"event_location";N;s:13:"event_street1";N;' .
186 's:13:"event_street2";N;s:10:"event_city";N;s:11:"event_state";N;s:12:"event_postal";N;}';
188 // More garbage, but this time 1 character of it is used to save the
189 // repeat type.
190 if ($_POST['form_repeat']) {
191 $recurrspec = 'a:5:{' .
192 's:17:"event_repeat_freq";s:1:"' . $_POST['form_repeat_freq'] . '";' .
193 's:22:"event_repeat_freq_type";s:1:"' . $_POST['form_repeat_type'] . '";' .
194 's:19:"event_repeat_on_num";s:1:"1";' .
195 's:19:"event_repeat_on_day";s:1:"0";' .
196 's:20:"event_repeat_on_freq";s:1:"0";}';
197 } else {
198 $recurrspec = 'a:5:{' .
199 's:17:"event_repeat_freq";N;' .
200 's:22:"event_repeat_freq_type";s:1:"0";' .
201 's:19:"event_repeat_on_num";s:1:"1";' .
202 's:19:"event_repeat_on_day";s:1:"0";' .
203 's:20:"event_repeat_on_freq";s:1:"1";}';
206 //The modification of the start date for events that take place on one day of the week
207 //for example monday, or thursday. We set the start date on the first day of the week
208 //that the event is scheduled. For example if you set the event to repeat on each monday
209 //the start date of the event will be set on the first monday after the day the event is scheduled
210 if ($_POST['form_repeat_type'] == 5) {
211 $exploded_date= explode("-", $event_date);
212 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
213 if ($edate=="Tue") {
214 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
215 } elseif ($edate=="Wed") {
216 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
217 } elseif ($edate=="Thu") {
218 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
219 } elseif ($edate=="Fri") {
220 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
221 } elseif ($edate=="Sat") {
222 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
223 } elseif ($edate=="Sun") {
224 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
226 } elseif ($_POST['form_repeat_type'] == 6) {
227 $exploded_date= explode("-", $event_date);
228 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
229 if ($edate=="Wed") {
230 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
231 } elseif ($edate=="Thu") {
232 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
233 } elseif ($edate=="Fri") {
234 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
235 } elseif ($edate=="Sat") {
236 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
237 } elseif ($edate=="Sun") {
238 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
239 } elseif ($edate=="Mon") {
240 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
242 } elseif ($_POST['form_repeat_type'] == 7) {
243 $exploded_date= explode("-", $event_date);
244 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
245 if ($edate=="Thu") {
246 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
247 } elseif ($edate=="Fri") {
248 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
249 } elseif ($edate=="Sat") {
250 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
251 } elseif ($edate=="Sun") {
252 $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]));
255 } elseif ($edate=="Tue") {
256 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
258 } elseif ($_POST['form_repeat_type'] == 8) {
259 $exploded_date= explode("-", $event_date);
260 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
261 if ($edate=="Fri") {
262 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
263 } elseif ($edate=="Sat") {
264 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
265 } elseif ($edate=="Sun") {
266 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
267 } elseif ($edate=="Mon") {
268 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
269 } elseif ($edate=="Tue") {
270 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
271 } elseif ($edate=="Wed") {
272 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
274 } elseif ($_POST['form_repeat_type'] == 9) {
275 $exploded_date= explode("-", $event_date);
276 $edate = date("D", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2], $exploded_date[0]));
277 if ($edate=="Sat") {
278 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+6, $exploded_date[0]));
279 } elseif ($edate=="Sun") {
280 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+5, $exploded_date[0]));
281 } elseif ($edate=="Mon") {
282 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+4, $exploded_date[0]));
283 } elseif ($edate=="Tue") {
284 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+3, $exploded_date[0]));
285 } elseif ($edate=="Wed") {
286 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+2, $exploded_date[0]));
287 } elseif ($edate=="Thu") {
288 $event_date=date("Y-m-d", mktime(0, 0, 0, $exploded_date[1], $exploded_date[2]+1, $exploded_date[0]));
290 }//if end
291 /* =======================================================
292 // UPDATE EVENTS
293 ========================================================*/
294 if ($eid) {
295 // what is multiple key around this $eid?
296 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
298 if ($GLOBALS['select_multi_providers'] && $row['pc_multiple']) {
299 /* ==========================================
300 // multi providers BOS
301 ==========================================*/
303 // obtain current list of providers regarding the multiple key
304 $up = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_multiple={$row['pc_multiple']}");
305 while ($current = sqlFetchArray($up)) {
306 $providers_current[] = $current['pc_aid'];
309 $providers_new = $_POST['form_provider'];
311 // this difference means that some providers from current was UNCHECKED
312 // so we must delete this event for them
313 $r1 = array_diff($providers_current, $providers_new);
314 if (count($r1)) {
315 foreach ($r1 as $to_be_removed) {
316 sqlQuery("DELETE FROM openemr_postcalendar_events WHERE pc_aid='$to_be_removed' AND pc_multiple={$row['pc_multiple']}");
320 // this difference means that some providers was added
321 // so we must insert this event for them
322 $r2 = array_diff($providers_new, $providers_current);
323 if (count($r2)) {
324 foreach ($r2 as $to_be_inserted) {
325 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)
326 VALUES ( " .
327 "'" . $_POST['form_category'] . "', " .
328 "'" . $row['pc_multiple'] . "', " .
329 "'" . $to_be_inserted . "', " .
330 "'" . $_POST['form_pid'] . "', " .
331 "'" . $_POST['form_title'] . "', " .
332 "NOW(), " .
333 "'" . $_POST['form_comments'] . "', " .
334 "'" . $_SESSION['authUserID'] . "', " .
335 "'" . $event_date . "', " .
336 "'" . fixDate($_POST['form_enddate']) . "', " .
337 "'" . ($duration * 60) . "', " .
338 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
339 "'$recurrspec', " .
340 "'$starttime', " .
341 "'$endtime', " .
342 "'" . $_POST['form_allday'] . "', " .
343 "'" . $_POST['form_apptstatus'] . "', " .
344 "'" . $_POST['form_prefcat'] . "', " .
345 "'$locationspec', " .
346 "1, " .
347 "1, " .(int)$_POST['facility']. " )"); // FF stuff
348 } // foreach
349 } //if count
352 // after the two diffs above, we must update for remaining providers
353 // those who are intersected in $providers_current and $providers_new
354 foreach ($_POST['form_provider'] as $provider) {
355 sqlStatement("UPDATE openemr_postcalendar_events SET " .
356 "pc_catid = '" . $_POST['form_category'] . "', " .
357 "pc_pid = '" . $_POST['form_pid'] . "', " .
358 "pc_title = '" . $_POST['form_title'] . "', " .
359 "pc_time = NOW(), " .
360 "pc_hometext = '" . $_POST['form_comments'] . "', " .
361 "pc_informant = '" . $_SESSION['authUserID'] . "', " .
362 "pc_eventDate = '" . $event_date . "', " .
363 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
364 "pc_duration = '" . ($duration * 60) . "', " .
365 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
366 "pc_recurrspec = '$recurrspec', " .
367 "pc_startTime = '$starttime', " .
368 "pc_endTime = '$endtime', " .
369 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
370 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
371 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
372 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
373 "WHERE pc_aid = '$provider' AND pc_multiple={$row['pc_multiple']}");
374 } // foreach
376 /* ==========================================
377 // multi providers EOS
378 ==========================================*/
379 } elseif (!$row['pc_multiple']) {
380 if ($GLOBALS['select_multi_providers']) {
381 $prov = $_POST['form_provider'][0];
382 } else {
383 $prov = $_POST['form_provider'];
386 // simple provider case
387 sqlStatement("UPDATE openemr_postcalendar_events SET " .
388 "pc_catid = '" . $_POST['form_category'] . "', " .
389 "pc_aid = '" . $prov . "', " .
390 "pc_pid = '" . $_POST['form_pid'] . "', " .
391 "pc_title = '" . $_POST['form_title'] . "', " .
392 "pc_time = NOW(), " .
393 "pc_hometext = '" . $_POST['form_comments'] . "', " .
394 "pc_informant = '" . $_SESSION['authUserID'] . "', " .
395 "pc_eventDate = '" . $event_date . "', " .
396 "pc_endDate = '" . fixDate($_POST['form_enddate']) . "', " .
397 "pc_duration = '" . ($duration * 60) . "', " .
398 "pc_recurrtype = '" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
399 "pc_recurrspec = '$recurrspec', " .
400 "pc_startTime = '$starttime', " .
401 "pc_endTime = '$endtime', " .
402 "pc_alldayevent = '" . $_POST['form_allday'] . "', " .
403 "pc_apptstatus = '" . $_POST['form_apptstatus'] . "', " .
404 "pc_prefcatid = '" . $_POST['form_prefcat'] . "' ," .
405 "pc_facility = '" .(int)$_POST['facility'] ."' " . // FF stuff
406 "WHERE pc_eid = '$eid'");
409 // =======================================
410 // EOS multi providers case
411 // =======================================
413 // EVENTS TO FACILITIES
415 $e2f = (int)$eid;
417 /* =======================================================
418 // INSERT EVENTS
419 ========================================================*/
420 } else {
421 // =======================================
422 // multi providers case
423 // =======================================
425 if (is_array($_POST['form_provider'])) {
426 // obtain the next available unique key to group multiple providers around some event
427 $q = sqlStatement("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
428 $max = sqlFetchArray($q);
429 $new_multiple_value = $max['max'] + 1;
431 foreach ($_POST['form_provider'] as $provider) {
432 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
433 "pc_catid, pc_multiple, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
434 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
435 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
436 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
437 ") VALUES ( " .
438 "'" . $_POST['form_category'] . "', " .
439 "'" . $new_multiple_value . "', " .
440 "'" . $provider . "', " .
441 "'" . $_POST['form_pid'] . "', " .
442 "'" . $_POST['form_title'] . "', " .
443 "NOW(), " .
444 "'" . $_POST['form_comments'] . "', " .
445 "'" . $_SESSION['authUserID'] . "', " .
446 "'" . $event_date . "', " .
447 "'" . fixDate($_POST['form_enddate']) . "', " .
448 "'" . ($duration * 60) . "', " .
449 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
450 "'$recurrspec', " .
451 "'$starttime', " .
452 "'$endtime', " .
453 "'" . $_POST['form_allday'] . "', " .
454 "'" . $_POST['form_apptstatus'] . "', " .
455 "'" . $_POST['form_prefcat'] . "', " .
456 "'$locationspec', " .
457 "1, " .
458 "1, " .(int)$_POST['facility']. " )"); // FF stuff
459 } // foreach
460 } else {
461 sqlInsert("INSERT INTO openemr_postcalendar_events ( " .
462 "pc_catid, pc_aid, pc_pid, pc_title, pc_time, pc_hometext, " .
463 "pc_informant, pc_eventDate, pc_endDate, pc_duration, pc_recurrtype, " .
464 "pc_recurrspec, pc_startTime, pc_endTime, pc_alldayevent, " .
465 "pc_apptstatus, pc_prefcatid, pc_location, pc_eventstatus, pc_sharing, pc_facility " .
466 ") VALUES ( " .
467 "'" . $_POST['form_category'] . "', " .
468 "'" . $_POST['form_provider'] . "', " .
469 "'" . $_POST['form_pid'] . "', " .
470 "'" . $_POST['form_title'] . "', " .
471 "NOW(), " .
472 "'" . $_POST['form_comments'] . "', " .
473 "'" . $_SESSION['authUserID'] . "', " .
474 "'" . $event_date . "', " .
475 "'" . fixDate($_POST['form_enddate']) . "', " .
476 "'" . ($duration * 60) . "', " .
477 "'" . ($_POST['form_repeat'] ? '1' : '0') . "', " .
478 "'$recurrspec', " .
479 "'$starttime', " .
480 "'$endtime', " .
481 "'" . $_POST['form_allday'] . "', " .
482 "'" . $_POST['form_apptstatus'] . "', " .
483 "'" . $_POST['form_prefcat'] . "', " .
484 "'$locationspec', " .
485 "1, " .
486 "1," .(int)$_POST['facility']. ")"); // FF stuff
487 } // INSERT single
488 } // else - insert
490 // Save new DOB if it's there.
491 $patient_dob = trim($_POST['form_dob']);
492 if ($patient_dob && $_POST['form_pid']) {
493 sqlStatement("UPDATE patient_data SET DOB = '$patient_dob' WHERE " .
494 "pid = '" . $_POST['form_pid'] . "'");
497 // Auto-create a new encounter if appropriate.
500 /* if ($GLOBALS['auto_create_new_encounters'] &&
501 $_POST['form_apptstatus'] == '@' && $event_date == date('Y-m-d'))
504 // We decided not to auto-create blank enconter when user arrives. Todd's decision 18 Jun 2010
505 // Applied by Cassian Lup (cassian.lup@clinicdr.com)
507 if (0) {
508 $tmprow = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
509 "pid = '" . $_POST['form_pid'] . "' AND date = '$event_date 00:00:00'");
510 if ($tmprow['count'] == 0) {
511 $tmprow = sqlQuery("SELECT username, facility, facility_id FROM users WHERE id = '" .
512 $_POST['form_provider'] . "'");
513 $username = $tmprow['username'];
514 $facility = $tmprow['facility'];
515 $facility_id = $tmprow['facility_id'];
516 $conn = $GLOBALS['adodb']['db'];
517 $encounter = $conn->GenID("sequences");
518 addForm(
519 $encounter,
520 "New Patient Encounter",
521 sqlInsert("INSERT INTO form_encounter SET " .
522 "date = '$event_date', " .
523 "onset_date = '$event_date', " .
524 "reason = '" . $_POST['form_comments'] . "', " .
525 "facility = '$facility', " .
526 "facility_id = '$facility_id', " .
527 "pid = '" . $_POST['form_pid'] . "', " .
528 "encounter = '$encounter'"),
529 "newpatient",
530 $_POST['form_pid'],
531 "1",
532 "NOW()",
533 $username
535 $info_msg .= "New encounter $encounter was created. ";
538 } else if ($_POST['form_action'] == "delete") {
539 // =======================================
540 // multi providers case
541 // =======================================
542 if ($GLOBALS['select_multi_providers']) {
543 // what is multiple key around this $eid?
544 $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = $eid");
545 if ($row['pc_multiple']) {
546 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_multiple = {$row['pc_multiple']}");
547 } else {
548 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = $eid");
551 // =======================================
552 // EOS multi providers case
553 // =======================================
554 } else {
555 sqlStatement("DELETE FROM openemr_postcalendar_events WHERE pc_eid = '$eid'");
559 if ($_POST['form_action'] != "") {
560 // Close this window and refresh the calendar display.
561 echo "<html>\n<body>\n<script language='JavaScript'>\n";
562 if ($info_msg) {
563 echo " alert('$info_msg');\n";
566 // echo " if (!opener.closed && opener.refreshme) opener.refreshme();\n";
567 // echo " if (!opener.closed && opener.refreshme) window.opener.location.reload(true);\n";
568 // echo " opener.refreshme();";
569 // 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='";
570 echo " parent.jQuery.fn.fancybox.close();\n";
571 echo "</script>\n</body>\n</html>\n";
572 exit();
575 // If we get this far then we are displaying the form.
577 $statuses = array(
578 '-' => '',
579 '*' => xl('* Reminder done'),
580 '+' => xl('+ Chart pulled'),
581 'x' => xl('x Cancelled'), // added Apr 2008 by JRM
582 '?' => xl('? No show'),
583 '@' => xl('@ Arrived'),
584 '~' => xl('~ Arrived late'),
585 '!' => xl('! Left w/o visit'),
586 '#' => xl('# Ins/fin issue'),
587 '<' => xl('< In exam room'),
588 '>' => xl('> Checked out'),
589 '$' => xl('$ Coding done'),
592 $repeats = 0; // if the event repeats
593 $repeattype = '0';
594 $repeatfreq = '0';
595 $patienttitle = "";
596 $hometext = "";
597 $row = array();
599 // If we are editing an existing event, then get its data.
600 if ($eid) {
601 $row = sqlQuery("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = $eid");
602 $date = $row['pc_eventDate'];
603 $userid = $row['pc_aid'];
604 $patientid = $row['pc_pid'];
605 $starttimeh = substr($row['pc_startTime'], 0, 2) + 0;
606 $starttimem = substr($row['pc_startTime'], 3, 2);
607 $repeats = $row['pc_recurrtype'];
608 $multiple_value = $row['pc_multiple'];
610 if (preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
611 $repeattype = $matches[1];
614 if (preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches)) {
615 $repeatfreq = $matches[1];
618 $hometext = $row['pc_hometext'];
619 if (substr($hometext, 0, 6) == ':text:') {
620 $hometext = substr($hometext, 6);
622 } else {
623 $patientid=$_GET['pid'];
626 // If we have a patient ID, get the name and phone numbers to display.
627 if ($patientid) {
628 $prow = sqlQuery("SELECT lname, fname, phone_home, phone_biz, DOB " .
629 "FROM patient_data WHERE pid = '" . $patientid . "'");
630 $patientname = $prow['lname'] . ", " . $prow['fname'];
631 if ($prow['phone_home']) {
632 $patienttitle .= " H=" . $prow['phone_home'];
635 if ($prow['phone_biz']) {
636 $patienttitle .= " W=" . $prow['phone_biz'];
640 // Get the providers list.
641 $ures = sqlStatement("SELECT id, username, fname, lname FROM users WHERE " .
642 "authorized != 0 AND active = 1 ORDER BY lname, fname");
644 //-------------------------------------
645 //(CHEMED)
646 //Set default facility for a new event based on the given 'userid'
647 if ($userid) {
648 $pref_facility = sqlFetchArray(sqlStatement("SELECT facility_id, facility FROM users WHERE id = $userid"));
649 $e2f = $pref_facility['facility_id'];
650 $e2f_name = $pref_facility['facility'];
653 //END of CHEMED -----------------------
655 // Get event categories.
656 $cres = sqlStatement("SELECT pc_catid, pc_catname, pc_recurrtype, pc_duration, pc_end_all_day " .
657 "FROM openemr_postcalendar_categories ORDER BY pc_catname");
659 // Fix up the time format for AM/PM.
660 $startampm = '1';
661 if ($starttimeh >= 12) { // p.m. starts at noon and not 12:01
662 $startampm = '2';
663 if ($starttimeh > 12) {
664 $starttimeh -= 12;
669 <html>
670 <head>
671 <?php html_header_show(); ?>
672 <title><?php echo $eid ? "Edit" : "Add New" ?> <?php xl('Event', 'e');?></title>
673 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
675 <style>
676 td { font-size:0.8em; }
677 </style>
679 <style type="text/css">@import url(../library/dynarch_calendar.css);</style>
680 <script type="text/javascript" src="../library/topdialog.js"></script>
681 <script type="text/javascript" src="../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
682 <script type="text/javascript" src="../library/textformat.js"></script>
683 <script type="text/javascript" src="../library/dynarch_calendar.js"></script>
684 <script type="text/javascript" src="../library/dynarch_calendar_en.js"></script>
685 <script type="text/javascript" src="../library/dynarch_calendar_setup.js"></script>
687 <script language="JavaScript">
689 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
691 var durations = new Array();
692 // var rectypes = new Array();
693 <?php
694 // Read the event categories, generate their options list, and get
695 // the default event duration from them if this is a new event.
696 $catoptions = "";
697 $prefcat_options = " <option value='0'>-- None --</option>\n";
698 $thisduration = 0;
699 if ($eid) {
700 $thisduration = $row['pc_alldayevent'] ? 1440 : round($row['pc_duration'] / 60);
703 while ($crow = sqlFetchArray($cres)) {
704 $duration = round($crow['pc_duration'] / 60);
705 if ($crow['pc_end_all_day']) {
706 $duration = 1440;
709 echo " durations[" . $crow['pc_catid'] . "] = $duration\n";
710 // echo " rectypes[" . $crow['pc_catid'] . "] = " . $crow['pc_recurrtype'] . "\n";
711 $catoptions .= " <option value='" . $crow['pc_catid'] . "'";
712 if ($eid) {
713 if ($crow['pc_catid'] == $row['pc_catid']) {
714 $catoptions .= " selected";
716 } else {
717 if ($crow['pc_catid'] == $default_catid) {
718 $catoptions .= " selected";
719 $thisduration = $duration;
723 $catoptions .= ">" . $crow['pc_catname'] . "</option>\n";
725 // This section is to build the list of preferred categories:
726 if ($duration) {
727 $prefcat_options .= " <option value='" . $crow['pc_catid'] . "'";
728 if ($eid) {
729 if ($crow['pc_catid'] == $row['pc_prefcatid']) {
730 $prefcat_options .= " selected";
734 $prefcat_options .= ">" . $crow['pc_catname'] . "</option>\n";
739 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
741 // This is for callback by the find-patient popup.
742 function setpatient(pid, lname, fname, dob) {
743 var f = document.forms[0];
744 f.form_patient.value = lname + ', ' + fname;
745 f.form_pid.value = pid;
746 dobstyle = (dob == '' || dob.substr(5, 10) == '00-00') ? '' : 'none';
747 document.getElementById('dob_row').style.display = dobstyle;
749 function change_provider(){
750 var f = document.forms[0];
751 f.form_date.value='';
752 f.form_hour.value='';
753 f.form_minute.value='';
755 // This is for callback by the find-patient popup.
756 function unsetpatient() {
757 var f = document.forms[0];
758 f.form_patient.value = '';
759 f.form_pid.value = '';
762 // This invokes the find-patient popup.
763 function sel_patient() {
764 dlgopen('find_patient_popup.php', '_blank', 500, 400);
767 // Do whatever is needed when a new event category is selected.
768 // For now this means changing the event title and duration.
769 function set_display() {
770 var f = document.forms[0];
771 var s = f.form_category;
772 if (s.selectedIndex >= 0) {
773 var catid = s.options[s.selectedIndex].value;
774 var style_apptstatus = document.getElementById('title_apptstatus').style;
775 var style_prefcat = document.getElementById('title_prefcat').style;
776 if (catid == '2') { // In Office
777 style_apptstatus.display = 'none';
778 style_prefcat.display = '';
779 f.form_apptstatus.style.display = 'none';
780 f.form_prefcat.style.display = '';
781 } else {
782 style_prefcat.display = 'none';
783 style_apptstatus.display = '';
784 f.form_prefcat.style.display = 'none';
785 f.form_apptstatus.style.display = '';
790 // Gray out certain fields according to selection of Category DDL
791 function categoryChanged() {
792 var value = '5';
794 document.getElementById("form_patient").disabled=false;
795 //document.getElementById("form_apptstatus").disabled=false;
796 //document.getElementById("form_prefcat").disabled=false;
800 // Do whatever is needed when a new event category is selected.
801 // For now this means changing the event title and duration.
802 function set_category() {
803 var f = document.forms[0];
804 var s = f.form_category;
805 if (s.selectedIndex >= 0) {
806 var catid = s.options[s.selectedIndex].value;
807 f.form_title.value = s.options[s.selectedIndex].text;
808 f.form_duration.value = durations[catid];
809 set_display();
813 // Modify some visual attributes when the all-day or timed-event
814 // radio buttons are clicked.
815 function set_allday() {
816 var f = document.forms[0];
817 var color1 = '#777777';
818 var color2 = '#777777';
819 var disabled2 = true;
820 /*if (document.getElementById('rballday1').checked) {
821 color1 = '#000000';
823 if (document.getElementById('rballday2').checked) {
824 color2 = '#000000';
825 disabled2 = false;
827 document.getElementById('tdallday1').style.color = color1;
828 document.getElementById('tdallday2').style.color = color2;
829 document.getElementById('tdallday3').style.color = color2;
830 document.getElementById('tdallday4').style.color = color2;
831 document.getElementById('tdallday5').style.color = color2;
832 f.form_hour.disabled = disabled2;
833 f.form_minute.disabled = disabled2;
834 f.form_ampm.disabled = disabled2;
835 f.form_duration.disabled = disabled2;
838 // Modify some visual attributes when the Repeat checkbox is clicked.
839 function set_repeat() {
840 var f = document.forms[0];
841 var isdisabled = true;
842 var mycolor = '#777777';
843 var myvisibility = 'hidden';
844 /*if (f.form_repeat.checked) {
845 isdisabled = false;
846 mycolor = '#000000';
847 myvisibility = 'visible';
849 //f.form_repeat_type.disabled = isdisabled;
850 //f.form_repeat_freq.disabled = isdisabled;
851 //f.form_enddate.disabled = isdisabled;
852 document.getElementById('tdrepeat1').style.color = mycolor;
853 document.getElementById('tdrepeat2').style.color = mycolor;
854 document.getElementById('img_enddate').style.visibility = myvisibility;
857 // This is for callback by the find-available popup.
858 function setappt(year,mon,mday,hours,minutes) {
859 var f = document.forms[0];
860 f.form_date.value = '' + year + '-' +
861 ('' + (mon + 100)).substring(1) + '-' +
862 ('' + (mday + 100)).substring(1);
863 f.form_ampm.selectedIndex = (hours >= 12) ? 1 : 0;
864 f.form_hour.value = (hours > 12) ? hours - 12 : hours;
865 f.form_minute.value = ('' + (minutes + 100)).substring(1);
868 // Invoke the find-available popup.
869 function find_available() {
870 //top.restoreSession();
871 // (CHEMED) Conditional value selection, because there is no <select> element
872 // when making an appointment for a specific provider
873 var s = document.forms[0].form_provider;
874 <?php if ($userid != 0) { ?>
875 s = document.forms[0].form_provider.value;
876 <?php } else {?>
877 s = document.forms[0].form_provider.options[s.selectedIndex].value;
878 <?php }?>
879 // var fd2=document.forms[0].form_date2.value;
880 // document.forms[0].form_date.value=fd2.substring(6)+'-'+fd2.substring(0,2)+'-'+fd2.substring(3,5);
882 var formDate = document.forms[0].form_date;
883 window.open('find_appt_popup_user.php?bypatient&providerid=' + s +
884 '&catid=5' +
885 '&startdate=' + formDate.value, '_blank', 500, 400);
886 //END (CHEMED) modifications
889 // Check for errors when the form is submitted.
890 function validate() {
891 var f = document.getElementById('theform');
892 if (!f.form_date.value || !f.form_hour.value || !f.form_minute.value) {
893 alert('Please click on "Openings" to select a time.');
894 return false;
897 // in lunch outofoffice reserved vacation
898 f.form_category.value='12';
899 if (f.form_patient.value=='Click to select' && (!(
900 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'
901 || f.form_category.value=='10'))) {
902 alert('Please select a patient.');
903 return false;
904 } else if (f.form_category.value=='10') {
905 unsetpatient();
907 var form_action = document.getElementById('form_action');
908 form_action.value="save";
909 f.submit();
910 //top.restoreSession();
911 // top
912 return true;
915 function deleteEvent() {
916 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?")) {
917 var f = document.getElementById('theform');
918 var form_action = document.getElementById('form_action');
919 form_action.value="delete";
920 f.submit();
921 return true;
923 return false;
926 </script>
928 </head>
930 <body class="body_top" onunload='imclosing()' onload='categoryChanged()'>
932 <form method='post' name='theform' id='theform' action='add_edit_event_user.php?eid=<?php echo $eid ?>' />
933 <input type="hidden" name="form_action" id="form_action" value="">
934 <center>
936 <table border='0' width='100%'>
938 <tr>
939 <td width='1%' nowrap>
940 <b><?php xl('Category', 'e'); ?>:</b>
941 </td>
942 <td nowrap>
943 <input type="text" id='form_category' name='form_category' value='Office Visit' readonly='readonly' style='width:100%'/>
944 </td>
945 <td></td>
946 <td width='1%' nowrap>
947 <b><?php xl('Date', 'e'); ?>:</b>
948 </td>
949 <td colspan='2' nowrap id='tdallday1'>
950 <input type='text' size='10' name='form_date' readonly id='form_date' <?php echo $disabled ?>
951 value='<?php if (isset($eid)) {
952 echo $eid ? $row['pc_eventDate'] : $date;
953 } ?>'
954 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
955 </td>
956 </tr>
958 <tr>
959 <td nowrap>
960 <b><?php xl('Title', 'e'); ?>:</b>
961 </td>
962 <td nowrap>
963 <input type='text' size='10' name='form_title' value='<?php echo addslashes($row['pc_title']) ?>'
964 style='width:100%'
965 title='<?php xl('Event title', 'e'); ?>' />
966 </td>
967 <td nowrap>
968 </td>
969 <td width='1%' nowrap id='tdallday2'>
970 <?php xl('Time', 'e'); ?>
971 </td>
972 <td width='1%' nowrap id='tdallday3'>
973 <input type='text' size='2' name='form_hour' value='<?php if (isset($eid)) {
974 echo $starttimeh;
975 } ?>'
976 title='<?php xl('Event start time', 'e'); ?>' readonly/> :
977 <input type='text' size='2' name='form_minute' value='<?php if (isset($eid)) {
978 echo $starttimem;
979 } ?>'
980 title='<?php xl('Event start time', 'e'); ?>' readonly/>&nbsp;
981 <select name='form_ampm' title='Note: 12:00 noon is PM, not AM' disabled="disabled">
982 <option value='1'><?php xl('AM', 'e'); ?></option>
983 <option value='2'<?php if ($startampm == '2') {
984 echo " selected";
985 } ?>><?php xl('PM', 'e'); ?></option>
986 </select>
987 </td>
988 </tr>
989 <tr>
990 <td nowrap>
991 <b><?php xl('Patient', 'e'); ?>:</b>
992 </td>
993 <td nowrap>
994 <input type='text' size='10' id='form_patient' name='form_patient' style='width:100%;' value='<?php echo $patientname ?>' title='Patient' readonly />
995 <input type='hidden' name='form_pid' value='<?php echo $patientid ?>' />
996 </td>
997 <td nowrap>
998 &nbsp;
999 </td>
1000 <td nowrap id='tdallday4'><?php xl('duration', 'e'); ?>
1001 </td>
1002 <td nowrap id='tdallday5'>
1003 <input type='text' size='4' name='form_duration' readonly value='<?php echo $thisduration ?>' title='<?php xl('Event duration in minutes', 'e'); ?>' />
1004 <?php xl('minutes', 'e'); ?>
1006 </td>
1007 </tr>
1009 <tr>
1011 </tr>
1014 <tr>
1015 <td nowrap>
1016 <b><?php xl('Provider', 'e'); ?>:</b>
1017 </td>
1018 <td nowrap>
1019 <?php
1021 // present a list of providers to choose from
1022 // default to the currently logged-in user
1023 echo "<select name='form_provider' onchange='change_provider();' style='width:100%' />";
1024 while ($urow = sqlFetchArray($ures)) {
1025 echo " <option value='" . $urow['id'] . "'";
1026 // if ($urow['id'] == $_SESSION['authUserID']) echo " selected";
1027 if (($urow['id'] == $_GET['userid'])||($urow['id']== $userid)) {
1028 echo " selected";
1031 echo ">" . $urow['lname'];
1032 if ($urow['fname']) {
1033 echo ", " . $urow['fname'];
1036 echo "</option>\n";
1039 echo "</select>";
1041 // } //END (CHEMED) IF
1044 </td>
1045 <td nowrap style='font-size:8pt'>
1047 </td>
1048 <td><input type='button' value='<?php xl('Openings', 'e');?>' onclick='find_available()' /></td>
1049 <td></td>
1050 </tr>
1052 <tr>
1053 <td nowrap>
1054 <b><?php xl('Comments', 'e'); ?>:</b>
1055 </td>
1056 <td colspan='4' nowrap>
1057 <input type='text' size='40' name='form_comments' style='width:100%' value='<?php echo $hometext ?>' title='<?php xl('Optional information about this event', 'e');?>' />
1058 </td>
1059 </tr>
1061 <?php
1062 // DOB is important for the clinic, so if it's missing give them a chance
1063 // to enter it right here. We must display or hide this row dynamically
1064 // in case the patient-select popup is used.
1065 $patient_dob = trim($prow['DOB']);
1066 $dobstyle = ($prow && (!$patient_dob || substr($patient_dob, 5) == '00-00')) ?
1067 '' : 'none';
1069 <tr id='dob_row' style='display:none<?php //echo $dobstyle
1070 ?>'>
1071 <td colspan='4' nowrap style='display:none'>
1072 <font color='white'><?php xl('DOB is missing, please enter if possible', 'e'); ?>:</font></b>
1073 </td>
1074 <td nowrap>
1075 <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)' />
1076 <img src='../../pic/show_calendar.gif' align='absbottom' width='24' height='22'
1077 id='img_dob' border='0' alt='[?]' style='cursor:pointer;cursor:hand;display:none'
1078 title='<?php xl('Click here to choose a date', 'e');?>'>
1079 </td>
1080 </tr>
1082 </table>
1085 <input type='button' name='form_save' value='<?php xl('Save', 'e');?>' onclick="validate()" />
1086 &nbsp;
1087 <input type='button' value='<?php xl('Cancel', 'e');?>' onclick='parent.$.fn.fancybox.close()' />
1088 </p>
1089 </center>
1090 </form>
1092 <script language='JavaScript'>
1093 <?php if ($eid) { ?>
1094 set_display();
1095 <?php } else { ?>
1096 //set_category();
1097 <?php } ?>
1098 //set_allday();
1099 //set_repeat();
1101 //Calendar.setup({inputField:"form_dob", ifFormat:"%Y-%m-%d", button:"img_dob"});
1102 </script>
1104 </body>
1105 </html>