/set_blackout.php

https://github.com/gwu-libraries/srrs-mobile · PHP · 151 lines · 87 code · 20 blank · 44 comment · 19 complexity · 4e92baa4cdc2c2a843f38c3694bc4a2c MD5 · raw file

  1. <?php
  2. /**
  3. * Interface form for placing/modifying/viewing a blackout
  4. * This file will present a form for a user to
  5. * make a new blackout or modify/delete an old one.
  6. * It will also allow other users to view this blackout.
  7. * @author Nick Korbel <lqqkout13@users.sourceforge.net>
  8. * @version 02-22-04
  9. * @package phpScheduleIt
  10. */
  11. /**
  12. * Template class
  13. */
  14. include_once('lib/Template.class.php');
  15. /**
  16. * Reservation class
  17. */
  18. include_once('lib/Blackout.class.php');
  19. // Check that the admin is logged in
  20. if (!Auth::isAdmin()) {
  21. CmnFns::do_error_box('This section is only available to the administrator.<br />'
  22. . '<a href="ctrlpnl.php">Back to My Control Panel</a>');
  23. }
  24. $t = new Template();
  25. //AK HTTP_REFERER is blocked is cleared by gelman firewall will not run this check
  26. if (isset($_POST['submit']) /*&& strstr($_SERVER['HTTP_REFERER'], $_SERVER['PHP_SELF'])*/) {
  27. $t->set_title('Processing Blackout');
  28. $t->printHTMLHeader();
  29. $t->startMain();
  30. process_blackout($_POST['fn']);
  31. }
  32. else {
  33. $blackout_info = getBlackoutInfo();
  34. $t->set_title($blackout_info['title']);
  35. $t->printHTMLHeader();
  36. $t->startMain();
  37. present_blackout($blackout_info['resid']);
  38. }
  39. // End main table
  40. $t->endMain();
  41. // Print HTML footer
  42. $t->printHTMLFooter();
  43. /**
  44. * Processes a reservation request (add/del/edit)
  45. * @param string $fn function to perform
  46. */
  47. function process_blackout($fn) {
  48. $success = false;
  49. if (isset($_POST['resid']))
  50. $res = new Blackout($_POST['resid']);
  51. else if (isset($_GET['resid']))
  52. $res = new Blackout($_GET['resid']);
  53. else {
  54. $res = new Blackout();
  55. if (isset($_POST['repeat_day'])) { // Check for reservation repeation
  56. $res->is_repeat = true;
  57. $repeat = get_repeat_dates($_POST['ts'], $_POST['repeat_day'], $_POST['duration']);
  58. }
  59. else
  60. $repeat = array($_POST['ts']);
  61. }
  62. if ($fn == 'create')
  63. $res->add_blackout($_POST['machid'], $_POST['starttime'], $_POST['endtime'], $repeat, $_POST['summary']);
  64. else if ($fn == 'modify')
  65. $res->mod_blackout($_POST['starttime'], $_POST['endtime'], isset($_POST['del']), isset($_POST['mod_recur']), $_POST['summary']);
  66. else if ($fn == 'delete')
  67. $res->del_blackout(isset($_POST['mod_recur']));
  68. }
  69. /**
  70. * Prints out reservation info depending on what parameters
  71. * were passed in through the query string
  72. * @param none
  73. */
  74. function present_blackout($blackoutid) {
  75. // Get info about this reservation
  76. $blackout = new Blackout($blackoutid);
  77. $blackout->print_res();
  78. }
  79. /**
  80. * Return array of data from query string about this reservation
  81. * or about a new reservation being created
  82. * @param none
  83. */
  84. function getBlackoutInfo() {
  85. $blackout_info = array();
  86. // Determine title and set needed variables
  87. $blackout_info['type'] = $_GET['type'];
  88. switch($blackout_info['type']) {
  89. case 'r' :
  90. $blackout_info['title'] = 'New Blackout';
  91. $blackout_info['resid'] = null;
  92. break;
  93. case 'm' :
  94. $blackout_info['title'] = 'Modify Blackout';
  95. $blackout_info['resid'] = $_GET['resid'];
  96. break;
  97. case 'd' :
  98. $blackout_info['title'] = 'Delete Blackout';
  99. $blackout_info['resid'] = $_GET['resid'];
  100. break;
  101. }
  102. return $blackout_info;
  103. }
  104. /**
  105. * Returns an array of all timestamps for repeat reservations
  106. * @param int $res_date initial reservation date
  107. * @param array $days_to_repeat days of week to repeat reservation
  108. * @param int $duration weeks to repeat reservation for
  109. * @return array of timestamps of reservations
  110. */
  111. function get_repeat_dates($blackout_date, $days_to_repeat, $duration) {
  112. $day_of_week = date('w', $blackout_date); // Day of week for initial reservation
  113. $blackout_dates = array($blackout_date);
  114. $date_vals = getdate($blackout_date);
  115. // Repeat for all weeks
  116. $additional_days = 0;
  117. for ($repeat_week = 0; $repeat_week < $duration; $repeat_week++) { // Repeat for duration
  118. for ($i = 0; $i < count($days_to_repeat); $i++) { // Repeat for all days selected
  119. $days_between = ($days_to_repeat[$i] - $day_of_week) + $additional_days;
  120. // If the day of week is less than reservation day of week, move ahead one week
  121. if ($days_to_repeat[$i] <= $day_of_week) {
  122. $days_between += 7;
  123. }
  124. $blackout_date = mktime(0,0,0, $date_vals['mon'], $date_vals['mday'] + $days_between);
  125. array_push($blackout_dates, $blackout_date);
  126. }
  127. $additional_days += 7; // Move ahead one week
  128. }
  129. return $blackout_dates;
  130. }
  131. ?>