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_roomReservationTimeslice.inc");
32 /*****************************************************************************/
34 * @page timeslicelistbox_actions Actions of a
35 * mod_roomReservationTimesliceListBox instance
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.
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);
50 /** @todo document, add a delete confirmation */
51 class mod_roomReservationTimesliceListBox /* extends mclControl */ {
53 /** (mod_roomReservationConfig) Reference to the configuration object */
56 * (constant) The action to be done (GET data).
57 * See @ref timeslicelistbox_actions.
60 /** (string) The beginning for a new timeslice (GET data) */
62 /** (string) The ending for a new timeslice (GET data) */
64 /** (array of strings) Errors that occur while processing the form */
65 protected $asFormErrors;
67 /***************************************************************************/
72 * @param $oCfg (reference to mod_roomReservationConfig) Reference to the
74 * @return mod_roomReservationBookingTable
76 public function __construct(mod_roomReservationConfig &$oCfg) {
79 $this->processRequestVariables();
82 /***************************************************************************/
85 * @name Initialization
90 * Process the REQUEST variables and preset the some variables
93 protected function processRequestVariables() {
95 if(isset($_GET["mod_roomReservationTimesliceListBox"])) {
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)));
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"] : "");
113 // perform the requested action
114 if($this->getAction() == MOD_ROOM_RESERVATION_TLB_ACTION_ADD) {
115 // add a timeslice to the configuration file
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 ".
125 if(strtotime($this->getNewEnd()." +0000") === false) {
126 $this->asFormErrors[] = _c("room-reservation:The ending time is ".
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();
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
152 $this->oCfg->deleteTimeslice(new mod_roomReservationTimeslice(
153 $ao[$n]->getBegin(), $ao[$n]->getEnd()));
156 $this->oCfg->writeConfig();
162 /***************************************************************************/
165 * @name Access to attributes
170 * Set the action to be done (GET data)
171 * @param $c (constant) Action. See @ref timeslicelistbox_actions.
173 protected function setAction($c) { $this->cAction = intval($c); }
176 * Set the beginning for a new timeslice (GET data)
179 protected function setNewBegin($s) { $this->sNewBegin = $s; }
182 * Set the beginning for a new timeslice (GET data)
185 protected function setNewEnd($s) { $this->sNewEnd = $s; }
188 * Get the action to be done (GET data). See @ref timeslicelistbox_actions.
191 function getAction() { return $this->cAction; }
194 * Get the beginning for a new timeslice (GET data)
197 public function getNewBegin() { return $this->sNewBegin; }
200 * Get the beginning for a new timeslice (GET data)
203 public function getNewEnd() { return $this->sNewEnd; }
205 /***************************************************************************/
216 public function show() {
217 TreeView(array(_c("room-reservation:Begin"), _c("room-reservation:End")));
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))))));
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"))));
236 TreeViewTitle(_c("room-reservation:Periods"));
237 $aoTs = $this->oCfg->getTimeslices();
238 if(count($aoTs) > 0) {
239 echo "<form action='?mod_roomReservationTimesliceListBox[action]=delete' ".
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())));
253 printf("<tr><td class='tbbtm' colspan='%d'>", $GLOBALS["treeview_cols"]);
255 printf("<%s name='mod_roomReservationTimesliceListBox[submit]' ".
256 "value='%s' />", $GLOBALS["stdbtn"], _("Delete"));