PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/mrbs/tags/mrbs_MDB-latest/web/week.php

https://github.com/jessfishenden/mrbs-mcr
PHP | 472 lines | 340 code | 52 blank | 80 comment | 82 complexity | 62536df60deae0a6a2145cc7205c6068 MD5 | raw file
  1. <?php
  2. # $Id$
  3. # mrbs/week.php - Week-at-a-time view
  4. require_once "grab_globals.inc.php";
  5. include "config.inc.php";
  6. include "functions.inc";
  7. require_once("database.inc.php");
  8. include "$dbsys.inc";
  9. include "mincals.inc";
  10. if (empty($debug_flag)) $debug_flag = 0;
  11. $num_of_days=7; #could also pass this in as a parameter or whatever
  12. # If we don't know the right date then use today:
  13. if (!isset($day) or !isset($month) or !isset($year))
  14. {
  15. $day = date("d");
  16. $month = date("m");
  17. $year = date("Y");
  18. } else {
  19. # Make the date valid if day is more then number of days in month:
  20. while (!checkdate($month, $day, $year))
  21. $day--;
  22. }
  23. # Set the date back to the previous $weekstarts day (Sunday, if 0):
  24. $time = mktime(12, 0, 0, $month, $day, $year);
  25. if (($weekday = (date("w", $time) - $weekstarts + 7) % 7) > 0)
  26. {
  27. $time -= $weekday * 86400;
  28. $day = date("d", $time);
  29. $month = date("m", $time);
  30. $year = date("Y", $time);
  31. }
  32. if (empty($area))
  33. $area = get_default_area();
  34. if (empty($room))
  35. $room = get_default_room($area);
  36. # Note $room will be 0 if there are no rooms; this is checked for below.
  37. # print the page header
  38. print_header($day, $month, $year, $area);
  39. $format = "Gi";
  40. if( $enable_periods ) {
  41. $format = "i";
  42. $resolution = 60;
  43. $morningstarts = 12;
  44. $eveningends = 12;
  45. $eveningends_minutes = count($periods)-1;
  46. }
  47. # Define the start and end of each day of the week in a way which is not
  48. # affected by daylight saving...
  49. for ($j = 0; $j<=($num_of_days-1); $j++) {
  50. # are we entering or leaving daylight saving
  51. # dst_change:
  52. # -1 => no change
  53. # 0 => entering DST
  54. # 1 => leaving DST
  55. $dst_change[$j] = is_dst($month,$day+$j,$year);
  56. $am7[$j]=mktime($morningstarts,0,0,$month,$day+$j,$year,is_dst($month,$day+$j,$year,$morningstarts));
  57. $pm7[$j]=mktime($eveningends,$eveningends_minutes,0,$month,$day+$j,$year,is_dst($month,$day+$j,$year,$eveningends));
  58. }
  59. if ( $pview != 1 ) {
  60. # Table with areas, rooms, minicals.
  61. echo "<table width=\"100%\"><tr>";
  62. $this_area_name = "";
  63. $this_room_name = "";
  64. # Show all areas
  65. echo "<td width=\"30%\"><u>".get_vocab("areas")."</u><br>";
  66. }
  67. # show either a select box or the normal html list
  68. if ($area_list_format == "select") {
  69. echo make_area_select_html('week.php', $area, $year, $month, $day); # from functions.inc
  70. $this_area_name = $mdb->queryOne("SELECT area_name
  71. FROM mrbs_area
  72. WHERE id=$area", 'text');
  73. $this_room_name = $mdb->queryOne("SELECT room_name
  74. FROM mrbs_room
  75. WHERE id=$room", 'text');
  76. } else {
  77. $sql = "SELECT id, area_name
  78. FROM mrbs_area
  79. ORDER BY area_name";
  80. $types = array('integer', 'text');
  81. $res = $mdb->query($sql, $types);
  82. if (!MDB::isError($res))
  83. {
  84. while ($row = $mdb->fetchInto($res))
  85. {
  86. if ( $pview != 1 )
  87. {
  88. echo "<a href=\"week.php?year=$year&month=$month&day=$day&area=$row[0]\">";
  89. }
  90. if ($row[0] == $area)
  91. {
  92. $this_area_name = htmlspecialchars($row[1]);
  93. if ( $pview != 1 )
  94. {
  95. echo "<font color=\"red\">$this_area_name</font></a><br>\n";
  96. }
  97. }
  98. else if ( $pview != 1 )
  99. {
  100. echo htmlspecialchars($row[1]) . "</a><br>\n";
  101. }
  102. }
  103. }
  104. } // end area display if
  105. if ( $pview != 1) {
  106. echo "</td>\n";
  107. # Show all rooms in the current area
  108. echo "<td width=\"30%\"><u>".get_vocab("room")."</u><br>";
  109. }
  110. # should we show a drop-down for the room list, or not?
  111. if ($area_list_format == "select") {
  112. echo make_room_select_html('week.php', $area, $room, $year, $month, $day); # from functions.inc
  113. } else {
  114. $sql = "SELECT id, room_name, description
  115. FROM mrbs_room
  116. WHERE area_id=$area
  117. ORDER BY room_name";
  118. $types = array('integer', 'text');
  119. $res = $mdb->query($sql, $types);
  120. if (!MDB::isError($res))
  121. {
  122. while ($row = $mdb->fetchInto($res))
  123. {
  124. if ( $pview != 1 )
  125. {
  126. echo "<a href=\"week.php?year=$year&month=$month&day=$day&area=$area&room=$row[0]\" title=\"$row[2]\">";
  127. }
  128. if ($row[0] == $room)
  129. {
  130. $this_room_name = htmlspecialchars($row[1]);
  131. if ( $pview != 1 )
  132. {
  133. echo "<font color=\"red\">$this_room_name</font></a><br>\n";
  134. }
  135. }
  136. else if ( $pview != 1 )
  137. {
  138. echo htmlspecialchars($row[1]) . "</a><br>\n";
  139. }
  140. }
  141. }
  142. } # end select if
  143. if ( $pview != 1 ) {
  144. echo "</td>\n";
  145. #Draw the three month calendars
  146. minicals($year, $month, $day, $area, 'week');
  147. echo "</tr></table>\n";
  148. # Don't continue if this area has no rooms:
  149. if ($room <= 0)
  150. {
  151. echo "<h1>".get_vocab("no_rooms_for_area")."</h1>";
  152. include "trailer.inc";
  153. exit;
  154. }
  155. }
  156. # Show area and room:
  157. echo "<h2 align=center>$this_area_name - $this_room_name</h2>\n";
  158. #y? are year, month and day of the previous week.
  159. #t? are year, month and day of the next week.
  160. $i= mktime(12,0,0,$month,$day-7,$year);
  161. $yy = date("Y",$i);
  162. $ym = date("m",$i);
  163. $yd = date("d",$i);
  164. $i= mktime(12,0,0,$month,$day+7,$year);
  165. $ty = date("Y",$i);
  166. $tm = date("m",$i);
  167. $td = date("d",$i);
  168. if ( $pview != 1 ) {
  169. #Show Go to week before and after links
  170. echo "<table width=\"100%\"><tr><td>
  171. <a href=\"week.php?year=$yy&month=$ym&day=$yd&area=$area&room=$room\">
  172. &lt;&lt; ".get_vocab("weekbefore")."</a></td>
  173. <td align=center><a href=\"week.php?area=$area&room=$room\">".get_vocab("gotothisweek")."</a></td>
  174. <td align=right><a href=\"week.php?year=$ty&month=$tm&day=$td&area=$area&room=$room\">
  175. ".get_vocab("weekafter")."&gt;&gt;</a></td></tr></table>";
  176. }
  177. #Get all appointments for this week in the room that we care about
  178. # row[0] = Start time
  179. # row[1] = End time
  180. # row[2] = Entry type
  181. # row[3] = Entry name (brief description)
  182. # row[4] = Entry ID
  183. # row[5] = Complete description
  184. # This data will be retrieved day-by-day
  185. for ($j = 0; $j<=($num_of_days-1) ; $j++) {
  186. $sql = "SELECT start_time, end_time, type, name, id, description
  187. FROM mrbs_entry
  188. WHERE room_id=$room
  189. AND start_time <= $pm7[$j]
  190. AND end_time > $am7[$j]";
  191. # Each row returned from the query is a meeting. Build an array of the
  192. # form: d[weekday][slot][x], where x = id, color, data, long_desc.
  193. # [slot] is based at 000 (HHMM) for midnight, but only slots within
  194. # the hours of interest (morningstarts : eveningends) are filled in.
  195. # [id], [data] and [long_desc] are only filled in when the meeting
  196. # should be labeled, which is once for each meeting on each weekday.
  197. # Note: weekday here is relative to the $weekstarts configuration variable.
  198. # If 0, then weekday=0 means Sunday. If 1, weekday=0 means Monday.
  199. if ($debug_flag) echo "<br>DEBUG: query=$sql\n";
  200. $types = array('integer', 'integer', 'text', 'text', 'integer');
  201. $res = $mdb->query($sql, $types);
  202. if (MDB::isError($res))
  203. {
  204. echo $res->getMessage() . "<br>" . $res->getUserInfo() . "<br>";
  205. }
  206. else
  207. {
  208. while ($row = $mdb->fetchInto($res))
  209. {
  210. if ($debug_flag)
  211. echo "<br>DEBUG: result $i, id $row[4], starts $row[0], ends $row[1]\n";
  212. # $d is a map of the screen that will be displayed
  213. # It looks like:
  214. # $d[Day][Time][id]
  215. # [color]
  216. # [data]
  217. # where Day is in the range 0 to $num_of_days.
  218. # Fill in the map for this meeting. Start at the meeting start time,
  219. # or the day start time, whichever is later. End one slot before the
  220. # meeting end time (since the next slot is for meetings which start then),
  221. # or at the last slot in the day, whichever is earlier.
  222. # Note: int casts on database rows for max may be needed for PHP3.
  223. # Adjust the starting and ending times so that bookings which don't
  224. # start or end at a recognized time still appear.
  225. $start_t = max(round_t_down($row[0], $resolution, $am7[$j]), $am7[$j]);
  226. $end_t = min(round_t_up($row[1], $resolution, $am7[$j]) - $resolution, $pm7[$j]);
  227. for ($t = $start_t; $t <= $end_t; $t += $resolution)
  228. {
  229. $d[$j][date($format,$t)]["id"] = $row[4];
  230. $d[$j][date($format,$t)]["color"] = $row[2];
  231. $d[$j][date($format,$t)]["data"] = "";
  232. $d[$j][date($format,$t)]["long_descr"] = "";
  233. }
  234. # Show the name of the booker in the first segment that the booking
  235. # happens in, or at the start of the day if it started before today.
  236. if ($row[1] < $am7[$j])
  237. {
  238. $d[$j][date($format,$am7[$j])]["data"] = $row[3];
  239. $d[$j][date($format,$am7[$j])]["long_descr"] = $row[5];
  240. }
  241. else
  242. {
  243. $d[$j][date($format,$start_t)]["data"] = $row[3];
  244. $d[$j][date($format,$start_t)]["long_descr"] = $row[5];
  245. }
  246. }
  247. }
  248. }
  249. if ($debug_flag)
  250. {
  251. echo "<p>DEBUG:<pre>\n";
  252. echo "\$dst_change = ";
  253. print_r( $dst_change );
  254. print "\n";
  255. print "\$am7 =\n";
  256. foreach( $am7 as $am7_val)
  257. print "$am7_val - " . date("r", $am7_val) . "\n";
  258. print "\$pm7 =\n";
  259. foreach( $pm7 as $pm7_val)
  260. print "$pm7_val - " . date("r", $pm7_val) . "\n";
  261. echo "<p>\$d =\n";
  262. if (gettype($d) == "array")
  263. while (list($w_k, $w_v) = each($d))
  264. while (list($t_k, $t_v) = each($w_v))
  265. while (list($k_k, $k_v) = each($t_v))
  266. echo "d[$w_k][$t_k][$k_k] = '$k_v'\n";
  267. else echo "d is not an array!\n";
  268. echo "</pre><p>\n";
  269. }
  270. #This is where we start displaying stuff
  271. echo "<table cellspacing=0 border=1 width=\"100%\">";
  272. # The header row contains the weekday names and short dates.
  273. echo "<tr><th width=\"1%\"><br>".($enable_periods ? get_vocab("period") : get_vocab("time"))."</th>";
  274. if (empty($dateformat))
  275. $dformat = "%a<br>%b %d";
  276. else
  277. $dformat = "%a<br>%d %b";
  278. for ($j = 0; $j<=($num_of_days-1) ; $j++)
  279. {
  280. $t = mktime( 12, 0, 0, $month, $day+$j, $year);
  281. echo "<th width=\"14%\"><a href=\"day.php?year=" . strftime("%Y", $t) .
  282. "&month=" . strftime("%m", $t) . "&day=" . strftime("%d", $t) .
  283. "&area=$area\" title=\"" . get_vocab("viewday") . "\">"
  284. . utf8_strftime($dformat, $t) . "</a></th>\n";
  285. }
  286. # next line to display times on right side
  287. if ( FALSE != $times_right_side )
  288. {
  289. echo "<th width=\"1%\"><br>"
  290. . ( $enable_periods ? get_vocab("period") : get_vocab("time") )
  291. . "</th>";
  292. }
  293. echo "</tr>\n";
  294. # This is the main bit of the display. Outer loop is for the time slots,
  295. # inner loop is for days of the week.
  296. # URL for highlighting a time. Don't use REQUEST_URI or you will get
  297. # the timetohighlight parameter duplicated each time you click.
  298. $hilite_url="week.php?year=$year&month=$month&day=$day&area=$area&room=$room&timetohighlight";
  299. # if the first day of the week to be displayed contains as DST change then
  300. # move to the next day to get the hours in the day.
  301. ( $dst_change[0] != -1 ) ? $j = 1 : $j = 0;
  302. for (
  303. $t = mktime($morningstarts, 0, 0, $month, $day+$j, $year);
  304. $t <= mktime($eveningends, $eveningends_minutes, 0, $month, $day+$j, $year);
  305. $t += $resolution
  306. )
  307. {
  308. # use hour:minute format
  309. $time_t = date($format, $t);
  310. # Show the time linked to the URL for highlighting that time:
  311. echo "<tr>";
  312. tdcell("red");
  313. if( $enable_periods ){
  314. $time_t_stripped = preg_replace( "/^0/", "", $time_t );
  315. echo "<a href=\"$hilite_url=$time_t\" title=\""
  316. . get_vocab("highlight_line") . "\">"
  317. . $periods[$time_t_stripped] . "</a></td>";
  318. } else {
  319. echo "<a href=\"$hilite_url=$time_t\" title=\""
  320. . get_vocab("highlight_line") . "\">"
  321. . utf8_date(hour_min_format(),$t) . "</a></td>";
  322. }
  323. # Color to use for empty cells: white, unless highlighting this row:
  324. if (isset($timetohighlight) && $timetohighlight == $time_t)
  325. $empty_color = "red";
  326. else
  327. $empty_color = "white";
  328. # See note above: weekday==0 is day $weekstarts, not necessarily Sunday.
  329. for ($thisday = 0; $thisday<=($num_of_days-1) ; $thisday++)
  330. {
  331. # Three cases:
  332. # color: id: Slot is: Color: Link to:
  333. # ----- ----- -------- --------- -----------------------
  334. # unset - empty white,red add new entry
  335. # set unset used by type none (unlabelled slot)
  336. # set set used by type view entry
  337. $wt = mktime( 12, 0, 0, $month, $day+$thisday, $year );
  338. $wday = date("d", $wt);
  339. $wmonth = date("m", $wt);
  340. $wyear = date("Y", $wt);
  341. if(isset($d[$thisday][$time_t]["id"]))
  342. {
  343. $id = $d[$thisday][$time_t]["id"];
  344. $color = $d[$thisday][$time_t]["color"];
  345. $descr = htmlspecialchars($d[$thisday][$time_t]["data"]);
  346. $long_descr = htmlspecialchars($d[$thisday][$time_t]["long_descr"]);
  347. }
  348. else
  349. unset($id);
  350. # $c is the colour of the cell that the browser sees. White normally,
  351. # red if were hightlighting that line and a nice attractive green if the room is booked.
  352. # We tell if its booked by $id having something in it
  353. if (isset($id))
  354. $c = $color;
  355. elseif (isset($timetohighlight) && ($time_t == $timetohighlight))
  356. $c = "red";
  357. else
  358. $c = "white";
  359. tdcell($c);
  360. # If the room isnt booked then allow it to be booked
  361. if(!isset($id))
  362. {
  363. $hour = date("H",$t);
  364. $minute = date("i",$t);
  365. echo "<center>";
  366. if ( $pview != 1 ) {
  367. if( $enable_periods ) {
  368. echo "<a href=\"edit_entry.php?room=$room&area=$area"
  369. . "&period=$time_t_stripped&year=$wyear&month=$wmonth"
  370. . "&day=$wday\"><img src=new.gif width=10 height=10 border=0></a>";
  371. } else {
  372. echo "<a href=\"edit_entry.php?room=$room&area=$area"
  373. . "&hour=$hour&minute=$minute&year=$wyear&month=$wmonth"
  374. . "&day=$wday\"><img src=new.gif width=10 height=10 border=0></a>";
  375. }
  376. } else
  377. echo '&nbsp;';
  378. echo "</center>";
  379. }
  380. elseif ($descr != "")
  381. {
  382. #if it is booked then show
  383. echo " <a href=\"view_entry.php?id=$id"
  384. . "&area=$area&day=$wday&month=$wmonth&year=$wyear\" "
  385. . "title=\"$long_descr\">$descr</a>";
  386. }
  387. else
  388. echo "&nbsp;\"&nbsp;";
  389. echo "</td>\n";
  390. }
  391. # next lines to display times on right side
  392. if ( FALSE != $times_right_side )
  393. {
  394. if( $enable_periods )
  395. {
  396. tdcell("red");
  397. $time_t_stripped = preg_replace( "/^0/", "", $time_t );
  398. echo "<a href=\"$hilite_url=$time_t\" title=\""
  399. . get_vocab("highlight_line") . "\">"
  400. . $periods[$time_t_stripped] . "</a></td>";
  401. }
  402. else
  403. {
  404. tdcell("red");
  405. echo "<a href=\"$hilite_url=$time_t\" title=\""
  406. . get_vocab("highlight_line") . "\">"
  407. . utf8_date(hour_min_format(),$t) . "</a></td>";
  408. }
  409. }
  410. echo "</tr>\n";
  411. }
  412. echo "</table>";
  413. if ( $pview != 1 ) show_colour_key();
  414. include "trailer.inc";
  415. ?>