2 // Copyright (C) 2005-2006 Rod Roark <rod@sunsetsystems.com>
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.
13 //landing page definition -- where to go if something goes wrong
14 $landingpage = "index.php?site=".$_SESSION['site_id'];
17 // kick out if patient not authenticated
18 if ( isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite']) ) {
19 $pid = $_SESSION['pid'];
23 header('Location: '.$landingpage.'&w');
30 include_once("../interface/globals.php");
31 include_once("$srcdir/patient.inc");
33 // Exit if the modify calendar for portal flag is not set
34 if (!($GLOBALS['portal_onsite_appt_modify'])) {
35 echo htmlspecialchars( xl('You are not authorized to schedule appointments.'),ENT_NOQUOTES
);
39 $input_catid = $_REQUEST['catid'];
41 // Record an event into the slots array for a specified day.
42 function doOneDay($catid, $udate, $starttime, $duration, $prefcatid) {
43 global $slots, $slotsecs, $slotstime, $slotbase, $slotcount, $input_catid;
44 $udate = strtotime($starttime, $udate);
45 if ($udate < $slotstime) return;
46 $i = (int) ($udate / $slotsecs) - $slotbase;
47 $iend = (int) (($duration +
$slotsecs - 1) / $slotsecs) +
$i;
48 if ($iend > $slotcount) $iend = $slotcount;
49 if ($iend <= $i) $iend = $i +
1;
50 for (; $i < $iend; ++
$i) {
51 if ($catid == 2) { // in office
52 // If a category ID was specified when this popup was invoked, then select
53 // only IN events with a matching preferred category or with no preferred
54 // category; other IN events are to be treated as OUT events.
56 if ($prefcatid == $input_catid ||
!$prefcatid)
63 break; // ignore any positive duration for IN
64 } else if ($catid == 3) { // out of office
66 break; // ignore any positive duration for OUT
67 } else { // all other events reserve time
73 // seconds per time slot
74 $slotsecs = $GLOBALS['calendar_interval'] * 60;
78 $srow = sqlQuery("SELECT pc_duration FROM openemr_postcalendar_categories WHERE pc_catid = '$input_catid'");
79 if ($srow['pc_duration']) $catslots = ceil($srow['pc_duration'] / $slotsecs);
84 $searchdays = 7; // default to a 1-week lookahead
85 if ($_REQUEST['searchdays']) $searchdays = $_REQUEST['searchdays'];
88 if ($_REQUEST['startdate'] && preg_match("/(\d\d\d\d)\D*(\d\d)\D*(\d\d)/",
89 $_REQUEST['startdate'], $matches))
91 $sdate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
93 $sdate = date("Y-m-d");
96 // Get an end date - actually the date after the end date.
97 preg_match("/(\d\d\d\d)\D*(\d\d)\D*(\d\d)/", $sdate, $matches);
98 $edate = date("Y-m-d",
99 mktime(0, 0, 0, $matches[2], $matches[3] +
$searchdays, $matches[1]));
101 // compute starting time slot number and number of slots.
102 $slotstime = strtotime("$sdate 00:00:00");
103 $slotetime = strtotime("$edate 00:00:00");
104 $slotbase = (int) ($slotstime / $slotsecs);
105 $slotcount = (int) ($slotetime / $slotsecs) - $slotbase;
107 if ($slotcount <= 0 ||
$slotcount > 100000) die("Invalid date range.");
109 $slotsperday = (int) (60 * 60 * 24 / $slotsecs);
111 // If we have a provider, search.
113 if ($_REQUEST['providerid']) {
114 $providerid = $_REQUEST['providerid'];
116 // Create and initialize the slot array. Values are bit-mapped:
117 // bit 0 = in-office occurs here
118 // bit 1 = out-of-office occurs here
120 // So, values may range from 0 to 7.
122 $slots = array_pad(array(), $slotcount, 0);
124 // Note there is no need to sort the query results.
125 // echo $sdate." -- ".$edate;
126 $query = "SELECT pc_eventDate, pc_endDate, pc_startTime, pc_duration, " .
127 "pc_recurrtype, pc_recurrspec, pc_alldayevent, pc_catid, pc_prefcatid, pc_title " .
128 "FROM openemr_postcalendar_events " .
129 "WHERE pc_aid = '$providerid' AND " .
130 "((pc_endDate >= '$sdate' AND pc_eventDate < '$edate') OR " .
131 "(pc_endDate = '0000-00-00' AND pc_eventDate >= '$sdate' AND pc_eventDate < '$edate'))";
132 $res = sqlStatement($query);
135 while ($row = sqlFetchArray($res)) {
136 $thistime = strtotime($row['pc_eventDate'] . " 00:00:00");
137 if ($row['pc_recurrtype']) {
139 preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
140 $repeattype = $matches[1];
141 // echo "<br>".$row['pc_title'].", ".$row['pc_catid']."<br>repeattype = ".$repeattype."<br>";
143 preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
144 $repeatfreq = $matches[1];
145 // echo "repeatfreq = ".$repeatfreq."<br>";
146 if (! $repeatfreq) $repeatfreq = 1;
148 $endtime = strtotime($row['pc_endDate'] . " 00:00:00") +
(24 * 60 * 60);
149 // echo "endtime= ".$endtime."<br>";
150 if ($endtime > $slotetime) $endtime = $slotetime;
151 // echo "endtime= ".$endtime."<br>";
154 while ($thistime < $endtime) {
156 // Skip the event if a repeat frequency > 1 was specified and this is
157 // not the desired occurrence.
159 doOneDay($row['pc_catid'], $thistime, $row['pc_startTime'],
160 $row['pc_duration'], $row['pc_prefcatid']);
162 if (++
$repeatix >= $repeatfreq) $repeatix = 0;
164 $adate = getdate($thistime);
165 if ($repeattype == 0) { // daily
167 } else if ($repeattype == 1) { // weekly
169 } else if ($repeattype == 2) { // monthly
171 } else if ($repeattype == 3) { // yearly
173 } else if ($repeattype == 4) { // work days
174 if ($adate['wday'] == 5) // if friday, skip to monday
176 else if ($adate['wday'] == 6) // saturday should not happen
180 } else if ($repeattype == 5) { // monday
182 } else if ($repeattype == 6) { // tuesday
184 } else if ($repeattype == 7) { // wednesday
186 } else if ($repeattype == 8) { // thursday
188 } else if ($repeattype == 9) { // friday
191 die("Invalid repeat type '$repeattype'");
193 $thistime = mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']);
194 // echo 'thistime:'.$thistime.'<br>';
197 doOneDay($row['pc_catid'], $thistime, $row['pc_startTime'],
198 $row['pc_duration'], $row['pc_prefcatid']);
202 // Mark all slots reserved where the provider is not in-office.
203 // Actually we could do this in the display loop instead.
205 for ($i = 0; $i < $slotcount; ++
$i) {
206 if (($i %
$slotsperday) == 0) $inoffice = false;
207 if ($slots[$i] & 1) $inoffice = true;
208 if ($slots[$i] & 2) $inoffice = false;
209 if (! $inoffice) $slots[$i] |
= 4;
215 <?php
html_header_show(); ?
>
216 <title
><?php
xl('Find Available Appointments','e'); ?
></title
>
217 <link rel
="stylesheet" href
='<?php echo $css_header ?>' type
='text/css'>
219 <!-- for the pop up calendar
-->
220 <style type
="text/css">@import
url(../library
/dynarch_calendar
.css
);</style
>
221 <script type
="text/javascript" src
="../library/dynarch_calendar.js"></script
>
222 <script type
="text/javascript" src
="../library/dynarch_calendar_en.js"></script
>
223 <script type
="text/javascript" src
="../library/dynarch_calendar_setup.js"></script
>
225 <!-- for ajax
-y stuff
-->
226 <script type
="text/javascript" src
="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.2.2.min.js"></script
>
228 <script language
="JavaScript">
230 function setappt(year
,mon
,mday
,hours
,minutes
) {
231 if (opener
.closed ||
! opener
.setappt
)
232 alert('<?php xl('The destination form was closed
; I cannot act on your selection
.','e
'); ?>');
234 opener
.setappt(year
,mon
,mday
,hours
,minutes
);
244 /* this eliminates the padding normally around a FORM tag */
252 background
-color
: #ddddff;
256 #searchResultsHeader {
258 background
-color
: lightgrey
;
260 #searchResultsHeader table {
261 width
: 96%
; /* not 100% because the 'searchResults' table has a scrollbar */
262 border
-collapse
: collapse
;
264 #searchResultsHeader th {
273 .srDate
{ width
: 20%
; }
274 .srTimes
{ width
: 80%
; }
276 #searchResults table {
278 border
-collapse
: collapse
;
279 background
-color
: white
;
283 border
-bottom
: 1px solid gray
;
284 padding
: 1px
5px
1px
5px
;
286 .highlight
{ background
-color
: #ff9; }
287 .blue_highlight
{ background
-color
: #336699; color: white; }
289 border
-bottom
: 1px solid lightgrey
;
293 #pm a { color: #c00; }
298 <body
class="body_top">
300 <div id
="searchCriteria">
301 <form method
='post' name
='theform' action
='find_appt_popup.php?providerid=<?php echo $providerid ?>&catid=<?php echo $input_catid ?>'>
302 <input type
="hidden" name
='bypatient' />
304 <?php
xl('Start date:','e'); ?
>
307 <input type
='text' name
='startdate' id
='startdate' size
='10' value
='<?php echo $sdate ?>'
308 title
='yyyy-mm-dd starting date for search'/>
310 <img src
='../interface/pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
311 id
='img_date' border
='0' alt
='[?]' style
='cursor:pointer'
312 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
315 <?php
xl('for','e'); ?
>
316 <input type
='text' name
='searchdays' size
='3' value
='<?php echo $searchdays ?>'
317 title
='Number of days to search from the start date' />
318 <?php
xl('days','e'); ?
> 
;
319 <input type
='submit' value
='<?php xl('Search
','e
'); ?>'>
322 <?php
if (!empty($slots)) : ?
>
324 <div id
="searchResultsHeader">
327 <th
class="srDate"><?php
xl ('Day','e'); ?
></th
>
328 <th
class="srTimes"><?php
xl ('Available Times','e'); ?
></th
>
333 <div id
="searchResults">
337 $ampmFlag = "am"; // establish an AM-PM line break flag
338 for ($i = 0; $i < $slotcount; ++
$i) {
341 for ($j = $i; $j < $i +
$catslots; ++
$j) {
342 if ($slots[$j] >= 4) $available = false;
344 if (!$available) continue; // skip reserved slots
346 $utime = ($slotbase +
$i) * $slotsecs;
347 $thisdate = date("Y-m-d", $utime);
348 if ($thisdate != $lastdate) {
349 // if a new day, start a new row
355 $lastdate = $thisdate;
356 echo " <tr class='oneresult'>\n";
357 echo " <td class='srDate'>" . date("l", $utime)."<br>".date("Y-m-d", $utime) . "</td>\n";
358 echo " <td class='srTimes'>";
359 echo "<div id='am'>AM ";
360 $ampmFlag = "am"; // reset the AMPM flag
363 $ampm = date('a', $utime);
364 if ($ampmFlag != $ampm) { echo "</div><div id='pm'>PM "; }
367 $atitle = "Choose ".date("h:i a", $utime);
368 $adate = getdate($utime);
369 $anchor = "<a href='' onclick='return setappt(" .
370 $adate['year'] . "," .
371 $adate['mon'] . "," .
372 $adate['mday'] . "," .
373 $adate['hours'] . "," .
374 $adate['minutes'] . ")'".
375 " title='$atitle' alt='$atitle'".
377 echo (strlen(date('g',$utime)) < 2 ?
"<span style='visibility:hidden'>0</span>" : "") .
378 $anchor . date("g:i", $utime) . "</a> ";
380 // If category duration is more than 1 slot, increment $i appropriately.
381 // This is to avoid reporting available times on undesirable boundaries.
388 echo " <tr><td colspan='2'> " . xl('No openings were found for this period.','e') . "</td></tr>\n";
399 <!-- for the pop up calendar
-->
400 <script language
='JavaScript'>
401 Calendar
.setup({inputField
:"startdate", ifFormat
:"%Y-%m-%d", button
:"img_date"});
403 // jQuery stuff to make the page a little easier to use
405 $
(document
).ready(function(){
406 $
(".oneresult").mouseover(function() { $
(this
).toggleClass("highlight"); });
407 $
(".oneresult").mouseout(function() { $
(this
).toggleClass("highlight"); });
408 $
(".oneresult a").mouseover(function () { $
(this
).toggleClass("blue_highlight"); $
(this
).children().toggleClass("blue_highlight"); });
409 $
(".oneresult a").mouseout(function() { $
(this
).toggleClass("blue_highlight"); $
(this
).children().toggleClass("blue_highlight"); });
410 //$(".event").dblclick(function() { EditEvent(this); });