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)
8 * Copyright © 2007 Roland Hieber
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 require_once("ctrl.inc");
30 require_once("mod_room-reservation/mod_roomReservationControl.inc");
31 require_once("mod_room-reservation/mod_roomReservationTimeslice.inc");
33 /*****************************************************************************/
35 * @page timeslicelistbox_actions Actions of a
36 * mod_roomReservationTimesliceListBox instance
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.
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);
51 /** @todo document, add a delete confirmation */
52 class mod_roomReservationTimesliceListBox extends mod_roomReservationControl {
55 * (constant) The action to be done (GET data).
56 * See @ref timeslicelistbox_actions.
59 /** (string) The beginning for a new timeslice (GET data) */
61 /** (string) The ending for a new timeslice (GET data) */
64 /***************************************************************************/
69 * @param $oCfg (reference to mod_roomReservationConfig) Reference to the
71 * @return mod_roomReservationTimesliceListBox
73 public function __construct(mod_roomReservationConfig &$oCfg) {
74 parent::__construct($oCfg);
77 /***************************************************************************/
80 * @name Initialization
85 * Process the REQUEST variables and preset the some variables
88 protected function processRequestVariables() {
90 if(isset($_GET["mod_roomReservationTimesliceListBox"])) {
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)));
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"] : "");
108 // perform the requested action
109 if($this->getAction() == MOD_ROOM_RESERVATION_TLB_ACTION_ADD) {
110 // add a timeslice to the configuration file
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 ".
120 if(strtotime($this->getNewEnd()." +0000") === false) {
121 $this->asMessages[] = _c("room-reservation:The ending time is ".
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();
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
147 $this->oCfg->deleteTimeslice(new mod_roomReservationTimeslice(
148 $ao[$n]->getBegin(), $ao[$n]->getEnd()));
151 $this->oCfg->writeConfig();
157 /***************************************************************************/
160 * @name Access to attributes
165 * Set the action to be done (GET data)
166 * @param $c (constant) Action. See @ref timeslicelistbox_actions.
168 protected function setAction($c) { $this->cAction = intval($c); }
171 * Set the beginning for a new timeslice (GET data)
174 protected function setNewBegin($s) { $this->sNewBegin = $s; }
177 * Set the beginning for a new timeslice (GET data)
180 protected function setNewEnd($s) { $this->sNewEnd = $s; }
183 * Get the action to be done (GET data). See @ref timeslicelistbox_actions.
186 function getAction() { return $this->cAction; }
189 * Get the beginning for a new timeslice (GET data)
192 public function getNewBegin() { return $this->sNewBegin; }
195 * Get the beginning for a new timeslice (GET data)
198 public function getNewEnd() { return $this->sNewEnd; }
200 /***************************************************************************/
208 * Actually show the control
211 public function doShow() {
212 TreeView(array(_c("room-reservation:Begin"), _c("room-reservation:End")));
215 printf("<form method='get'>");
216 hidden("mod_roomReservationTimesliceListBox[action]", "add");
217 TreeViewTitle(_("Add"));
219 $sMessages = $this->getMessages();
220 if(trim($sMessages) != "") {
221 TreeViewLine($sMessages);
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"))));
233 TreeViewTitle(_c("room-reservation:Periods"));
234 $aoTs = $this->oCfg->getTimeslices();
235 if(count($aoTs) > 0) {
236 echo "<form action='?mod_roomReservationTimesliceListBox[action]=delete' ".
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())));
250 printf("<tr><td class='tbbtm' colspan='%d'>", $GLOBALS["treeview_cols"]);
252 printf("<%s name='mod_roomReservationTimesliceListBox[submit]' ".
253 "value='%s' />", $GLOBALS["stdbtn"], _("Delete"));