From: rohieb Date: Tue, 5 Aug 2008 16:28:22 +0000 (+0200) Subject: renamed includes/* to inc/* X-Git-Tag: REL_8.09.13~5 X-Git-Url: https://git.rohieb.name/iserv-mod-room-reservation.git/commitdiff_plain/b4fea5220b8c3fd880137f0f83d27b31f794860a renamed includes/* to inc/* --- diff --git a/Makefile b/Makefile index c1de84b..6775183 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ links: sourcedoc # includes $(MKDIR) $(DESTDIR)/usr/share/iserv/www/inc/mod_room-reservation - $(LN) includes/*.inc $(DESTDIR)/usr/share/iserv/www/inc/mod_room-reservation/ + $(LN) inc/*.inc $(DESTDIR)/usr/share/iserv/www/inc/mod_room-reservation/ if [ ! -e $(DESTDIR)/usr/share/iserv/www/inc/mod_room-reservation/config.inc ] ; then touch $(DESTDIR)/usr/share/iserv/www/inc/mod_room-reservation/config.inc; fi ifeq ($(USER),root) chmod g+w $(DESTDIR)/usr/share/iserv/www/inc/mod_room-reservation/config.inc diff --git a/inc/exceptions.inc b/inc/exceptions.inc new file mode 100644 index 0000000..8d7c688 --- /dev/null +++ b/inc/exceptions.inc @@ -0,0 +1,44 @@ + \ No newline at end of file diff --git a/inc/functions.inc b/inc/functions.inc new file mode 100644 index 0000000..78e3690 --- /dev/null +++ b/inc/functions.inc @@ -0,0 +1,279 @@ + 0); +} + +/** + * Get the real user name for an account name + * @param $strAct (string) Account name of the user to look up + * @return (string) The real name of the user. If the function fails, it returns null. + * Call getLastError() to get more information about the error. + */ +function getRealUserName($strAct) { + $hQuery = db_query("SELECT firstname, lastname FROM users WHERE act = $1;", $strAct); + if(!is_resource($hQuery)) { + // TODO throw exception + setLastError(RS_ERROR_SQL); + return null; + } + if(pg_num_rows($hQuery) == 0) { + return $strAct; // User not found in database, return account name + } + $arResult = pg_fetch_array($hQuery); + return user_join_name($arResult); +} + +/** + * Determine if a specified group exists + * @param $strAct (string) Account name of the group + * @return (bool / null) If the function fails, it returns null. Call getLastError() to + * get more information about the error. + */ +function isGroup($strAct) { + $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct); + if(!is_resource($hQuery)) { + // TODO throw exception + setLastError(RS_ERROR_SQL); + return null; + } + return (pg_num_rows($hQuery) > 0); +} + +/** + * Look up the name of a group + * @param $strAct (string) Account name of the group + * @return (string) The name of the group. If the function fails, it returns null. + * Call getLastError() to get more information about the error. + */ +function getGroupName($strAct) { + $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct); + if(!is_resource($hQuery)) { + // TODO throw exception + setLastError(RS_ERROR_SQL); + return null; + } + if(pg_num_rows($hQuery) == 0) { + return $strAct; // Group not found in database, return account name + } + $arResult = pg_fetch_array($hQuery); + return $arResult["name"]; +} + +/** + * Create a link to write a mail to the specified account name. + * This function returns a link if the specified account exists, otherwise it returns the + * account name. + * @param $strAct (string) Account name + * @param $strColor (string) Background color for icon() + * @param $strParams (string) additional URL parameters + * @return string + */ +function mailToUserLink($strAct, $strColor = "bl", $strParams = "") { + if(!isAct($strAct)) { + return $strAct; + } + return popup(relroot("msg/write/?to=".user_mail_addr($strAct).$strParams), + 600, 400, nobr(icon("mail-reply-usr", array("size" => 16, "bg" => + $strColor)) . getRealUserName($strAct))); +} + +/** + * Determine if a specified string is a valid mail address + * @param $strAddr string + * @return string + */ +function isMailAddress($strAddr) { + return ((preg_match("/([a-zA-Z0-9_\-\.]*(@[a-zA-Z0-9\-\.]*)?(\s*,\s*)?)+/", $strAddr) > 0) + and (preg_match("/(\s*,\s*)$/", $strAddr) == 0)); +} + +/** + * Module-specific logging function. + * Prefixes the log message with a module-specific string and writes it to the logs. + * @param $strLog (string) Log message + * @return void + */ +function rrInsertLog($strLog) { + log_insert($strLog, null, "Room Reservation Schedule"); +} + +/** + * Get the SQL day number from a given timestamp. + * @param $ts (timestamp) + * @return (int) 0-6 for Sunday to Monday + */ +function rrDateToSQLDayNumber($ts) { + $aDays = array("Sun" => 0, "Mon" => 1, "Tue" => 2, + "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6); + return $aDays[date("D", $ts)]; +} + +/** + * Convert a UNIX timestamp to an SQL date string + * @param $ts (timestamp) + * @return string + */ +function dateToSql($ts) { return date("Y\-m\-d", intval($ts)); } + +/** + * Calculate the timestamp of the monday in the current week + * @param $ts (timestamp) Calculate the value relative to this date + * @return timestamp + */ +function rrGetMonday($ts = null) { + if($ts === null) { + $ts = time(); + } + if(date("D", $ts) == "Mon") { + // Today is monday + return strtotime("00:00", $ts); + } else { + return strtotime("last monday", $ts); + } +} + +/** (array of strings) Additional CSS rules */ +$GLOBALS["rrLocalCss"] = array(); + +/** + * Add CSS rules to the page + * @param $s (string) + * @return void + */ +function rrAddCss($s) { + rrDebug("rrAddCss: add \"$s\""); + $GLOBALS["rrLocalCss"][] = $s; +} + +/** + * Get CSS rules that have been added with rrAddCss() + * @return string + */ +function rrGetCss() { + rrDebug("rrGetCss: Local CSS is ".var_export($GLOBALS["rrLocalCss"], true)); + return implode("\n", $GLOBALS["rrLocalCss"]); +} + +function rrDebug($s, $bReturn = false) { + if(isset($_GET["debug"])) { + if(!$bReturn) { + echo "\n"; + } else { + return $s; + } + } +} + +/** + * sprintf with support for ordinal numbers. + * This version of sprintf replaces all substrings of the type /\\d+#/ + * (i.e. a decimal number with a hash sign appended) in the input string with + * the corresponding english ordinal number prefices (st, nd, rd, th). + * @param $str (string) Input string + * @param $args (mixed) Additional parameters to be passed to sprintf() + * @return (string) + */ +function _sprintf_ord($str, $args /*leave this parameters for doxygen*/) { + $args = func_get_args(); + if(preg_match_all("/%[bcdufosxX]/", $args[0], $temp) != func_num_args()-1) { + trigger_error("Too few arguments", E_USER_ERROR); + return false; + } + $str = call_user_func_array("sprintf", $args); + while(preg_match("/(.*)(\d+)#(.*)/", $str, $m)) + $str = $m[1]._(append_ord_suffix($m[2])).$m[3]; + return $str; +} +?> diff --git a/inc/globals.inc b/inc/globals.inc new file mode 100644 index 0000000..eb2977e --- /dev/null +++ b/inc/globals.inc @@ -0,0 +1,47 @@ + diff --git a/inc/mod_roomReservationBooking.inc b/inc/mod_roomReservationBooking.inc new file mode 100644 index 0000000..a6a1c8a --- /dev/null +++ b/inc/mod_roomReservationBooking.inc @@ -0,0 +1,201 @@ +setUid(null); + $this->setRoom($strRoom); + $this->setDate($tsDate); + $this->setTsFirst($nTsFirst); + $this->setTsLast($nTsLast); + $this->setAct($strAct); + $this->setReason($strReason); + $this->setInterval($nInterval); + } + + /***************************************************************************/ + /** + * @} + * @name Access to attributes + * @{ + */ + + /** + * Set the unique ID in database + * @param $n (int) Unique ID in database + * @return void + */ + public function setUid($n) { + if(is_null($n)) { + $this->nUid = null; + } else { + $this->nUid = intval($n); + } + } + + /** + * Set the name of the room + * @param $str (string) Name of the room + * @return void + */ + public function setRoom($str) { $this->strRoom = $str; } + + /** + * Set the date when the booking takes place + * @param $ts (timestamp) Date, only the date part is taken care of + * @return void + */ + public function setDate($ts) { + // Only take the date part + $this->tsDate = intval(strtotime(date("Y\-m\-d", intval($ts)))); } + + /** + * Set the first timeslice + * @param $n (int) Number of the first timeslice + * @return void + */ + public function setTsFirst($n) { $this->nTsFirst = intval($n); } + + /** + * Set the end timeslice + * @param $n (int) Number of the last timeslice (may be the start timeslice) + * @return void + */ + public function setTsLast($n) { $this->nTsLast = intval($n); } + + /** + * Set the account name of the owner + * @param $str (string) Account name + * @return void + */ + public function setAct($str) { $this->strAct = $str; } + + /** + * Set the reason for the booking + * @param $str (string) Reason + * @return void + */ + public function setReason($str) { $this->strReason = $str; } + + /** + * Set the flag whether the booking repeates every week + * @param $n (int) interval in weeks, 0 for no recurrence + * @return void + */ + public function setInterval($n) { $this->nInterval = intval(abs($n)); } + + /** + * Get the unique ID in database + * @return int / null + */ + public function getUid() { return intval($this->nUid); } + + /** + * Get the name of the room + * @return string + */ + public function getRoom() { return $this->strRoom; } + + /** + * Get the date when the booking takes place + * @return timestamp + */ + public function getDate() { return intval($this->tsDate); } + + /** + * Get the the number of the first timeslice + * @return int + */ + public function getTsFirst() { return intval($this->nTsFirst); } + + /** + * Get the number of the last timeslice (may be the start timeslice) + * @return int + */ + public function getTsLast() { return intval($this->nTsLast); } + + /** + * Get the account name of the owner + * @return string + */ + public function getAct() { return $this->strAct; } + + /** + * Get the reason for the booking + * @return string + */ + public function getReason() { return $this->strReason; } + + /** + * Get the recurrence interval + * @return int + */ + public function getInterval() { return $this->nInterval; } + + /**@}*/ +} +?> diff --git a/inc/mod_roomReservationBookingPage.inc b/inc/mod_roomReservationBookingPage.inc new file mode 100644 index 0000000..219c7bd --- /dev/null +++ b/inc/mod_roomReservationBookingPage.inc @@ -0,0 +1,215 @@ +oCfg = $oCfg; + $this->oRm = $oRm; + $this->oBm = $oBm; + + // create the booking table here, so the CSS is already added + /** @todo maybe move it into beforeAddCSS()... ? */ + $this->oBt = new mod_roomReservationBookingsTable($this->oCfg, $this->oRm, + $this->oBm, "?bookingpage[action]=edit", "?bookingpage[action]=delete"); + + parent::__construct($oCfg); + $this->setTitle(_c("Room Reservation Schedule")); + $this->setIcon("mod_room-reservation_index"); + } + + /***************************************************************************/ + /** + * @} + * @name Initialization + * @{ + */ + + /** + * Process the REQUEST variables and preset the some variables + * @return void + */ + protected function processRequestVariables() { + // take all settings from the booking table + $this->setRoom($this->oBt->getRoom()); + $this->setStart($this->oBt->getDate()); + } + + /***************************************************************************/ + /** + * @} + * @name Access to attributes + * @{ + */ + + /** + * Set the starting date + * @param $ts (timestamp) + */ + public function setStart($ts) { $this->tsStart = intval($ts); } + + /** + * Set the room to show in the booking table + * @param $str (string) Name of the room + */ + public function setRoom($str) { $this->strRoom = $str; } + + /** + * Get the starting date + * @return int + */ + public function getStart() { return intval($this->tsStart); } + + /** + * Get the name of the room to show in the booking table + * @return string + */ + public function getRoom() { return $this->strRoom; } + + /***************************************************************************/ + /** + * @} + * @name Output + * @{ + */ + + /** + * Show the page. + * @return void + */ + public function doShow() { + // Protect access + if(!$this->oCfg->userCanView()) { + echo sprintf("

%s

", + MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); + _PageBlue(); + die(); + } + + Title(_c("room-reservation:Book rooms")); + + // Form for room selection + /** @todo checkbox for recurring booking */ + echo sprintf("
", + $_SERVER["PHP_SELF"]); + echo sprintf("\n", $this->getStart()); + + // Show rooms only if it is whitelisted + try { + $aor = $this->oCfg->getWhitelistedRooms(); + } catch(SQLException $e) { + trigger_error($e->getMessage()); + } + if(count($aor) > 0) { + echo _c("room-reservation:Room:") . sprintf("  <%s value='%s' />

\n", + $GLOBALS["stdbtn"], _("Change")); + } else { + printf("

%s

\n", _c("room-reservation:No rooms have been ". + "configured yet.")); + return; + } + + // Print line with next 5 or so weeks + $strSep = " | "; + $strLink = sprintf("%%s", + $_SERVER["PHP_SELF"]); + echo "

".sprintf($strLink, time(), qu($this->getRoom()), + _c("room-reservation:Current Week")) . $strSep; + echo sprintf($strLink, strtotime("1 week ago", $this->getStart()), + qu($this->getRoom()), _c("room-reservation:< Back")) . $strSep; + echo sprintf("%s", _sprintf_ord(_c("room-reservation:%d# week"), + date("W", $this->getStart()))) . $strSep; + for($i = 1; $i <= 5; $i++) { + $nNextWeek = strtotime("$i week", $this->getStart()); + echo sprintf($strLink, $nNextWeek, qu($this->getRoom()), _sprintf_ord( + _c("room-reservation:%d# week"), date("W", $nNextWeek))); + echo $strSep; + } + echo sprintf($strLink, strtotime("1 week", $this->getStart()), + qu($this->getRoom()), _c("room-reservation:Next >"))."

\n"; + + $this->oBt->show(); + } +} +?> diff --git a/inc/mod_roomReservationBookingTable.inc b/inc/mod_roomReservationBookingTable.inc new file mode 100644 index 0000000..fd85ef1 --- /dev/null +++ b/inc/mod_roomReservationBookingTable.inc @@ -0,0 +1,783 @@ +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()); + } + // if weekends are not shown, show the next week already on saturday + if(!$this->oCfg->isShowWeekend() and (date("w") == 6 or date("w") == 0)) { + $this->setDate(strtotime("monday")); + } else { + $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 = <<oCfg->isShowWeekend()) { + $strCss .= "#mod_roomReservationBookingTable td.cell { width:13%; }"; + } else { + $strCss .= "#mod_roomReservationBookingTable td.cell { width:18.2%; }"; + } + rrAddCss($strCss); + } + + /** + * Show the timetable + * @return void + * @throws AccessException + * @todo increase the height of the cells a little + */ + public function show() { + // Protect access + if(!$this->oCfg->userCanView()) { + throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); + return; + } + + // print error messages and return if there are any + if(count($this->asErrors) > 0) { + printf("

%s

", join("
\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 "", $strClass, $strTitle); + } + 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) . "
" : ""; + $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("", $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 "\n"; + } + echo "
"; + 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
%s

"; + } + + /** + * 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", $asMsgs)))."

\n"; + } + + // calculate the timespan of the current booking + $nSpan = $ob->getTsLast() - $ob->getTsFirst() + 1; + + if(($cFlags & MOD_ROOM_RESERVATION_BTPB_DELETE) == + MOD_ROOM_RESERVATION_BTPB_DELETE) { + + // Restrict access + if(!($this->oBm->userIsOwner($ob->getUid()) or + $this->oCfg->userIsAdmin())) { + $strBefore .= "

" . + MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED . "

\n"; + #return $nSpan; + } else { + // print delete form + $strWarning = sprintf("
%s%s
", icon("dlg-warn", + array("bg" =>"gb")), _c("room-reservation:Attention: This ". + "booking is a recurring booking. If you delete it, the period will ". + "be deallocated for every week, not just this single week!")); + $strAfter .= sprintf("

%s%s
<%s name='mod_roomReservationBookingTable". + "[submitdelete]' value='%s' /> <%s name='". + "mod_roomReservationBookingTable[submitdelete]' value='%s' />". + "

", $_SERVER["PHP_SELF"], + _c("room-reservation:Delete this booking?"), + ($ob->getInterval() > 0 ? $strWarning : ""), $GLOBALS["smlbtn"], + _("Delete"), $GLOBALS["smlbtn"], _("Cancel"), $ob->getUid(), + $this->getRoom(), $this->getDate()); + } + } else { + // delete and edit links, show only if user is allowed to + if($this->oBm->userIsOwner($ob->getUid()) || + $this->oCfg->userIsAdmin()) { + /** @todo edit form */ + $strAfter .= sprintf("
(". + "%s)", + $_SERVER["PHP_SELF"], $ob->getUid(), $ts, + _c("room-reservation:Edit this booking"), + _c("room-reservation:edit"), $_SERVER["PHP_SELF"], $ob->getUid(), + $ts, _c("room-reservation:Delete this booking"), + _c("room-reservation:delete")); + } + } + + // test if booking is new and should be highlighted + $strClass = "cell booking".($ob->getInterval() > 0 ? " recurring" : ""); + if(($cFlags & MOD_ROOM_RESERVATION_BTPB_NEW) == + MOD_ROOM_RESERVATION_BTPB_NEW) { + $strClass .= " new"; + } + // Use a different style for the current day + $strClass .= (date("Ymd", $ob->getDate()) == date("Ymd") ? " today" : ""); + /** @todo: add ?subject=... to mailto link */ + echo sprintf("%s%s
%s%s\n", + $nSpan, $strClass, $strBefore, mailto($ob->getAct()), + q(getRealUserName($ob->getAct())), q($ob->getReason()), $strAfter); + + return $nSpan; + } + + /** + * Print the booking form. + * @param $nTs (int) current timeslice + * @param $ts (timestamp) current date + * @param $asErrors (array of strings) Additional error message to be printed + * inside the cell, one array element per message + * @return (int) the span of the booking (i.e., 1) + */ + protected function printBookingForm($nTs, $ts, $asErrors = array()) { + // Restrict access + if(!($this->oCfg->userCanBook() or $this->oCfg->userIsAdmin())) { + printf("%s\n", + MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); + return 1; + } + + $strErrors = "

".nl2br(q(join("\n", $asErrors)))."

"; + + // form to allow fixed bookings for admins + $sWeeklyForm = ""; + if($this->oCfg->userIsAdmin()) { + $sWeeklyForm = sprintf(" %s
". + " <%s name='". + "mod_roomReservationBookingTable[account]' id='account' value='%s' ". + "size='15' />
", _c("room-reservation:Repetition:"), + select("mod_roomReservationBookingTable[interval]", + $this->nPostInterval, array(0 => _c("Select:None"), 1 => + _c("room-reservation:every week")), array("add" => "id='interval'")), + _c("room-reservation:Account, if not yourself:"), $GLOBALS["stdedt"], + $this->strPostAccount); + } + + echo sprintf("%s". + "
%s". + "
<%s id='reason' size='15' ". + "value='%s' name='mod_roomReservationBookingTable[reason]' />
%s". + "<%s name='mod_roomReservationBookingTable[submitbooking]' value='%s' />". + "
\n", (count($asErrors) > 0) ? $strErrors : "", + $_SERVER["PHP_SELF"], _c("room-reservation:until:"), + select("mod_roomReservationBookingTable[tslast]", $this->getTsLast(), + $this->oCfg->getTimesliceEndings(true)), _c("room-reservation:Reason:"), + $GLOBALS["stdedt"], $this->getReason(), $sWeeklyForm, $GLOBALS["smlbtn"], + _c("room-reservation:Book"), $this->getDate(), $this->getRoom(), + $this->getTsFirst()); + return 1; + } + + /** + * Print the booking link + * @param $nTs (int) current timeslice + * @param $ts (timestamp) current date + * @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 (i.e., 1) + */ + protected function printBookingLink($nTs, $ts, $asMsgs = array()) { + + // Restrict access + if(!($this->oCfg->userCanBook() or $this->oCfg->userIsAdmin())) { + echo "\n"; + return 1; + } + + // messages + $strBefore = ""; + if(count($asMsgs) > 0) { + $strBefore .= "

".join("
", $asMsgs)."

\n"; + } + + // print link to booking if the timeslice is later than now + $oTs = $this->oCfg->getTimeslice($nTs); + // note: only the timeslices are in GMT! + $tsCur = strtotime(date("Y-m-d ", $ts) . gmdate(" G:i", + $oTs->getEnd())); + if($tsCur > time()) { + $strURL = $_SERVER["PHP_SELF"] . + sprintf("?mod_roomReservationBookingTable[action]=book&". + "mod_roomReservationBookingTable[date]=%d&". + "mod_roomReservationBookingTable[room]=%s&". + "mod_roomReservationBookingTable[tsfirst]=%d#form", $ts, + qu($this->getRoom()), $nTs); + echo sprintf("%s%s\n", + $strBefore, $strURL, _c("room-reservation:Book this room from here"), + _c("room-reservation:(Book from here)")); + } else { + // only print the messages + echo sprintf("%s\n", + $strBefore); + } + return 1; + } + /** @} */ +} +?> diff --git a/inc/mod_roomReservationBookingsManager.inc b/inc/mod_roomReservationBookingsManager.inc new file mode 100644 index 0000000..124068d --- /dev/null +++ b/inc/mod_roomReservationBookingsManager.inc @@ -0,0 +1,274 @@ +oCfg = $oCfg; + } + + /***************************************************************************/ + /** + * @} + * @name Retrieving bookings + * @{ + */ + + /** + * Fetch a booking with the given unique ID from the SQL table + * @param $nUid (int) Unique ID of the booking + * @return mod_roomReservationBooking + */ + public static function getBookingByUid($nUid) { + $h = db_query("SELECT * FROM mod_roomreservation_bookings WHERE rrb_uid = $1;", $nUid); + $a = pg_fetch_array($h); + $o = new mod_roomReservationBooking($a["rrb_room"], strtotime($a["rrb_date"]), + intval($a["rrb_tsfirst"]), intval($a["rrb_tslast"]), $a["rrb_act"], + $a["rrb_reason"], intval($a["rrb_interval"])); + $o->setUid(intval($a["rrb_uid"])); + return $o; + } + + /** + * Test if there is a booking which takes place on the specified position at + * the specified date. + * @param $strRoom (string) Name of the room + * @param $tsDate (timestamp) The date + * @param $nTimeslice (int) The number of the timeslice + * @return mod_roomReservationBooking The booking which takes place on the + * specified time or null if no booking could be found. + */ + public static function getBookingByTimeslice($strRoom, $tsDate, + $nTimeslice) { + $a = mod_roomReservationBookingsManager::getOverlappingBookings( + new mod_roomReservationBooking($strRoom, $tsDate, $nTimeslice, + $nTimeslice, null, null)); + return isset($a[0]) ? $a[0] : null; + } + + /** + * Get all bookings in database which overlap with the given booking. + * @param $ob (mod_roomReservationBooking) New booking that should be tested + * if it overlaps + * @return array with elements of type mod_roomReservationBooking + */ + public static function getOverlappingBookings( + mod_roomReservationBooking $ob) { + // TODO: Test for bookings that only take place every n.th week (modulo n) + + // Two bookings overlap, when they are on the same day and if + // old beginning < new ending AND old ending > new beginning + $hQuery = db_query("SELECT * FROM mod_roomreservation_bookings WHERE ". + "rrb_room = $1 AND ((rrb_interval > 0 AND EXTRACT(DOW FROM rrb_date) ". + "= $2) OR (rrb_interval = 0 AND rrb_date = $3)) AND rrb_tsfirst <= ". + "$4 AND rrb_tslast >= $5 ORDER BY rrb_tsfirst;", $ob->getRoom(), + date("w", $ob->getDate()), date("Y-m-d", $ob->getDate()), + intval($ob->getTsLast()), intval($ob->getTsFirst())); + $aoReturn = array(); + while($aResult = pg_fetch_array($hQuery)) { + $aoReturn[] = mod_roomReservationBookingsManager::getBookingByUid( + $aResult["rrb_uid"]); + } + return $aoReturn; + } + + /***************************************************************************/ + /** + * @} + * @name Management of bookings + * @{ + */ + + /** + * Insert or update a booking in the database. + * The function throws an AccessException if the user was not allowed to + * write the booking, or an SQLException if there was an error while trying + * to insert or update the booking into the database. + * @param $ob (mod_roomReservationBooking) Booking to write to the database + * @return (int) The UID of the written booking + * @throws SQLException, AccessException + * @todo document + */ + function write(mod_roomReservationBooking $ob) { + // protect access + if(($ob->getUid() != null and !$this->oCfg->userIsAdmin() and + !$this->userIsOwner($ob->nUid)) or + ($ob->getUid() == null and !$this->oCfg->userCanBook())) { + throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); + } + + // test if room is whitelisted + if(!$this->oCfg->isRoomWhitelisted($ob->getRoom())) { + throw new Exception(MOD_ROOM_RESERVATION_ERROR_ROOM_NOT_WHITELISTED); + } + + $strWhere = null; + $strLog = ""; + + // check if everything is right and throw exceptions + if(trim($ob->getAct()) == "") { + $ob->setAct($SESSION["act"]); + } elseif(!isAct($ob->getAct())) { + throw new Exception(MOD_ROOM_RESERVATION_ERROR_NO_SUCH_ACCOUNT); + return false; + } + if($ob->getTsFirst() > $ob->getTsLast()) { + throw new SQLException(MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN); + return false; + } + if(trim($ob->getReason()) == "") { + throw new SQLException(MOD_ROOM_RESERVATION_ERROR_NO_REASON); + return false; + } + + // Test for overlapping bookings + if($this->getOverlappingBookings($ob) != array()) { + throw new SQLException(MOD_ROOM_RESERVATION_ERROR_BOOKING_OVERLAPS); + return false; + } + + // Show real times in log, but don't use the user's locale! + $oTsB = $this->oCfg->getTimesliceBeginnings(false); + $oTsE = $this->oCfg->getTimesliceEndings(false); + + // Update or insert? + if($ob->getUid() == null) { + // No UID yet, insert new booking + // @todo write interval and user if interval > 0 + $strLog = sprintf("Raum „%s“ am %s von %s bis %s gebucht ". + "(Begründung: %s)", $ob->getRoom(), date("d\.m\.Y", $ob->getDate()), + gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i", + $oTsE[$ob->getTsLast()]), $ob->getReason()); + } else { + // Update an existing booking + // @todo write old and new times into log + $strWhere = "rs_uid = ".qdb(intval($ob->getUid())); + $strLog = sprintf("Buchung im Raum „%s“ auf %s von %s bis %s ". + "geändert (Begründung: „%s“)", $ob->getRoom(), date("d\.m\.Y", + $ob->getDate()), gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i", + $oTsE[$ob->getTsLast()]), $ob->getReason()); + } + $aPut["rrb_room"] = $ob->getRoom(); + $aPut["rrb_date"] = date("Y\-m\-d", $ob->getDate()); + $aPut["rrb_tsfirst"] = intval($ob->getTsFirst()); + $aPut["rrb_tslast"] = intval($ob->getTsLast()); + $aPut["rrb_act"] = $ob->getAct(); + $aPut["rrb_reason"] = $ob->getReason(); + $aPut["rrb_interval"] = intval($ob->getInterval()); + + // @todo test if the foreign keys are being violated and throw an error + // message if neccessary + db_store("mod_roomreservation_bookings", $aPut, $strWhere); + + $hQuery = db_query("SELECT currval('mod_roomreservation_bookings_rrb_uid_seq');"); + $nNewUid = pg_fetch_result($hQuery, 0, "currval"); + + rrInsertLog($strLog); + + // Return new UID + return $nNewUid; + } + + /** + * Delete a booking from the database + * @param $nUid (int) Unique ID of the booking + * @return (bool) true on success, otherwise false. + * @todo test + */ + public function delete($nUid) { + // Only administrators and owners are allowed to delete bookings + if(!($this->oCfg->userIsAdmin() or $this->userIsOwner($nUid))) { + throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); + return false; + } + + // Don't use the user's locale! + $oTsB = $this->oCfg->getTimesliceBeginnings(false); + $oTsE = $this->oCfg->getTimesliceEndings(false); + $ob = $this->getBookingByUid($nUid); + $strLog = sprintf("Buchung in Raum „%s“ am %s von %s bis %s ". + "gelöscht (Begründung war: %s)", $ob->getRoom(), date("d\.m\.Y", + $ob->getDate()), gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i", + $oTsE[$ob->getTsLast()]), $ob->getReason()); + // Delete it from the database + if(!db_query("DELETE FROM mod_roomreservation_bookings WHERE ". + "rrb_uid = $1;", $nUid)) { + throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); + return false; + } else { + rrInsertLog($strLog); + return true; + } + } + + /** + * Determine if the current user is the owner of a specified error report. + * If this function fails, call getLastError() to get more information. + * @param $nID (int) Unique ID of the error report + * @throws SQLException + * @return bool + */ + public static function userIsOwner($nID) { + if(!isset($_SESSION["act"])) { + return false; // user is not logged in + } else { + $hQuery = db_query("SELECT rrb_act FROM mod_roomreservation_bookings WHERE ". + "rrb_uid = $1;", intval($nID)); + if(!is_resource($hQuery)) { + throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); + return false; + } + $arResult = pg_fetch_array($hQuery); + return ($arResult["rrb_act"] == $_SESSION["act"]); + } + } +} +?> diff --git a/inc/mod_roomReservationConfig.inc b/inc/mod_roomReservationConfig.inc new file mode 100644 index 0000000..8dde982 --- /dev/null +++ b/inc/mod_roomReservationConfig.inc @@ -0,0 +1,454 @@ + 0; +} + +/** + * Retrieve all groups that have a privilege assigned + * @param $strPriv (string) Privilege to test + * @return array + */ +function rrPrivilegedGroups($strPriv) { + $aReturn = array(); + $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1 ". + "ORDER BY act;", $strPriv); + if(pg_num_rows($h) > 0) { + while($a = pg_fetch_array($h)) { + $aReturn[] = $a["act"]; + } + } + return $aReturn; +} + +/** + * User-defined compare function to compare timeslices + * @param $oTs1 (mod_roomReservationTimeslice) + * @param $oTs2 (mod_roomReservationTimeslice) + * @return (int) -1 if $oTs1 begins before $oTs2, + * 0 if the $oTs1 and $oTs2 have the same beginning, + * 1 if $oTs1 begins after $oTs2. + */ +function rrConfigSortTimeslices(mod_roomReservationTimeslice $oTs1, + mod_roomReservationTimeslice $oTs2) { + if($oTs1->getBegin() == $oTs2->getBegin()) { + return 0; + } else { + return ($oTs1->getBegin() > $oTs2->getBegin()) ? 1 : -1; + } +} + +define("MOD_ROOM_RESERVATION_CONFIGFILE_HEADER", "flushTimeslices(); + $this->setShowWeekend(false); + $this->setShowLessons(true); + $this->asMessages = array(); + + $this->readConfig(); + } + + /** + * ************************************************************************** + * @} + * @name Access to attributes + * @{ + */ + + /** + * Add a timeslice. A check is done that the timeslices do not overlap, and + * in this case, an Exception is thrown. + * @param $oTs (mod_roomReservationTimeslice) + * @throws Exception + * @return void + */ + public function addTimeslice(mod_roomReservationTimeslice $oTs) { + // Check for overlapping timeslices + foreach($this->aoTimeslices as $oOldTs) { + if(($oOldTs->getBegin() < $oTs->getEnd() and + $oOldTs->getEnd() > $oTs->getBegin())) { + throw new Exception( + MOD_ROOM_RESERVATION_ERROR_CONFIG_OVERLAPPING_TIMESLICE); + } + } + $this->aoTimeslices[] = $oTs; + usort($this->aoTimeslices, "rrConfigSortTimeslices"); + return; + } + + /** + * Delete a timeslice + * @param $oTs (mod_roomReservationTimeslice) the timeslice to delete. If + * the timeslice is not found, an Exception is thrown. + * @throws Exception + * @return void + */ + public function deleteTimeslice(mod_roomReservationTimeslice $oTs) { + for($i = 0; $i < count($this->aoTimeslices); $i++) { + if($this->aoTimeslices[$i]->getBegin() == $oTs->getBegin() and + $this->aoTimeslices[$i]->getEnd() == $oTs->getEnd()) { + // use array_splice because it renumbers the keys + array_splice($this->aoTimeslices, $i, 1); + return; + } + } + throw new Exception(MOD_ROOM_RESERVATION_ERROR_CONFIG_NO_SUCH_TIMESLICE); + } + + /** + * Delete all timeslices. + * @return void + */ + public function flushTimeslices() { $this->aoTimeslices = array(); } + + /** + * Add a room to the list of rooms who can be booked. Throws an SQLException + * in case of an error. + * @param $sRoom (string) The name of the room + * @throws SQLException, Exception + * @return void + */ + public function whitelistRoom($sRoom) { + if(!$this->isRoomWhitelisted($sRoom)) { + $r = db_store("mod_roomreservation_roomswhitelist", + array("rrr_name" => $sRoom)); + if(!$r) { + throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); + } else { + log_insert(sprintf("Raum „%s“ für Buchungen zur Verfügung gestellt", + $sRoom)); + } + } + } + + /** + * Forbid bookings for a room. Throws an SQLException in case of an error. + * @param $sRoom The name of the room + * @throws SQLException + */ + public function unWhitelistRoom($sRoom) { + $h = db_query("DELETE FROM mod_roomreservation_roomswhitelist WHERE ". + "rrr_name = $1;", $sRoom); + if(!$h) { + throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); + } else { + log_insert(sprintf("Raum „%s“ für Buchungen gesperrt", $sRoom)); + } + } + + /** + * Determine if a room is allowed for booking. Throws an SQLException + * in case of an error. + * @param $sRoom (string) The name of the room + * @return bool + * @throws SQLException + */ + public function isRoomWhitelisted($sRoom) { + $h = db_query("SELECT * FROM mod_roomreservation_roomswhitelist WHERE ". + "rrr_name=$1;", $sRoom); + if(!$h) { + throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); + } + return (pg_num_rows($h) > 0); + } + + /** + * Get all rooms that are allowed for booking. Throws an SQLException + * in case of an error. + * @throws SQLException + * @return array of mod_roomReservationRoomsManager objects + */ + public function getWhitelistedRooms() { + $aor = mod_roomReservationRoomsManager::getRooms(); + $ar = array(); + foreach($aor as $key => $or) { + if($this->isRoomWhitelisted($or->getName())) { + $ar[] = $or; + } + } + return $ar; + } + + /** + * Show or hide the weekend + * @param $b (bool) + */ + public function setShowWeekend($b) { $this->bShowWeekend = ($b == true); } + + /** + * Show or hide the lesson strings in the booking table + * @param $b (bool) + */ + public function setShowLessons($b) { $this->bShowLessons = ($b == true); } + + /** + * Add a message to the internal array of (error) messages + * @param $sMessage (string) + */ + public function addMessage($sMessage) { + array_merge($this->asMessages, array($sMessage)); + } + + /** + * Get all timeslices in chronological order + * @return array of rmTimeslice + */ + public function getTimeslices() { return $this->aoTimeslices; } + + /** + * Return the starting times of every timeslice + * @param $bFormat (bool) true: Format the times according to the + * current locale + * false: return just the timestamps + * @return array + */ + public function getTimesliceBeginnings($bFormat = false) { + $aot = $this->getTimeslices(); + $aRet = array(); + foreach($aot as $ao) { + $aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getBegin()) : + $ao->getBegin(); + } + return $aRet; + } + + /** + * Return the ending times of every timeslice + * @param $bFormat (bool) true: Format the times according to the + * current locale + * false: return just the timestamps + * @return array + */ + public function getTimesliceEndings($bFormat = false) { + $aot = $this->getTimeslices(); + $aRet = array(); + foreach($aot as $ao) { + $aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getEnd()) : + $ao->getEnd(); + } + return $aRet; + } + + /** + * Get a timeslice + * @param $n (int) index of the timeslice in the array + * @return rmTimeslice + */ + public function getTimeslice($n) { return $this->aoTimeslices[$n]; } + + /** + * Determine if the weekend is shown + * @return bool + */ + public function isShowWeekend() { return ($this->bShowWeekend == true); } + + /** + * Determine if the lesson strings in the booking table are shown + * @return bool + */ + public function isShowLessons() { return ($this->bShowLessons == true); } + + /** + * Determine if the current user has admin rights. This function tests + * if the user is in a group which has the privilege of admin rights. + * @todo test + * @return bool + */ + public function userIsAdmin() { + return secure_privilege("mod_roomreservation_admin"); + } + + /** + * Determine if the current user can book rooms. This function tests + * if the user is in a group which has the privilege to book rooms. + * If no group has this privilege, all users can book. + * @todo test + * @return bool + */ + public function userCanBook() { + if(!rrPrivilegeAssigned("mod_roomreservation_book")) { + // If the privilege is not assigned to any group, all users can book + return true; + } else { + // If user is admin, it makes sense that he can book rooms ;-) + return secure_privilege("mod_roomreservation_book") || + secure_privilege("mod_roomreservation_admin"); + } + } + + /** + * Determine if the current user can view bookings. This function tests + * if the user is in a group which has been configured as a group who + * can view bookings. If no groups are configured, any user can view the + * bookings table. + * @todo test + * @return bool + */ + public function userCanView() { + if(!rrPrivilegeAssigned("mod_roomreservation_view")) { + // If the privilege is not assigned to any group, all users can view + return true; + } else { + // If user is admin or can book, it makes sense that he can view bookings + return secure_privilege("mod_roomreservation_admin") || + secure_privilege("mod_roomreservation_book") || + secure_privilege("mod_roomreservation_view"); + } + } + + /** + * Get the messages that have been produced + * @return string + */ + public function getMessages() { + return join("\n", $this->asMessages); + } + + /***************************************************************************/ + /** + * @} + * @name Operations + * @{ + */ + + /** + * Write the current state of this instance to the config file. + * @throws IOException + * @return bool + */ + public function writeConfig() { + // Open config file + $hFile = fopen("mod_room-reservation/config.inc", "w", true); + if(!is_resource($hFile)) { + throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE); + return false; + } + // Try to lock file repeatedly + for($n = 0; !flock($hFile, LOCK_EX); $n++) { + if($n > 10) { + throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE); + return false; // Give up + } else { + sleep(0.2); // Retry after 100 ms + } + } + + // Create text for config file + $strFile = MOD_ROOM_RESERVATION_CONFIGFILE_HEADER; + + // Timeslices + $strFile .= "\$this->flushTimeslices();\n"; + foreach($this->getTimeslices() as $oTs) { + $strFile .= sprintf("\$this->addTimeslice(new ". + "mod_roomReservationTimeslice(%d, %d));\n", $oTs->getBegin(), + $oTs->getEnd()); + } + + // Show weekend + $strFile .= sprintf("\$this->setShowWeekend(%s);\n", + $this->isShowWeekend() ? "true" : "false"); + + // Show lessons + $strFile .= sprintf("\$this->setShowLessons(%s);\n", + $this->isShowLessons() ? "true" : "false"); + + $strFile .= "?>"; + + // Write to config file and unlock it + if(fwrite($hFile, $strFile) == false) { + throw new IOException(MOD_ROOM_RESERVATION_ERROR_WRITE_FILE); + return false; + } + if(!flock($hFile, LOCK_UN)) { + throw new IOException(MOD_ROOM_RESERVATION_ERROR_UNLOCK_FILE); + return false; + } + + rrInsertLog("Konfiguration verändert"); + return true; + } + + /** + * Read configuration from file. Returns false if an error occured, + * in this case getMessages() contains error messages. + * @return bool + */ + public function readConfig() { + global $g_rmCfg; + try { + require("mod_room-reservation/config.inc"); + } catch(Exception $e) { + $this->addMessage($e->getMessage()); + } + } + + /** @} */ +} +?> diff --git a/inc/mod_roomReservationConfigPage.inc b/inc/mod_roomReservationConfigPage.inc new file mode 100644 index 0000000..46ce45a --- /dev/null +++ b/inc/mod_roomReservationConfigPage.inc @@ -0,0 +1,179 @@ +otlb = new mod_roomReservationTimesliceListBox($this->oCfg); + $this->orwlb = new mod_roomReservationRoomWhitelistListBox($this->oCfg); + $this->setTitle(_c("room-reservation:Configuration")); + $this->setIcon("mod_room-reservation_config"); + } + + public function processRequestVariables() { + // default values + $this->bPostShowWeekend = $this->oCfg->isShowWeekend(); + $this->bPostShowLessons = $this->oCfg->isShowLessons(); + + if(isset($_POST["mod_roomReservationConfigPage"])) { + if(isset($_POST["mod_roomReservationConfigPage"]["showweekend"])) { + $this->bPostShowWeekend = + ($_POST["mod_roomReservationConfigPage"]["showweekend"] == true); + } + + if(isset($_POST["mod_roomReservationConfigPage"]["showlessons"])) { + $this->bPostShowLessons = + ($_POST["mod_roomReservationConfigPage"]["showlessons"] == true); + } + + // process the request + if(isset($_POST["mod_roomReservationConfigPage"]["submit"])) { + $this->oCfg->setShowWeekend($this->bPostShowWeekend); + $this->oCfg->setShowLessons($this->bPostShowLessons); + try { + $this->oCfg->writeConfig(); + } catch(Exception $e) { + $this->asMessages[] = $e->getMessage(); + } + } + } + } + + public function doShow() { + // error messages + if(count($this->asMessages) > 0) { + printf("

%s

", nl2br(q(join("\n", $this->asMessages)))); + } + + // first column + echo "". + "
\n"; + + GroupBox(_c("room-reservation:Available rooms"), "host"); + printf("

%s

", _c("room-reservation:The following rooms are ". + "available for booking:")); + echo "
"; + $this->orwlb->show(); + echo "
\n"; + _GroupBox(); + + GroupBox(_("Privileges"), "keys"); + $asAdminGroups = rrPrivilegedGroups("mod_roomreservation_admin"); + $asBookGroups = rrPrivilegedGroups("mod_roomreservation_book"); + $asViewGroups = rrPrivilegedGroups("mod_roomreservation_view"); + echo sprintf("

%s

\n

%s

\n

%s

", _c("room-reservation:This is ". + "a short summary of the privileges related to the room reservation ". + "schedulde and the groups which have them assigned."), + sprintf(_c("room-reservation:If one of these privileges is not assigned to ". + "any group, all users on this server are allowed to perform the specified ". + "action. Please use the %sgroup administration%s to assign and revoke ". + "privileges."), "", ""), + _c("room-reservation:Please note that every group with the booking ". + "privilege can also implicitly view the booking table and every group with ". + "the administration privilege can also implicitly book and view the ". + "booking table.")); + echo "

\n"; + echo sprintf("\n", icon("keys"), + _("View the booking table").":", $asViewGroups == array() ? + _c("room-reservation:all users") : icon("act-group") . join(", ", + array_map("getGroupName", $asViewGroups))); + echo "\n"; + echo sprintf("\n", icon("keys"), + _("Book rooms").":", $asBookGroups == array() ? + _c("room-reservation:all users") : icon("act-group") . join(", ", + array_map("getGroupName", $asBookGroups))); + echo "\n"; + echo sprintf("\n", icon("keys"), + _("Administration of the room reservation schedule"), + $asAdminGroups == array() ? _c("room-reservation:all users") : + icon("act-group") . join(", ", array_map("getGroupName", $asAdminGroups))); + echo "
%s%s%s
%s%s%s
%s%s%s

\n"; + _GroupBox(); + + // second column + echo "
\n"; + + GroupBox(_c("room-reservation:Periods"), "mod_room-reservation_timeslice"); + printf("

%s

", _c("room-reservation:Here you can fill in the ". + "periods where bookings can be undertaken. A booking period can ". + "e. g. correspond to a lesson.")); + echo "
"; + $this->otlb->show(); + echo "
\n"; + _GroupBox(); + + GroupBox(_c("room-reservation:Further options"), "manage"); + printf("
". + "\n", $_SERVER["PHP_SELF"]); + + printf("\n"; + printf("", $GLOBALS["stdbtn"], _("OK")); + echo "
<%s name='mod_roomReservationConfigPage". + "[showweekend]' id='mod_roomReservationConfigPageShowWeekend' ". + "value='1' %s />
". + "%s

", + $GLOBALS["smlchk"], $this->bPostShowWeekend ? "checked='checked' " : "", + _c("room-reservation:Show weekend"), _c("room-reservation:If ". + "selected, the weekdays saturday and sunday are also shown in the ". + "booking table.")); + + echo "
<%s name='mod_roomReservationConfigPage". + "[showlessons]' id='mod_roomReservationConfigPageShowLessons' ". + "value='1'%s />
". + "%s

", + $GLOBALS["smlchk"], $this->bPostShowLessons ? "checked='checked' " : "", + _c("room-reservation:Show „lesson” texts"), + _c("room-reservation:Check this box to show texts like ". + "„nth lesson&rdquo in the booking page. If this box is ". + "unchecked, none of these texts are shown.")); + + printf("
<%s name='mod_roomReservationConfigPage". + "[submit]' value='%s' />
\n"; + _GroupBox(); + + echo "
\n"; + } +} +?> \ No newline at end of file diff --git a/inc/mod_roomReservationControl.inc b/inc/mod_roomReservationControl.inc new file mode 100644 index 0000000..4d364c5 --- /dev/null +++ b/inc/mod_roomReservationControl.inc @@ -0,0 +1,114 @@ +oCfg = $oCfg; + $this->processRequestVariables(); + } + + /***************************************************************************/ + /** + * @} + * @name Initialization + * @{ + */ + + /** + * Process the REQUEST variables and preset the some variables + * @return void + */ + protected function processRequestVariables() { } + + /***************************************************************************/ + /** + * @} + * @name Output + * @{ + */ + + /** + * Get the messages that have been produced. Returns HTML. + * @return string + */ + protected function getMessages() { + if(count($this->asMessages) > 0) { + return sprintf("
%s

\n", + nl2br(q(join("\n", $this->asMessages)))); + } + } + + /** + * Show the beginning of the control. + * @return void + */ + protected function beginShow() { } + + /** + * Show the control. Override this function to print your HTML code. + * @return void + */ + protected abstract function doShow(); + + /** + * Show the end of the control. + * @return void + */ + protected function endShow() { } + + /** + * Show the full control. You don't need to override this function. Instead, + * override doShow(). + * @return void + */ + public function show() { + $this->beginShow(); + $this->doShow(); + $this->endShow(); + } +} +?> \ No newline at end of file diff --git a/inc/mod_roomReservationPage.inc b/inc/mod_roomReservationPage.inc new file mode 100644 index 0000000..6d0e018 --- /dev/null +++ b/inc/mod_roomReservationPage.inc @@ -0,0 +1,150 @@ +oCfg = $oCfg; + + $this->processRequestVariables(); + } + + /***************************************************************************/ + /** + * @} + * @name Initialization + * @{ + */ + + /** + * Process the REQUEST variables and preset the some variables. Override + * this function to process GET and POST parameters. + * @return void + */ + protected function processRequestVariables() { } + + /***************************************************************************/ + /** + * @} + * @name Access to attributes + * @{ + */ + + /** + * Set the page title + * @param $str (string) + * @return void + */ + public function setTitle($str) { $this->strTitle = $str; } + + /** + * Set the title icon + * @param $str (string) + * @return void + */ + public function setIcon($str) { $this->strIcon = $str; } + + /** + * Get the page title + * @return string + */ + public function getTitle() { return $this->strTitle; } + + /** + * Get the title icon + * @return string + */ + public function getIcon() { return $this->strIcon; } + + /***************************************************************************/ + /** + * @} + * @name Output + * @{ + */ + + /** + * Show the beginning of the page. + * @return void + */ + protected function beginShow() { + html_header("\n"); + PageBlue(q($this->getTitle()), $this->getIcon()); + + // print error messages from the configuration + if($s = trim($this->oCfg->getMessages()) != "") { + printf("

%s

\n", nl2br(q($s))); + } + } + + /** + * Show the beginning of the page. Override this function to print your + * HTML code. + * @return void + */ + protected abstract function doShow(); + + /** + * Show the end of the page. + * @return void + */ + protected function endShow() { + _PageBlue(); + } + + /** + * Show the full page. You don't need to override this function. Instead, + * override doShow(). + * @return void + */ + public function show() { + $this->beginShow(); + $this->doShow(); + $this->endShow(); + } + +} +?> \ No newline at end of file diff --git a/inc/mod_roomReservationRoom.inc b/inc/mod_roomReservationRoom.inc new file mode 100644 index 0000000..64ed6cb --- /dev/null +++ b/inc/mod_roomReservationRoom.inc @@ -0,0 +1,143 @@ +setName($strName); + $this->setRoomNo($strRoomNo); + $this->setFloor($strFloor); + $this->setBuilding($strBuilding); + $this->setLocation($strLocation); + } + + /***************************************************************************/ + /** + * @} + * @name Access to attributes + * @{ + */ + + /** + * Set the name of the room + * @param $str (string) + * @return void + */ + public function setName($str) { $this->strName = $str; } + + /** + * Set the room number + * @param $str (string) + * @return void + */ + public function setRoomNo($str) { $this->strRoomNo = $str; } + + /** + * Set the floor + * @param $str (string) + * @return void + */ + public function setFloor($str) { $this->strFloor = $str; } + + /** + * Set the building + * @param $str (string) + * @return void + */ + public function setBuilding($str) { $this->strBuilding = $str; } + + /** + * Set the location + * @param $str (string) + * @return void + */ + public function setLocation($str) { $this->strLocation = $str; } + + /** + * Get the name of the room + * @return string + */ + public function getName() { return $this->strName; } + + /** + * Get the room number + * @return string + */ + public function geRoomNo() { return $this->strRoomNo; } + + /** + * Get the floor + * @return string + */ + public function getFloor() { return $this->strFloor; } + + /** + * Get the building + * @return string + */ + public function getBuilding() { return $this->strBuilding; } + + /** + * Get the location + * @return string + */ + public function getLocation() { return $this->strLocation; } + + /** + * Conversion to string + * @return string + */ + public function __toString() { return $this->getName(); /* name is key */ } + /**@}*/ +} +?> diff --git a/inc/mod_roomReservationRoomWhitelistListBox.inc b/inc/mod_roomReservationRoomWhitelistListBox.inc new file mode 100644 index 0000000..27bd6c2 --- /dev/null +++ b/inc/mod_roomReservationRoomWhitelistListBox.inc @@ -0,0 +1,214 @@ +cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_SHOW; + + // POST data + if(isset($_POST["mod_roomReservationRoomWhitelistListBox"])) { + $aPost = $_POST["mod_roomReservationRoomWhitelistListBox"]; + // mode + if(isset($aPost["action"])) { + if(isset($aPost["action"]["add"])) { + if($aPost["action"]["add"] == _("Add")) { + $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_ADD; + } elseif($aPost["action"]["add"] == _("OK")) { + $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITADD; + } + } elseif(isset($aPost["action"]["delete"])) { + if($aPost["action"]["delete"] == _("Delete")) { + $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_DELETE; + } elseif($aPost["action"]["delete"] == _("OK")) { + $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITDELETE; + } + } + } + // selection + if(isset($aPost["l"])) { + foreach($aPost["l"] as $nOid => $bChecked) { + if($bChecked) { + $this->anPostSelection[] = $nOid; + } + } + } + } + + // process the forms + if($this->cMode == MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITADD) { + $h = db_query("SELECT name FROM rooms WHERE oid IN ". + qdb_arr($this->anPostSelection)); + while($a = pg_fetch_array($h)) { + $this->oCfg->whitelistRoom($a["name"]); + } + } + + if($this->cMode == MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITDELETE) { + $h = db_query("SELECT name FROM rooms WHERE oid IN ". + qdb_arr($this->anPostSelection)); + while($a = pg_fetch_array($h)) { + $this->oCfg->unWhitelistRoom($a["name"]); + } + } + } + + protected function doShow() { + echo "
"; + TreeView(array(_("Room"))); + switch($this->cMode) { + case MOD_ROOM_RESERVATION_RWLB_ACTION_DELETE: + $this->showDeleteForm(); + break; + case MOD_ROOM_RESERVATION_RWLB_ACTION_ADD: $this->showAddForm(); break; + default: + case MOD_ROOM_RESERVATION_RWLB_ACTION_SHOW: $this->showForm(); break; + } + _TreeView(); + echo "
\n"; + } + + /** + * Print the form if not delete nor add was requested + * @return void + */ + protected function showForm() { + $aoRooms = $this->oCfg->getWhitelistedRooms(); + // only show add button if there are still some unlisted rooms + if(count(mod_roomReservationRoomsManager::getRooms()) > count($aoRooms)) { + TreeViewLine(sprintf("<%s name='mod_roomReservationRoomWhitelistListBox". + "[action][add]' value='%s' />", $GLOBALS["stdbtn"], _("Add"))); + } + $this->showList($aoRooms); + // toolbar + printf("", $GLOBALS["treeview_cols"]); + CheckCombo(); + printf("<%s name='mod_roomReservationRoomWhitelistListBox[action]". + "[delete]' value='%s' />", $GLOBALS["stdbtn"], _("Delete")); + echo "\n"; + } + + /** + * Print the addition form + * @return void + */ + protected function showAddForm() { + // only list rooms that are not already whitelisted + $aoRooms = array_diff(mod_roomReservationRoomsManager::getRooms(), + $this->oCfg->getWhitelistedRooms()); + TreeViewSubtitle(_("Add")); + $this->showList($aoRooms); + TreeViewLine(sprintf("

<%s name='mod_roomReservationRoomWhitelistList". + "Box[action][add]' value='%s' /> <%s name='mod_roomReservationRoom". + "WhitelistListBox[action][add]' value='%s' />

", $GLOBALS["stdbtn"], + _("OK"), $GLOBALS["stdbtn"], _("Cancel"))); + } + + /** + * Show the deletion form + * @return void + */ + protected function showDeleteForm() { + // list rooms in selection + $aoRooms = array(); + $h = db_query("SELECT name FROM rooms WHERE oid IN ". + qdb_arr($this->anPostSelection)); + foreach($this->anPostSelection as $nOid) { + $aoRooms[] = mod_roomReservationRoomsManager::getRoomByOid($nOid); + } + TreeViewSubtitle(sprintf(_("Following %s will be deleted"), + _c("room-reservation:rooms"))); + $this->showList($aoRooms, false); + TreeViewLine(sprintf("

<%s name='mod_roomReservationRoomWhitelistList". + "Box[action][delete]' value='%s' /> <%s name='mod_roomReservationRoom". + "WhitelistListBox[action][delete]' value='%s' />

", + $GLOBALS["stdbtn"], _("OK"), $GLOBALS["stdbtn"], _("Cancel"))); + } + + /** + * Show the list items + * @param $aoRooms (array of mod_roomReservationRoom objects) List items + * @param $bCheckboxes (bool) Whether to show checkboxes + */ + protected function showList($aoRooms, $bCheckboxes = true) { + if(count($aoRooms) > 0) { + foreach($aoRooms as $o) { + // fetch oid from SQL table + $nOid = pg_fetch_result(db_query("SELECT oid FROM ". + "rooms WHERE name = $1", $o->getName()), 0, "oid"); + $sBox = $bCheckboxes ? sprintf("<%s id='box%d' name='mod_room". + "ReservationRoomWhitelistListBox[l][%d]' value='1'%s />", $GLOBALS["smlchk"], $nOid, $nOid, + @$this->anPostSelection[$nOid] ? " checked='checked'" : "", $nOid, + icon("host"), $o->getName()) : + sprintf("%s%s", $nOid, icon("host"), + $o->getName()); + TreeViewLine($sBox); + } + } else { + TreeViewEmpty(); + } + } +} +?> \ No newline at end of file diff --git a/inc/mod_roomReservationRoomsManager.inc b/inc/mod_roomReservationRoomsManager.inc new file mode 100644 index 0000000..edce221 --- /dev/null +++ b/inc/mod_roomReservationRoomsManager.inc @@ -0,0 +1,165 @@ +oCfg = $oCfg; + } + + /** + * Insert or update a room in the database + * param $or (rsRoom) Room to store in the database + * return (int) The UID of the booking, or -1 in case of a failure. + * Call getLastError() to get more information about the error. + */ +/** public function write(rsRoom $or) { + // Only administrators are allowed to create and alter rooms + if(!$this->oCfg->userIsAdmin()) { + // TODO throw exception + setLastError(RS_ERROR_ACCESS_DENIED); + return -1; + } + + $strWhere = ""; + $strLog = ""; + + // Update or insert? + if($or->getUid() == null) { + // No UID yet, insert new room + $strLog = sprintf("Raum „%s“ angelegt", $or->getName()); + } else { + $strWhere = "rsr_uid = ".qp(intval($or->getUid())); + $strLog = sprintf("Raum „%s“ geändert", $or->getName()); + } + + $aPut["rsr_name"] = $or->getName(); + db_store("rooms", $aPut, $strWhere == "" ? null : $strWhere); + + $hQuery = db_query("SELECT currval('roomschedule_rooms_rsr_uid_seq') ". + "FROM roomschedule_rooms;"); + $nNewUid = pg_fetch_result($hQuery, 0, "currval"); + + rrInsertLog($strLog); + + // Return new UID + return $nNewUid; + } + */ + /** + * Delete a room from the database + * param $nUid (int) Unique ID of the room + * return (bool) true on success, otherwise false. + * Call getLastError() to get more information about the error. + */ +/* public function delete($nUid) { + // Only administrators are allowed to delete rooms + if(!$this->oCfg->userIsAdmin()) { + // TODO throw exception + setLastError(RS_ERROR_ACCESS_DENIED); + return false; + } + // Delete it from the database + $strRoom = $this->getRoomName($nUid); + if(!db_query("DELETE FROM roomschedule_rooms WHERE rsr_uid = $1;", + intval($nUid))) { + // TODO throw exception + setLastError(RS_ERROR_SQL); + return false; + } else { + rsInsertLog(sprintf("Raum „%s“ gelöscht", $strRoom)); + return true; + } + } +*/ + + /** + * Get a room by its OID. Returns null if the room was not found. + * @param $nOid (integer) The OID of the room + * @return mod_roomReservationRoom + */ + static function getRoomByOid($nOid) { + $o = null; + $h = db_query("SELECT * FROM rooms WHERE oid = $1;", $nOid); + if(pg_num_rows($h) > 0) { + $arResult = pg_fetch_array($h); + $o = new mod_roomReservationRoom($arResult["name"], + $arResult["room_no"], $arResult["floor"], $arResult["building"], + $arResult["location"]); + } + return $o; + } + + /** + * Get a room by its name. Returns null if the room was not found. + * @param $sName (string) The name of the room + * @return mod_roomReservationRoom + */ + static function getRoomByName($sName) { + $o = null; + $h = db_query("SELECT * FROM rooms WHERE name = $1;", $sName); + if(pg_num_rows($h) > 0) { + $arResult = pg_fetch_array($h); + $o = new mod_roomReservationRoom($arResult["name"], + $arResult["room_no"], $arResult["floor"], $arResult["building"], + $arResult["location"]); + } + return $o; + } + + /** + * Get all rooms from the database + * @return array of mod_roomReservationRoom + */ + static function getRooms() { + $aoReturn = array(); + $hQuery = db_query("SELECT * FROM rooms ORDER BY name;"); + while($arResult = pg_fetch_array($hQuery)) { + $aoReturn[] = new mod_roomReservationRoom($arResult["name"], + $arResult["room_no"], $arResult["floor"], $arResult["building"], + $arResult["location"]); + } + return $aoReturn; + } +} +?> diff --git a/inc/mod_roomReservationTimeslice.inc b/inc/mod_roomReservationTimeslice.inc new file mode 100644 index 0000000..f6bd997 --- /dev/null +++ b/inc/mod_roomReservationTimeslice.inc @@ -0,0 +1,102 @@ +setBegin($tsBegin); + $this->setEnd($tsEnd); + } + + /***************************************************************************/ + /** + * @} + * @name Access to attributes + * @{ + */ + + /** + * Set the beginning. Only the time part is used, the date part is ignored. + * If the timestamp is invalid, an Exception is thrown. + * @param $ts (timestamp) + * @throws Exception + */ + public function setBegin($ts) { + if(intval($ts) >= $this->getEnd()) { + throw new Exception(MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN); + } else { + $this->tsBegin = (intval($ts) % 86400); + } + } + + /** + * Set the ending. Only the time part is used, the date part is ignored. + * If the timestamp is invalid, an Exception is thrown. + * @param $ts (timestamp) + * @throws Exception + */ + public function setEnd($ts) { + if($this->getBegin() >= intval($ts)) { + throw new Exception(MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN); + } else { + $this->tsEnd = (intval($ts) % 86400); + } + } + + /** + * Get the beginning. + * @return timestamp + */ + public function getBegin() { return $this->tsBegin; } + + /** + * Get the ending. + * @return timestamp + */ + public function getEnd() { return $this->tsEnd; } + + /** @} */ +} +?> diff --git a/inc/mod_roomReservationTimesliceListBox.inc b/inc/mod_roomReservationTimesliceListBox.inc new file mode 100644 index 0000000..6928a33 --- /dev/null +++ b/inc/mod_roomReservationTimesliceListBox.inc @@ -0,0 +1,264 @@ +setAction((($ga == "add") ? MOD_ROOM_RESERVATION_TLB_ACTION_ADD : + (($ga == "delete") ? MOD_ROOM_RESERVATION_TLB_ACTION_DELETE : + MOD_ROOM_RESERVATION_TLB_ACTION_SHOW))); + } + + // new beginning and new ending + $this->setNewBegin(isset( + $_GET["mod_roomReservationTimesliceListBox"]["begin"]) ? + $_GET["mod_roomReservationTimesliceListBox"]["begin"] : ""); + $this->setNewEnd(isset( + $_GET["mod_roomReservationTimesliceListBox"]["end"]) ? + $_GET["mod_roomReservationTimesliceListBox"]["end"] : ""); + } + + // perform the requested action + if($this->getAction() == MOD_ROOM_RESERVATION_TLB_ACTION_ADD) { + // add a timeslice to the configuration file + + $bErrors = false; + + // Note: we want to handle the timestamps in GMT format, hence the "+0000" + if(strtotime($this->getNewBegin()." +0000") === false) { + $this->asMessages[] = _c("room-reservation:The beginning time is ". + "invalid."); + $bErrors = true; + } + if(strtotime($this->getNewEnd()." +0000") === false) { + $this->asMessages[] = _c("room-reservation:The ending time is ". + "invalid."); + $bErrors = true; + } + + if(!$bErrors) { + try { + $this->oCfg->addTimeslice(new mod_roomReservationTimeslice( + strtotime($this->getNewBegin()." +0000") % 86400, + strtotime($this->getNewEnd()." +0000") % 86400)); + $this->oCfg->writeConfig(); + $this->setNewBegin(""); + $this->setNewEnd(""); + } catch(Exception $e) { + $this->asMessages[] = $e->getMessage(); + } + } + + } elseif($this->getAction() == MOD_ROOM_RESERVATION_TLB_ACTION_DELETE) { + // delete a timeslice from the configuration file + if(isset($_POST["mod_roomReservationTimesliceListBox"])) { + if(isset($_POST["mod_roomReservationTimesliceListBox"]["l"])) { + $ao = $this->oCfg->getTimeslices(); + foreach($_POST["mod_roomReservationTimesliceListBox"]["l"] as + $n => $b) { + if($b) { + $this->oCfg->deleteTimeslice(new mod_roomReservationTimeslice( + $ao[$n]->getBegin(), $ao[$n]->getEnd())); + } + } + $this->oCfg->writeConfig(); + } + } + } + } + + /***************************************************************************/ + /** + * @} + * @name Access to attributes + * @{ + */ + + /** + * Set the action to be done (GET data) + * @param $c (constant) Action. See @ref timeslicelistbox_actions. + */ + protected function setAction($c) { $this->cAction = intval($c); } + + /** + * Set the beginning for a new timeslice (GET data) + * @param $s (string) + */ + protected function setNewBegin($s) { $this->sNewBegin = $s; } + + /** + * Set the beginning for a new timeslice (GET data) + * @param $s (string) + */ + protected function setNewEnd($s) { $this->sNewEnd = $s; } + + /** + * Get the action to be done (GET data). See @ref timeslicelistbox_actions. + * @return constant + */ + function getAction() { return $this->cAction; } + + /** + * Get the beginning for a new timeslice (GET data) + * @return string + */ + public function getNewBegin() { return $this->sNewBegin; } + + /** + * Get the beginning for a new timeslice (GET data) + * @return string + */ + public function getNewEnd() { return $this->sNewEnd; } + + /***************************************************************************/ + /** + * @} + * @name Output + * @{ + */ + + /** + * Actually show the control + * @return void + */ + public function doShow() { + TreeView(array(_c("room-reservation:Begin"), _c("room-reservation:End"))); + + // addition form + printf("
"); + hidden("mod_roomReservationTimesliceListBox[action]", "add"); + TreeViewTitle(_("Add")); + + $sMessages = $this->getMessages(); + if(trim($sMessages) != "") { + TreeViewLine($sMessages); + } + + TreeViewLine(array(sprintf("<%s name='mod_roomReservationTimesliceListBox". + "[begin]' value='%s' size='8'/>", $GLOBALS["stdedt"], + $this->getNewBegin()), sprintf("<%s name='". + "mod_roomReservationTimesliceListBox[end]' value='%s' size='8'/> <%s ". + "name='mod_roomReservationTimesliceListBox[submit]' value='%s' />", + $GLOBALS["stdedt"], $this->getNewEnd(), $GLOBALS["stdbtn"], _("Add")))); + echo "
\n"; + + // deletion form + TreeViewTitle(_c("room-reservation:Periods")); + $aoTs = $this->oCfg->getTimeslices(); + if(count($aoTs) > 0) { + echo "
"; + $i = 0; + foreach($aoTs as $oTs) { + $sBox = sprintf("". + "<%s name='mod_roomReservationTimesliceListBox[l][%d]' value='1' />", + $i, $GLOBALS["smlchk"], $i); + // Note: we have only GMT timestamps in the timeslice objects + TreeViewLine(array($sBox . gmstrftime(_("%#I:%M %p"), $oTs->getBegin()), + gmstrftime(_("%#I:%M %p"), $oTs->getEnd()))); + $i++; + } + // toolbar + printf("", $GLOBALS["treeview_cols"]); + CheckCombo(); + printf("<%s name='mod_roomReservationTimesliceListBox[submit]' ". + "value='%s' />", $GLOBALS["stdbtn"], _("Delete")); + echo "\n"; + } else { + TreeViewEmpty(); + } + + echo "
\n"; + _TreeView(); + } + /** @} */ +} +?> \ No newline at end of file diff --git a/includes/exceptions.inc b/includes/exceptions.inc deleted file mode 100644 index 8d7c688..0000000 --- a/includes/exceptions.inc +++ /dev/null @@ -1,44 +0,0 @@ - \ No newline at end of file diff --git a/includes/functions.inc b/includes/functions.inc deleted file mode 100644 index 78e3690..0000000 --- a/includes/functions.inc +++ /dev/null @@ -1,279 +0,0 @@ - 0); -} - -/** - * Get the real user name for an account name - * @param $strAct (string) Account name of the user to look up - * @return (string) The real name of the user. If the function fails, it returns null. - * Call getLastError() to get more information about the error. - */ -function getRealUserName($strAct) { - $hQuery = db_query("SELECT firstname, lastname FROM users WHERE act = $1;", $strAct); - if(!is_resource($hQuery)) { - // TODO throw exception - setLastError(RS_ERROR_SQL); - return null; - } - if(pg_num_rows($hQuery) == 0) { - return $strAct; // User not found in database, return account name - } - $arResult = pg_fetch_array($hQuery); - return user_join_name($arResult); -} - -/** - * Determine if a specified group exists - * @param $strAct (string) Account name of the group - * @return (bool / null) If the function fails, it returns null. Call getLastError() to - * get more information about the error. - */ -function isGroup($strAct) { - $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct); - if(!is_resource($hQuery)) { - // TODO throw exception - setLastError(RS_ERROR_SQL); - return null; - } - return (pg_num_rows($hQuery) > 0); -} - -/** - * Look up the name of a group - * @param $strAct (string) Account name of the group - * @return (string) The name of the group. If the function fails, it returns null. - * Call getLastError() to get more information about the error. - */ -function getGroupName($strAct) { - $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct); - if(!is_resource($hQuery)) { - // TODO throw exception - setLastError(RS_ERROR_SQL); - return null; - } - if(pg_num_rows($hQuery) == 0) { - return $strAct; // Group not found in database, return account name - } - $arResult = pg_fetch_array($hQuery); - return $arResult["name"]; -} - -/** - * Create a link to write a mail to the specified account name. - * This function returns a link if the specified account exists, otherwise it returns the - * account name. - * @param $strAct (string) Account name - * @param $strColor (string) Background color for icon() - * @param $strParams (string) additional URL parameters - * @return string - */ -function mailToUserLink($strAct, $strColor = "bl", $strParams = "") { - if(!isAct($strAct)) { - return $strAct; - } - return popup(relroot("msg/write/?to=".user_mail_addr($strAct).$strParams), - 600, 400, nobr(icon("mail-reply-usr", array("size" => 16, "bg" => - $strColor)) . getRealUserName($strAct))); -} - -/** - * Determine if a specified string is a valid mail address - * @param $strAddr string - * @return string - */ -function isMailAddress($strAddr) { - return ((preg_match("/([a-zA-Z0-9_\-\.]*(@[a-zA-Z0-9\-\.]*)?(\s*,\s*)?)+/", $strAddr) > 0) - and (preg_match("/(\s*,\s*)$/", $strAddr) == 0)); -} - -/** - * Module-specific logging function. - * Prefixes the log message with a module-specific string and writes it to the logs. - * @param $strLog (string) Log message - * @return void - */ -function rrInsertLog($strLog) { - log_insert($strLog, null, "Room Reservation Schedule"); -} - -/** - * Get the SQL day number from a given timestamp. - * @param $ts (timestamp) - * @return (int) 0-6 for Sunday to Monday - */ -function rrDateToSQLDayNumber($ts) { - $aDays = array("Sun" => 0, "Mon" => 1, "Tue" => 2, - "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6); - return $aDays[date("D", $ts)]; -} - -/** - * Convert a UNIX timestamp to an SQL date string - * @param $ts (timestamp) - * @return string - */ -function dateToSql($ts) { return date("Y\-m\-d", intval($ts)); } - -/** - * Calculate the timestamp of the monday in the current week - * @param $ts (timestamp) Calculate the value relative to this date - * @return timestamp - */ -function rrGetMonday($ts = null) { - if($ts === null) { - $ts = time(); - } - if(date("D", $ts) == "Mon") { - // Today is monday - return strtotime("00:00", $ts); - } else { - return strtotime("last monday", $ts); - } -} - -/** (array of strings) Additional CSS rules */ -$GLOBALS["rrLocalCss"] = array(); - -/** - * Add CSS rules to the page - * @param $s (string) - * @return void - */ -function rrAddCss($s) { - rrDebug("rrAddCss: add \"$s\""); - $GLOBALS["rrLocalCss"][] = $s; -} - -/** - * Get CSS rules that have been added with rrAddCss() - * @return string - */ -function rrGetCss() { - rrDebug("rrGetCss: Local CSS is ".var_export($GLOBALS["rrLocalCss"], true)); - return implode("\n", $GLOBALS["rrLocalCss"]); -} - -function rrDebug($s, $bReturn = false) { - if(isset($_GET["debug"])) { - if(!$bReturn) { - echo "\n"; - } else { - return $s; - } - } -} - -/** - * sprintf with support for ordinal numbers. - * This version of sprintf replaces all substrings of the type /\\d+#/ - * (i.e. a decimal number with a hash sign appended) in the input string with - * the corresponding english ordinal number prefices (st, nd, rd, th). - * @param $str (string) Input string - * @param $args (mixed) Additional parameters to be passed to sprintf() - * @return (string) - */ -function _sprintf_ord($str, $args /*leave this parameters for doxygen*/) { - $args = func_get_args(); - if(preg_match_all("/%[bcdufosxX]/", $args[0], $temp) != func_num_args()-1) { - trigger_error("Too few arguments", E_USER_ERROR); - return false; - } - $str = call_user_func_array("sprintf", $args); - while(preg_match("/(.*)(\d+)#(.*)/", $str, $m)) - $str = $m[1]._(append_ord_suffix($m[2])).$m[3]; - return $str; -} -?> diff --git a/includes/globals.inc b/includes/globals.inc deleted file mode 100644 index eb2977e..0000000 --- a/includes/globals.inc +++ /dev/null @@ -1,47 +0,0 @@ - diff --git a/includes/mod_roomReservationBooking.inc b/includes/mod_roomReservationBooking.inc deleted file mode 100644 index a6a1c8a..0000000 --- a/includes/mod_roomReservationBooking.inc +++ /dev/null @@ -1,201 +0,0 @@ -setUid(null); - $this->setRoom($strRoom); - $this->setDate($tsDate); - $this->setTsFirst($nTsFirst); - $this->setTsLast($nTsLast); - $this->setAct($strAct); - $this->setReason($strReason); - $this->setInterval($nInterval); - } - - /***************************************************************************/ - /** - * @} - * @name Access to attributes - * @{ - */ - - /** - * Set the unique ID in database - * @param $n (int) Unique ID in database - * @return void - */ - public function setUid($n) { - if(is_null($n)) { - $this->nUid = null; - } else { - $this->nUid = intval($n); - } - } - - /** - * Set the name of the room - * @param $str (string) Name of the room - * @return void - */ - public function setRoom($str) { $this->strRoom = $str; } - - /** - * Set the date when the booking takes place - * @param $ts (timestamp) Date, only the date part is taken care of - * @return void - */ - public function setDate($ts) { - // Only take the date part - $this->tsDate = intval(strtotime(date("Y\-m\-d", intval($ts)))); } - - /** - * Set the first timeslice - * @param $n (int) Number of the first timeslice - * @return void - */ - public function setTsFirst($n) { $this->nTsFirst = intval($n); } - - /** - * Set the end timeslice - * @param $n (int) Number of the last timeslice (may be the start timeslice) - * @return void - */ - public function setTsLast($n) { $this->nTsLast = intval($n); } - - /** - * Set the account name of the owner - * @param $str (string) Account name - * @return void - */ - public function setAct($str) { $this->strAct = $str; } - - /** - * Set the reason for the booking - * @param $str (string) Reason - * @return void - */ - public function setReason($str) { $this->strReason = $str; } - - /** - * Set the flag whether the booking repeates every week - * @param $n (int) interval in weeks, 0 for no recurrence - * @return void - */ - public function setInterval($n) { $this->nInterval = intval(abs($n)); } - - /** - * Get the unique ID in database - * @return int / null - */ - public function getUid() { return intval($this->nUid); } - - /** - * Get the name of the room - * @return string - */ - public function getRoom() { return $this->strRoom; } - - /** - * Get the date when the booking takes place - * @return timestamp - */ - public function getDate() { return intval($this->tsDate); } - - /** - * Get the the number of the first timeslice - * @return int - */ - public function getTsFirst() { return intval($this->nTsFirst); } - - /** - * Get the number of the last timeslice (may be the start timeslice) - * @return int - */ - public function getTsLast() { return intval($this->nTsLast); } - - /** - * Get the account name of the owner - * @return string - */ - public function getAct() { return $this->strAct; } - - /** - * Get the reason for the booking - * @return string - */ - public function getReason() { return $this->strReason; } - - /** - * Get the recurrence interval - * @return int - */ - public function getInterval() { return $this->nInterval; } - - /**@}*/ -} -?> diff --git a/includes/mod_roomReservationBookingPage.inc b/includes/mod_roomReservationBookingPage.inc deleted file mode 100644 index 219c7bd..0000000 --- a/includes/mod_roomReservationBookingPage.inc +++ /dev/null @@ -1,215 +0,0 @@ -oCfg = $oCfg; - $this->oRm = $oRm; - $this->oBm = $oBm; - - // create the booking table here, so the CSS is already added - /** @todo maybe move it into beforeAddCSS()... ? */ - $this->oBt = new mod_roomReservationBookingsTable($this->oCfg, $this->oRm, - $this->oBm, "?bookingpage[action]=edit", "?bookingpage[action]=delete"); - - parent::__construct($oCfg); - $this->setTitle(_c("Room Reservation Schedule")); - $this->setIcon("mod_room-reservation_index"); - } - - /***************************************************************************/ - /** - * @} - * @name Initialization - * @{ - */ - - /** - * Process the REQUEST variables and preset the some variables - * @return void - */ - protected function processRequestVariables() { - // take all settings from the booking table - $this->setRoom($this->oBt->getRoom()); - $this->setStart($this->oBt->getDate()); - } - - /***************************************************************************/ - /** - * @} - * @name Access to attributes - * @{ - */ - - /** - * Set the starting date - * @param $ts (timestamp) - */ - public function setStart($ts) { $this->tsStart = intval($ts); } - - /** - * Set the room to show in the booking table - * @param $str (string) Name of the room - */ - public function setRoom($str) { $this->strRoom = $str; } - - /** - * Get the starting date - * @return int - */ - public function getStart() { return intval($this->tsStart); } - - /** - * Get the name of the room to show in the booking table - * @return string - */ - public function getRoom() { return $this->strRoom; } - - /***************************************************************************/ - /** - * @} - * @name Output - * @{ - */ - - /** - * Show the page. - * @return void - */ - public function doShow() { - // Protect access - if(!$this->oCfg->userCanView()) { - echo sprintf("

%s

", - MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); - _PageBlue(); - die(); - } - - Title(_c("room-reservation:Book rooms")); - - // Form for room selection - /** @todo checkbox for recurring booking */ - echo sprintf("
", - $_SERVER["PHP_SELF"]); - echo sprintf("\n", $this->getStart()); - - // Show rooms only if it is whitelisted - try { - $aor = $this->oCfg->getWhitelistedRooms(); - } catch(SQLException $e) { - trigger_error($e->getMessage()); - } - if(count($aor) > 0) { - echo _c("room-reservation:Room:") . sprintf("  <%s value='%s' />

\n", - $GLOBALS["stdbtn"], _("Change")); - } else { - printf("

%s

\n", _c("room-reservation:No rooms have been ". - "configured yet.")); - return; - } - - // Print line with next 5 or so weeks - $strSep = " | "; - $strLink = sprintf("%%s", - $_SERVER["PHP_SELF"]); - echo "

".sprintf($strLink, time(), qu($this->getRoom()), - _c("room-reservation:Current Week")) . $strSep; - echo sprintf($strLink, strtotime("1 week ago", $this->getStart()), - qu($this->getRoom()), _c("room-reservation:< Back")) . $strSep; - echo sprintf("%s", _sprintf_ord(_c("room-reservation:%d# week"), - date("W", $this->getStart()))) . $strSep; - for($i = 1; $i <= 5; $i++) { - $nNextWeek = strtotime("$i week", $this->getStart()); - echo sprintf($strLink, $nNextWeek, qu($this->getRoom()), _sprintf_ord( - _c("room-reservation:%d# week"), date("W", $nNextWeek))); - echo $strSep; - } - echo sprintf($strLink, strtotime("1 week", $this->getStart()), - qu($this->getRoom()), _c("room-reservation:Next >"))."

\n"; - - $this->oBt->show(); - } -} -?> diff --git a/includes/mod_roomReservationBookingTable.inc b/includes/mod_roomReservationBookingTable.inc deleted file mode 100644 index fd85ef1..0000000 --- a/includes/mod_roomReservationBookingTable.inc +++ /dev/null @@ -1,783 +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()); - } - // if weekends are not shown, show the next week already on saturday - if(!$this->oCfg->isShowWeekend() and (date("w") == 6 or date("w") == 0)) { - $this->setDate(strtotime("monday")); - } else { - $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 = <<oCfg->isShowWeekend()) { - $strCss .= "#mod_roomReservationBookingTable td.cell { width:13%; }"; - } else { - $strCss .= "#mod_roomReservationBookingTable td.cell { width:18.2%; }"; - } - rrAddCss($strCss); - } - - /** - * Show the timetable - * @return void - * @throws AccessException - * @todo increase the height of the cells a little - */ - public function show() { - // Protect access - if(!$this->oCfg->userCanView()) { - throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); - return; - } - - // print error messages and return if there are any - if(count($this->asErrors) > 0) { - printf("

%s

", join("
\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 "", $strClass, $strTitle); - } - 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) . "
" : ""; - $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("", $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 "\n"; - } - echo "
"; - 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
%s

"; - } - - /** - * 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", $asMsgs)))."

\n"; - } - - // calculate the timespan of the current booking - $nSpan = $ob->getTsLast() - $ob->getTsFirst() + 1; - - if(($cFlags & MOD_ROOM_RESERVATION_BTPB_DELETE) == - MOD_ROOM_RESERVATION_BTPB_DELETE) { - - // Restrict access - if(!($this->oBm->userIsOwner($ob->getUid()) or - $this->oCfg->userIsAdmin())) { - $strBefore .= "

" . - MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED . "

\n"; - #return $nSpan; - } else { - // print delete form - $strWarning = sprintf("
%s%s
", icon("dlg-warn", - array("bg" =>"gb")), _c("room-reservation:Attention: This ". - "booking is a recurring booking. If you delete it, the period will ". - "be deallocated for every week, not just this single week!")); - $strAfter .= sprintf("

%s%s
<%s name='mod_roomReservationBookingTable". - "[submitdelete]' value='%s' /> <%s name='". - "mod_roomReservationBookingTable[submitdelete]' value='%s' />". - "

", $_SERVER["PHP_SELF"], - _c("room-reservation:Delete this booking?"), - ($ob->getInterval() > 0 ? $strWarning : ""), $GLOBALS["smlbtn"], - _("Delete"), $GLOBALS["smlbtn"], _("Cancel"), $ob->getUid(), - $this->getRoom(), $this->getDate()); - } - } else { - // delete and edit links, show only if user is allowed to - if($this->oBm->userIsOwner($ob->getUid()) || - $this->oCfg->userIsAdmin()) { - /** @todo edit form */ - $strAfter .= sprintf("
(". - "%s)", - $_SERVER["PHP_SELF"], $ob->getUid(), $ts, - _c("room-reservation:Edit this booking"), - _c("room-reservation:edit"), $_SERVER["PHP_SELF"], $ob->getUid(), - $ts, _c("room-reservation:Delete this booking"), - _c("room-reservation:delete")); - } - } - - // test if booking is new and should be highlighted - $strClass = "cell booking".($ob->getInterval() > 0 ? " recurring" : ""); - if(($cFlags & MOD_ROOM_RESERVATION_BTPB_NEW) == - MOD_ROOM_RESERVATION_BTPB_NEW) { - $strClass .= " new"; - } - // Use a different style for the current day - $strClass .= (date("Ymd", $ob->getDate()) == date("Ymd") ? " today" : ""); - /** @todo: add ?subject=... to mailto link */ - echo sprintf("%s%s
%s%s\n", - $nSpan, $strClass, $strBefore, mailto($ob->getAct()), - q(getRealUserName($ob->getAct())), q($ob->getReason()), $strAfter); - - return $nSpan; - } - - /** - * Print the booking form. - * @param $nTs (int) current timeslice - * @param $ts (timestamp) current date - * @param $asErrors (array of strings) Additional error message to be printed - * inside the cell, one array element per message - * @return (int) the span of the booking (i.e., 1) - */ - protected function printBookingForm($nTs, $ts, $asErrors = array()) { - // Restrict access - if(!($this->oCfg->userCanBook() or $this->oCfg->userIsAdmin())) { - printf("%s\n", - MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); - return 1; - } - - $strErrors = "

".nl2br(q(join("\n", $asErrors)))."

"; - - // form to allow fixed bookings for admins - $sWeeklyForm = ""; - if($this->oCfg->userIsAdmin()) { - $sWeeklyForm = sprintf(" %s
". - " <%s name='". - "mod_roomReservationBookingTable[account]' id='account' value='%s' ". - "size='15' />
", _c("room-reservation:Repetition:"), - select("mod_roomReservationBookingTable[interval]", - $this->nPostInterval, array(0 => _c("Select:None"), 1 => - _c("room-reservation:every week")), array("add" => "id='interval'")), - _c("room-reservation:Account, if not yourself:"), $GLOBALS["stdedt"], - $this->strPostAccount); - } - - echo sprintf("%s". - "
%s". - "
<%s id='reason' size='15' ". - "value='%s' name='mod_roomReservationBookingTable[reason]' />
%s". - "<%s name='mod_roomReservationBookingTable[submitbooking]' value='%s' />". - "
\n", (count($asErrors) > 0) ? $strErrors : "", - $_SERVER["PHP_SELF"], _c("room-reservation:until:"), - select("mod_roomReservationBookingTable[tslast]", $this->getTsLast(), - $this->oCfg->getTimesliceEndings(true)), _c("room-reservation:Reason:"), - $GLOBALS["stdedt"], $this->getReason(), $sWeeklyForm, $GLOBALS["smlbtn"], - _c("room-reservation:Book"), $this->getDate(), $this->getRoom(), - $this->getTsFirst()); - return 1; - } - - /** - * Print the booking link - * @param $nTs (int) current timeslice - * @param $ts (timestamp) current date - * @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 (i.e., 1) - */ - protected function printBookingLink($nTs, $ts, $asMsgs = array()) { - - // Restrict access - if(!($this->oCfg->userCanBook() or $this->oCfg->userIsAdmin())) { - echo "\n"; - return 1; - } - - // messages - $strBefore = ""; - if(count($asMsgs) > 0) { - $strBefore .= "

".join("
", $asMsgs)."

\n"; - } - - // print link to booking if the timeslice is later than now - $oTs = $this->oCfg->getTimeslice($nTs); - // note: only the timeslices are in GMT! - $tsCur = strtotime(date("Y-m-d ", $ts) . gmdate(" G:i", - $oTs->getEnd())); - if($tsCur > time()) { - $strURL = $_SERVER["PHP_SELF"] . - sprintf("?mod_roomReservationBookingTable[action]=book&". - "mod_roomReservationBookingTable[date]=%d&". - "mod_roomReservationBookingTable[room]=%s&". - "mod_roomReservationBookingTable[tsfirst]=%d#form", $ts, - qu($this->getRoom()), $nTs); - echo sprintf("%s%s\n", - $strBefore, $strURL, _c("room-reservation:Book this room from here"), - _c("room-reservation:(Book from here)")); - } else { - // only print the messages - echo sprintf("%s\n", - $strBefore); - } - return 1; - } - /** @} */ -} -?> diff --git a/includes/mod_roomReservationBookingsManager.inc b/includes/mod_roomReservationBookingsManager.inc deleted file mode 100644 index 124068d..0000000 --- a/includes/mod_roomReservationBookingsManager.inc +++ /dev/null @@ -1,274 +0,0 @@ -oCfg = $oCfg; - } - - /***************************************************************************/ - /** - * @} - * @name Retrieving bookings - * @{ - */ - - /** - * Fetch a booking with the given unique ID from the SQL table - * @param $nUid (int) Unique ID of the booking - * @return mod_roomReservationBooking - */ - public static function getBookingByUid($nUid) { - $h = db_query("SELECT * FROM mod_roomreservation_bookings WHERE rrb_uid = $1;", $nUid); - $a = pg_fetch_array($h); - $o = new mod_roomReservationBooking($a["rrb_room"], strtotime($a["rrb_date"]), - intval($a["rrb_tsfirst"]), intval($a["rrb_tslast"]), $a["rrb_act"], - $a["rrb_reason"], intval($a["rrb_interval"])); - $o->setUid(intval($a["rrb_uid"])); - return $o; - } - - /** - * Test if there is a booking which takes place on the specified position at - * the specified date. - * @param $strRoom (string) Name of the room - * @param $tsDate (timestamp) The date - * @param $nTimeslice (int) The number of the timeslice - * @return mod_roomReservationBooking The booking which takes place on the - * specified time or null if no booking could be found. - */ - public static function getBookingByTimeslice($strRoom, $tsDate, - $nTimeslice) { - $a = mod_roomReservationBookingsManager::getOverlappingBookings( - new mod_roomReservationBooking($strRoom, $tsDate, $nTimeslice, - $nTimeslice, null, null)); - return isset($a[0]) ? $a[0] : null; - } - - /** - * Get all bookings in database which overlap with the given booking. - * @param $ob (mod_roomReservationBooking) New booking that should be tested - * if it overlaps - * @return array with elements of type mod_roomReservationBooking - */ - public static function getOverlappingBookings( - mod_roomReservationBooking $ob) { - // TODO: Test for bookings that only take place every n.th week (modulo n) - - // Two bookings overlap, when they are on the same day and if - // old beginning < new ending AND old ending > new beginning - $hQuery = db_query("SELECT * FROM mod_roomreservation_bookings WHERE ". - "rrb_room = $1 AND ((rrb_interval > 0 AND EXTRACT(DOW FROM rrb_date) ". - "= $2) OR (rrb_interval = 0 AND rrb_date = $3)) AND rrb_tsfirst <= ". - "$4 AND rrb_tslast >= $5 ORDER BY rrb_tsfirst;", $ob->getRoom(), - date("w", $ob->getDate()), date("Y-m-d", $ob->getDate()), - intval($ob->getTsLast()), intval($ob->getTsFirst())); - $aoReturn = array(); - while($aResult = pg_fetch_array($hQuery)) { - $aoReturn[] = mod_roomReservationBookingsManager::getBookingByUid( - $aResult["rrb_uid"]); - } - return $aoReturn; - } - - /***************************************************************************/ - /** - * @} - * @name Management of bookings - * @{ - */ - - /** - * Insert or update a booking in the database. - * The function throws an AccessException if the user was not allowed to - * write the booking, or an SQLException if there was an error while trying - * to insert or update the booking into the database. - * @param $ob (mod_roomReservationBooking) Booking to write to the database - * @return (int) The UID of the written booking - * @throws SQLException, AccessException - * @todo document - */ - function write(mod_roomReservationBooking $ob) { - // protect access - if(($ob->getUid() != null and !$this->oCfg->userIsAdmin() and - !$this->userIsOwner($ob->nUid)) or - ($ob->getUid() == null and !$this->oCfg->userCanBook())) { - throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); - } - - // test if room is whitelisted - if(!$this->oCfg->isRoomWhitelisted($ob->getRoom())) { - throw new Exception(MOD_ROOM_RESERVATION_ERROR_ROOM_NOT_WHITELISTED); - } - - $strWhere = null; - $strLog = ""; - - // check if everything is right and throw exceptions - if(trim($ob->getAct()) == "") { - $ob->setAct($SESSION["act"]); - } elseif(!isAct($ob->getAct())) { - throw new Exception(MOD_ROOM_RESERVATION_ERROR_NO_SUCH_ACCOUNT); - return false; - } - if($ob->getTsFirst() > $ob->getTsLast()) { - throw new SQLException(MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN); - return false; - } - if(trim($ob->getReason()) == "") { - throw new SQLException(MOD_ROOM_RESERVATION_ERROR_NO_REASON); - return false; - } - - // Test for overlapping bookings - if($this->getOverlappingBookings($ob) != array()) { - throw new SQLException(MOD_ROOM_RESERVATION_ERROR_BOOKING_OVERLAPS); - return false; - } - - // Show real times in log, but don't use the user's locale! - $oTsB = $this->oCfg->getTimesliceBeginnings(false); - $oTsE = $this->oCfg->getTimesliceEndings(false); - - // Update or insert? - if($ob->getUid() == null) { - // No UID yet, insert new booking - // @todo write interval and user if interval > 0 - $strLog = sprintf("Raum „%s“ am %s von %s bis %s gebucht ". - "(Begründung: %s)", $ob->getRoom(), date("d\.m\.Y", $ob->getDate()), - gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i", - $oTsE[$ob->getTsLast()]), $ob->getReason()); - } else { - // Update an existing booking - // @todo write old and new times into log - $strWhere = "rs_uid = ".qdb(intval($ob->getUid())); - $strLog = sprintf("Buchung im Raum „%s“ auf %s von %s bis %s ". - "geändert (Begründung: „%s“)", $ob->getRoom(), date("d\.m\.Y", - $ob->getDate()), gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i", - $oTsE[$ob->getTsLast()]), $ob->getReason()); - } - $aPut["rrb_room"] = $ob->getRoom(); - $aPut["rrb_date"] = date("Y\-m\-d", $ob->getDate()); - $aPut["rrb_tsfirst"] = intval($ob->getTsFirst()); - $aPut["rrb_tslast"] = intval($ob->getTsLast()); - $aPut["rrb_act"] = $ob->getAct(); - $aPut["rrb_reason"] = $ob->getReason(); - $aPut["rrb_interval"] = intval($ob->getInterval()); - - // @todo test if the foreign keys are being violated and throw an error - // message if neccessary - db_store("mod_roomreservation_bookings", $aPut, $strWhere); - - $hQuery = db_query("SELECT currval('mod_roomreservation_bookings_rrb_uid_seq');"); - $nNewUid = pg_fetch_result($hQuery, 0, "currval"); - - rrInsertLog($strLog); - - // Return new UID - return $nNewUid; - } - - /** - * Delete a booking from the database - * @param $nUid (int) Unique ID of the booking - * @return (bool) true on success, otherwise false. - * @todo test - */ - public function delete($nUid) { - // Only administrators and owners are allowed to delete bookings - if(!($this->oCfg->userIsAdmin() or $this->userIsOwner($nUid))) { - throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED); - return false; - } - - // Don't use the user's locale! - $oTsB = $this->oCfg->getTimesliceBeginnings(false); - $oTsE = $this->oCfg->getTimesliceEndings(false); - $ob = $this->getBookingByUid($nUid); - $strLog = sprintf("Buchung in Raum „%s“ am %s von %s bis %s ". - "gelöscht (Begründung war: %s)", $ob->getRoom(), date("d\.m\.Y", - $ob->getDate()), gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i", - $oTsE[$ob->getTsLast()]), $ob->getReason()); - // Delete it from the database - if(!db_query("DELETE FROM mod_roomreservation_bookings WHERE ". - "rrb_uid = $1;", $nUid)) { - throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); - return false; - } else { - rrInsertLog($strLog); - return true; - } - } - - /** - * Determine if the current user is the owner of a specified error report. - * If this function fails, call getLastError() to get more information. - * @param $nID (int) Unique ID of the error report - * @throws SQLException - * @return bool - */ - public static function userIsOwner($nID) { - if(!isset($_SESSION["act"])) { - return false; // user is not logged in - } else { - $hQuery = db_query("SELECT rrb_act FROM mod_roomreservation_bookings WHERE ". - "rrb_uid = $1;", intval($nID)); - if(!is_resource($hQuery)) { - throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); - return false; - } - $arResult = pg_fetch_array($hQuery); - return ($arResult["rrb_act"] == $_SESSION["act"]); - } - } -} -?> diff --git a/includes/mod_roomReservationConfig.inc b/includes/mod_roomReservationConfig.inc deleted file mode 100644 index 8dde982..0000000 --- a/includes/mod_roomReservationConfig.inc +++ /dev/null @@ -1,454 +0,0 @@ - 0; -} - -/** - * Retrieve all groups that have a privilege assigned - * @param $strPriv (string) Privilege to test - * @return array - */ -function rrPrivilegedGroups($strPriv) { - $aReturn = array(); - $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1 ". - "ORDER BY act;", $strPriv); - if(pg_num_rows($h) > 0) { - while($a = pg_fetch_array($h)) { - $aReturn[] = $a["act"]; - } - } - return $aReturn; -} - -/** - * User-defined compare function to compare timeslices - * @param $oTs1 (mod_roomReservationTimeslice) - * @param $oTs2 (mod_roomReservationTimeslice) - * @return (int) -1 if $oTs1 begins before $oTs2, - * 0 if the $oTs1 and $oTs2 have the same beginning, - * 1 if $oTs1 begins after $oTs2. - */ -function rrConfigSortTimeslices(mod_roomReservationTimeslice $oTs1, - mod_roomReservationTimeslice $oTs2) { - if($oTs1->getBegin() == $oTs2->getBegin()) { - return 0; - } else { - return ($oTs1->getBegin() > $oTs2->getBegin()) ? 1 : -1; - } -} - -define("MOD_ROOM_RESERVATION_CONFIGFILE_HEADER", "flushTimeslices(); - $this->setShowWeekend(false); - $this->setShowLessons(true); - $this->asMessages = array(); - - $this->readConfig(); - } - - /** - * ************************************************************************** - * @} - * @name Access to attributes - * @{ - */ - - /** - * Add a timeslice. A check is done that the timeslices do not overlap, and - * in this case, an Exception is thrown. - * @param $oTs (mod_roomReservationTimeslice) - * @throws Exception - * @return void - */ - public function addTimeslice(mod_roomReservationTimeslice $oTs) { - // Check for overlapping timeslices - foreach($this->aoTimeslices as $oOldTs) { - if(($oOldTs->getBegin() < $oTs->getEnd() and - $oOldTs->getEnd() > $oTs->getBegin())) { - throw new Exception( - MOD_ROOM_RESERVATION_ERROR_CONFIG_OVERLAPPING_TIMESLICE); - } - } - $this->aoTimeslices[] = $oTs; - usort($this->aoTimeslices, "rrConfigSortTimeslices"); - return; - } - - /** - * Delete a timeslice - * @param $oTs (mod_roomReservationTimeslice) the timeslice to delete. If - * the timeslice is not found, an Exception is thrown. - * @throws Exception - * @return void - */ - public function deleteTimeslice(mod_roomReservationTimeslice $oTs) { - for($i = 0; $i < count($this->aoTimeslices); $i++) { - if($this->aoTimeslices[$i]->getBegin() == $oTs->getBegin() and - $this->aoTimeslices[$i]->getEnd() == $oTs->getEnd()) { - // use array_splice because it renumbers the keys - array_splice($this->aoTimeslices, $i, 1); - return; - } - } - throw new Exception(MOD_ROOM_RESERVATION_ERROR_CONFIG_NO_SUCH_TIMESLICE); - } - - /** - * Delete all timeslices. - * @return void - */ - public function flushTimeslices() { $this->aoTimeslices = array(); } - - /** - * Add a room to the list of rooms who can be booked. Throws an SQLException - * in case of an error. - * @param $sRoom (string) The name of the room - * @throws SQLException, Exception - * @return void - */ - public function whitelistRoom($sRoom) { - if(!$this->isRoomWhitelisted($sRoom)) { - $r = db_store("mod_roomreservation_roomswhitelist", - array("rrr_name" => $sRoom)); - if(!$r) { - throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); - } else { - log_insert(sprintf("Raum „%s“ für Buchungen zur Verfügung gestellt", - $sRoom)); - } - } - } - - /** - * Forbid bookings for a room. Throws an SQLException in case of an error. - * @param $sRoom The name of the room - * @throws SQLException - */ - public function unWhitelistRoom($sRoom) { - $h = db_query("DELETE FROM mod_roomreservation_roomswhitelist WHERE ". - "rrr_name = $1;", $sRoom); - if(!$h) { - throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); - } else { - log_insert(sprintf("Raum „%s“ für Buchungen gesperrt", $sRoom)); - } - } - - /** - * Determine if a room is allowed for booking. Throws an SQLException - * in case of an error. - * @param $sRoom (string) The name of the room - * @return bool - * @throws SQLException - */ - public function isRoomWhitelisted($sRoom) { - $h = db_query("SELECT * FROM mod_roomreservation_roomswhitelist WHERE ". - "rrr_name=$1;", $sRoom); - if(!$h) { - throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL); - } - return (pg_num_rows($h) > 0); - } - - /** - * Get all rooms that are allowed for booking. Throws an SQLException - * in case of an error. - * @throws SQLException - * @return array of mod_roomReservationRoomsManager objects - */ - public function getWhitelistedRooms() { - $aor = mod_roomReservationRoomsManager::getRooms(); - $ar = array(); - foreach($aor as $key => $or) { - if($this->isRoomWhitelisted($or->getName())) { - $ar[] = $or; - } - } - return $ar; - } - - /** - * Show or hide the weekend - * @param $b (bool) - */ - public function setShowWeekend($b) { $this->bShowWeekend = ($b == true); } - - /** - * Show or hide the lesson strings in the booking table - * @param $b (bool) - */ - public function setShowLessons($b) { $this->bShowLessons = ($b == true); } - - /** - * Add a message to the internal array of (error) messages - * @param $sMessage (string) - */ - public function addMessage($sMessage) { - array_merge($this->asMessages, array($sMessage)); - } - - /** - * Get all timeslices in chronological order - * @return array of rmTimeslice - */ - public function getTimeslices() { return $this->aoTimeslices; } - - /** - * Return the starting times of every timeslice - * @param $bFormat (bool) true: Format the times according to the - * current locale - * false: return just the timestamps - * @return array - */ - public function getTimesliceBeginnings($bFormat = false) { - $aot = $this->getTimeslices(); - $aRet = array(); - foreach($aot as $ao) { - $aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getBegin()) : - $ao->getBegin(); - } - return $aRet; - } - - /** - * Return the ending times of every timeslice - * @param $bFormat (bool) true: Format the times according to the - * current locale - * false: return just the timestamps - * @return array - */ - public function getTimesliceEndings($bFormat = false) { - $aot = $this->getTimeslices(); - $aRet = array(); - foreach($aot as $ao) { - $aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getEnd()) : - $ao->getEnd(); - } - return $aRet; - } - - /** - * Get a timeslice - * @param $n (int) index of the timeslice in the array - * @return rmTimeslice - */ - public function getTimeslice($n) { return $this->aoTimeslices[$n]; } - - /** - * Determine if the weekend is shown - * @return bool - */ - public function isShowWeekend() { return ($this->bShowWeekend == true); } - - /** - * Determine if the lesson strings in the booking table are shown - * @return bool - */ - public function isShowLessons() { return ($this->bShowLessons == true); } - - /** - * Determine if the current user has admin rights. This function tests - * if the user is in a group which has the privilege of admin rights. - * @todo test - * @return bool - */ - public function userIsAdmin() { - return secure_privilege("mod_roomreservation_admin"); - } - - /** - * Determine if the current user can book rooms. This function tests - * if the user is in a group which has the privilege to book rooms. - * If no group has this privilege, all users can book. - * @todo test - * @return bool - */ - public function userCanBook() { - if(!rrPrivilegeAssigned("mod_roomreservation_book")) { - // If the privilege is not assigned to any group, all users can book - return true; - } else { - // If user is admin, it makes sense that he can book rooms ;-) - return secure_privilege("mod_roomreservation_book") || - secure_privilege("mod_roomreservation_admin"); - } - } - - /** - * Determine if the current user can view bookings. This function tests - * if the user is in a group which has been configured as a group who - * can view bookings. If no groups are configured, any user can view the - * bookings table. - * @todo test - * @return bool - */ - public function userCanView() { - if(!rrPrivilegeAssigned("mod_roomreservation_view")) { - // If the privilege is not assigned to any group, all users can view - return true; - } else { - // If user is admin or can book, it makes sense that he can view bookings - return secure_privilege("mod_roomreservation_admin") || - secure_privilege("mod_roomreservation_book") || - secure_privilege("mod_roomreservation_view"); - } - } - - /** - * Get the messages that have been produced - * @return string - */ - public function getMessages() { - return join("\n", $this->asMessages); - } - - /***************************************************************************/ - /** - * @} - * @name Operations - * @{ - */ - - /** - * Write the current state of this instance to the config file. - * @throws IOException - * @return bool - */ - public function writeConfig() { - // Open config file - $hFile = fopen("mod_room-reservation/config.inc", "w", true); - if(!is_resource($hFile)) { - throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE); - return false; - } - // Try to lock file repeatedly - for($n = 0; !flock($hFile, LOCK_EX); $n++) { - if($n > 10) { - throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE); - return false; // Give up - } else { - sleep(0.2); // Retry after 100 ms - } - } - - // Create text for config file - $strFile = MOD_ROOM_RESERVATION_CONFIGFILE_HEADER; - - // Timeslices - $strFile .= "\$this->flushTimeslices();\n"; - foreach($this->getTimeslices() as $oTs) { - $strFile .= sprintf("\$this->addTimeslice(new ". - "mod_roomReservationTimeslice(%d, %d));\n", $oTs->getBegin(), - $oTs->getEnd()); - } - - // Show weekend - $strFile .= sprintf("\$this->setShowWeekend(%s);\n", - $this->isShowWeekend() ? "true" : "false"); - - // Show lessons - $strFile .= sprintf("\$this->setShowLessons(%s);\n", - $this->isShowLessons() ? "true" : "false"); - - $strFile .= "?>"; - - // Write to config file and unlock it - if(fwrite($hFile, $strFile) == false) { - throw new IOException(MOD_ROOM_RESERVATION_ERROR_WRITE_FILE); - return false; - } - if(!flock($hFile, LOCK_UN)) { - throw new IOException(MOD_ROOM_RESERVATION_ERROR_UNLOCK_FILE); - return false; - } - - rrInsertLog("Konfiguration verändert"); - return true; - } - - /** - * Read configuration from file. Returns false if an error occured, - * in this case getMessages() contains error messages. - * @return bool - */ - public function readConfig() { - global $g_rmCfg; - try { - require("mod_room-reservation/config.inc"); - } catch(Exception $e) { - $this->addMessage($e->getMessage()); - } - } - - /** @} */ -} -?> diff --git a/includes/mod_roomReservationConfigPage.inc b/includes/mod_roomReservationConfigPage.inc deleted file mode 100644 index 46ce45a..0000000 --- a/includes/mod_roomReservationConfigPage.inc +++ /dev/null @@ -1,179 +0,0 @@ -otlb = new mod_roomReservationTimesliceListBox($this->oCfg); - $this->orwlb = new mod_roomReservationRoomWhitelistListBox($this->oCfg); - $this->setTitle(_c("room-reservation:Configuration")); - $this->setIcon("mod_room-reservation_config"); - } - - public function processRequestVariables() { - // default values - $this->bPostShowWeekend = $this->oCfg->isShowWeekend(); - $this->bPostShowLessons = $this->oCfg->isShowLessons(); - - if(isset($_POST["mod_roomReservationConfigPage"])) { - if(isset($_POST["mod_roomReservationConfigPage"]["showweekend"])) { - $this->bPostShowWeekend = - ($_POST["mod_roomReservationConfigPage"]["showweekend"] == true); - } - - if(isset($_POST["mod_roomReservationConfigPage"]["showlessons"])) { - $this->bPostShowLessons = - ($_POST["mod_roomReservationConfigPage"]["showlessons"] == true); - } - - // process the request - if(isset($_POST["mod_roomReservationConfigPage"]["submit"])) { - $this->oCfg->setShowWeekend($this->bPostShowWeekend); - $this->oCfg->setShowLessons($this->bPostShowLessons); - try { - $this->oCfg->writeConfig(); - } catch(Exception $e) { - $this->asMessages[] = $e->getMessage(); - } - } - } - } - - public function doShow() { - // error messages - if(count($this->asMessages) > 0) { - printf("

%s

", nl2br(q(join("\n", $this->asMessages)))); - } - - // first column - echo "". - "
\n"; - - GroupBox(_c("room-reservation:Available rooms"), "host"); - printf("

%s

", _c("room-reservation:The following rooms are ". - "available for booking:")); - echo "
"; - $this->orwlb->show(); - echo "
\n"; - _GroupBox(); - - GroupBox(_("Privileges"), "keys"); - $asAdminGroups = rrPrivilegedGroups("mod_roomreservation_admin"); - $asBookGroups = rrPrivilegedGroups("mod_roomreservation_book"); - $asViewGroups = rrPrivilegedGroups("mod_roomreservation_view"); - echo sprintf("

%s

\n

%s

\n

%s

", _c("room-reservation:This is ". - "a short summary of the privileges related to the room reservation ". - "schedulde and the groups which have them assigned."), - sprintf(_c("room-reservation:If one of these privileges is not assigned to ". - "any group, all users on this server are allowed to perform the specified ". - "action. Please use the %sgroup administration%s to assign and revoke ". - "privileges."), "", ""), - _c("room-reservation:Please note that every group with the booking ". - "privilege can also implicitly view the booking table and every group with ". - "the administration privilege can also implicitly book and view the ". - "booking table.")); - echo "

\n"; - echo sprintf("\n", icon("keys"), - _("View the booking table").":", $asViewGroups == array() ? - _c("room-reservation:all users") : icon("act-group") . join(", ", - array_map("getGroupName", $asViewGroups))); - echo "\n"; - echo sprintf("\n", icon("keys"), - _("Book rooms").":", $asBookGroups == array() ? - _c("room-reservation:all users") : icon("act-group") . join(", ", - array_map("getGroupName", $asBookGroups))); - echo "\n"; - echo sprintf("\n", icon("keys"), - _("Administration of the room reservation schedule"), - $asAdminGroups == array() ? _c("room-reservation:all users") : - icon("act-group") . join(", ", array_map("getGroupName", $asAdminGroups))); - echo "
%s%s%s
%s%s%s
%s%s%s

\n"; - _GroupBox(); - - // second column - echo "
\n"; - - GroupBox(_c("room-reservation:Periods"), "mod_room-reservation_timeslice"); - printf("

%s

", _c("room-reservation:Here you can fill in the ". - "periods where bookings can be undertaken. A booking period can ". - "e. g. correspond to a lesson.")); - echo "
"; - $this->otlb->show(); - echo "
\n"; - _GroupBox(); - - GroupBox(_c("room-reservation:Further options"), "manage"); - printf("
". - "\n", $_SERVER["PHP_SELF"]); - - printf("\n"; - printf("", $GLOBALS["stdbtn"], _("OK")); - echo "
<%s name='mod_roomReservationConfigPage". - "[showweekend]' id='mod_roomReservationConfigPageShowWeekend' ". - "value='1' %s />
". - "%s

", - $GLOBALS["smlchk"], $this->bPostShowWeekend ? "checked='checked' " : "", - _c("room-reservation:Show weekend"), _c("room-reservation:If ". - "selected, the weekdays saturday and sunday are also shown in the ". - "booking table.")); - - echo "
<%s name='mod_roomReservationConfigPage". - "[showlessons]' id='mod_roomReservationConfigPageShowLessons' ". - "value='1'%s />
". - "%s

", - $GLOBALS["smlchk"], $this->bPostShowLessons ? "checked='checked' " : "", - _c("room-reservation:Show „lesson” texts"), - _c("room-reservation:Check this box to show texts like ". - "„nth lesson&rdquo in the booking page. If this box is ". - "unchecked, none of these texts are shown.")); - - printf("
<%s name='mod_roomReservationConfigPage". - "[submit]' value='%s' />
\n"; - _GroupBox(); - - echo "
\n"; - } -} -?> \ No newline at end of file diff --git a/includes/mod_roomReservationControl.inc b/includes/mod_roomReservationControl.inc deleted file mode 100644 index 4d364c5..0000000 --- a/includes/mod_roomReservationControl.inc +++ /dev/null @@ -1,114 +0,0 @@ -oCfg = $oCfg; - $this->processRequestVariables(); - } - - /***************************************************************************/ - /** - * @} - * @name Initialization - * @{ - */ - - /** - * Process the REQUEST variables and preset the some variables - * @return void - */ - protected function processRequestVariables() { } - - /***************************************************************************/ - /** - * @} - * @name Output - * @{ - */ - - /** - * Get the messages that have been produced. Returns HTML. - * @return string - */ - protected function getMessages() { - if(count($this->asMessages) > 0) { - return sprintf("
%s

\n", - nl2br(q(join("\n", $this->asMessages)))); - } - } - - /** - * Show the beginning of the control. - * @return void - */ - protected function beginShow() { } - - /** - * Show the control. Override this function to print your HTML code. - * @return void - */ - protected abstract function doShow(); - - /** - * Show the end of the control. - * @return void - */ - protected function endShow() { } - - /** - * Show the full control. You don't need to override this function. Instead, - * override doShow(). - * @return void - */ - public function show() { - $this->beginShow(); - $this->doShow(); - $this->endShow(); - } -} -?> \ No newline at end of file diff --git a/includes/mod_roomReservationPage.inc b/includes/mod_roomReservationPage.inc deleted file mode 100644 index 6d0e018..0000000 --- a/includes/mod_roomReservationPage.inc +++ /dev/null @@ -1,150 +0,0 @@ -oCfg = $oCfg; - - $this->processRequestVariables(); - } - - /***************************************************************************/ - /** - * @} - * @name Initialization - * @{ - */ - - /** - * Process the REQUEST variables and preset the some variables. Override - * this function to process GET and POST parameters. - * @return void - */ - protected function processRequestVariables() { } - - /***************************************************************************/ - /** - * @} - * @name Access to attributes - * @{ - */ - - /** - * Set the page title - * @param $str (string) - * @return void - */ - public function setTitle($str) { $this->strTitle = $str; } - - /** - * Set the title icon - * @param $str (string) - * @return void - */ - public function setIcon($str) { $this->strIcon = $str; } - - /** - * Get the page title - * @return string - */ - public function getTitle() { return $this->strTitle; } - - /** - * Get the title icon - * @return string - */ - public function getIcon() { return $this->strIcon; } - - /***************************************************************************/ - /** - * @} - * @name Output - * @{ - */ - - /** - * Show the beginning of the page. - * @return void - */ - protected function beginShow() { - html_header("\n"); - PageBlue(q($this->getTitle()), $this->getIcon()); - - // print error messages from the configuration - if($s = trim($this->oCfg->getMessages()) != "") { - printf("

%s

\n", nl2br(q($s))); - } - } - - /** - * Show the beginning of the page. Override this function to print your - * HTML code. - * @return void - */ - protected abstract function doShow(); - - /** - * Show the end of the page. - * @return void - */ - protected function endShow() { - _PageBlue(); - } - - /** - * Show the full page. You don't need to override this function. Instead, - * override doShow(). - * @return void - */ - public function show() { - $this->beginShow(); - $this->doShow(); - $this->endShow(); - } - -} -?> \ No newline at end of file diff --git a/includes/mod_roomReservationRoom.inc b/includes/mod_roomReservationRoom.inc deleted file mode 100644 index 64ed6cb..0000000 --- a/includes/mod_roomReservationRoom.inc +++ /dev/null @@ -1,143 +0,0 @@ -setName($strName); - $this->setRoomNo($strRoomNo); - $this->setFloor($strFloor); - $this->setBuilding($strBuilding); - $this->setLocation($strLocation); - } - - /***************************************************************************/ - /** - * @} - * @name Access to attributes - * @{ - */ - - /** - * Set the name of the room - * @param $str (string) - * @return void - */ - public function setName($str) { $this->strName = $str; } - - /** - * Set the room number - * @param $str (string) - * @return void - */ - public function setRoomNo($str) { $this->strRoomNo = $str; } - - /** - * Set the floor - * @param $str (string) - * @return void - */ - public function setFloor($str) { $this->strFloor = $str; } - - /** - * Set the building - * @param $str (string) - * @return void - */ - public function setBuilding($str) { $this->strBuilding = $str; } - - /** - * Set the location - * @param $str (string) - * @return void - */ - public function setLocation($str) { $this->strLocation = $str; } - - /** - * Get the name of the room - * @return string - */ - public function getName() { return $this->strName; } - - /** - * Get the room number - * @return string - */ - public function geRoomNo() { return $this->strRoomNo; } - - /** - * Get the floor - * @return string - */ - public function getFloor() { return $this->strFloor; } - - /** - * Get the building - * @return string - */ - public function getBuilding() { return $this->strBuilding; } - - /** - * Get the location - * @return string - */ - public function getLocation() { return $this->strLocation; } - - /** - * Conversion to string - * @return string - */ - public function __toString() { return $this->getName(); /* name is key */ } - /**@}*/ -} -?> diff --git a/includes/mod_roomReservationRoomWhitelistListBox.inc b/includes/mod_roomReservationRoomWhitelistListBox.inc deleted file mode 100644 index 27bd6c2..0000000 --- a/includes/mod_roomReservationRoomWhitelistListBox.inc +++ /dev/null @@ -1,214 +0,0 @@ -cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_SHOW; - - // POST data - if(isset($_POST["mod_roomReservationRoomWhitelistListBox"])) { - $aPost = $_POST["mod_roomReservationRoomWhitelistListBox"]; - // mode - if(isset($aPost["action"])) { - if(isset($aPost["action"]["add"])) { - if($aPost["action"]["add"] == _("Add")) { - $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_ADD; - } elseif($aPost["action"]["add"] == _("OK")) { - $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITADD; - } - } elseif(isset($aPost["action"]["delete"])) { - if($aPost["action"]["delete"] == _("Delete")) { - $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_DELETE; - } elseif($aPost["action"]["delete"] == _("OK")) { - $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITDELETE; - } - } - } - // selection - if(isset($aPost["l"])) { - foreach($aPost["l"] as $nOid => $bChecked) { - if($bChecked) { - $this->anPostSelection[] = $nOid; - } - } - } - } - - // process the forms - if($this->cMode == MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITADD) { - $h = db_query("SELECT name FROM rooms WHERE oid IN ". - qdb_arr($this->anPostSelection)); - while($a = pg_fetch_array($h)) { - $this->oCfg->whitelistRoom($a["name"]); - } - } - - if($this->cMode == MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITDELETE) { - $h = db_query("SELECT name FROM rooms WHERE oid IN ". - qdb_arr($this->anPostSelection)); - while($a = pg_fetch_array($h)) { - $this->oCfg->unWhitelistRoom($a["name"]); - } - } - } - - protected function doShow() { - echo "
"; - TreeView(array(_("Room"))); - switch($this->cMode) { - case MOD_ROOM_RESERVATION_RWLB_ACTION_DELETE: - $this->showDeleteForm(); - break; - case MOD_ROOM_RESERVATION_RWLB_ACTION_ADD: $this->showAddForm(); break; - default: - case MOD_ROOM_RESERVATION_RWLB_ACTION_SHOW: $this->showForm(); break; - } - _TreeView(); - echo "
\n"; - } - - /** - * Print the form if not delete nor add was requested - * @return void - */ - protected function showForm() { - $aoRooms = $this->oCfg->getWhitelistedRooms(); - // only show add button if there are still some unlisted rooms - if(count(mod_roomReservationRoomsManager::getRooms()) > count($aoRooms)) { - TreeViewLine(sprintf("<%s name='mod_roomReservationRoomWhitelistListBox". - "[action][add]' value='%s' />", $GLOBALS["stdbtn"], _("Add"))); - } - $this->showList($aoRooms); - // toolbar - printf("", $GLOBALS["treeview_cols"]); - CheckCombo(); - printf("<%s name='mod_roomReservationRoomWhitelistListBox[action]". - "[delete]' value='%s' />", $GLOBALS["stdbtn"], _("Delete")); - echo "\n"; - } - - /** - * Print the addition form - * @return void - */ - protected function showAddForm() { - // only list rooms that are not already whitelisted - $aoRooms = array_diff(mod_roomReservationRoomsManager::getRooms(), - $this->oCfg->getWhitelistedRooms()); - TreeViewSubtitle(_("Add")); - $this->showList($aoRooms); - TreeViewLine(sprintf("

<%s name='mod_roomReservationRoomWhitelistList". - "Box[action][add]' value='%s' /> <%s name='mod_roomReservationRoom". - "WhitelistListBox[action][add]' value='%s' />

", $GLOBALS["stdbtn"], - _("OK"), $GLOBALS["stdbtn"], _("Cancel"))); - } - - /** - * Show the deletion form - * @return void - */ - protected function showDeleteForm() { - // list rooms in selection - $aoRooms = array(); - $h = db_query("SELECT name FROM rooms WHERE oid IN ". - qdb_arr($this->anPostSelection)); - foreach($this->anPostSelection as $nOid) { - $aoRooms[] = mod_roomReservationRoomsManager::getRoomByOid($nOid); - } - TreeViewSubtitle(sprintf(_("Following %s will be deleted"), - _c("room-reservation:rooms"))); - $this->showList($aoRooms, false); - TreeViewLine(sprintf("

<%s name='mod_roomReservationRoomWhitelistList". - "Box[action][delete]' value='%s' /> <%s name='mod_roomReservationRoom". - "WhitelistListBox[action][delete]' value='%s' />

", - $GLOBALS["stdbtn"], _("OK"), $GLOBALS["stdbtn"], _("Cancel"))); - } - - /** - * Show the list items - * @param $aoRooms (array of mod_roomReservationRoom objects) List items - * @param $bCheckboxes (bool) Whether to show checkboxes - */ - protected function showList($aoRooms, $bCheckboxes = true) { - if(count($aoRooms) > 0) { - foreach($aoRooms as $o) { - // fetch oid from SQL table - $nOid = pg_fetch_result(db_query("SELECT oid FROM ". - "rooms WHERE name = $1", $o->getName()), 0, "oid"); - $sBox = $bCheckboxes ? sprintf("<%s id='box%d' name='mod_room". - "ReservationRoomWhitelistListBox[l][%d]' value='1'%s />", $GLOBALS["smlchk"], $nOid, $nOid, - @$this->anPostSelection[$nOid] ? " checked='checked'" : "", $nOid, - icon("host"), $o->getName()) : - sprintf("%s%s", $nOid, icon("host"), - $o->getName()); - TreeViewLine($sBox); - } - } else { - TreeViewEmpty(); - } - } -} -?> \ No newline at end of file diff --git a/includes/mod_roomReservationRoomsManager.inc b/includes/mod_roomReservationRoomsManager.inc deleted file mode 100644 index edce221..0000000 --- a/includes/mod_roomReservationRoomsManager.inc +++ /dev/null @@ -1,165 +0,0 @@ -oCfg = $oCfg; - } - - /** - * Insert or update a room in the database - * param $or (rsRoom) Room to store in the database - * return (int) The UID of the booking, or -1 in case of a failure. - * Call getLastError() to get more information about the error. - */ -/** public function write(rsRoom $or) { - // Only administrators are allowed to create and alter rooms - if(!$this->oCfg->userIsAdmin()) { - // TODO throw exception - setLastError(RS_ERROR_ACCESS_DENIED); - return -1; - } - - $strWhere = ""; - $strLog = ""; - - // Update or insert? - if($or->getUid() == null) { - // No UID yet, insert new room - $strLog = sprintf("Raum „%s“ angelegt", $or->getName()); - } else { - $strWhere = "rsr_uid = ".qp(intval($or->getUid())); - $strLog = sprintf("Raum „%s“ geändert", $or->getName()); - } - - $aPut["rsr_name"] = $or->getName(); - db_store("rooms", $aPut, $strWhere == "" ? null : $strWhere); - - $hQuery = db_query("SELECT currval('roomschedule_rooms_rsr_uid_seq') ". - "FROM roomschedule_rooms;"); - $nNewUid = pg_fetch_result($hQuery, 0, "currval"); - - rrInsertLog($strLog); - - // Return new UID - return $nNewUid; - } - */ - /** - * Delete a room from the database - * param $nUid (int) Unique ID of the room - * return (bool) true on success, otherwise false. - * Call getLastError() to get more information about the error. - */ -/* public function delete($nUid) { - // Only administrators are allowed to delete rooms - if(!$this->oCfg->userIsAdmin()) { - // TODO throw exception - setLastError(RS_ERROR_ACCESS_DENIED); - return false; - } - // Delete it from the database - $strRoom = $this->getRoomName($nUid); - if(!db_query("DELETE FROM roomschedule_rooms WHERE rsr_uid = $1;", - intval($nUid))) { - // TODO throw exception - setLastError(RS_ERROR_SQL); - return false; - } else { - rsInsertLog(sprintf("Raum „%s“ gelöscht", $strRoom)); - return true; - } - } -*/ - - /** - * Get a room by its OID. Returns null if the room was not found. - * @param $nOid (integer) The OID of the room - * @return mod_roomReservationRoom - */ - static function getRoomByOid($nOid) { - $o = null; - $h = db_query("SELECT * FROM rooms WHERE oid = $1;", $nOid); - if(pg_num_rows($h) > 0) { - $arResult = pg_fetch_array($h); - $o = new mod_roomReservationRoom($arResult["name"], - $arResult["room_no"], $arResult["floor"], $arResult["building"], - $arResult["location"]); - } - return $o; - } - - /** - * Get a room by its name. Returns null if the room was not found. - * @param $sName (string) The name of the room - * @return mod_roomReservationRoom - */ - static function getRoomByName($sName) { - $o = null; - $h = db_query("SELECT * FROM rooms WHERE name = $1;", $sName); - if(pg_num_rows($h) > 0) { - $arResult = pg_fetch_array($h); - $o = new mod_roomReservationRoom($arResult["name"], - $arResult["room_no"], $arResult["floor"], $arResult["building"], - $arResult["location"]); - } - return $o; - } - - /** - * Get all rooms from the database - * @return array of mod_roomReservationRoom - */ - static function getRooms() { - $aoReturn = array(); - $hQuery = db_query("SELECT * FROM rooms ORDER BY name;"); - while($arResult = pg_fetch_array($hQuery)) { - $aoReturn[] = new mod_roomReservationRoom($arResult["name"], - $arResult["room_no"], $arResult["floor"], $arResult["building"], - $arResult["location"]); - } - return $aoReturn; - } -} -?> diff --git a/includes/mod_roomReservationTimeslice.inc b/includes/mod_roomReservationTimeslice.inc deleted file mode 100644 index f6bd997..0000000 --- a/includes/mod_roomReservationTimeslice.inc +++ /dev/null @@ -1,102 +0,0 @@ -setBegin($tsBegin); - $this->setEnd($tsEnd); - } - - /***************************************************************************/ - /** - * @} - * @name Access to attributes - * @{ - */ - - /** - * Set the beginning. Only the time part is used, the date part is ignored. - * If the timestamp is invalid, an Exception is thrown. - * @param $ts (timestamp) - * @throws Exception - */ - public function setBegin($ts) { - if(intval($ts) >= $this->getEnd()) { - throw new Exception(MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN); - } else { - $this->tsBegin = (intval($ts) % 86400); - } - } - - /** - * Set the ending. Only the time part is used, the date part is ignored. - * If the timestamp is invalid, an Exception is thrown. - * @param $ts (timestamp) - * @throws Exception - */ - public function setEnd($ts) { - if($this->getBegin() >= intval($ts)) { - throw new Exception(MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN); - } else { - $this->tsEnd = (intval($ts) % 86400); - } - } - - /** - * Get the beginning. - * @return timestamp - */ - public function getBegin() { return $this->tsBegin; } - - /** - * Get the ending. - * @return timestamp - */ - public function getEnd() { return $this->tsEnd; } - - /** @} */ -} -?> diff --git a/includes/mod_roomReservationTimesliceListBox.inc b/includes/mod_roomReservationTimesliceListBox.inc deleted file mode 100644 index 6928a33..0000000 --- a/includes/mod_roomReservationTimesliceListBox.inc +++ /dev/null @@ -1,264 +0,0 @@ -setAction((($ga == "add") ? MOD_ROOM_RESERVATION_TLB_ACTION_ADD : - (($ga == "delete") ? MOD_ROOM_RESERVATION_TLB_ACTION_DELETE : - MOD_ROOM_RESERVATION_TLB_ACTION_SHOW))); - } - - // new beginning and new ending - $this->setNewBegin(isset( - $_GET["mod_roomReservationTimesliceListBox"]["begin"]) ? - $_GET["mod_roomReservationTimesliceListBox"]["begin"] : ""); - $this->setNewEnd(isset( - $_GET["mod_roomReservationTimesliceListBox"]["end"]) ? - $_GET["mod_roomReservationTimesliceListBox"]["end"] : ""); - } - - // perform the requested action - if($this->getAction() == MOD_ROOM_RESERVATION_TLB_ACTION_ADD) { - // add a timeslice to the configuration file - - $bErrors = false; - - // Note: we want to handle the timestamps in GMT format, hence the "+0000" - if(strtotime($this->getNewBegin()." +0000") === false) { - $this->asMessages[] = _c("room-reservation:The beginning time is ". - "invalid."); - $bErrors = true; - } - if(strtotime($this->getNewEnd()." +0000") === false) { - $this->asMessages[] = _c("room-reservation:The ending time is ". - "invalid."); - $bErrors = true; - } - - if(!$bErrors) { - try { - $this->oCfg->addTimeslice(new mod_roomReservationTimeslice( - strtotime($this->getNewBegin()." +0000") % 86400, - strtotime($this->getNewEnd()." +0000") % 86400)); - $this->oCfg->writeConfig(); - $this->setNewBegin(""); - $this->setNewEnd(""); - } catch(Exception $e) { - $this->asMessages[] = $e->getMessage(); - } - } - - } elseif($this->getAction() == MOD_ROOM_RESERVATION_TLB_ACTION_DELETE) { - // delete a timeslice from the configuration file - if(isset($_POST["mod_roomReservationTimesliceListBox"])) { - if(isset($_POST["mod_roomReservationTimesliceListBox"]["l"])) { - $ao = $this->oCfg->getTimeslices(); - foreach($_POST["mod_roomReservationTimesliceListBox"]["l"] as - $n => $b) { - if($b) { - $this->oCfg->deleteTimeslice(new mod_roomReservationTimeslice( - $ao[$n]->getBegin(), $ao[$n]->getEnd())); - } - } - $this->oCfg->writeConfig(); - } - } - } - } - - /***************************************************************************/ - /** - * @} - * @name Access to attributes - * @{ - */ - - /** - * Set the action to be done (GET data) - * @param $c (constant) Action. See @ref timeslicelistbox_actions. - */ - protected function setAction($c) { $this->cAction = intval($c); } - - /** - * Set the beginning for a new timeslice (GET data) - * @param $s (string) - */ - protected function setNewBegin($s) { $this->sNewBegin = $s; } - - /** - * Set the beginning for a new timeslice (GET data) - * @param $s (string) - */ - protected function setNewEnd($s) { $this->sNewEnd = $s; } - - /** - * Get the action to be done (GET data). See @ref timeslicelistbox_actions. - * @return constant - */ - function getAction() { return $this->cAction; } - - /** - * Get the beginning for a new timeslice (GET data) - * @return string - */ - public function getNewBegin() { return $this->sNewBegin; } - - /** - * Get the beginning for a new timeslice (GET data) - * @return string - */ - public function getNewEnd() { return $this->sNewEnd; } - - /***************************************************************************/ - /** - * @} - * @name Output - * @{ - */ - - /** - * Actually show the control - * @return void - */ - public function doShow() { - TreeView(array(_c("room-reservation:Begin"), _c("room-reservation:End"))); - - // addition form - printf("
"); - hidden("mod_roomReservationTimesliceListBox[action]", "add"); - TreeViewTitle(_("Add")); - - $sMessages = $this->getMessages(); - if(trim($sMessages) != "") { - TreeViewLine($sMessages); - } - - TreeViewLine(array(sprintf("<%s name='mod_roomReservationTimesliceListBox". - "[begin]' value='%s' size='8'/>", $GLOBALS["stdedt"], - $this->getNewBegin()), sprintf("<%s name='". - "mod_roomReservationTimesliceListBox[end]' value='%s' size='8'/> <%s ". - "name='mod_roomReservationTimesliceListBox[submit]' value='%s' />", - $GLOBALS["stdedt"], $this->getNewEnd(), $GLOBALS["stdbtn"], _("Add")))); - echo "
\n"; - - // deletion form - TreeViewTitle(_c("room-reservation:Periods")); - $aoTs = $this->oCfg->getTimeslices(); - if(count($aoTs) > 0) { - echo "
"; - $i = 0; - foreach($aoTs as $oTs) { - $sBox = sprintf("". - "<%s name='mod_roomReservationTimesliceListBox[l][%d]' value='1' />", - $i, $GLOBALS["smlchk"], $i); - // Note: we have only GMT timestamps in the timeslice objects - TreeViewLine(array($sBox . gmstrftime(_("%#I:%M %p"), $oTs->getBegin()), - gmstrftime(_("%#I:%M %p"), $oTs->getEnd()))); - $i++; - } - // toolbar - printf("", $GLOBALS["treeview_cols"]); - CheckCombo(); - printf("<%s name='mod_roomReservationTimesliceListBox[submit]' ". - "value='%s' />", $GLOBALS["stdbtn"], _("Delete")); - echo "\n"; - } else { - TreeViewEmpty(); - } - - echo "
\n"; - _TreeView(); - } - /** @} */ -} -?> \ No newline at end of file