Adapting some reports to the new GUI style.
[openemr.git] / interface / main / calendar / find_appt_popup.php
blobe68728defbde360a7d713f51a12ca20eadcc2854
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 include_once("../../globals.php");
10 include_once("$srcdir/patient.inc");
12 $input_catid = $_REQUEST['catid'];
14 // Record an event into the slots array for a specified day.
15 function doOneDay($catid, $udate, $starttime, $duration, $prefcatid) {
16 global $slots, $slotsecs, $slotstime, $slotbase, $slotcount, $input_catid;
17 $udate = strtotime($starttime, $udate);
18 if ($udate < $slotstime) return;
19 $i = (int) ($udate / $slotsecs) - $slotbase;
20 $iend = (int) (($duration + $slotsecs - 1) / $slotsecs) + $i;
21 if ($iend > $slotcount) $iend = $slotcount;
22 if ($iend <= $i) $iend = $i + 1;
23 for (; $i < $iend; ++$i) {
24 if ($catid == 2) { // in office
25 // If a category ID was specified when this popup was invoked, then select
26 // only IN events with a matching preferred category or with no preferred
27 // category; other IN events are to be treated as OUT events.
28 if ($input_catid) {
29 if ($prefcatid == $input_catid || !$prefcatid)
30 $slots[$i] |= 1;
31 else
32 $slots[$i] |= 2;
33 } else {
34 $slots[$i] |= 1;
36 break; // ignore any positive duration for IN
37 } else if ($catid == 3) { // out of office
38 $slots[$i] |= 2;
39 break; // ignore any positive duration for OUT
40 } else { // all other events reserve time
41 $slots[$i] |= 4;
46 // seconds per time slot
47 $slotsecs = $GLOBALS['calendar_interval'] * 60;
49 $catslots = 1;
50 if ($input_catid) {
51 $srow = sqlQuery("SELECT pc_duration FROM openemr_postcalendar_categories WHERE pc_catid = '$input_catid'");
52 if ($srow['pc_duration']) $catslots = ceil($srow['pc_duration'] / $slotsecs);
55 $info_msg = "";
57 $searchdays = 7; // default to a 1-week lookahead
58 if ($_REQUEST['searchdays']) $searchdays = $_REQUEST['searchdays'];
60 // Get a start date.
61 if ($_REQUEST['startdate'] && preg_match("/(\d\d\d\d)\D*(\d\d)\D*(\d\d)/",
62 $_REQUEST['startdate'], $matches))
64 $sdate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
65 } else {
66 $sdate = date("Y-m-d");
69 // Get an end date - actually the date after the end date.
70 preg_match("/(\d\d\d\d)\D*(\d\d)\D*(\d\d)/", $sdate, $matches);
71 $edate = date("Y-m-d",
72 mktime(0, 0, 0, $matches[2], $matches[3] + $searchdays, $matches[1]));
74 // compute starting time slot number and number of slots.
75 $slotstime = strtotime("$sdate 00:00:00");
76 $slotetime = strtotime("$edate 00:00:00");
77 $slotbase = (int) ($slotstime / $slotsecs);
78 $slotcount = (int) ($slotetime / $slotsecs) - $slotbase;
80 if ($slotcount <= 0 || $slotcount > 100000) die("Invalid date range.");
82 $slotsperday = (int) (60 * 60 * 24 / $slotsecs);
84 // If we have a provider, search.
86 if ($_REQUEST['providerid']) {
87 $providerid = $_REQUEST['providerid'];
89 // Create and initialize the slot array. Values are bit-mapped:
90 // bit 0 = in-office occurs here
91 // bit 1 = out-of-office occurs here
92 // bit 2 = reserved
93 // So, values may range from 0 to 7.
95 $slots = array_pad(array(), $slotcount, 0);
97 // Note there is no need to sort the query results.
98 $query = "SELECT pc_eventDate, pc_endDate, pc_startTime, pc_duration, " .
99 "pc_recurrtype, pc_recurrspec, pc_alldayevent, pc_catid, pc_prefcatid " .
100 "FROM openemr_postcalendar_events " .
101 "WHERE pc_aid = '$providerid' AND " .
102 "((pc_endDate >= '$sdate' AND pc_eventDate < '$edate') OR " .
103 "(pc_endDate = '0000-00-00' AND pc_eventDate >= '$sdate' AND pc_eventDate < '$edate'))";
104 // phyaura whimmel facility filtering
105 if ($_REQUEST['facility'] > 0 ) {
106 $facility = $_REQUEST['facility'];
107 $query .= " AND pc_facility = $facility";
109 // end facility filtering whimmel 29apr08
110 $res = sqlStatement($query);
112 while ($row = sqlFetchArray($res)) {
113 $thistime = strtotime($row['pc_eventDate'] . " 00:00:00");
114 if ($row['pc_recurrtype']) {
116 preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
117 $repeattype = $matches[1];
119 preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
120 $repeatfreq = $matches[1];
121 if (! $repeatfreq) $repeatfreq = 1;
123 $endtime = strtotime($row['pc_endDate'] . " 00:00:00") + (24 * 60 * 60);
124 if ($endtime > $slotetime) $endtime = $slotetime;
126 $repeatix = 0;
127 while ($thistime < $endtime) {
129 // Skip the event if a repeat frequency > 1 was specified and this is
130 // not the desired occurrence.
131 if (! $repeatix) {
132 doOneDay($row['pc_catid'], $thistime, $row['pc_startTime'],
133 $row['pc_duration'], $row['pc_prefcatid']);
135 if (++$repeatix >= $repeatfreq) $repeatix = 0;
137 $adate = getdate($thistime);
138 if ($repeattype == 0) { // daily
139 $adate['mday'] += 1;
140 } else if ($repeattype == 1) { // weekly
141 $adate['mday'] += 7;
142 } else if ($repeattype == 2) { // monthly
143 $adate['mon'] += 1;
144 } else if ($repeattype == 3) { // yearly
145 $adate['year'] += 1;
146 } else if ($repeattype == 4) { // work days
147 if ($adate['wday'] == 5) // if friday, skip to monday
148 $adate['mday'] += 3;
149 else if ($adate['wday'] == 6) // saturday should not happen
150 $adate['mday'] += 2;
151 else
152 $adate['mday'] += 1;
153 } else {
154 die("Invalid repeat type '$repeattype'");
156 $thistime = mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']);
158 } else {
159 doOneDay($row['pc_catid'], $thistime, $row['pc_startTime'],
160 $row['pc_duration'], $row['pc_prefcatid']);
164 // Mark all slots reserved where the provider is not in-office.
165 // Actually we could do this in the display loop instead.
166 $inoffice = false;
167 for ($i = 0; $i < $slotcount; ++$i) {
168 if (($i % $slotsperday) == 0) $inoffice = false;
169 if ($slots[$i] & 1) $inoffice = true;
170 if ($slots[$i] & 2) $inoffice = false;
171 if (! $inoffice) $slots[$i] |= 4;
175 <html>
176 <head>
177 <?php html_header_show(); ?>
178 <title><?php xl('Find Available Appointments','e'); ?></title>
179 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
181 <!-- for the pop up calendar -->
182 <style type="text/css">@import url(../../../library/dynarch_calendar.css);</style>
183 <script type="text/javascript" src="../../../library/dynarch_calendar.js"></script>
184 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
185 <script type="text/javascript" src="../../../library/dynarch_calendar_setup.js"></script>
187 <!-- for ajax-y stuff -->
188 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.2.2.min.js"></script>
190 <script language="JavaScript">
192 function setappt(year,mon,mday,hours,minutes) {
193 if (opener.closed || ! opener.setappt)
194 alert('<?php xl('The destination form was closed; I cannot act on your selection.','e'); ?>');
195 else
196 opener.setappt(year,mon,mday,hours,minutes);
197 window.close();
198 return false;
201 </script>
204 <style>
205 form {
206 /* this eliminates the padding normally around a FORM tag */
207 padding: 0px;
208 margin: 0px;
210 #searchCriteria {
211 text-align: center;
212 width: 100%;
213 font-size: 0.8em;
214 background-color: #ddddff;
215 font-weight: bold;
216 padding: 3px;
218 #searchResultsHeader {
219 width: 100%;
220 background-color: lightgrey;
222 #searchResultsHeader table {
223 width: 96%; /* not 100% because the 'searchResults' table has a scrollbar */
224 border-collapse: collapse;
226 #searchResultsHeader th {
227 font-size: 0.7em;
229 #searchResults {
230 width: 100%;
231 height: 350px;
232 overflow: auto;
235 .srDate { width: 20%; }
236 .srTimes { width: 80%; }
238 #searchResults table {
239 width: 100%;
240 border-collapse: collapse;
241 background-color: white;
243 #searchResults td {
244 font-size: 0.7em;
245 border-bottom: 1px solid gray;
246 padding: 1px 5px 1px 5px;
248 .highlight { background-color: #ff9; }
249 .blue_highlight { background-color: #336699; color: white; }
250 #am {
251 border-bottom: 1px solid lightgrey;
252 color: #00c;
254 #pm { color: #c00; }
255 #pm a { color: #c00; }
256 </style>
258 </head>
260 <body class="body_top">
262 <div id="searchCriteria">
263 <form method='post' name='theform' action='find_appt_popup.php?providerid=<?php echo $providerid ?>&catid=<?php echo $input_catid ?>'>
264 <?php xl('Start date:','e'); ?>
265 <input type='text' name='startdate' id='startdate' size='10' value='<?php echo $sdate ?>'
266 title='yyyy-mm-dd starting date for search' />
267 <img src='../../pic/show_calendar.gif' align='absbottom' width='24' height='22'
268 id='img_date' border='0' alt='[?]' style='cursor:pointer'
269 title='<?php xl('Click here to choose a date','e'); ?>'>
270 <?php xl('for','e'); ?>
271 <input type='text' name='searchdays' size='3' value='<?php echo $searchdays ?>'
272 title='Number of days to search from the start date' />
273 <?php xl('days','e'); ?>&nbsp;
274 <input type='submit' value='<?php xl('Search','e'); ?>'>
275 </div>
277 <?php if (!empty($slots)) : ?>
279 <div id="searchResultsHeader">
280 <table>
281 <tr>
282 <th class="srDate"><?php xl ('Day','e'); ?></th>
283 <th class="srTimes"><?php xl ('Available Times','e'); ?></th>
284 </tr>
285 </table>
286 </div>
288 <div id="searchResults">
289 <table>
290 <?php
291 $lastdate = "";
292 $ampmFlag = "am"; // establish an AM-PM line break flag
293 for ($i = 0; $i < $slotcount; ++$i) {
295 $available = true;
296 for ($j = $i; $j < $i + $catslots; ++$j) {
297 if ($slots[$j] >= 4) $available = false;
299 if (!$available) continue; // skip reserved slots
301 $utime = ($slotbase + $i) * $slotsecs;
302 $thisdate = date("Y-m-d", $utime);
303 if ($thisdate != $lastdate) {
304 // if a new day, start a new row
305 if ($lastdate) {
306 echo "</div>";
307 echo "</td>\n";
308 echo " </tr>\n";
310 $lastdate = $thisdate;
311 echo " <tr class='oneresult'>\n";
312 echo " <td class='srDate'>" . date("l", $utime)."<br>".date("Y-m-d", $utime) . "</td>\n";
313 echo " <td class='srTimes'>";
314 echo "<div id='am'>AM ";
315 $ampmFlag = "am"; // reset the AMPM flag
318 $ampm = date('a', $utime);
319 if ($ampmFlag != $ampm) { echo "</div><div id='pm'>PM "; }
320 $ampmFlag = $ampm;
322 $atitle = "Choose ".date("h:i a", $utime);
323 $adate = getdate($utime);
324 $anchor = "<a href='' onclick='return setappt(" .
325 $adate['year'] . "," .
326 $adate['mon'] . "," .
327 $adate['mday'] . "," .
328 $adate['hours'] . "," .
329 $adate['minutes'] . ")'".
330 " title='$atitle' alt='$atitle'".
331 ">";
332 echo (strlen(date('g',$utime)) < 2 ? "<span style='visibility:hidden'>0</span>" : "") .
333 $anchor . date("g:i", $utime) . "</a> ";
335 // If category duration is more than 1 slot, increment $i appropriately.
336 // This is to avoid reporting available times on undesirable boundaries.
337 $i += $catslots - 1;
339 if ($lastdate) {
340 echo "</td>\n";
341 echo " </tr>\n";
342 } else {
343 echo " <tr><td colspan='2'> " . xl('No openings were found for this period.','e') . "</td></tr>\n";
346 </table>
347 </div>
348 </div>
349 <?php endif; ?>
351 </form>
352 </body>
354 <!-- for the pop up calendar -->
355 <script language='JavaScript'>
356 Calendar.setup({inputField:"startdate", ifFormat:"%Y-%m-%d", button:"img_date"});
358 // jQuery stuff to make the page a little easier to use
360 $(document).ready(function(){
361 $(".oneresult").mouseover(function() { $(this).toggleClass("highlight"); });
362 $(".oneresult").mouseout(function() { $(this).toggleClass("highlight"); });
363 $(".oneresult a").mouseover(function () { $(this).toggleClass("blue_highlight"); $(this).children().toggleClass("blue_highlight"); });
364 $(".oneresult a").mouseout(function() { $(this).toggleClass("blue_highlight"); $(this).children().toggleClass("blue_highlight"); });
365 //$(".event").dblclick(function() { EditEvent(this); });
368 </script>
370 </html>