translation patches from Dr. Bosman and his crew
[openemr.git] / interface / main / calendar / find_appt_popup.php
blob55a7d5c5bed2ed3cce0f885812fcf6c58eaf4142
1 <?
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 <title><?php xl('Find Available Appointments','e'); ?></title>
172 <link rel=stylesheet href='<? echo $css_header ?>' type='text/css'>
174 <style>
175 td { font-size:10pt; }
176 </style>
178 <script language="JavaScript">
180 function setappt(year,mon,mday,hours,minutes) {
181 if (opener.closed || ! opener.setappt)
182 alert('<?php xl('The destination form was closed; I cannot act on your selection.','e'); ?>');
183 else
184 opener.setappt(year,mon,mday,hours,minutes);
185 window.close();
186 return false;
189 </script>
191 </head>
193 <body <?php echo $top_bg_line;?>>
196 <form method='post' name='theform' action='find_appt_popup.php?providerid=<?php echo $providerid ?>&catid=<?php echo $input_catid ?>'>
197 <center>
199 <table border='0' cellpadding='5' cellspacing='0'>
201 <tr>
202 <td height="1">
203 </td>
204 </tr>
206 <tr bgcolor='#ddddff'>
207 <td>
209 <?php xl('Start date:','e'); ?>
210 <input type='text' name='startdate' size='10' value='<? echo $sdate ?>'
211 title='yyyy-mm-dd starting date for search' />
212 <?php xl('for','e'); ?>
213 <input type='text' name='searchdays' size='3' value='<? echo $searchdays ?>'
214 title='Number of days to search from the start date' />
215 <?php xl('days','e'); ?>&nbsp;
216 <input type='submit' value='<?php xl('Search','e'); ?>'>
217 </b>
218 </td>
219 </tr>
221 <tr>
222 <td height="1">
223 </td>
224 </tr>
226 </table>
228 <? if (!empty($slots)) { ?>
230 <table border='0'>
231 <tr>
232 <td><b><?php xl('Day','e'); ?></b></td>
233 <td><b><?php xl('Date','e'); ?></b></td>
234 <td><b><?php xl('Available Times','e'); ?></b><?php xl('(red = p.m.)','e'); ?></td>
235 </tr>
237 $lastdate = "";
238 for ($i = 0; $i < $slotcount; ++$i) {
240 $available = true;
241 for ($j = $i; $j < $i + $catslots; ++$j) {
242 if ($slots[$j] >= 4) $available = false;
244 if (!$available) continue; // skip reserved slots
246 $utime = ($slotbase + $i) * $slotsecs;
247 $thisdate = date("Y-m-d", $utime);
248 if ($thisdate != $lastdate) { // if a new day, start a new row
249 if ($lastdate) {
250 echo "</td>\n";
251 echo " </tr>\n";
253 $lastdate = $thisdate;
254 echo " <tr>\n";
255 echo " <td valign='top'>" . date("l", $utime) . "</td>\n";
256 echo " <td valign='top'>" . date("Y-m-d", $utime) . "</td>\n";
257 echo " <td valign='top'>";
259 $adate = getdate($utime);
260 $anchor = "<a href='' style='color:" .
261 (date('a', $utime) == 'am' ? '#0000cc' : '#cc0000') .
262 "' onclick='return setappt(" .
263 $adate['year'] . "," .
264 $adate['mon'] . "," .
265 $adate['mday'] . "," .
266 $adate['hours'] . "," .
267 $adate['minutes'] . ")'>";
268 echo (strlen(date('g',$utime)) < 2 ? "<span style='visibility:hidden'>0</span>" : "") .
269 $anchor . date("g:i", $utime) . "</a> ";
271 // If category duration is more than 1 slot, increment $i appropriately.
272 // This is to avoid reporting available times on undesirable boundaries.
273 $i += $catslots - 1;
275 if ($lastdate) {
276 echo "</td>\n";
277 echo " </tr>\n";
278 } else {
279 echo " <tr><td colspan='3'> " . xl('No openings were found for this period.','e') . "</td></tr>\n";
282 </table>
284 <? } ?>
286 </center>
287 </form>
288 </body>
289 </html>