Updated documentation.
[openemr.git] / interface / main / calendar / find_appt_popup.php
blob52bd9b8567bc68e6f01faefb4627fc6def4b98d8
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 $res = sqlStatement($query);
106 while ($row = sqlFetchArray($res)) {
107 $thistime = strtotime($row['pc_eventDate'] . " 00:00:00");
108 if ($row['pc_recurrtype']) {
110 preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
111 $repeattype = $matches[1];
113 preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
114 $repeatfreq = $matches[1];
115 if (! $repeatfreq) $repeatfreq = 1;
117 $endtime = strtotime($row['pc_endDate'] . " 00:00:00") + (24 * 60 * 60);
118 if ($endtime > $slotetime) $endtime = $slotetime;
120 $repeatix = 0;
121 while ($thistime < $endtime) {
123 // Skip the event if a repeat frequency > 1 was specified and this is
124 // not the desired occurrence.
125 if (! $repeatix) {
126 doOneDay($row['pc_catid'], $thistime, $row['pc_startTime'],
127 $row['pc_duration'], $row['pc_prefcatid']);
129 if (++$repeatix >= $repeatfreq) $repeatix = 0;
131 $adate = getdate($thistime);
132 if ($repeattype == 0) { // daily
133 $adate['mday'] += 1;
134 } else if ($repeattype == 1) { // weekly
135 $adate['mday'] += 7;
136 } else if ($repeattype == 2) { // monthly
137 $adate['mon'] += 1;
138 } else if ($repeattype == 3) { // yearly
139 $adate['year'] += 1;
140 } else if ($repeattype == 4) { // work days
141 if ($adate['wday'] == 5) // if friday, skip to monday
142 $adate['mday'] += 3;
143 else if ($adate['wday'] == 6) // saturday should not happen
144 $adate['mday'] += 2;
145 else
146 $adate['mday'] += 1;
147 } else {
148 die("Invalid repeat type '$repeattype'");
150 $thistime = mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']);
152 } else {
153 doOneDay($row['pc_catid'], $thistime, $row['pc_startTime'],
154 $row['pc_duration'], $row['pc_prefcatid']);
158 // Mark all slots reserved where the provider is not in-office.
159 // Actually we could do this in the display loop instead.
160 $inoffice = false;
161 for ($i = 0; $i < $slotcount; ++$i) {
162 if (($i % $slotsperday) == 0) $inoffice = false;
163 if ($slots[$i] & 1) $inoffice = true;
164 if ($slots[$i] & 2) $inoffice = false;
165 if (! $inoffice) $slots[$i] |= 4;
169 <html>
170 <head>
171 <?php html_header_show(); ?>
172 <title><?php xl('Find Available Appointments','e'); ?></title>
173 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
175 <!-- for the pop up calendar -->
176 <style type="text/css">@import url(../../../library/dynarch_calendar.css);</style>
177 <script type="text/javascript" src="../../../library/dynarch_calendar.js"></script>
178 <script type="text/javascript" src="../../../library/dynarch_calendar_en.js"></script>
179 <script type="text/javascript" src="../../../library/dynarch_calendar_setup.js"></script>
181 <!-- for ajax-y stuff -->
182 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.2.2.min.js"></script>
184 <script language="JavaScript">
186 function setappt(year,mon,mday,hours,minutes) {
187 if (opener.closed || ! opener.setappt)
188 alert('<?php xl('The destination form was closed; I cannot act on your selection.','e'); ?>');
189 else
190 opener.setappt(year,mon,mday,hours,minutes);
191 window.close();
192 return false;
195 </script>
198 <style>
199 form {
200 /* this eliminates the padding normally around a FORM tag */
201 padding: 0px;
202 margin: 0px;
204 #searchCriteria {
205 text-align: center;
206 width: 100%;
207 font-size: 0.8em;
208 background-color: #ddddff;
209 font-weight: bold;
210 padding: 3px;
212 #searchResultsHeader {
213 width: 100%;
214 background-color: lightgrey;
216 #searchResultsHeader table {
217 width: 96%; /* not 100% because the 'searchResults' table has a scrollbar */
218 border-collapse: collapse;
220 #searchResultsHeader th {
221 font-size: 0.7em;
223 #searchResults {
224 width: 100%;
225 height: 350px;
226 overflow: auto;
229 .srDate { width: 20%; }
230 .srTimes { width: 80%; }
232 #searchResults table {
233 width: 100%;
234 border-collapse: collapse;
235 background-color: white;
237 #searchResults td {
238 font-size: 0.7em;
239 border-bottom: 1px solid gray;
240 padding: 1px 5px 1px 5px;
242 .highlight { background-color: #ff9; }
243 .blue_highlight { background-color: #336699; color: white; }
244 #am {
245 border-bottom: 1px solid lightgrey;
246 color: #00c;
248 #pm { color: #c00; }
249 #pm a { color: #c00; }
250 </style>
252 </head>
254 <body class="body_top">
256 <div id="searchCriteria">
257 <form method='post' name='theform' action='find_appt_popup.php?providerid=<?php echo $providerid ?>&catid=<?php echo $input_catid ?>'>
258 <?php xl('Start date:','e'); ?>
259 <input type='text' name='startdate' size='10' value='<?php echo $sdate ?>'
260 title='yyyy-mm-dd starting date for search' />
261 <img src='../../pic/show_calendar.gif' align='absbottom' width='24' height='22'
262 id='img_date' border='0' alt='[?]' style='cursor:pointer'
263 title='<?php xl('Click here to choose a date','e'); ?>'>
264 <?php xl('for','e'); ?>
265 <input type='text' name='searchdays' size='3' value='<?php echo $searchdays ?>'
266 title='Number of days to search from the start date' />
267 <?php xl('days','e'); ?>&nbsp;
268 <input type='submit' value='<?php xl('Search','e'); ?>'>
269 </div>
271 <?php if (!empty($slots)) : ?>
273 <div id="searchResultsHeader">
274 <table>
275 <tr>
276 <th class="srDate"><?php xl ('Day','e'); ?></th>
277 <th class="srTimes"><?php xl ('Available Times','e'); ?></th>
278 </tr>
279 </table>
280 </div>
282 <div id="searchResults">
283 <table>
284 <?php
285 $lastdate = "";
286 $ampmFlag = "am"; // establish an AM-PM line break flag
287 for ($i = 0; $i < $slotcount; ++$i) {
289 $available = true;
290 for ($j = $i; $j < $i + $catslots; ++$j) {
291 if ($slots[$j] >= 4) $available = false;
293 if (!$available) continue; // skip reserved slots
295 $utime = ($slotbase + $i) * $slotsecs;
296 $thisdate = date("Y-m-d", $utime);
297 if ($thisdate != $lastdate) {
298 // if a new day, start a new row
299 if ($lastdate) {
300 echo "</div>";
301 echo "</td>\n";
302 echo " </tr>\n";
304 $lastdate = $thisdate;
305 echo " <tr class='oneresult'>\n";
306 echo " <td class='srDate'>" . date("l", $utime)."<br>".date("Y-m-d", $utime) . "</td>\n";
307 echo " <td class='srTimes'>";
308 echo "<div id='am'>AM ";
309 $ampmFlag = "am"; // reset the AMPM flag
312 $ampm = date('a', $utime);
313 if ($ampmFlag != $ampm) { echo "</div><div id='pm'>PM "; }
314 $ampmFlag = $ampm;
316 $atitle = "Choose ".date("h:i a", $utime);
317 $adate = getdate($utime);
318 $anchor = "<a href='' onclick='return setappt(" .
319 $adate['year'] . "," .
320 $adate['mon'] . "," .
321 $adate['mday'] . "," .
322 $adate['hours'] . "," .
323 $adate['minutes'] . ")'".
324 " title='$atitle' alt='$atitle'".
325 ">";
326 echo (strlen(date('g',$utime)) < 2 ? "<span style='visibility:hidden'>0</span>" : "") .
327 $anchor . date("g:i", $utime) . "</a> ";
329 // If category duration is more than 1 slot, increment $i appropriately.
330 // This is to avoid reporting available times on undesirable boundaries.
331 $i += $catslots - 1;
333 if ($lastdate) {
334 echo "</td>\n";
335 echo " </tr>\n";
336 } else {
337 echo " <tr><td colspan='2'> " . xl('No openings were found for this period.','e') . "</td></tr>\n";
340 </table>
341 </div>
342 </div>
343 <?php endif; ?>
345 </form>
346 </body>
348 <!-- for the pop up calendar -->
349 <script language='JavaScript'>
350 Calendar.setup({inputField:"startdate", ifFormat:"%Y-%m-%d", button:"img_date"});
352 // jQuery stuff to make the page a little easier to use
354 $(document).ready(function(){
355 $(".oneresult").mouseover(function() { $(this).toggleClass("highlight"); });
356 $(".oneresult").mouseout(function() { $(this).toggleClass("highlight"); });
357 $(".oneresult a").mouseover(function () { $(this).toggleClass("blue_highlight"); $(this).children().toggleClass("blue_highlight"); });
358 $(".oneresult a").mouseout(function() { $(this).toggleClass("blue_highlight"); $(this).children().toggleClass("blue_highlight"); });
359 //$(".event").dblclick(function() { EditEvent(this); });
362 </script>
364 </html>