X-Git-Url: https://git.rohieb.name/iserv-mod-room-reservation.git/blobdiff_plain/acb7976b2c4752b087fa187f10ec5f51c0a7b1d2..b4fea5220b8c3fd880137f0f83d27b31f794860a:/includes/mod_roomReservationBookingTable.inc
diff --git a/includes/mod_roomReservationBookingTable.inc b/includes/mod_roomReservationBookingTable.inc
deleted file mode 100644
index 706d757..0000000
--- a/includes/mod_roomReservationBookingTable.inc
+++ /dev/null
@@ -1,776 +0,0 @@
-oCfg = $oCfg;
- $this->oRm = $oRm;
- $this->oBm = $oBm;
-
- try {
- $this->processRequestVariables();
- } catch(Exception $e) {
- $this->asErrors[] = $e->getMessage();
- }
- $this->addCSS();
- }
-
- /***************************************************************************/
- /**
- * @}
- * @name Initialization
- * @{
- */
-
- /**
- * Process the REQUEST variables and preset the some variables. Throws an
- * exception if the room provided by the GET data is not allowed for booking
- * @return void
- * @throws Exception
- */
- protected function processRequestVariables() {
-
- // default values
- $aoRooms = $this->oCfg->getWhitelistedRooms();
- if(count($aoRooms) < 1) {
- $this->setRoom("");
- } else {
- $this->setRoom($aoRooms[0]->getName());
- }
- $this->setDate(time());
- $this->setAction(MOD_ROOM_RESERVATION_BT_ACTION_SHOW);
- $this->nPostInterval = 0;
-
- // handle GET parameters
- if(isset($_GET["mod_roomReservationBookingTable"])) {
- $ga = isset($_GET["mod_roomReservationBookingTable"]["action"]) ?
- $_GET["mod_roomReservationBookingTable"]["action"] : "";
- $this->setAction(($ga == "book") ?
- MOD_ROOM_RESERVATION_BT_ACTION_BOOK : (($ga == "edit") ?
- MOD_ROOM_RESERVATION_BT_ACTION_EDIT : (($ga == "delete") ?
- MOD_ROOM_RESERVATION_BT_ACTION_DELETE : (($ga == "submit") ?
- MOD_ROOM_RESERVATION_BT_ACTION_SUBMIT : (($ga == "submitdelete") ?
- MOD_ROOM_RESERVATION_BT_ACTION_SUBMITDELETE :
- MOD_ROOM_RESERVATION_BT_ACTION_SHOW)))));
- $this->setDate(isset($_GET["mod_roomReservationBookingTable"]["date"]) ?
- intval($_GET["mod_roomReservationBookingTable"]["date"]) : time());
- if(isset($_GET["mod_roomReservationBookingTable"]["room"])) {
- $this->setRoom($_GET["mod_roomReservationBookingTable"]["room"]);
- }
- $this->setTsFirst(
- isset($_GET["mod_roomReservationBookingTable"]["tsfirst"]) ?
- intval($_GET["mod_roomReservationBookingTable"]["tsfirst"]) : 0);
- $this->setTsLast($this->getTsFirst());
-
- // if deletion form is requested, set the right date, room etc.
- if($this->getAction() == MOD_ROOM_RESERVATION_BT_ACTION_DELETE) {
- if(isset($_GET["mod_roomReservationBookingTable"]["uid"]) &&
- $_GET["mod_roomReservationBookingTable"]["uid"] >= 0) {
- $this->setUid(intval(
- $_GET["mod_roomReservationBookingTable"]["uid"]));
- } else {
- trigger_error("The UID is invalid.", E_USER_ERROR);
- }
- $ob = mod_roomReservationBookingsManager::getBookingByUid(
- $this->getUid());
- $this->setRoom($ob->getRoom());
- if($ob->getInterval() > 0) {
- // don't show the first date when the booking was created, but the
- // date of the page where the user clicked the delete button
- $this->setDate(
- isset($_GET["mod_roomReservationBookingTable"]["date"]) ?
- intval($_GET["mod_roomReservationBookingTable"]["date"]) : time());
- } else {
- $this->setDate($ob->getDate());
- }
- $this->setTsFirst($ob->getTsFirst());
- }
- }
-
- if(isset($_POST["mod_roomReservationBookingTable"])) {
- if(isset($_POST["mod_roomReservationBookingTable"]["submitbooking"])) {
- // submission of the booking form
- // let POST variables overwrite the variables
- $this->setDate(
- isset($_POST["mod_roomReservationBookingTable"]["date"]) ?
- intval($_POST["mod_roomReservationBookingTable"]["date"]) : time());
- $this->setRoom(
- isset($_POST["mod_roomReservationBookingTable"]["room"]) ?
- $_POST["mod_roomReservationBookingTable"]["room"] : "");
- $this->setTsFirst(
- isset($_POST["mod_roomReservationBookingTable"]["tsfirst"]) ?
- intval($_POST["mod_roomReservationBookingTable"]["tsfirst"]) : 0);
- $this->setTsLast(
- isset($_POST["mod_roomReservationBookingTable"]["tslast"]) ?
- intval($_POST["mod_roomReservationBookingTable"]["tslast"]) :
- $this->getTsFirst());
- $this->setReason(
- isset($_POST["mod_roomReservationBookingTable"]["reason"]) ?
- $_POST["mod_roomReservationBookingTable"]["reason"] : "");
- $this->nPostInterval =
- isset($_POST["mod_roomReservationBookingTable"]["interval"]) ?
- intval($_POST["mod_roomReservationBookingTable"]["interval"]) : 0;
- $this->strPostAccount =
- isset($_POST["mod_roomReservationBookingTable"]["account"]) ?
- $_POST["mod_roomReservationBookingTable"]["account"] : "";
- }
-
- if(isset($_POST["mod_roomReservationBookingTable"]["submitdelete"])) {
- // submission of the deletion form
- if(isset($_POST["mod_roomReservationBookingTable"]["uid"]) &&
- $_POST["mod_roomReservationBookingTable"]["uid"] >= 0) {
- $this->setUid(
- intval($_POST["mod_roomReservationBookingTable"]["uid"]));
- } else {
- trigger_error("The UID is invalid.", E_USER_ERROR);
- }
- // set the right date, room etc.
- $ob = mod_roomReservationBookingsManager::getBookingByUid(
- $this->getUid());
- $this->setRoom($ob->getRoom());
- $this->setDate($ob->getDate());
- $this->setTsFirst($ob->getTsFirst());
- $this->setSubmitButtonValue(isset(
- $_POST["mod_roomReservationBookingTable"]["submitdelete"]) ?
- $_POST["mod_roomReservationBookingTable"]["submitdelete"] : "");
- }
- }
- }
-
- /***************************************************************************/
- /**
- * @}
- * @name Access to attributes
- * @{
- */
-
- /**
- * Set the action that should be done
- * @param $c (constant) See @ref bookingtable_actions for possible values
- */
- protected function setAction($c) { $this->cAction = intval($c); }
-
- /**
- * Set the starting timeslice of the requested booking
- * @param $n (int)
- */
- protected function setTsFirst($n) { $this->nTsFirst = intval($n); }
-
- /**
- * Set the ending timeslice of the requested booking
- * @param $n (int)
- */
- protected function setTsLast($n) { $this->nTsLast = intval($n); }
-
- /**
- * Set the date of the requested booking or the date to be shown in the
- * booking table
- * @param $ts (timestamp)
- */
- public function setDate($ts) { $this->tsDate = intval($ts); }
-
- /**
- * Set the room of the requested booking or the room to be shown in the
- * booking table. Throws an Exception if the room is not allowed for booking.
- * @param $str (string)
- * @throws Exception
- */
- protected function setRoom($str) {
- // only allow whitelisted rooms
- if($this->oCfg->isRoomWhitelisted($str)) {
- $this->strRoom = $str;
- } else {
- throw new Exception(_c("room-reservation:This room is not available ".
- "for booking."));
- }
- }
-
- /**
- * Set the reason of the requested booking
- * @param $str (string)
- */
- protected function setReason($str) { $this->strReason = $str; }
-
- /**
- * Set the UID of the booking to be deleted / edited
- * @param $n (int)
- */
- protected function setUid($n) { $this->nUid = intval($n); }
-
- /**
- * Set the value of the submit button that the user clicked
- * @param $str (string)
- */
- protected function setSubmitButtonValue($str) {
- $this->strSubmitButtonValue = $str;
- }
-
- /**
- * Get the name of the room of the requested booking or the room to show in
- * the booking table
- * @return string
- */
- public function getRoom() { return $this->strRoom; }
-
- /**
- * Get the action that should be done
- * @return constant See @ref bookingtable_actions for possible values
- */
- public function getAction() { return $this->cAction; }
-
- /**
- * Get the the starting timeslice of the requested booking
- * @return int
- */
- public function getTsFirst() { return $this->nTsFirst; }
-
- /**
- * Get the the ending timeslice of the requested booking
- * @return int
- */
- public function getTsLast() { return $this->nTsLast; }
-
- /**
- * Get the the date of the requested booking or the date to be shown in the
- * booking table
- * @return timestamp
- */
- public function getDate() { return $this->tsDate; }
-
- /**
- * Get the the reason of the requested booking
- * @return string
- */
- public function getReason() { return $this->strReason; }
-
- /**
- * Get the UID of the booking to be deleted / edited
- * @return int
- */
- public function getUid() { return $this->nUid; }
-
- /**
- * Get the value of the submit button that the user clicked
- * @return string
- */
- public function getSubmitButtonValue() {
- return $this->strSubmitButtonValue;
- }
-
- /***************************************************************************/
- /**
- * @}
- * @name Output
- * @{
- */
-
- /**
- * Add the CSS rules needed for this page
- * @return void
- */
- protected function addCSS() {
- $strCss = << %s ".nl2br(q(join("\n", $asMsgs)))." " .
- MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED . "
\n", $this->asErrors));
- return;
- }
-
- // Print the header with the days
- $ncTs = sizeof($this->oCfg->getTimeslices());
- $nDays = ($this->oCfg->isShowWeekend()) ? 7 : 5;
-
- echo "";
-
- // Print header with day names
- echo " \n";
-
- // Print timetable
- // To take care of bookings with more than one timeslice, we use an array
- // that tells us which cell in the current column is the next to fill
- $anNextRow = array_fill(0, $nDays, 0);
- // Iterate over the timeslices
- for($nTs = 0; $nTs < $ncTs; $nTs++) {
- $strLessons = $this->oCfg->isShowLessons() ?_sprintf_ord(
- _c("room-reservation:%s# lesson"), $nTs + 1) . "";
- for($ts = rrGetMonday($this->getDate()), $i=0; $i < $nDays;
- $ts = strtotime("1 day", $ts), $i++) {
- // Use a different color for the current day
- $strClass = "heading";
- $strTitle = strftime("%A
%x", $ts);
- if(date("Ymd") === date("Ymd", $ts)) {
- $strClass .= " today";
- $strTitle .= " "._c("room-reservation:(today)");
- }
- echo sprintf("%s ", $strClass, $strTitle);
- }
- echo "
" : "";
- $oTs = $this->oCfg->getTimeslice($nTs);
- $strTs = sprintf("%s - %s", gmstrftime(_("%#I:%M %p"), $oTs->getBegin()),
- gmstrftime(_("%#I:%M %p"), $oTs->getEnd()));
- // First column: Lesson
- echo sprintf(" \n";
- }
- echo "%s ", $strLessons . $strTs);
-
- // Iterate over the days
- for($ts = rrGetMonday($this->getDate()), $i = 0; $i <= $nDays;
- $ts = strtotime("1 day", $ts), $i++) {
- // Don't print if there is a spanning booking on the current cell
- if(isset($anNextRow[$i]) && $anNextRow[$i] == $nTs) {
- if(($ob = $this->oBm->getBookingByTimeslice($this->getRoom(), $ts,
- $nTs)) !== null) {
- // a booking exists here
- // print booking or deletion form or handle the deletion form
-
- // deletion form is requested:
- if(($this->getAction() == MOD_ROOM_RESERVATION_BT_ACTION_DELETE) &&
- (date("Ymd", $this->getDate()) == date("Ymd", $ts)) &&
- ($this->getTsFirst() == $nTs) &&
- ($this->getRoom() == $this->getRoom())) {
- $anNextRow[$i] += $this->printBooking($nTs, $ts, $ob,
- MOD_ROOM_RESERVATION_BTPB_DELETE);
-
- // deletion form is submitted:
- } else if(($this->getAction() ==
- MOD_ROOM_RESERVATION_BT_ACTION_SUBMITDELETE) &&
- (date("Ymd", $this->getDate()) == date("Ymd", $ts)) &&
- ($this->getTsFirst() == $nTs) &&
- ($this->getRoom() == $this->getRoom())) {
- if($this->getSubmitButtonValue() == _("Delete")) {
- // the user clicked the "delete" button
- $bSuccess = false;
- try {
- $bSuccess = $this->oBm->delete($this->getUid());
- } catch(Exception $e) {
- $anNextRow[$i] += $this->printBooking($nTs, $ts, $ob, 0,
- array($e->getMessage()));
- }
- // print booking link and a success message
- if($bSuccess) {
- $anNextRow[$i] += $this->printBookingLink($nTs, $ts,
- array(_c("room-reservation:The booking was deleted.")));
- }
- } else {
- // the user cancelled the request
- $anNextRow[$i] += $this->printBooking($nTs, $ts, $ob);
- }
-
- // Something else -- print booking
- } else {
- $anNextRow[$i] += $this->printBooking($nTs, $ts, $ob);
- }
- } else {
- // no booking is here
- // print booking link, booking form or handle booking form
- $asErrors = array();
-
- // booking form is requested:
- if(($this->getAction() == MOD_ROOM_RESERVATION_BT_ACTION_BOOK) &&
- (date("Ymd", $this->getDate()) == date("Ymd", $ts)) &&
- ($this->getTsFirst() == $nTs) &&
- ($this->getRoom() == $this->getRoom())) {
- $anNextRow[$i] += $this->printBookingForm($nTs, $ts, $asErrors);
-
- // booking form is submitted:
- } else if(($this->getAction() ==
- MOD_ROOM_RESERVATION_BT_ACTION_SUBMIT) &&
- // only handle the request if the form was in the current cell
- (date("Ymd", $this->getDate()) == date("Ymd", $ts)) &&
- ($this->getTsFirst() == $nTs) &&
- ($this->getRoom() == $this->getRoom())) {
-
- // try writing the booking to the database
- $nNewUid = -1;
- $oNewBooking = new mod_roomReservationBooking($this->getRoom(),
- $this->getDate(), $this->getTsFirst(), $this->getTsLast(),
- (trim($this->strPostAccount) == "") ? $_SESSION["act"] :
- $this->strPostAccount, $this->getReason(),
- $this->nPostInterval);
- try {
- $nNewUid = $this->oBm->write($oNewBooking);
- } catch(Exception $s) {
- // print the booking form again with the user's input
- // @todo check for overlapping bookings and print them
- $asErrors[] = $s->getMessage();
- $anNextRow[$i] += $this->printBookingForm($nTs, $ts,
- $asErrors);
- }
- if($nNewUid > 0) {
- // print new booking and increment the "next row" variable by
- // the current span
- $oNewBooking->setUid($nNewUid);
- $anNextRow[$i] += $this->printBooking($nTs, $ts, $oNewBooking,
- MOD_ROOM_RESERVATION_BTPB_NEW);
- }
-
- // Something else -- print booking link:
- } else {
- $anNextRow[$i] += $this->printBookingLink($nTs, $ts);
- }
- }
- }
- }
- echo "
";
- }
-
- /**
- * Print a single booking in the booking table.
- * @param $nTs (int) current timeslice
- * @param $ts (timestamp) current date
- * @param $ob (mod_roomReservationBooking) the booking
- * @param $cFlags (constant) Flags,
- * See @ref bookingtable_printbooking_flags for more information.
- * @param $asMsgs (array of strings) Additional messages to be printed
- * inside the cell, one array element per message
- * @return (int) the span of the booking
- */
- protected function printBooking($nTs, $ts, mod_roomReservationBooking $ob,
- $cFlags = 0, $asMsgs = array()) {
- $strAfter = "";
- $strBefore = "";
-
- // messages
- if(count($asMsgs) > 0) {
- $strBefore .= "
".nl2br(q(join("\n", $asErrors)))."
"; - - // form to allow fixed bookings for admins - $sWeeklyForm = ""; - if($this->oCfg->userIsAdmin()) { - $sWeeklyForm = sprintf(" %s".join("
", $asMsgs)."