config.inc is now touched by the Makefile for standard values, removed .hgignore
[iserv-mod-room-reservation.git] / includes / mod_roomReservationTimesliceListBox.inc
1 <?php
2 /**
3 * @file mod_roomReservationTimesliceListBox.inc
4 * A list box that allows the user to add and delete timeslices
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 23.06.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("ctrl.inc");
30 require_once("mod_room-reservation/mod_roomReservationTimeslice.inc");
31
32 /*****************************************************************************/
33 /**
34 * @page timeslicelistbox_actions Actions of a
35 * mod_roomReservationTimesliceListBox instance
36 * @{
37 * The following constants describe the actions that a
38 * mod_roomReservationTimesliceListBox instance can handle. They are used in
39 * processRequestVariables() to determine the action that should be done when
40 * the control is shown.
41 */
42 /** Show the control (default action) */
43 define("MOD_ROOM_RESERVATION_TLB_ACTION_SHOW", 0);
44 /** Add a timeslice */
45 define("MOD_ROOM_RESERVATION_TLB_ACTION_ADD", 1);
46 /** Delete a timeslice */
47 define("MOD_ROOM_RESERVATION_TLB_ACTION_DELETE", 2);
48 /** @} */
49
50 /** @todo document, add a delete confirmation */
51 class mod_roomReservationTimesliceListBox /* extends mclControl */ {
52
53 /** (mod_roomReservationConfig) Reference to the configuration object */
54 protected $oCfg;
55 /**
56 * (constant) The action to be done (GET data).
57 * See @ref timeslicelistbox_actions.
58 */
59 protected $cAction;
60 /** (string) The beginning for a new timeslice (GET data) */
61 protected $sNewBegin;
62 /** (string) The ending for a new timeslice (GET data) */
63 protected $sNewEnd;
64 /** (array of strings) Errors that occur while processing the form */
65 protected $asFormErrors;
66
67 /***************************************************************************/
68 /**
69 * @name Constructor
70 * @{
71 * Constructor
72 * @param $oCfg (reference to mod_roomReservationConfig) Reference to the
73 * configuration
74 * @return mod_roomReservationBookingTable
75 */
76 public function __construct(mod_roomReservationConfig &$oCfg) {
77 $this->oCfg = $oCfg;
78
79 $this->processRequestVariables();
80 }
81
82 /***************************************************************************/
83 /**
84 * @}
85 * @name Initialization
86 * @{
87 */
88
89 /**
90 * Process the REQUEST variables and preset the some variables
91 * @return void
92 */
93 protected function processRequestVariables() {
94
95 if(isset($_GET["mod_roomReservationTimesliceListBox"])) {
96 // action
97 if(isset($_GET["mod_roomReservationTimesliceListBox"]["action"])) {
98 $ga = $_GET["mod_roomReservationTimesliceListBox"]["action"];
99 $this->setAction((($ga == "add") ? MOD_ROOM_RESERVATION_TLB_ACTION_ADD :
100 (($ga == "delete") ? MOD_ROOM_RESERVATION_TLB_ACTION_DELETE :
101 MOD_ROOM_RESERVATION_TLB_ACTION_SHOW)));
102 }
103
104 // new beginning and new ending
105 $this->setNewBegin(isset(
106 $_GET["mod_roomReservationTimesliceListBox"]["begin"]) ?
107 $_GET["mod_roomReservationTimesliceListBox"]["begin"] : "");
108 $this->setNewEnd(isset(
109 $_GET["mod_roomReservationTimesliceListBox"]["end"]) ?
110 $_GET["mod_roomReservationTimesliceListBox"]["end"] : "");
111 }
112
113 // perform the requested action
114 if($this->getAction() == MOD_ROOM_RESERVATION_TLB_ACTION_ADD) {
115 // add a timeslice to the configuration file
116
117 $bErrors = false;
118
119 // Note: we want to handle the timestamps in GMT format, hence the "+0000"
120 if(strtotime($this->getNewBegin()." +0000") === false) {
121 $this->asFormErrors[] = _c("room-reservation:The beginning time is ".
122 "invalid.");
123 $bErrors = true;
124 }
125 if(strtotime($this->getNewEnd()." +0000") === false) {
126 $this->asFormErrors[] = _c("room-reservation:The ending time is ".
127 "invalid.");
128 $bErrors = true;
129 }
130
131 if(!$bErrors) {
132 try {
133 $this->oCfg->addTimeslice(new mod_roomReservationTimeslice(
134 strtotime($this->getNewBegin()." +0000") % 86400,
135 strtotime($this->getNewEnd()." +0000") % 86400));
136 $this->oCfg->writeConfig();
137 $this->setNewBegin("");
138 $this->setNewEnd("");
139 } catch(Exception $e) {
140 $this->asFormErrors[] = $e->getMessage();
141 }
142 }
143
144 } elseif($this->getAction() == MOD_ROOM_RESERVATION_TLB_ACTION_DELETE) {
145 // delete a timeslice from the configuration file
146 if(isset($_POST["mod_roomReservationTimesliceListBox"])) {
147 if(isset($_POST["mod_roomReservationTimesliceListBox"]["l"])) {
148 $ao = $this->oCfg->getTimeslices();
149 foreach($_POST["mod_roomReservationTimesliceListBox"]["l"] as
150 $n => $b) {
151 if($b) {
152 $this->oCfg->deleteTimeslice(new mod_roomReservationTimeslice(
153 $ao[$n]->getBegin(), $ao[$n]->getEnd()));
154 }
155 }
156 $this->oCfg->writeConfig();
157 }
158 }
159 }
160 }
161
162 /***************************************************************************/
163 /**
164 * @}
165 * @name Access to attributes
166 * @{
167 */
168
169 /**
170 * Set the action to be done (GET data)
171 * @param $c (constant) Action. See @ref timeslicelistbox_actions.
172 */
173 protected function setAction($c) { $this->cAction = intval($c); }
174
175 /**
176 * Set the beginning for a new timeslice (GET data)
177 * @param $s (string)
178 */
179 protected function setNewBegin($s) { $this->sNewBegin = $s; }
180
181 /**
182 * Set the beginning for a new timeslice (GET data)
183 * @param $s (string)
184 */
185 protected function setNewEnd($s) { $this->sNewEnd = $s; }
186
187 /**
188 * Get the action to be done (GET data). See @ref timeslicelistbox_actions.
189 * @return constant
190 */
191 function getAction() { return $this->cAction; }
192
193 /**
194 * Get the beginning for a new timeslice (GET data)
195 * @return string
196 */
197 public function getNewBegin() { return $this->sNewBegin; }
198
199 /**
200 * Get the beginning for a new timeslice (GET data)
201 * @return string
202 */
203 public function getNewEnd() { return $this->sNewEnd; }
204
205 /***************************************************************************/
206 /**
207 * @}
208 * @name Output
209 * @{
210 */
211
212 /**
213 * Show the control
214 * @return void
215 */
216 public function show() {
217 TreeView(array(_c("room-reservation:Begin"), _c("room-reservation:End")));
218
219 // addition form
220 printf("<form method='get'>");
221 hidden("mod_roomReservationTimesliceListBox[action]", "add");
222 TreeViewTitle(_("Add"));
223 if(count($this->asFormErrors) > 0) {
224 TreeViewLine(sprintf("<div class='err' style='color:red;'>%s</div>",
225 nl2br(q(trim(join("\n", $this->asFormErrors))))));
226 }
227 TreeViewLine(array(sprintf("<%s name='mod_roomReservationTimesliceListBox".
228 "[begin]' value='%s' size='8'/>", $GLOBALS["stdedt"],
229 $this->getNewBegin()), sprintf("<%s name='".
230 "mod_roomReservationTimesliceListBox[end]' value='%s' size='8'/> <%s ".
231 "name='mod_roomReservationTimesliceListBox[submit]' value='%s' />",
232 $GLOBALS["stdedt"], $this->getNewEnd(), $GLOBALS["stdbtn"], _("Add"))));
233 echo "</form>\n";
234
235 // deletion form
236 TreeViewTitle(_c("room-reservation:Periods"));
237 $aoTs = $this->oCfg->getTimeslices();
238 if(count($aoTs) > 0) {
239 echo "<form action='?mod_roomReservationTimesliceListBox[action]=delete' ".
240 "method='post'>";
241 $i = 0;
242 foreach($aoTs as $oTs) {
243 $sBox = sprintf("<input type='hidden' ".
244 "name='mod_roomReservationTimesliceListBox[l][%d]' value='0' />".
245 "<%s name='mod_roomReservationTimesliceListBox[l][%d]' value='1' />",
246 $i, $GLOBALS["smlchk"], $i);
247 // Note: we have only GMT timestamps in the timeslice objects
248 TreeViewLine(array($sBox . gmstrftime(_("%#I:%M %p"), $oTs->getBegin()),
249 gmstrftime(_("%#I:%M %p"), $oTs->getEnd())));
250 $i++;
251 }
252 // toolbar
253 printf("<tr><td class='tbbtm' colspan='%d'>", $GLOBALS["treeview_cols"]);
254 CheckCombo();
255 printf("<%s name='mod_roomReservationTimesliceListBox[submit]' ".
256 "value='%s' />", $GLOBALS["stdbtn"], _("Delete"));
257 echo "</td></tr>\n";
258 } else {
259 TreeViewEmpty();
260 }
261
262 echo "</form>\n";
263 _TreeView();
264 }
265 /** @} */
266 }
267 ?>
This page took 0.070411 seconds and 5 git commands to generate.