incremented patch version 13
[openemr.git] / patients / find_appt_popup_user.php
blob34d7c6627c341b0a7748f9263965c71f48ed4524
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 //landing page definition -- where to go if something goes wrong
10 $landingpage = "index.php";
13 // kick out if patient not authenticated
14 session_start();
15 if ( isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite']) ) {
16 $pid = $_SESSION['pid'];
18 else {
19 session_destroy();
20 header('Location: '.$landingpage.'?w');
21 exit;
25 $ignoreAuth = 1;
27 include_once("../interface/globals.php");
28 include_once("$srcdir/patient.inc");
30 // Exit if the modify calendar for portal flag is not set
31 if (!($GLOBALS['portal_onsite_appt_modify'])) {
32 echo htmlspecialchars( xl('You are not authorized to schedule appointments.'),ENT_NOQUOTES);
33 exit;
36 $input_catid = $_REQUEST['catid'];
38 // Record an event into the slots array for a specified day.
39 function doOneDay($catid, $udate, $starttime, $duration, $prefcatid) {
40 global $slots, $slotsecs, $slotstime, $slotbase, $slotcount, $input_catid;
41 $udate = strtotime($starttime, $udate);
42 if ($udate < $slotstime) return;
43 $i = (int) ($udate / $slotsecs) - $slotbase;
44 $iend = (int) (($duration + $slotsecs - 1) / $slotsecs) + $i;
45 if ($iend > $slotcount) $iend = $slotcount;
46 if ($iend <= $i) $iend = $i + 1;
47 for (; $i < $iend; ++$i) {
48 if ($catid == 2) { // in office
49 // If a category ID was specified when this popup was invoked, then select
50 // only IN events with a matching preferred category or with no preferred
51 // category; other IN events are to be treated as OUT events.
52 if ($input_catid) {
53 if ($prefcatid == $input_catid || !$prefcatid)
54 $slots[$i] |= 1;
55 else
56 $slots[$i] |= 2;
57 } else {
58 $slots[$i] |= 1;
60 break; // ignore any positive duration for IN
61 } else if ($catid == 3) { // out of office
62 $slots[$i] |= 2;
63 break; // ignore any positive duration for OUT
64 } else { // all other events reserve time
65 $slots[$i] |= 4;
70 // seconds per time slot
71 $slotsecs = $GLOBALS['calendar_interval'] * 60;
73 $catslots = 1;
74 if ($input_catid) {
75 $srow = sqlQuery("SELECT pc_duration FROM openemr_postcalendar_categories WHERE pc_catid = '$input_catid'");
76 if ($srow['pc_duration']) $catslots = ceil($srow['pc_duration'] / $slotsecs);
79 $info_msg = "";
81 $searchdays = 7; // default to a 1-week lookahead
82 if ($_REQUEST['searchdays']) $searchdays = $_REQUEST['searchdays'];
84 // Get a start date.
85 if ($_REQUEST['startdate'] && preg_match("/(\d\d\d\d)\D*(\d\d)\D*(\d\d)/",
86 $_REQUEST['startdate'], $matches))
88 $sdate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
89 } else {
90 $sdate = date("Y-m-d");
93 // Get an end date - actually the date after the end date.
94 preg_match("/(\d\d\d\d)\D*(\d\d)\D*(\d\d)/", $sdate, $matches);
95 $edate = date("Y-m-d",
96 mktime(0, 0, 0, $matches[2], $matches[3] + $searchdays, $matches[1]));
98 // compute starting time slot number and number of slots.
99 $slotstime = strtotime("$sdate 00:00:00");
100 $slotetime = strtotime("$edate 00:00:00");
101 $slotbase = (int) ($slotstime / $slotsecs);
102 $slotcount = (int) ($slotetime / $slotsecs) - $slotbase;
104 if ($slotcount <= 0 || $slotcount > 100000) die("Invalid date range.");
106 $slotsperday = (int) (60 * 60 * 24 / $slotsecs);
108 // If we have a provider, search.
110 if ($_REQUEST['providerid']) {
111 $providerid = $_REQUEST['providerid'];
113 // Create and initialize the slot array. Values are bit-mapped:
114 // bit 0 = in-office occurs here
115 // bit 1 = out-of-office occurs here
116 // bit 2 = reserved
117 // So, values may range from 0 to 7.
119 $slots = array_pad(array(), $slotcount, 0);
121 // Note there is no need to sort the query results.
122 // echo $sdate." -- ".$edate;
123 $query = "SELECT pc_eventDate, pc_endDate, pc_startTime, pc_duration, " .
124 "pc_recurrtype, pc_recurrspec, pc_alldayevent, pc_catid, pc_prefcatid, pc_title " .
125 "FROM openemr_postcalendar_events " .
126 "WHERE pc_aid = '$providerid' AND " .
127 "((pc_endDate >= '$sdate' AND pc_eventDate < '$edate') OR " .
128 "(pc_endDate = '0000-00-00' AND pc_eventDate >= '$sdate' AND pc_eventDate < '$edate'))";
129 $res = sqlStatement($query);
130 // print_r($res);
132 while ($row = sqlFetchArray($res)) {
133 $thistime = strtotime($row['pc_eventDate'] . " 00:00:00");
134 if ($row['pc_recurrtype']) {
136 preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
137 $repeattype = $matches[1];
138 // echo "<br>".$row['pc_title'].", ".$row['pc_catid']."<br>repeattype = ".$repeattype."<br>";
140 preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
141 $repeatfreq = $matches[1];
142 // echo "repeatfreq = ".$repeatfreq."<br>";
143 if (! $repeatfreq) $repeatfreq = 1;
145 $endtime = strtotime($row['pc_endDate'] . " 00:00:00") + (24 * 60 * 60);
146 // echo "endtime= ".$endtime."<br>";
147 if ($endtime > $slotetime) $endtime = $slotetime;
148 // echo "endtime= ".$endtime."<br>";
150 $repeatix = 0;
151 while ($thistime < $endtime) {
153 // Skip the event if a repeat frequency > 1 was specified and this is
154 // not the desired occurrence.
155 if (! $repeatix) {
156 doOneDay($row['pc_catid'], $thistime, $row['pc_startTime'],
157 $row['pc_duration'], $row['pc_prefcatid']);
159 if (++$repeatix >= $repeatfreq) $repeatix = 0;
161 $adate = getdate($thistime);
162 if ($repeattype == 0) { // daily
163 $adate['mday'] += 1;
164 } else if ($repeattype == 1) { // weekly
165 $adate['mday'] += 7;
166 } else if ($repeattype == 2) { // monthly
167 $adate['mon'] += 1;
168 } else if ($repeattype == 3) { // yearly
169 $adate['year'] += 1;
170 } else if ($repeattype == 4) { // work days
171 if ($adate['wday'] == 5) // if friday, skip to monday
172 $adate['mday'] += 3;
173 else if ($adate['wday'] == 6) // saturday should not happen
174 $adate['mday'] += 2;
175 else
176 $adate['mday'] += 1;
177 } else if ($repeattype == 5) { // monday
178 $adate['mday'] += 7;
179 } else if ($repeattype == 6) { // tuesday
180 $adate['mday'] += 7;
181 } else if ($repeattype == 7) { // wednesday
182 $adate['mday'] += 7;
183 } else if ($repeattype == 8) { // thursday
184 $adate['mday'] += 7;
185 } else if ($repeattype == 9) { // friday
186 $adate['mday'] += 7;
187 } else {
188 die("Invalid repeat type '$repeattype'");
190 $thistime = mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']);
191 // echo 'thistime:'.$thistime.'<br>';
193 } else {
194 doOneDay($row['pc_catid'], $thistime, $row['pc_startTime'],
195 $row['pc_duration'], $row['pc_prefcatid']);
199 // Mark all slots reserved where the provider is not in-office.
200 // Actually we could do this in the display loop instead.
201 $inoffice = false;
202 for ($i = 0; $i < $slotcount; ++$i) {
203 if (($i % $slotsperday) == 0) $inoffice = false;
204 if ($slots[$i] & 1) $inoffice = true;
205 if ($slots[$i] & 2) $inoffice = false;
206 if (! $inoffice) $slots[$i] |= 4;
210 <html>
211 <head>
212 <?php html_header_show(); ?>
213 <title><?php xl('Find Available Appointments','e'); ?></title>
214 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
216 <!-- for the pop up calendar -->
217 <style type="text/css">@import url(../library/dynarch_calendar.css);</style>
218 <script type="text/javascript" src="../library/dynarch_calendar.js"></script>
219 <script type="text/javascript" src="../library/dynarch_calendar_en.js"></script>
220 <script type="text/javascript" src="../library/dynarch_calendar_setup.js"></script>
222 <!-- for ajax-y stuff -->
223 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.2.2.min.js"></script>
225 <script language="JavaScript">
227 function setappt(year,mon,mday,hours,minutes) {
228 if (opener.closed || ! opener.setappt)
229 alert('<?php xl('The destination form was closed; I cannot act on your selection.','e'); ?>');
230 else
231 opener.setappt(year,mon,mday,hours,minutes);
232 window.close();
233 return false;
236 </script>
239 <style>
240 form {
241 /* this eliminates the padding normally around a FORM tag */
242 padding: 0px;
243 margin: 0px;
245 #searchCriteria {
246 text-align: center;
247 width: 100%;
248 font-size: 0.8em;
249 background-color: #ddddff;
250 font-weight: bold;
251 padding: 3px;
253 #searchResultsHeader {
254 width: 100%;
255 background-color: lightgrey;
257 #searchResultsHeader table {
258 width: 96%; /* not 100% because the 'searchResults' table has a scrollbar */
259 border-collapse: collapse;
261 #searchResultsHeader th {
262 font-size: 0.7em;
264 #searchResults {
265 width: 100%;
266 height: 350px;
267 overflow: auto;
270 .srDate { width: 20%; }
271 .srTimes { width: 80%; }
273 #searchResults table {
274 width: 100%;
275 border-collapse: collapse;
276 background-color: white;
278 #searchResults td {
279 font-size: 0.7em;
280 border-bottom: 1px solid gray;
281 padding: 1px 5px 1px 5px;
283 .highlight { background-color: #ff9; }
284 .blue_highlight { background-color: #336699; color: white; }
285 #am {
286 border-bottom: 1px solid lightgrey;
287 color: #00c;
289 #pm { color: #c00; }
290 #pm a { color: #c00; }
291 </style>
293 </head>
295 <body class="body_top">
297 <div id="searchCriteria">
298 <form method='post' name='theform' action='find_appt_popup.php?providerid=<?php echo $providerid ?>&catid=<?php echo $input_catid ?>'>
299 <input type="hidden" name='bypatient' />
301 <?php xl('Start date:','e'); ?>
304 <input type='text' name='startdate' id='startdate' size='10' value='<?php echo $sdate ?>'
305 title='yyyy-mm-dd starting date for search'/>
307 <img src='../interface/pic/show_calendar.gif' align='absbottom' width='24' height='22'
308 id='img_date' border='0' alt='[?]' style='cursor:pointer'
309 title='<?php xl('Click here to choose a date','e'); ?>'>
312 <?php xl('for','e'); ?>
313 <input type='text' name='searchdays' size='3' value='<?php echo $searchdays ?>'
314 title='Number of days to search from the start date' />
315 <?php xl('days','e'); ?>&nbsp;
316 <input type='submit' value='<?php xl('Search','e'); ?>'>
317 </div>
319 <?php if (!empty($slots)) : ?>
321 <div id="searchResultsHeader">
322 <table>
323 <tr>
324 <th class="srDate"><?php xl ('Day','e'); ?></th>
325 <th class="srTimes"><?php xl ('Available Times','e'); ?></th>
326 </tr>
327 </table>
328 </div>
330 <div id="searchResults">
331 <table>
332 <?php
333 $lastdate = "";
334 $ampmFlag = "am"; // establish an AM-PM line break flag
335 for ($i = 0; $i < $slotcount; ++$i) {
337 $available = true;
338 for ($j = $i; $j < $i + $catslots; ++$j) {
339 if ($slots[$j] >= 4) $available = false;
341 if (!$available) continue; // skip reserved slots
343 $utime = ($slotbase + $i) * $slotsecs;
344 $thisdate = date("Y-m-d", $utime);
345 if ($thisdate != $lastdate) {
346 // if a new day, start a new row
347 if ($lastdate) {
348 echo "</div>";
349 echo "</td>\n";
350 echo " </tr>\n";
352 $lastdate = $thisdate;
353 echo " <tr class='oneresult'>\n";
354 echo " <td class='srDate'>" . date("l", $utime)."<br>".date("Y-m-d", $utime) . "</td>\n";
355 echo " <td class='srTimes'>";
356 echo "<div id='am'>AM ";
357 $ampmFlag = "am"; // reset the AMPM flag
360 $ampm = date('a', $utime);
361 if ($ampmFlag != $ampm) { echo "</div><div id='pm'>PM "; }
362 $ampmFlag = $ampm;
364 $atitle = "Choose ".date("h:i a", $utime);
365 $adate = getdate($utime);
366 $anchor = "<a href='' onclick='return setappt(" .
367 $adate['year'] . "," .
368 $adate['mon'] . "," .
369 $adate['mday'] . "," .
370 $adate['hours'] . "," .
371 $adate['minutes'] . ")'".
372 " title='$atitle' alt='$atitle'".
373 ">";
374 echo (strlen(date('g',$utime)) < 2 ? "<span style='visibility:hidden'>0</span>" : "") .
375 $anchor . date("g:i", $utime) . "</a> ";
377 // If category duration is more than 1 slot, increment $i appropriately.
378 // This is to avoid reporting available times on undesirable boundaries.
379 $i += $catslots - 1;
381 if ($lastdate) {
382 echo "</td>\n";
383 echo " </tr>\n";
384 } else {
385 echo " <tr><td colspan='2'> " . xl('No openings were found for this period.','e') . "</td></tr>\n";
388 </table>
389 </div>
390 </div>
391 <?php endif; ?>
393 </form>
394 </body>
396 <!-- for the pop up calendar -->
397 <script language='JavaScript'>
398 Calendar.setup({inputField:"startdate", ifFormat:"%Y-%m-%d", button:"img_date"});
400 // jQuery stuff to make the page a little easier to use
402 $(document).ready(function(){
403 $(".oneresult").mouseover(function() { $(this).toggleClass("highlight"); });
404 $(".oneresult").mouseout(function() { $(this).toggleClass("highlight"); });
405 $(".oneresult a").mouseover(function () { $(this).toggleClass("blue_highlight"); $(this).children().toggleClass("blue_highlight"); });
406 $(".oneresult a").mouseout(function() { $(this).toggleClass("blue_highlight"); $(this).children().toggleClass("blue_highlight"); });
407 //$(".event").dblclick(function() { EditEvent(this); });
410 </script>
412 </html>