4 * additional functions for iserv-room-reservation
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
8 * Copyright © 2007 Roland Hieber
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * @todo move some functions into M Class Library
31 require_once("share.inc");
32 require_once("db.inc");
33 require_once("user.inc");
34 require_once("sec/secure.inc");
37 * @page errorcodes Error Codes
40 /** Access denied. This can be due to missing access rights. */
41 define("MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED",
42 _c("room-reservation:Access denied."));
43 /** Error while querying the database */
44 define("MOD_ROOM_RESERVATION_ERROR_SQL",
45 _c("room-reservation:Error while trying to query the database."));
46 /** Error while trying to open a file */
47 define("MOD_ROOM_RESERVATION_ERROR_OPEN_FILE",
48 _c("room-reservation:Could not open file."));
49 /** Error while trying to write a file */
50 define("MOD_ROOM_RESERVATION_ERROR_WRITE_FILE",
51 _c("room-reservation:Could not write to file."));
52 /** Error while trying to lock a file */
53 define("MOD_ROOM_RESERVATION_ERROR_LOCK_FILE",
54 _c("room-reservation:Could not lock file."));
55 /** Error while trying to unlock a file */
56 define("MOD_ROOM_RESERVATION_ERROR_UNLOCK_FILE",
57 _c("room-reservation:Could not unlock file."));
58 /** The current booking overlaps with an existing booking */
59 define("MOD_ROOM_RESERVATION_ERROR_BOOKING_OVERLAPS", _c("room-reservation:".
60 "The current booking overlaps with an existing booking."));
61 /** The booking ends before it begins */
62 define("MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN", _c("room-reservation:".
63 "The ending of your booking cannot lie before the beginning."));
64 define("MOD_ROOM_RESERVATION_ERROR_NO_REASON", _c("room-reservation:Please ".
66 /** A new timeslice overlaps with an existing timeslice */
67 define("MOD_ROOM_RESERVATION_ERROR_CONFIG_OVERLAPPING_TIMESLICE",
68 _c("room-reservation:The period overlaps with an existing one."));
69 /** There is no such timeslice */
70 define("MOD_ROOM_RESERVATION_ERROR_CONFIG_NO_SUCH_TIMESLICE",
71 _c("room-reservation:The specified period does not exist."));
72 /** There is no such account */
73 define("MOD_ROOM_RESERVATION_ERROR_NO_SUCH_ACCOUNT", _c("room-reservation:".
74 "The specified account does not exist."));
80 * Determine if the specified user exists. Throws an SQLException if an error
82 * @param $strAct (string) Account name of the user
83 * @throws SQLException
86 function isAct($strAct) {
87 $hQuery = db_query("SELECT * FROM users WHERE act = $1;", $strAct);
88 if(!is_resource($hQuery)) {
89 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
92 return (pg_num_rows($hQuery) > 0);
96 * Get the real user name for an account name
97 * @param $strAct (string) Account name of the user to look up
98 * @return (string) The real name of the user. If the function fails, it returns <tt>null</tt>.
99 * Call getLastError() to get more information about the error.
101 function getRealUserName($strAct) {
102 $hQuery = db_query("SELECT firstname, lastname FROM users WHERE act = $1;", $strAct);
103 if(!is_resource($hQuery)) {
104 // TODO throw exception
105 setLastError(RS_ERROR_SQL);
108 if(pg_num_rows($hQuery) == 0) {
109 return $strAct; // User not found in database, return account name
111 $arResult = pg_fetch_array($hQuery);
112 return user_join_name($arResult);
116 * Determine if a specified group exists
117 * @param $strAct (string) Account name of the group
118 * @return (bool / null) If the function fails, it returns <tt>null</tt>. Call getLastError() to
119 * get more information about the error.
121 function isGroup($strAct) {
122 $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct);
123 if(!is_resource($hQuery)) {
124 // TODO throw exception
125 setLastError(RS_ERROR_SQL);
128 return (pg_num_rows($hQuery) > 0);
132 * Look up the name of a group
133 * @param $strAct (string) Account name of the group
134 * @return (string) The name of the group. If the function fails, it returns <tt>null</tt>.
135 * Call getLastError() to get more information about the error.
137 function getGroupName($strAct) {
138 $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct);
139 if(!is_resource($hQuery)) {
140 // TODO throw exception
141 setLastError(RS_ERROR_SQL);
144 if(pg_num_rows($hQuery) == 0) {
145 return $strAct; // Group not found in database, return account name
147 $arResult = pg_fetch_array($hQuery);
148 return $arResult["name"];
152 * Create a link to write a mail to the specified account name.
153 * This function returns a link if the specified account exists, otherwise it returns the
155 * @param $strAct (string) Account name
156 * @param $strColor (string) Background color for icon()
157 * @param $strParams (string) additional URL parameters
160 function mailToUserLink($strAct, $strColor = "bl", $strParams = "") {
161 if(!isAct($strAct)) {
164 return popup(relroot("msg/write/?to=".user_mail_addr($strAct).$strParams),
165 600, 400, nobr(icon("mail-reply-usr", array("size" => 16, "bg" =>
166 $strColor)) . getRealUserName($strAct)));
170 * Determine if a specified string is a valid mail address
171 * @param $strAddr string
174 function isMailAddress($strAddr) {
175 return ((preg_match("/([a-zA-Z0-9_\-\.]*(@[a-zA-Z0-9\-\.]*)?(\s*,\s*)?)+/", $strAddr) > 0)
176 and (preg_match("/(\s*,\s*)$/", $strAddr) == 0));
180 * Module-specific logging function.
181 * Prefixes the log message with a module-specific string and writes it to the logs.
182 * @param $strLog (string) Log message
185 function rrInsertLog($strLog) {
186 log_insert($strLog, null, "Room Reservation Schedule");
190 * Get the SQL day number from a given timestamp.
191 * @param $ts (timestamp)
192 * @return (int) 0-6 for Sunday to Monday
194 function rrDateToSQLDayNumber($ts) {
195 $aDays = array("Sun" => 0, "Mon" => 1, "Tue" => 2,
196 "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6);
197 return $aDays[date("D", $ts)];
201 * Convert a UNIX timestamp to an SQL date string
202 * @param $ts (timestamp)
205 function dateToSql($ts) { return date("Y\-m\-d", intval($ts)); }
208 * Calculate the timestamp of the monday in the current week
209 * @param $ts (timestamp) Calculate the value relative to this date
212 function rrGetMonday($ts = null) {
216 if(date("D", $ts) == "Mon") {
218 return strtotime("00:00", $ts);
220 return strtotime("last monday", $ts);
224 /** (array of strings) Additional CSS rules */
225 $rsLocalCss = array();
228 * Add CSS rules to the page
232 function rrAddCss($s) {
233 rrDebug("rsAddCss: add \"$s\"");
234 $GLOBALS["rsLocalCss"][] = $s;
238 * Get CSS rules that have been added with rmAddCss()
241 function rrGetCSS() {
242 rrDebug("rsGetCss: Local CSS is ".var_export($GLOBALS["rsLocalCss"], true));
243 return implode("\n", $GLOBALS["rsLocalCss"]);
246 function rrDebug($s, $bReturn = false) {
247 if(isset($_GET["debug"])) {
249 echo "<!-- $s -->\n";
257 * sprintf with support for ordinal numbers.
258 * This version of sprintf replaces all substrings of the type <tt>/\\d+#/</tt>
259 * (i.e. a decimal number with a hash sign appended) in the input string with
260 * the corresponding english ordinal number prefices (st, nd, rd, th).
261 * @param $str (string) Input string
262 * @param $args (mixed) Additional parameters to be passed to sprintf()
265 function _sprintf_ord($str, $args /*leave this parameters for doxygen*/) {
266 $args = func_get_args();
267 if(preg_match_all("/%[bcdufosxX]/", $args[0], $temp) != func_num_args()-1) {
268 trigger_error("Too few arguments", E_USER_ERROR);
271 $str = call_user_func_array("sprintf", $args);
272 while(preg_match("/(.*)(\d+)#(.*)/", $str, $m))
273 $str = $m[1]._(append_ord_suffix($m[2])).$m[3];