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