fixed error messages concerning rooms_archive on update to iserv2 (see http://wiki...
[iserv-mod-room-reservation.git] / inc / mod_roomReservationRoomsManager.inc
1 <?php
2 /**
3 * @file mod_roomReservationRoomsManager.inc
4 * Class for the management of rooms
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 28.12.2007
7 *
8 * TODO: move into M Class Library
9 * Copyright © 2007 Roland Hieber
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29
30 require_once("db.inc");
31 require_once("mod_room-reservation/functions.inc");
32 require_once("mod_room-reservation/mod_roomReservationRoom.inc");
33 require_once("mod_room-reservation/mod_roomReservationConfig.inc");
34
35 /** Simple class for creating, editing and deleting rooms */
36 class mod_roomReservationRoomsManager {
37 /** (mod_roomReservationConfig) Reference to the configuration */
38 protected $oCfg;
39
40 /**
41 * Constructor
42 * @param $oCfg (reference to mod_roomReservationConfig) Reference to the
43 * configuration object
44 * @return rmRoomsManager
45 */
46 public function __construct(mod_roomReservationConfig &$oCfg) {
47 $this->oCfg = $oCfg;
48 }
49
50 /**
51 * Insert or update a room in the database
52 * param $or (rsRoom) Room to store in the database
53 * return (int) The UID of the booking, or <tt>-1</tt> in case of a failure.
54 * Call getLastError() to get more information about the error.
55 */
56 /** public function write(rsRoom $or) {
57 // Only administrators are allowed to create and alter rooms
58 if(!$this->oCfg->userIsAdmin()) {
59 // TODO throw exception
60 setLastError(RS_ERROR_ACCESS_DENIED);
61 return -1;
62 }
63
64 $strWhere = "";
65 $strLog = "";
66
67 // Update or insert?
68 if($or->getUid() == null) {
69 // No UID yet, insert new room
70 $strLog = sprintf("Raum „%s“ angelegt", $or->getName());
71 } else {
72 $strWhere = "rsr_uid = ".qp(intval($or->getUid()));
73 $strLog = sprintf("Raum „%s“ geändert", $or->getName());
74 }
75
76 $aPut["rsr_name"] = $or->getName();
77 db_store("rooms", $aPut, $strWhere == "" ? null : $strWhere);
78
79 $hQuery = db_query("SELECT currval('roomschedule_rooms_rsr_uid_seq') ".
80 "FROM roomschedule_rooms;");
81 $nNewUid = pg_fetch_result($hQuery, 0, "currval");
82
83 rrInsertLog($strLog);
84
85 // Return new UID
86 return $nNewUid;
87 }
88 */
89 /**
90 * Delete a room from the database
91 * param $nUid (int) Unique ID of the room
92 * return (bool) <tt>true</tt> on success, otherwise <tt>false</tt>.
93 * Call getLastError() to get more information about the error.
94 */
95 /* public function delete($nUid) {
96 // Only administrators are allowed to delete rooms
97 if(!$this->oCfg->userIsAdmin()) {
98 // TODO throw exception
99 setLastError(RS_ERROR_ACCESS_DENIED);
100 return false;
101 }
102 // Delete it from the database
103 $strRoom = $this->getRoomName($nUid);
104 if(!db_query("DELETE FROM roomschedule_rooms WHERE rsr_uid = $1;",
105 intval($nUid))) {
106 // TODO throw exception
107 setLastError(RS_ERROR_SQL);
108 return false;
109 } else {
110 rsInsertLog(sprintf("Raum „%s“ gelöscht", $strRoom));
111 return true;
112 }
113 }
114 */
115
116 /**
117 * Get a room by its OID. Returns <tt>null</tt> if the room was not found.
118 * @param $nOid (integer) The OID of the room
119 * @return mod_roomReservationRoom
120 */
121 static function getRoomByOid($nOid) {
122 $o = null;
123 $h = db_query("SELECT * FROM rooms WHERE oid = $1;", $nOid);
124 if(pg_num_rows($h) > 0) {
125 $arResult = pg_fetch_array($h);
126 $o = new mod_roomReservationRoom($arResult["name"],
127 $arResult["room_no"], $arResult["floor"], $arResult["building"],
128 $arResult["location"]);
129 }
130 return $o;
131 }
132
133 /**
134 * Get a room by its name. Returns <tt>null</tt> if the room was not found.
135 * @param $sName (string) The name of the room
136 * @return mod_roomReservationRoom
137 */
138 static function getRoomByName($sName) {
139 $o = null;
140 $h = db_query("SELECT * FROM rooms WHERE name = $1;", $sName);
141 if(pg_num_rows($h) > 0) {
142 $arResult = pg_fetch_array($h);
143 $o = new mod_roomReservationRoom($arResult["name"],
144 $arResult["room_no"], $arResult["floor"], $arResult["building"],
145 $arResult["location"]);
146 }
147 return $o;
148 }
149
150 /**
151 * Get all rooms from the database
152 * @return array of mod_roomReservationRoom
153 */
154 static function getRooms() {
155 $aoReturn = array();
156 $hQuery = db_query("SELECT * FROM rooms ORDER BY name;");
157 while($arResult = pg_fetch_array($hQuery)) {
158 $aoReturn[] = new mod_roomReservationRoom($arResult["name"],
159 $arResult["room_no"], $arResult["floor"], $arResult["building"],
160 $arResult["location"]);
161 }
162 return $aoReturn;
163 }
164 }
165 ?>
This page took 0.053224 seconds and 5 git commands to generate.