PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/mrbs/tags/mrbs-1_2_4/web/day.php

https://github.com/jessfishenden/mrbs-mcr
PHP | 372 lines | 255 code | 41 blank | 76 comment | 57 complexity | 8972e11016939f5566535fc2b4b6f67d MD5 | raw file
  1. <?php
  2. # $Id$
  3. require_once "grab_globals.inc.php";
  4. include "config.inc.php";
  5. include "functions.inc";
  6. include "$dbsys.inc";
  7. include "mrbs_auth.inc";
  8. include "mincals.inc";
  9. if (empty($debug_flag)) $debug_flag = 0;
  10. #If we dont know the right date then make it up
  11. if (!isset($day) or !isset($month) or !isset($year))
  12. {
  13. $day = date("d");
  14. $month = date("m");
  15. $year = date("Y");
  16. } else {
  17. # Make the date valid if day is more then number of days in month
  18. while (!checkdate(intval($month), intval($day), intval($year)))
  19. $day--;
  20. }
  21. if (empty($area))
  22. $area = get_default_area();
  23. # print the page header
  24. print_header($day, $month, $year, $area);
  25. $format = "Gi";
  26. if( $enable_periods ) {
  27. $format = "i";
  28. $resolution = 60;
  29. $morningstarts = 12;
  30. $morningstarts_minutes = 0;
  31. $eveningends = 12;
  32. $eveningends_minutes = count($periods)-1;
  33. }
  34. # ensure that $morningstarts_minutes defaults to zero if not set
  35. if( empty( $morningstarts_minutes ) )
  36. $morningstarts_minutes=0;
  37. # Define the start and end of each day in a way which is not affected by
  38. # daylight saving...
  39. # dst_change:
  40. # -1 => no change
  41. # 0 => entering DST
  42. # 1 => leaving DST
  43. $dst_change = is_dst($month,$day,$year);
  44. $am7=mktime($morningstarts,$morningstarts_minutes,0,$month,$day,$year,is_dst($month,$day,$year,$morningstarts));
  45. $pm7=mktime($eveningends,$eveningends_minutes,0,$month,$day,$year,is_dst($month,$day,$year,$eveningends));
  46. if ( $pview != 1 ) {
  47. echo "<table width=\"100%\"><tr><td width=\"60%\">";
  48. #Show all avaliable areas
  49. echo "<u>".get_vocab("areas")."</u><br>";
  50. # need to show either a select box or a normal html list,
  51. # depending on the settings in config.inc.php
  52. if ($area_list_format == "select") {
  53. echo make_area_select_html('day.php', $area, $year, $month, $day); # from functions.inc
  54. } else {
  55. # show the standard html list
  56. $sql = "select id, area_name from $tbl_area order by area_name";
  57. $res = sql_query($sql);
  58. if ($res) for ($i = 0; ($row = sql_row($res, $i)); $i++)
  59. {
  60. echo "<a href=\"day.php?year=$year&month=$month&day=$day&area=$row[0]\">";
  61. if ($row[0] == $area)
  62. echo "<font color=\"red\">" . htmlspecialchars($row[1]) . "</font></a><br>\n";
  63. else echo htmlspecialchars($row[1]) . "</a><br>\n";
  64. }
  65. }
  66. echo "</td>\n";
  67. #Draw the three month calendars
  68. minicals($year, $month, $day, $area, '', 'day');
  69. echo "</tr></table>";
  70. }
  71. #y? are year, month and day of yesterday
  72. #t? are year, month and day of tomorrow
  73. $i= mktime(12,0,0,$month,$day-1,$year);
  74. $yy = date("Y",$i);
  75. $ym = date("m",$i);
  76. $yd = date("d",$i);
  77. $i= mktime(12,0,0,$month,$day+1,$year);
  78. $ty = date("Y",$i);
  79. $tm = date("m",$i);
  80. $td = date("d",$i);
  81. #We want to build an array containing all the data we want to show
  82. #and then spit it out.
  83. #Get all appointments for today in the area that we care about
  84. #Note: The predicate clause 'start_time <= ...' is an equivalent but simpler
  85. #form of the original which had 3 BETWEEN parts. It selects all entries which
  86. #occur on or cross the current day.
  87. $sql = "SELECT $tbl_room.id, start_time, end_time, name, $tbl_entry.id, type,
  88. $tbl_entry.description
  89. FROM $tbl_entry, $tbl_room
  90. WHERE $tbl_entry.room_id = $tbl_room.id
  91. AND area_id = $area
  92. AND start_time <= $pm7 AND end_time > $am7";
  93. $res = sql_query($sql);
  94. if (! $res) fatal_error(0, sql_error());
  95. for ($i = 0; ($row = sql_row($res, $i)); $i++) {
  96. # Each row weve got here is an appointment.
  97. #Row[0] = Room ID
  98. #row[1] = start time
  99. #row[2] = end time
  100. #row[3] = short description
  101. #row[4] = id of this booking
  102. #row[5] = type (internal/external)
  103. #row[6] = description
  104. # $today is a map of the screen that will be displayed
  105. # It looks like:
  106. # $today[Room ID][Time][id]
  107. # [color]
  108. # [data]
  109. # [long_descr]
  110. # Fill in the map for this meeting. Start at the meeting start time,
  111. # or the day start time, whichever is later. End one slot before the
  112. # meeting end time (since the next slot is for meetings which start then),
  113. # or at the last slot in the day, whichever is earlier.
  114. # Time is of the format HHMM without leading zeros.
  115. #
  116. # Note: int casts on database rows for max may be needed for PHP3.
  117. # Adjust the starting and ending times so that bookings which don't
  118. # start or end at a recognized time still appear.
  119. $start_t = max(round_t_down($row[1], $resolution, $am7), $am7);
  120. $end_t = min(round_t_up($row[2], $resolution, $am7) - $resolution, $pm7);
  121. for ($t = $start_t; $t <= $end_t; $t += $resolution)
  122. {
  123. $today[$row[0]][date($format,$t)]["id"] = $row[4];
  124. $today[$row[0]][date($format,$t)]["color"] = $row[5];
  125. $today[$row[0]][date($format,$t)]["data"] = "";
  126. $today[$row[0]][date($format,$t)]["long_descr"] = "";
  127. }
  128. # Show the name of the booker in the first segment that the booking
  129. # happens in, or at the start of the day if it started before today.
  130. if ($row[1] < $am7)
  131. {
  132. $today[$row[0]][date($format,$am7)]["data"] = $row[3];
  133. $today[$row[0]][date($format,$am7)]["long_descr"] = $row[6];
  134. }
  135. else
  136. {
  137. $today[$row[0]][date($format,$start_t)]["data"] = $row[3];
  138. $today[$row[0]][date($format,$start_t)]["long_descr"] = $row[6];
  139. }
  140. }
  141. if ($debug_flag)
  142. {
  143. echo "<p>DEBUG:<pre>\n";
  144. echo "\$dst_change = $dst_change\n";
  145. echo "\$am7 = $am7 or " . date($format,$am7) . "\n";
  146. echo "\$pm7 = $pm7 or " . date($format,$pm7) . "\n";
  147. if (gettype($today) == "array")
  148. while (list($w_k, $w_v) = each($today))
  149. while (list($t_k, $t_v) = each($w_v))
  150. while (list($k_k, $k_v) = each($t_v))
  151. echo "d[$w_k][$t_k][$k_k] = '$k_v'\n";
  152. else echo "today is not an array!\n";
  153. echo "</pre><p>\n";
  154. }
  155. # We need to know what all the rooms area called, so we can show them all
  156. # pull the data from the db and store it. Convienently we can print the room
  157. # headings and capacities at the same time
  158. $sql = "select room_name, capacity, id, description from $tbl_room where area_id=$area order by 1";
  159. $res = sql_query($sql);
  160. # It might be that there are no rooms defined for this area.
  161. # If there are none then show an error and dont bother doing anything
  162. # else
  163. if (! $res) fatal_error(0, sql_error());
  164. if (sql_count($res) == 0)
  165. {
  166. echo "<h1>".get_vocab("no_rooms_for_area")."</h1>";
  167. sql_free($res);
  168. }
  169. else
  170. {
  171. #Show current date
  172. echo "<h2 align=center>" . utf8_strftime("%A %d %B %Y", $am7) . "</h2>\n";
  173. if ( $pview != 1 ) {
  174. #Show Go to day before and after links
  175. $output = "<table width=\"100%\"><tr><td><a href=\"day.php?year=$yy&month=$ym&day=$yd&area=$area\">&lt;&lt;".get_vocab("daybefore")."</a></td>
  176. <td align=center><a href=\"day.php?area=$area\">".get_vocab("gototoday")."</a></td>
  177. <td align=right><a href=\"day.php?year=$ty&month=$tm&day=$td&area=$area\">".get_vocab("dayafter")."&gt;&gt;</a></td></tr></table>\n";
  178. print $output;
  179. }
  180. // Include the active cell content management routines.
  181. // Must be included before the beginnning of the main table.
  182. if ($javascript_cursor) // If authorized in config.inc.php, include the javascript cursor management.
  183. {
  184. echo "<SCRIPT language=\"JavaScript\" type=\"text/javascript\" src=\"xbLib.js\"></SCRIPT>\n";
  185. echo "<SCRIPT language=\"JavaScript\">InitActiveCell("
  186. . ($show_plus_link ? "true" : "false") . ", "
  187. . "true, "
  188. . ((FALSE != $times_right_side) ? "true" : "false") . ", "
  189. . "\"$highlight_method\", "
  190. . "\"" . get_vocab("click_to_reserve") . "\""
  191. . ");</SCRIPT>\n";
  192. }
  193. #This is where we start displaying stuff
  194. echo "<table cellspacing=0 border=1 width=\"100%\">";
  195. echo "<tr><th width=\"1%\">".($enable_periods ? get_vocab("period") : get_vocab("time"))."</th>";
  196. $room_column_width = (int)(95 / sql_count($res));
  197. for ($i = 0; ($row = sql_row($res, $i)); $i++)
  198. {
  199. echo "<th width=\"$room_column_width%\">
  200. <a href=\"week.php?year=$year&month=$month&day=$day&area=$area&room=$row[2]\"
  201. title=\"" . get_vocab("viewweek") . " &#10;&#10;$row[3]\">"
  202. . htmlspecialchars($row[0]) . ($row[1] > 0 ? "($row[1])" : "") . "</a></th>";
  203. $rooms[] = $row[2];
  204. }
  205. # next line to display times on right side
  206. if ( FALSE != $times_right_side )
  207. {
  208. echo "<th width=\"1%\">". ( $enable_periods ? get_vocab("period") : get_vocab("time") )
  209. ."</th>";
  210. }
  211. echo "</tr>\n";
  212. # URL for highlighting a time. Don't use REQUEST_URI or you will get
  213. # the timetohighlight parameter duplicated each time you click.
  214. $hilite_url="day.php?year=$year&month=$month&day=$day&area=$area&timetohighlight";
  215. # This is the main bit of the display
  216. # We loop through time and then the rooms we just got
  217. # if the today is a day which includes a DST change then use
  218. # the day after to generate timesteps through the day as this
  219. # will ensure a constant time step
  220. ( $dst_change != -1 ) ? $j = 1 : $j = 0;
  221. $row_class = "even_row";
  222. for (
  223. $t = mktime($morningstarts, $morningstarts_minutes, 0, $month, $day+$j, $year);
  224. $t <= mktime($eveningends, $eveningends_minutes, 0, $month, $day+$j, $year);
  225. $t += $resolution, $row_class = ($row_class == "even_row")?"odd_row":"even_row"
  226. )
  227. {
  228. # convert timestamps to HHMM format without leading zeros
  229. $time_t = date($format, $t);
  230. # Show the time linked to the URL for highlighting that time
  231. echo "<tr>";
  232. tdcell("red");
  233. if( $enable_periods ){
  234. $time_t_stripped = preg_replace( "/^0/", "", $time_t );
  235. echo "<a href=\"$hilite_url=$time_t\" title=\""
  236. . get_vocab("highlight_line") . "\">"
  237. . $periods[$time_t_stripped] . "</a></td>\n";
  238. } else {
  239. echo "<a href=\"$hilite_url=$time_t\" title=\""
  240. . get_vocab("highlight_line") . "\">"
  241. . utf8_date(hour_min_format(),$t) . "</a></td>\n";
  242. }
  243. # Loop through the list of rooms we have for this area
  244. while (list($key, $room) = each($rooms))
  245. {
  246. if(isset($today[$room][$time_t]["id"]))
  247. {
  248. $id = $today[$room][$time_t]["id"];
  249. $color = $today[$room][$time_t]["color"];
  250. $descr = htmlspecialchars($today[$room][$time_t]["data"]);
  251. $long_descr = htmlspecialchars($today[$room][$time_t]["long_descr"]);
  252. }
  253. else
  254. unset($id);
  255. # $c is the colour of the cell that the browser sees. White normally,
  256. # red if were hightlighting that line and a nice attractive green if the room is booked.
  257. # We tell if its booked by $id having something in it
  258. if (isset($id))
  259. $c = $color;
  260. elseif (isset($timetohighlight) && ($time_t == $timetohighlight))
  261. $c = "red";
  262. else
  263. $c = $row_class; # Use the default color class for the row.
  264. tdcell($c);
  265. # If the room isnt booked then allow it to be booked
  266. if(!isset($id))
  267. {
  268. $hour = date("H",$t);
  269. $minute = date("i",$t);
  270. if ( $pview != 1 ) {
  271. if ($javascript_cursor)
  272. {
  273. echo "<SCRIPT language=\"JavaScript\">\n<!--\n";
  274. echo "BeginActiveCell();\n";
  275. echo "// -->\n</SCRIPT>";
  276. }
  277. echo "<center>";
  278. if( $enable_periods ) {
  279. echo "<a href=\"edit_entry.php?area=$area&room=$room&period=$time_t_stripped&year=$year&month=$month&day=$day\"><img src=new.gif width=10 height=10 border=0></a>";
  280. } else {
  281. echo "<a href=\"edit_entry.php?area=$area&room=$room&hour=$hour&minute=$minute&year=$year&month=$month&day=$day\"><img src=new.gif width=10 height=10 border=0></a>";
  282. }
  283. echo "</center>";
  284. if ($javascript_cursor)
  285. {
  286. echo "<SCRIPT language=\"JavaScript\">\n<!--\n";
  287. echo "EndActiveCell();\n";
  288. echo "// -->\n</SCRIPT>";
  289. }
  290. } else echo '&nbsp;';
  291. }
  292. elseif ($descr != "")
  293. {
  294. #if it is booked then show
  295. echo " <a href=\"view_entry.php?id=$id&area=$area&day=$day&month=$month&year=$year\" title=\"$long_descr\">$descr</a>";
  296. }
  297. else
  298. echo "&nbsp;\"&nbsp;";
  299. echo "</td>\n";
  300. }
  301. # next lines to display times on right side
  302. if ( FALSE != $times_right_side )
  303. {
  304. if( $enable_periods )
  305. {
  306. tdcell("red");
  307. $time_t_stripped = preg_replace( "/^0/", "", $time_t );
  308. echo "<a href=\"$hilite_url=$time_t\" title=\""
  309. . get_vocab("highlight_line") . "\">"
  310. . $periods[$time_t_stripped] . "</a></td>\n";
  311. }
  312. else
  313. {
  314. tdcell("red");
  315. echo "<a href=\"$hilite_url=$time_t\" title=\""
  316. . get_vocab("highlight_line") . "\">"
  317. . utf8_date(hour_min_format(),$t) . "</a></td>\n";
  318. }
  319. }
  320. echo "</tr>\n";
  321. reset($rooms);
  322. }
  323. echo "</table>\n";
  324. (isset($output)) ? print $output : '';
  325. show_colour_key();
  326. }
  327. include "trailer.inc";
  328. ?>