PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mrbs/tags/mrbs-1_4/web/edit_entry_handler.php

https://github.com/jessfishenden/mrbs-mcr
PHP | 512 lines | 408 code | 49 blank | 55 comment | 92 complexity | c0473c7529a35786d624db1efb65de3e 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 "mrbs_sql.inc";
  9. // Get form variables
  10. $day = get_form_var('day', 'int');
  11. $month = get_form_var('month', 'int');
  12. $year = get_form_var('year', 'int');
  13. $area = get_form_var('area', 'int');
  14. $room = get_form_var('room', 'int');
  15. $create_by = get_form_var('create_by', 'string');
  16. $name = get_form_var('name', 'string');
  17. $rep_type = get_form_var('rep_type', 'int');
  18. $description = get_form_var('description', 'string');
  19. $hour = get_form_var('hour', 'int');
  20. $ampm = get_form_var('ampm', 'string');
  21. $minute = get_form_var('minute', 'int');
  22. $period = get_form_var('period', 'int');
  23. $duration = get_form_var('duration', 'int');
  24. $dur_units = get_form_var('dur_units', 'string');
  25. $all_day = get_form_var('all_day', 'string'); // bool, actually
  26. $type = get_form_var('type', 'string');
  27. $rooms = get_form_var('rooms', 'array');
  28. $returl = get_form_var('returl', 'string');
  29. $rep_id = get_form_var('rep_id', 'int');
  30. $edit_type = get_form_var('edit_type', 'string');
  31. $id = get_form_var('id', 'int');
  32. $rep_end_day = get_form_var('rep_end_day', 'int');
  33. $rep_end_month = get_form_var('rep_end_month', 'int');
  34. $rep_end_year = get_form_var('rep_end_year', 'int');
  35. $rep_id = get_form_var('rep_id', 'int');
  36. $rep_day = get_form_var('rep_day', 'array'); // array of bools
  37. $rep_num_weeks = get_form_var('rep_num_weeks', 'int');
  38. // If we dont know the right date then make it up
  39. if (!isset($day) or !isset($month) or !isset($year))
  40. {
  41. $day = date("d");
  42. $month = date("m");
  43. $year = date("Y");
  44. }
  45. if (empty($area))
  46. {
  47. $area = get_default_area();
  48. }
  49. // Set up the return URL. As the user has tried to book a particular room and a particular
  50. // day, we must consider these to be the new "sticky room" and "sticky day", so modify the
  51. // return URL accordingly.
  52. // First get the return URL basename, having stipped off the old query string
  53. // (1) It's possible that $returl could be empty, for example if edit_entry.php had been called
  54. // direct, perhaps if the user has it set as a bookmark
  55. // (2) Avoid an endless loop. It shouldn't happen, but just in case ...
  56. $returl_base = explode('?', basename($returl));
  57. if (empty($returl) || ($returl_base[0] == "edit_entry.php") || ($returl_base[0] == "edit_entry_handler.php"))
  58. {
  59. switch ($default_view)
  60. {
  61. case "month":
  62. $returl = "month.php";
  63. break;
  64. case "week":
  65. $returl = "week.php";
  66. break;
  67. default:
  68. $returl = "day.php";
  69. }
  70. }
  71. else
  72. {
  73. $returl = $returl_base[0];
  74. }
  75. // Now construct the new query string
  76. $returl .= "?year=$year&month=$month&day=$day";
  77. // If the old sticky room is one of the rooms requested for booking, then don't change the sticky room.
  78. // Otherwise change the sticky room to be one of the new rooms.
  79. if (!in_array($room, $rooms))
  80. {
  81. $room = $rooms[0];
  82. }
  83. // Find the corresponding area
  84. $area = mrbsGetRoomArea($room);
  85. // Complete the query string
  86. $returl .= "&area=$area&room=$room";
  87. if (!getAuthorised(1))
  88. {
  89. showAccessDenied($day, $month, $year, $area, isset($room) ? $room : "");
  90. exit;
  91. }
  92. if (!getWritable($create_by, getUserName()))
  93. {
  94. showAccessDenied($day, $month, $year, $area, isset($room) ? $room : "");
  95. exit;
  96. }
  97. if ($name == '')
  98. {
  99. print_header($day, $month, $year, $area, isset($room) ? $room : "");
  100. ?>
  101. <h1><?php echo get_vocab('invalid_booking'); ?></h1>
  102. <p>
  103. <?php echo get_vocab('must_set_description'); ?>
  104. </p>
  105. </body>
  106. </html>
  107. <?php
  108. exit;
  109. }
  110. if ($rep_type == 2 || $rep_type == 6)
  111. {
  112. $got_rep_day = 0;
  113. for ($i = 0; $i < 7; $i++)
  114. {
  115. if ($rep_day[$i])
  116. {
  117. $got_rep_day =1;
  118. break;
  119. }
  120. }
  121. if ($got_rep_day == 0)
  122. {
  123. print_header($day, $month, $year, $area, isset($room) ? $room : "");
  124. ?>
  125. <h1><?php echo get_vocab('invalid_booking'); ?></h1>
  126. <p>
  127. <?php echo get_vocab('you_have_not_entered')." ".get_vocab("rep_rep_day"); ?>
  128. </p>
  129. </body>
  130. </html>
  131. <?php
  132. exit;
  133. }
  134. }
  135. if (($rep_type == 6) && ($rep_num_weeks < 2))
  136. {
  137. print_header($day, $month, $year, $area, isset($room) ? $room : "");
  138. ?>
  139. <h1><?php echo get_vocab('invalid_booking'); ?></h1>
  140. <p>
  141. <?php echo get_vocab('you_have_not_entered')." ".get_vocab("useful_n-weekly_value"); ?>
  142. </p>
  143. </body>
  144. </html>
  145. <?php
  146. exit;
  147. }
  148. // Support locales where ',' is used as the decimal point
  149. $duration = preg_replace('/,/', '.', $duration);
  150. if ( $enable_periods )
  151. {
  152. $resolution = 60;
  153. $hour = 12;
  154. $minute = $period;
  155. $max_periods = count($periods);
  156. if ( $dur_units == "periods" && ($minute + $duration) > $max_periods )
  157. {
  158. $duration = (24*60*floor($duration/$max_periods)) +
  159. ($duration%$max_periods);
  160. }
  161. if ( $dur_units == "days" && $minute == 0 )
  162. {
  163. $dur_units = "periods";
  164. $duration = $max_periods + ($duration-1)*60*24;
  165. }
  166. }
  167. // Units start in seconds
  168. $units = 1.0;
  169. switch($dur_units)
  170. {
  171. case "years":
  172. $units *= 52;
  173. case "weeks":
  174. $units *= 7;
  175. case "days":
  176. $units *= 24;
  177. case "hours":
  178. $units *= 60;
  179. case "periods":
  180. case "minutes":
  181. $units *= 60;
  182. case "seconds":
  183. break;
  184. }
  185. // Units are now in "$dur_units" numbers of seconds
  186. if (isset($all_day) && ($all_day == "yes"))
  187. {
  188. if ( $enable_periods )
  189. {
  190. $starttime = mktime(12, 0, 0, $month, $day, $year);
  191. $endtime = mktime(12, $max_periods, 0, $month, $day, $year);
  192. }
  193. else
  194. {
  195. $starttime = mktime($morningstarts, $morningstarts_minutes, 0,
  196. $month, $day , $year,
  197. is_dst($month, $day , $year));
  198. $endtime = mktime($eveningends, $eveningends_minutes, 0,
  199. $month, $day, $year,
  200. is_dst($month, $day, $year));
  201. $endtime += $resolution; // add on the duration (in seconds) of the last slot as
  202. // $eveningends and $eveningends_minutes specify the
  203. // beginning of the last slot
  204. }
  205. }
  206. else
  207. {
  208. if (!$twentyfourhour_format)
  209. {
  210. if (isset($ampm) && ($ampm == "pm") && ($hour<12))
  211. {
  212. $hour += 12;
  213. }
  214. if (isset($ampm) && ($ampm == "am") && ($hour>11))
  215. {
  216. $hour -= 12;
  217. }
  218. }
  219. $starttime = mktime($hour, $minute, 0,
  220. $month, $day, $year,
  221. is_dst($month, $day, $year, $hour));
  222. $endtime = mktime($hour, $minute, 0,
  223. $month, $day, $year,
  224. is_dst($month, $day, $year, $hour)) + ($units * $duration);
  225. // Round up the duration to the next whole resolution unit.
  226. // If they asked for 0 minutes, push that up to 1 resolution unit.
  227. $diff = $endtime - $starttime;
  228. if (($tmp = $diff % $resolution) != 0 || $diff == 0)
  229. {
  230. $endtime += $resolution - $tmp;
  231. }
  232. $endtime += cross_dst( $starttime, $endtime );
  233. }
  234. if (isset($rep_type) && ($rep_type > 0) &&
  235. isset($rep_end_month) && isset($rep_end_day) && isset($rep_end_year))
  236. {
  237. // Get the repeat entry settings
  238. $rep_enddate = mktime($hour, $minute, 0,
  239. $rep_end_month, $rep_end_day, $rep_end_year);
  240. }
  241. else
  242. {
  243. $rep_type = 0;
  244. }
  245. if (!isset($rep_day))
  246. {
  247. $rep_day = array();
  248. }
  249. // For weekly repeat(2), build string of weekdays to repeat on:
  250. $rep_opt = "";
  251. if (($rep_type == 2) || ($rep_type == 6))
  252. {
  253. for ($i = 0; $i < 7; $i++)
  254. {
  255. $rep_opt .= empty($rep_day[$i]) ? "0" : "1";
  256. }
  257. }
  258. // Expand a series into a list of start times:
  259. if ($rep_type != 0)
  260. {
  261. $reps = mrbsGetRepeatEntryList($starttime,
  262. isset($rep_enddate) ? $rep_enddate : 0,
  263. $rep_type, $rep_opt, $max_rep_entrys,
  264. $rep_num_weeks);
  265. }
  266. // When checking for overlaps, for Edit (not New), ignore this entry and series:
  267. $repeat_id = 0;
  268. if (isset($id))
  269. {
  270. $ignore_id = $id;
  271. $repeat_id = sql_query1("SELECT repeat_id FROM $tbl_entry WHERE id=$id");
  272. if ($repeat_id < 0)
  273. {
  274. $repeat_id = 0;
  275. }
  276. }
  277. else
  278. {
  279. $ignore_id = 0;
  280. }
  281. // Acquire mutex to lock out others trying to book the same slot(s).
  282. if (!sql_mutex_lock("$tbl_entry"))
  283. {
  284. fatal_error(1, get_vocab("failed_to_acquire"));
  285. }
  286. // Check for any schedule conflicts in each room we're going to try and
  287. // book in
  288. $err = "";
  289. foreach ( $rooms as $room_id )
  290. {
  291. if ($rep_type != 0 && !empty($reps))
  292. {
  293. if(count($reps) < $max_rep_entrys)
  294. {
  295. for ($i = 0; $i < count($reps); $i++)
  296. {
  297. // calculate diff each time and correct where events
  298. // cross DST
  299. $diff = $endtime - $starttime;
  300. $diff += cross_dst($reps[$i], $reps[$i] + $diff);
  301. $tmp = mrbsCheckFree($room_id,
  302. $reps[$i],
  303. $reps[$i] + $diff,
  304. $ignore_id,
  305. $repeat_id);
  306. if (!empty($tmp))
  307. {
  308. $err = $err . $tmp;
  309. }
  310. }
  311. }
  312. else
  313. {
  314. $err .= get_vocab("too_may_entrys") . "\n";
  315. $hide_title = 1;
  316. }
  317. }
  318. else
  319. {
  320. $err .= mrbsCheckFree($room_id, $starttime, $endtime-1, $ignore_id, 0);
  321. }
  322. } // end foreach rooms
  323. // If the rooms were free, go ahead an process the bookings
  324. if (empty($err))
  325. {
  326. foreach ( $rooms as $room_id )
  327. {
  328. if ($edit_type == "series")
  329. {
  330. $new_id = mrbsCreateRepeatingEntrys($starttime,
  331. $endtime,
  332. $rep_type,
  333. $rep_enddate,
  334. $rep_opt,
  335. $room_id,
  336. $create_by,
  337. $name,
  338. $type,
  339. $description,
  340. isset($rep_num_weeks) ? $rep_num_weeks : 0);
  341. // Send a mail to the Administrator
  342. if (MAIL_ADMIN_ON_BOOKINGS or MAIL_AREA_ADMIN_ON_BOOKINGS or
  343. MAIL_ROOM_ADMIN_ON_BOOKINGS or MAIL_BOOKER)
  344. {
  345. include_once "functions_mail.inc";
  346. // Send a mail only if this a new entry, or if this is an
  347. // edited entry but we have to send mail on every change,
  348. // and if mrbsCreateRepeatingEntrys is successful
  349. if ( ( (isset($id) && MAIL_ADMIN_ALL) or !isset($id) ) &&
  350. (0 != $new_id) )
  351. {
  352. // Get room name and area name. Would be better to avoid
  353. // a database access just for that. Ran only if we need
  354. // details
  355. if (MAIL_DETAILS)
  356. {
  357. $sql = "SELECT r.id AS room_id, r.room_name, r.area_id, a.area_name ";
  358. $sql .= "FROM $tbl_room r, $tbl_area a ";
  359. $sql .= "WHERE r.id=$room_id AND r.area_id = a.id";
  360. $res = sql_query($sql);
  361. $row = sql_row_keyed($res, 0);
  362. $room_name = $row['room_name'];
  363. $area_name = $row['area_name'];
  364. }
  365. // If this is a modified entry then call
  366. // getPreviousEntryData to prepare entry comparison.
  367. if ( isset($id) )
  368. {
  369. $mail_previous = getPreviousEntryData($id, 1);
  370. }
  371. $result = notifyAdminOnBooking(!isset($id), $new_id);
  372. }
  373. }
  374. }
  375. else
  376. {
  377. // Mark changed entry in a series with entry_type 2:
  378. if ($repeat_id > 0)
  379. {
  380. $entry_type = 2;
  381. }
  382. else
  383. {
  384. $entry_type = 0;
  385. }
  386. // Create the entry:
  387. $new_id = mrbsCreateSingleEntry($starttime,
  388. $endtime,
  389. $entry_type,
  390. $repeat_id,
  391. $room_id,
  392. $create_by,
  393. $name,
  394. $type,
  395. $description);
  396. // Send a mail to the Administrator
  397. if (MAIL_ADMIN_ON_BOOKINGS or MAIL_AREA_ADMIN_ON_BOOKINGS or
  398. MAIL_ROOM_ADMIN_ON_BOOKINGS or MAIL_BOOKER)
  399. {
  400. include_once "functions_mail.inc";
  401. // Send a mail only if this a new entry, or if this is an
  402. // edited entry but we have to send mail on every change,
  403. // and if mrbsCreateRepeatingEntrys is successful
  404. if ( ( (isset($id) && MAIL_ADMIN_ALL) or !isset($id) ) && (0 != $new_id) )
  405. {
  406. // Get room name and are name. Would be better to avoid
  407. // a database access just for that. Ran only if we need
  408. // details.
  409. if (MAIL_DETAILS)
  410. {
  411. $sql = "SELECT r.id AS room_id, r.room_name, r.area_id, a.area_name ";
  412. $sql .= "FROM $tbl_room r, $tbl_area a ";
  413. $sql .= "WHERE r.id=$room_id AND r.area_id = a.id";
  414. $res = sql_query($sql);
  415. $row = sql_row_keyed($res, 0);
  416. $room_name = $row['room_name'];
  417. $area_name = $row['area_id'];
  418. }
  419. // If this is a modified entry then call
  420. // getPreviousEntryData to prepare entry comparison.
  421. if ( isset($id) )
  422. {
  423. $mail_previous = getPreviousEntryData($id, 0);
  424. }
  425. $result = notifyAdminOnBooking(!isset($id), $new_id);
  426. }
  427. }
  428. }
  429. } // end foreach $rooms
  430. // Delete the original entry
  431. if (isset($id))
  432. {
  433. mrbsDelEntry(getUserName(), $id, ($edit_type == "series"), 1);
  434. }
  435. sql_mutex_unlock("$tbl_entry");
  436. // Now it's all done go back to the previous view
  437. header("Location: $returl");
  438. exit;
  439. }
  440. // The room was not free.
  441. sql_mutex_unlock("$tbl_entry");
  442. if (strlen($err))
  443. {
  444. print_header($day, $month, $year, $area, isset($room) ? $room : "");
  445. echo "<h2>" . get_vocab("sched_conflict") . "</h2>\n";
  446. if (!isset($hide_title))
  447. {
  448. echo "<p>\n";
  449. echo get_vocab("conflict").":\n";
  450. echo "</p>\n";
  451. echo "<ul>\n";
  452. }
  453. echo $err;
  454. if(!isset($hide_title))
  455. {
  456. echo "</ul>\n";
  457. }
  458. }
  459. echo "<p>\n";
  460. echo "<a href=\"" . htmlspecialchars($returl) . "\">" . get_vocab("returncal") . "</a>\n";
  461. echo "</p>\n";
  462. include "trailer.inc";
  463. ?>