471266643282cf41616fcf6b91407db1deb54fc7
[iserv-mod-room-reservation.git] / inc / mod_roomReservationRoomWhitelistListBox.inc
1 <?php
2 /**
3 * @file mod_roomReservationRoomWhitelistListBox.inc
4 * List box that shows the rooms who can be booked
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 24.07.2008
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("mod_roomReservationControl.inc");
30 require_once("mod_roomReservationRoomsManager.inc");
31
32 /*****************************************************************************/
33 /**
34 * @page roomwhitelistlistbox_actions Actions of a
35 * mod_roomReservationRoomWhitelistListBox instance
36 * @{
37 * The following constants describe the actions that a
38 * mod_roomReservationRoomWhitelistListBox instance can handle. They are used
39 * in processRequestVariables() to determine the action that should be done
40 * when the control is shown.
41 */
42 /** Show the control (default action) */
43 define("MOD_ROOM_RESERVATION_RWLB_ACTION_SHOW", 0);
44 /** Show the addition form */
45 define("MOD_ROOM_RESERVATION_RWLB_ACTION_ADD", 1);
46 /** Process the addition form */
47 define("MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITADD", 2);
48 /** Show the deletion form */
49 define("MOD_ROOM_RESERVATION_RWLB_ACTION_DELETE", 3);
50 /** Process the deletion form */
51 define("MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITDELETE", 4);
52 /** @} */
53
54 /** @todo document */
55 class mod_roomReservationRoomWhitelistListBox
56 extends mod_roomReservationControl {
57 /**
58 * (array of integers) OIDs of the rows in the rooms table that were
59 * selected (POST data)
60 */
61 protected $anPostSelection = array();
62 /** (constant) Display mode, see @ref roomwhitelistlistbox_actions */
63 protected $cMode;
64
65 protected function processRequestVariables() {
66 // default values
67 $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_SHOW;
68
69 // POST data
70 if(isset($_POST["mod_roomReservationRoomWhitelistListBox"])) {
71 $aPost = $_POST["mod_roomReservationRoomWhitelistListBox"];
72 // mode
73 if(isset($aPost["action"])) {
74 if(isset($aPost["action"]["add"])) {
75 if($aPost["action"]["add"] == _("Add")) {
76 $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_ADD;
77 } elseif($aPost["action"]["add"] == _("OK")) {
78 $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITADD;
79 }
80 } elseif(isset($aPost["action"]["delete"])) {
81 if($aPost["action"]["delete"] == _("Delete")) {
82 $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_DELETE;
83 } elseif($aPost["action"]["delete"] == _("OK")) {
84 $this->cMode = MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITDELETE;
85 }
86 }
87 }
88 // selection
89 if(isset($aPost["l"])) {
90 foreach($aPost["l"] as $nOid => $bChecked) {
91 if($bChecked) {
92 $this->anPostSelection[] = $nOid;
93 }
94 }
95 }
96 }
97
98 // process the forms
99 if($this->cMode == MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITADD) {
100 $h = db_query("SELECT name FROM rooms WHERE oid IN ".
101 qdb_arr($this->anPostSelection));
102 while($a = pg_fetch_array($h)) {
103 $this->oCfg->whitelistRoom($a["name"]);
104 }
105 }
106
107 if($this->cMode == MOD_ROOM_RESERVATION_RWLB_ACTION_SUBMITDELETE) {
108 $h = db_query("SELECT name FROM rooms WHERE oid IN ".
109 qdb_arr($this->anPostSelection));
110 while($a = pg_fetch_array($h)) {
111 $this->oCfg->unWhitelistRoom($a["name"]);
112 }
113 }
114 }
115
116 protected function doShow() {
117 echo "<form method='post'>";
118 TreeView(array(_("Room")));
119 switch($this->cMode) {
120 case MOD_ROOM_RESERVATION_RWLB_ACTION_DELETE:
121 $this->showDeleteForm();
122 break;
123 case MOD_ROOM_RESERVATION_RWLB_ACTION_ADD: $this->showAddForm(); break;
124 default:
125 case MOD_ROOM_RESERVATION_RWLB_ACTION_SHOW: $this->showForm(); break;
126 }
127 _TreeView();
128 echo "</form>\n";
129 }
130
131 /**
132 * Print the form if not delete nor add was requested
133 * @return void
134 */
135 protected function showForm() {
136 $aoRooms = $this->oCfg->getWhitelistedRooms();
137 // only show add button if there are still some unlisted rooms
138 if(count(mod_roomReservationRoomsManager::getRooms()) > count($aoRooms)) {
139 TreeViewLine(sprintf("<%s name='mod_roomReservationRoomWhitelistListBox".
140 "[action][add]' value='%s' />", $GLOBALS["stdbtn"], _("Add")));
141 }
142 $this->showList($aoRooms);
143 // toolbar
144 printf("<tr><td class='tbbtm' colspan='%d'>", $GLOBALS["treeview_cols"]);
145 CheckCombo();
146 printf("<%s name='mod_roomReservationRoomWhitelistListBox[action]".
147 "[delete]' value='%s' />", $GLOBALS["stdbtn"], _("Delete"));
148 echo "</td></tr>\n";
149 }
150
151 /**
152 * Print the addition form
153 * @return void
154 */
155 protected function showAddForm() {
156 // only list rooms that are not already whitelisted
157 $aoRooms = array_diff(mod_roomReservationRoomsManager::getRooms(),
158 $this->oCfg->getWhitelistedRooms());
159 TreeViewSubtitle(_("Add"));
160 $this->showList($aoRooms);
161 TreeViewLine(sprintf("<p><%s name='mod_roomReservationRoomWhitelistList".
162 "Box[action][add]' value='%s' /> <%s name='mod_roomReservationRoom".
163 "WhitelistListBox[action][add]' value='%s' /></p>", $GLOBALS["stdbtn"],
164 _("OK"), $GLOBALS["stdbtn"], _("Cancel")));
165 }
166
167 /**
168 * Show the deletion form
169 * @return void
170 */
171 protected function showDeleteForm() {
172 // list rooms in selection
173 $aoRooms = array();
174 $h = db_query("SELECT name FROM rooms WHERE oid IN ".
175 qdb_arr($this->anPostSelection));
176 foreach($this->anPostSelection as $nOid) {
177 $aoRooms[] = mod_roomReservationRoomsManager::getRoomByOid($nOid);
178 }
179 TreeViewSubtitle(sprintf(_("Following %s will be deleted"),
180 _c("room-reservation:rooms")));
181 $this->showList($aoRooms, false);
182 TreeViewLine(sprintf("<p><%s name='mod_roomReservationRoomWhitelistList".
183 "Box[action][delete]' value='%s' /> <%s name='mod_roomReservationRoom".
184 "WhitelistListBox[action][delete]' value='%s' /></p>",
185 $GLOBALS["stdbtn"], _("OK"), $GLOBALS["stdbtn"], _("Cancel")));
186 }
187
188 /**
189 * Show the list items
190 * @param $aoRooms (array of mod_roomReservationRoom objects) List items
191 * @param $bCheckboxes (bool) Whether to show checkboxes
192 */
193 protected function showList($aoRooms, $bCheckboxes = true) {
194 if(count($aoRooms) > 0) {
195 foreach($aoRooms as $o) {
196 // fetch oid from SQL table
197 $nOid = pg_fetch_result(db_query("SELECT oid FROM ".
198 "rooms WHERE name = $1", $o->getName()), 0, "oid");
199 $sBox = $bCheckboxes ? sprintf("<%s id='box%d' name='mod_room".
200 "ReservationRoomWhitelistListBox[l][%d]' value='1'%s /><label ".
201 "for='box%d'>%s%s</label>", $GLOBALS["smlchk"], $nOid, $nOid,
202 @$this->anPostSelection[$nOid] ? " checked='checked'" : "", $nOid,
203 icon("host"), $o->getName()) :
204 sprintf("<input type='hidden' name='mod_roomReservationRoom".
205 "WhitelistListBox[l][%d]' value='1' />%s%s", $nOid, icon("host"),
206 $o->getName());
207 TreeViewLine($sBox);
208 }
209 } else {
210 TreeViewEmpty();
211 }
212 }
213 }
214 ?>
This page took 0.055266 seconds and 3 git commands to generate.