ca915c269026ab0e4a73d83f9d72fd988c030345
[iserv-mod-room-reservation.git] / inc / functions.inc
1 <?php
2 /**
3 * @file functions.inc
4 * additional functions for iserv-mod-room-reservation
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 22.12.2007
7 *
8 * Copyright © 2007 Roland Hieber
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * THE SOFTWARE.
27 */
28
29 require_once("share.inc");
30 require_once("db.inc");
31 require_once("user.inc");
32 require_once("sec/secure.inc");
33
34 db_user("roomreservation");
35
36 /**
37 * @page errorcodes Error Codes
38 * @{
39 */
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 ".
65 "give a reason."));
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."));
75 /** The room is not available for booking */
76 define("MOD_ROOM_RESERVATION_ERROR_ROOM_NOT_WHITELISTED",
77 _c("room-reservation:This room is not available for booking."));
78 /**
79 * @}
80 */
81
82 /**
83 * Determine if the specified user exists. Throws an SQLException if an error
84 * occured.
85 * @param $strAct (string) Account name of the user
86 * @throws SQLException
87 * @return (bool)
88 */
89 function isAct($strAct) {
90 $hQuery = db_query("SELECT * FROM users WHERE act = $1;", $strAct);
91 if(!is_resource($hQuery)) {
92 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
93 return null;
94 }
95 return (pg_num_rows($hQuery) > 0);
96 }
97
98 /**
99 * Get the real user name for an account name. If the function fails, it throws
100 * an Exception.
101 * @param $strAct (string) Account name of the user to look up
102 * @return (string) The real name of the user.
103 * @throws Exception
104 */
105 function getRealUserName($strAct) {
106 $hQuery = db_query("SELECT firstname, lastname FROM users WHERE act = $1;",
107 $strAct);
108 if(!is_resource($hQuery)) {
109 throw new Exception(RS_ERROR_SQL);
110 }
111 if(pg_num_rows($hQuery) == 0) {
112 return $strAct; // User not found in database, return account name
113 }
114 $arResult = pg_fetch_array($hQuery);
115 return user_join_name($arResult);
116 }
117
118 /**
119 * Determine if a specified group exists. If the function fails, it throws an
120 * Exception.
121 * @param $strAct (string) Account name of the group
122 * @return bool
123 * @throws Exception
124 */
125 function isGroup($strAct) {
126 $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct);
127 if(!is_resource($hQuery)) {
128 throw new Exception(RS_ERROR_SQL);
129 }
130 return (pg_num_rows($hQuery) > 0);
131 }
132
133 /**
134 * Look up the name of a group. If the function fails, it throws an Exception.
135 * @param $strAct (string) Account name of the group
136 * @return (string) The name of the group.
137 */
138 function getGroupName($strAct) {
139 $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct);
140 if(!is_resource($hQuery)) {
141 throw new Exception(RS_ERROR_SQL);
142 }
143 if(pg_num_rows($hQuery) == 0) {
144 return $strAct; // Group not found in database, return account name
145 }
146 $arResult = pg_fetch_array($hQuery);
147 return $arResult["name"];
148 }
149
150 /**
151 * Create a link to write a mail to the specified account name. Returns a link
152 * if the specified account exists, otherwise it returns the account name.
153 * @param $strAct (string) Account name
154 * @param $strColor (string) Background color for icon()
155 * @param $strParams (string) additional URL parameters
156 * @return string
157 */
158 function mailToUserLink($strAct, $strColor = "bl", $strParams = "") {
159 if(!isAct($strAct)) {
160 return $strAct;
161 }
162 return popup(relroot("msg/write/?to=".user_mail_addr($strAct).$strParams),
163 600, 400, nobr(icon("mail-reply-usr", array("size" => 16, "bg" =>
164 $strColor)) . getRealUserName($strAct)));
165 }
166
167 /**
168 * Determine if a specified string is a valid mail address
169 * @param $strAddr string
170 * @return string
171 */
172 function isMailAddress($strAddr) {
173 return ((preg_match("/([a-zA-Z0-9_\-\.]*(@[a-zA-Z0-9\-\.]*)?(\s*,\s*)?)+/",
174 $strAddr) > 0) && (preg_match("/(\s*,\s*)$/", $strAddr) == 0));
175 }
176
177 /**
178 * Module-specific logging function. Prefixes the log message with a
179 * module-specific string and writes it to the logs.
180 * @param $strLog (string) Log message
181 * @return void
182 */
183 function rrInsertLog($strLog) {
184 log_insert($strLog, null, "Room Reservation Schedule");
185 }
186
187 /**
188 * Get the SQL day number from a given timestamp.
189 * @param $ts (timestamp)
190 * @return (int) 0-6 for Sunday to Monday
191 */
192 function rrDateToSQLDayNumber($ts) {
193 $aDays = array("Sun" => 0, "Mon" => 1, "Tue" => 2,
194 "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6);
195 return $aDays[date("D", $ts)];
196 }
197
198 /**
199 * Convert a UNIX timestamp to an SQL date string
200 * @param $ts (timestamp)
201 * @return string
202 */
203 function dateToSql($ts) { return date("Y\-m\-d", intval($ts)); }
204
205 /**
206 * Calculate the timestamp of the monday in the current week
207 * @param $ts (timestamp) Calculate the value relative to this date
208 * @return timestamp
209 */
210 function rrGetMonday($ts = null) {
211 if($ts === null) {
212 $ts = time();
213 }
214 if(date("D", $ts) == "Mon") {
215 // Today is monday
216 return strtotime("00:00", $ts);
217 } else {
218 return strtotime("last monday", $ts);
219 }
220 }
221
222 /** (array of strings) Additional CSS rules */
223 $GLOBALS["rrLocalCss"] = array();
224
225 /**
226 * Add CSS rules to the page
227 * @param $s (string)
228 * @return void
229 */
230 function rrAddCss($s) {
231 rrDebug("rrAddCss: add \"$s\"");
232 $GLOBALS["rrLocalCss"][] = $s;
233 }
234
235 /**
236 * Get CSS rules that have been added with rrAddCss()
237 * @return string
238 */
239 function rrGetCss() {
240 rrDebug("rrGetCss: Local CSS is ".var_export($GLOBALS["rrLocalCss"], true));
241 return implode("\n", $GLOBALS["rrLocalCss"]);
242 }
243
244 function rrDebug($s, $bReturn = false) {
245 if(isset($_GET["debug"])) {
246 if(!$bReturn) {
247 echo "<!-- $s -->\n";
248 } else {
249 return $s;
250 }
251 }
252 }
253
254 /**
255 * sprintf with support for ordinal numbers.
256 * This version of sprintf replaces all substrings of the type <tt>/\\d+#/</tt>
257 * (i.e. a decimal number with a hash sign appended) in the input string with
258 * the corresponding english ordinal number prefices (st, nd, rd, th).
259 * @param $str (string) Input string
260 * @param $args (mixed) Additional parameters to be passed to sprintf()
261 * @return (string)
262 */
263 function _sprintf_ord($str, $args /*leave this parameters for doxygen*/) {
264 $args = func_get_args();
265 if(preg_match_all("/%[bcdufosxX]/", $args[0], $temp) != func_num_args()-1) {
266 trigger_error("Too few arguments", E_USER_ERROR);
267 return false;
268 }
269 $str = call_user_func_array("sprintf", $args);
270 while(preg_match("/(.*)(\d+)#(.*)/", $str, $m))
271 $str = $m[1]._(append_ord_suffix($m[2])).$m[3];
272 return $str;
273 }
274 ?>
This page took 0.070125 seconds and 3 git commands to generate.