the last of the legacy date pickers (#990)
[openemr.git] / portal / find_appt_popup_user.php
blob2e0406c6426b117ec28d608b1ab165f02ed3f614
1 <?php
2 /**
4 * Modified from main codebase for the patient portal.
6 * Copyright (C) 2005-2006, 2013 Rod Roark <rod@sunsetsystems.com>
7 * Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
9 * LICENSE: This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
20 * @package OpenEMR
21 * @author Rod Roark <rod@sunsetsystems.com>
22 * @author Jerry Padgett <sjpadgett@gmail.com>
23 * @link http://www.open-emr.org
26 // Note from Rod 2013-01-22:
27 // This module needs to be refactored to share the same code that is in
28 // interface/main/calendar/find_appt_popup.php. It contains an old version
29 // of that logic and does not support exception dates for repeating events.
31 //continue session
32 session_start();
35 //landing page definition -- where to go if something goes wrong
36 $landingpage = "index.php?site=".$_SESSION['site_id'];
39 // kick out if patient not authenticated
40 if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite_two'])) {
41 $pid = $_SESSION['pid'];
42 } else {
43 session_destroy();
44 header('Location: '.$landingpage.'&w');
45 exit;
50 $ignoreAuth = 1;
52 include_once("../interface/globals.php");
53 include_once("$srcdir/patient.inc");
55 // Exit if the modify calendar for portal flag is not set patched out v5
56 /* if (!($GLOBALS['portal_onsite_appt_modify'])) {
57 echo htmlspecialchars( xl('You are not authorized to schedule appointments.'),ENT_NOQUOTES);
58 exit;
59 } */
61 $input_catid = $_REQUEST['catid'];
63 // Record an event into the slots array for a specified day.
64 function doOneDay($catid, $udate, $starttime, $duration, $prefcatid)
66 global $slots, $slotsecs, $slotstime, $slotbase, $slotcount, $input_catid;
67 $udate = strtotime($starttime, $udate);
68 if ($udate < $slotstime) {
69 return;
72 $i = (int) ($udate / $slotsecs) - $slotbase;
73 $iend = (int) (($duration + $slotsecs - 1) / $slotsecs) + $i;
74 if ($iend > $slotcount) {
75 $iend = $slotcount;
78 if ($iend <= $i) {
79 $iend = $i + 1;
82 for (; $i < $iend; ++$i) {
83 if ($catid == 2) { // in office
84 // If a category ID was specified when this popup was invoked, then select
85 // only IN events with a matching preferred category or with no preferred
86 // category; other IN events are to be treated as OUT events.
87 if ($input_catid) {
88 if ($prefcatid == $input_catid || !$prefcatid) {
89 $slots[$i] |= 1;
90 } else {
91 $slots[$i] |= 2;
93 } else {
94 $slots[$i] |= 1;
97 break; // ignore any positive duration for IN
98 } else if ($catid == 3) { // out of office
99 $slots[$i] |= 2;
100 break; // ignore any positive duration for OUT
101 } else { // all other events reserve time
102 $slots[$i] |= 4;
107 // seconds per time slot
108 $slotsecs = $GLOBALS['calendar_interval'] * 60;
110 $catslots = 1;
111 if ($input_catid) {
112 $srow = sqlQuery("SELECT pc_duration FROM openemr_postcalendar_categories WHERE pc_catid = '$input_catid'");
113 if ($srow['pc_duration']) {
114 $catslots = ceil($srow['pc_duration'] / $slotsecs);
118 $info_msg = "";
120 $searchdays = 7; // default to a 1-week lookahead
121 if ($_REQUEST['searchdays']) {
122 $searchdays = $_REQUEST['searchdays'];
125 // Get a start date.
126 if ($_REQUEST['startdate'] && preg_match(
127 "/(\d\d\d\d)\D*(\d\d)\D*(\d\d)/",
128 $_REQUEST['startdate'],
129 $matches
130 )) {
131 $sdate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
132 } else {
133 $sdate = date("Y-m-d");
136 // Get an end date - actually the date after the end date.
137 preg_match("/(\d\d\d\d)\D*(\d\d)\D*(\d\d)/", $sdate, $matches);
138 $edate = date(
139 "Y-m-d",
140 mktime(0, 0, 0, $matches[2], $matches[3] + $searchdays, $matches[1])
143 // compute starting time slot number and number of slots.
144 $slotstime = strtotime("$sdate 00:00:00");
145 $slotetime = strtotime("$edate 00:00:00");
146 $slotbase = (int) ($slotstime / $slotsecs);
147 $slotcount = (int) ($slotetime / $slotsecs) - $slotbase;
149 if ($slotcount <= 0 || $slotcount > 100000) {
150 die("Invalid date range.");
153 $slotsperday = (int) (60 * 60 * 24 / $slotsecs);
155 // If we have a provider, search.
157 if ($_REQUEST['providerid']) {
158 $providerid = $_REQUEST['providerid'];
160 // Create and initialize the slot array. Values are bit-mapped:
161 // bit 0 = in-office occurs here
162 // bit 1 = out-of-office occurs here
163 // bit 2 = reserved
164 // So, values may range from 0 to 7.
166 $slots = array_pad(array(), $slotcount, 0);
168 // Note there is no need to sort the query results.
169 // echo $sdate." -- ".$edate;
170 $query = "SELECT pc_eventDate, pc_endDate, pc_startTime, pc_duration, " .
171 "pc_recurrtype, pc_recurrspec, pc_alldayevent, pc_catid, pc_prefcatid, pc_title " .
172 "FROM openemr_postcalendar_events " .
173 "WHERE pc_aid = '$providerid' AND " .
174 "((pc_endDate >= '$sdate' AND pc_eventDate < '$edate') OR " .
175 "(pc_endDate = '0000-00-00' AND pc_eventDate >= '$sdate' AND pc_eventDate < '$edate'))";
176 $res = sqlStatement($query);
177 // print_r($res);
179 while ($row = sqlFetchArray($res)) {
180 $thistime = strtotime($row['pc_eventDate'] . " 00:00:00");
181 if ($row['pc_recurrtype']) {
182 preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
183 $repeattype = $matches[1];
185 preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
186 $repeatfreq = $matches[1];
187 if ($row['pc_recurrtype'] == 2) {
188 // Repeat type is 2 so frequency comes from event_repeat_on_freq.
189 preg_match('/"event_repeat_on_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
190 $repeatfreq = $matches[1];
193 if (! $repeatfreq) {
194 $repeatfreq = 1;
197 preg_match('/"event_repeat_on_num";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
198 $my_repeat_on_num = $matches[1];
200 preg_match('/"event_repeat_on_day";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
201 $my_repeat_on_day = $matches[1];
203 $endtime = strtotime($row['pc_endDate'] . " 00:00:00") + (24 * 60 * 60);
204 if ($endtime > $slotetime) {
205 $endtime = $slotetime;
208 $repeatix = 0;
209 while ($thistime < $endtime) {
210 // Skip the event if a repeat frequency > 1 was specified and this is
211 // not the desired occurrence.
212 if (! $repeatix) {
213 doOneDay(
214 $row['pc_catid'],
215 $thistime,
216 $row['pc_startTime'],
217 $row['pc_duration'],
218 $row['pc_prefcatid']
222 if (++$repeatix >= $repeatfreq) {
223 $repeatix = 0;
226 $adate = getdate($thistime);
228 if ($row['pc_recurrtype'] == 2) {
229 // Need to skip to nth or last weekday of the next month.
230 $adate['mon'] += 1;
231 if ($adate['mon'] > 12) {
232 $adate['year'] += 1;
233 $adate['mon'] -= 12;
236 if ($my_repeat_on_num < 5) { // not last
237 $adate['mday'] = 1;
238 $dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
239 if ($dow > $my_repeat_on_day) {
240 $dow -= 7;
243 $adate['mday'] += ($my_repeat_on_num - 1) * 7 + $my_repeat_on_day - $dow;
244 } else { // last weekday of month
245 $adate['mday'] = cal_days_in_month(CAL_GREGORIAN, $adate['mon'], $adate['year']);
246 $dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
247 if ($dow < $my_repeat_on_day) {
248 $dow += 7;
251 $adate['mday'] += $my_repeat_on_day - $dow;
253 } // end recurrtype 2
255 else { // recurrtype 1
257 if ($repeattype == 0) { // daily
258 $adate['mday'] += 1;
259 } else if ($repeattype == 1) { // weekly
260 $adate['mday'] += 7;
261 } else if ($repeattype == 2) { // monthly
262 $adate['mon'] += 1;
263 } else if ($repeattype == 3) { // yearly
264 $adate['year'] += 1;
265 } else if ($repeattype == 4) { // work days
266 if ($adate['wday'] == 5) { // if friday, skip to monday
267 $adate['mday'] += 3;
268 } else if ($adate['wday'] == 6) { // saturday should not happen
269 $adate['mday'] += 2;
270 } else {
271 $adate['mday'] += 1;
273 } else if ($repeattype == 5) { // monday
274 $adate['mday'] += 7;
275 } else if ($repeattype == 6) { // tuesday
276 $adate['mday'] += 7;
277 } else if ($repeattype == 7) { // wednesday
278 $adate['mday'] += 7;
279 } else if ($repeattype == 8) { // thursday
280 $adate['mday'] += 7;
281 } else if ($repeattype == 9) { // friday
282 $adate['mday'] += 7;
283 } else {
284 die("Invalid repeat type '$repeattype'");
286 } // end recurrtype 1
288 $thistime = mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']);
290 } else {
291 doOneDay(
292 $row['pc_catid'],
293 $thistime,
294 $row['pc_startTime'],
295 $row['pc_duration'],
296 $row['pc_prefcatid']
301 // Mark all slots reserved where the provider is not in-office.
302 // Actually we could do this in the display loop instead.
303 $inoffice = false;
304 for ($i = 0; $i < $slotcount; ++$i) {
305 if (($i % $slotsperday) == 0) {
306 $inoffice = false;
309 if ($slots[$i] & 1) {
310 $inoffice = true;
313 if ($slots[$i] & 2) {
314 $inoffice = false;
317 if (! $inoffice) {
318 $slots[$i] |= 4;
323 <html>
324 <head>
325 <?php html_header_show(); ?>
326 <title><?php xl('Find Available Appointments', 'e'); ?></title>
327 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
328 <link href="<?php echo $GLOBALS['assets_static_relative']; ?>/bootstrap-3-3-4/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
329 <?php if ($_SESSION['language_direction'] == 'rtl') { ?>
330 <link href="<?php echo $GLOBALS['assets_static_relative']; ?>/bootstrap-rtl-3-3-4/dist/css/bootstrap-rtl.min.css" rel="stylesheet" type="text/css" />
331 <?php } ?>
332 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
335 <!-- for the pop up calendar -->
336 <script src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-11-3/index.js" type="text/javascript"></script>
337 <script src="<?php echo $GLOBALS['assets_static_relative']; ?>/bootstrap-3-3-4/dist/js/bootstrap.min.js" type="text/javascript"></script>
338 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
340 <script>
342 function setappt(year,mon,mday,hours,minutes) {
343 if (opener.closed || ! opener.setappt)
344 alert('<?php xl('The destination form was closed; I cannot act on your selection.', 'e'); ?>');
345 else
346 opener.setappt(year,mon,mday,hours,minutes);
347 window.close();
348 return false;
351 </script>
354 <style>
355 form {
356 /* this eliminates the padding normally around a FORM tag */
357 padding: 0px;
358 margin: 0px;
360 #searchCriteria {
361 text-align: center;
362 width: 100%;
363 /* font-size: 0.8em; */
364 background-color: #bfe6ff;
365 font-weight: bold;
366 padding: 3px;
368 #searchResultsHeader {
369 width: 100%;
370 background-color: lightgrey;
372 #searchResultsHeader table {
373 width: 96%; /* not 100% because the 'searchResults' table has a scrollbar */
374 border-collapse: collapse;
376 #searchResultsHeader th {
377 /* font-size: 0.7em; */
379 #searchResults {
380 width: 100%;
381 height: 100%;
382 overflow: auto;
385 .srDate { width: 20%; }
386 .srTimes { width: 80%; }
388 #searchResults table {
389 width: 100%;
390 border-collapse: collapse;
391 background-color: white;
393 #searchResults td {
394 /* font-size: 0.7em; */
395 border-bottom: 1px solid gray;
396 padding: 1px 5px 1px 5px;
398 .highlight { background-color: #ff9; }
399 .blue_highlight { background-color: #BBCCDD; color: white; }
400 #am {
401 border-bottom: 1px solid lightgrey;
402 color: #00c;
404 #pm { color: #c00; }
405 #pm a { color: #c00; }
406 </style>
408 </head>
410 <body class="body_top">
412 <div id="searchCriteria">
413 <form method='post' name='theform' action='./find_appt_popup_user.php?providerid=<?php echo $providerid ?>&catid=<?php echo $input_catid ?>'>
414 <input type="hidden" name='bypatient' />
416 <?php xl('Start date:', 'e'); ?>
419 <input type='text' class='datepicker' name='startdate' id='startdate' size='10' value='<?php echo $sdate ?>'
420 title='yyyy-mm-dd starting date for search'/>
422 <?php xl('for', 'e'); ?>
423 <input type='text' name='searchdays' size='3' value='<?php echo $searchdays ?>'
424 title='Number of days to search from the start date' />
425 <?php xl('days', 'e'); ?>&nbsp;
426 <input type='submit' value='<?php xl('Search', 'e'); ?>'>
427 </div>
429 <?php if (!empty($slots)) : ?>
431 <div id="searchResultsHeader">
432 <table class='table table-bordered'>
433 <tr>
434 <th class="srDate"><?php xl('Day', 'e'); ?></th>
435 <th class="srTimes"><?php xl('Available Times', 'e'); ?></th>
436 </tr>
437 </table>
438 </div>
440 <div id="searchResults">
441 <table class='table table-condensed table-inversed table-bordered'>
442 <?php
443 $lastdate = "";
444 $ampmFlag = "am"; // establish an AM-PM line break flag
445 for ($i = 0; $i < $slotcount; ++$i) {
446 $available = true;
447 for ($j = $i; $j < $i + $catslots; ++$j) {
448 if ($slots[$j] >= 4) {
449 $available = false;
453 if (!$available) {
454 continue; // skip reserved slots
457 $utime = ($slotbase + $i) * $slotsecs;
458 $thisdate = date("Y-m-d", $utime);
459 if ($thisdate != $lastdate) {
460 // if a new day, start a new row
461 if ($lastdate) {
462 echo "</div>";
463 echo "</td>\n";
464 echo " </tr>\n";
467 $lastdate = $thisdate;
468 echo " <tr class='oneresult'>\n";
469 echo " <td class='srDate'>" . date("l", $utime)."<br>".date("Y-m-d", $utime) . "</td>\n";
470 echo " <td class='srTimes'>";
471 echo "<div id='am'>AM ";
472 $ampmFlag = "am"; // reset the AMPM flag
475 $ampm = date('a', $utime);
476 if ($ampmFlag != $ampm) {
477 echo "</div><div id='pm'>PM ";
480 $ampmFlag = $ampm;
482 $atitle = "Choose ".date("h:i a", $utime);
483 $adate = getdate($utime);
484 $anchor = "<a href='' onclick='return setappt(" .
485 $adate['year'] . "," .
486 $adate['mon'] . "," .
487 $adate['mday'] . "," .
488 $adate['hours'] . "," .
489 $adate['minutes'] . ")'".
490 " title='$atitle' alt='$atitle'".
491 ">";
492 echo (strlen(date('g', $utime)) < 2 ? "<span style='visibility:hidden'>0</span>" : "") .
493 $anchor . date("g:i", $utime) . "</a> ";
495 // If category duration is more than 1 slot, increment $i appropriately.
496 // This is to avoid reporting available times on undesirable boundaries.
497 $i += $catslots - 1;
500 if ($lastdate) {
501 echo "</td>\n";
502 echo " </tr>\n";
503 } else {
504 echo " <tr><td colspan='2'> " . xl('No openings were found for this period.', 'e') . "</td></tr>\n";
507 </table>
508 </div>
509 </div>
510 <?php endif; ?>
512 </form>
513 </body>
515 <script language='JavaScript'>
517 // jQuery stuff to make the page a little easier to use
518 $(document).ready(function(){
519 $(".oneresult").mouseover(function() { $(this).toggleClass("highlight"); });
520 $(".oneresult").mouseout(function() { $(this).toggleClass("highlight"); });
521 $(".oneresult a").mouseover(function () { $(this).toggleClass("blue_highlight"); $(this).children().toggleClass("blue_highlight"); });
522 $(".oneresult a").mouseout(function() { $(this).toggleClass("blue_highlight"); $(this).children().toggleClass("blue_highlight"); });
524 $('.datepicker').datetimepicker({
525 <?php $datetimepicker_timepicker = false; ?>
526 <?php $datetimepicker_formatInput = false; ?>
527 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
528 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
533 </script>
535 </html>