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

/mrbs/branches/improve_css_2008_06/web/edit_entry_handler.php

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