ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBookingScheduleGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
14 {
19  function __construct($a_parent_obj)
20  {
21  $this->ref_id = $a_parent_obj->ref_id;
22  $this->slots_no = $a_parent_obj->object->getNumberOfSlots();
23  }
24 
28  function executeCommand()
29  {
30  global $tpl, $ilTabs, $ilCtrl;
31 
32  $next_class = $ilCtrl->getNextClass($this);
33 
34  switch($next_class)
35  {
36  default:
37  $cmd = $ilCtrl->getCmd("render");
38  $this->$cmd();
39  break;
40  }
41  return true;
42  }
43 
49  function render()
50  {
51  global $tpl, $lng, $ilCtrl, $ilAccess;
52 
53  if ($ilAccess->checkAccess('write', '', $this->ref_id))
54  {
55  include_once 'Services/UIComponent/Toolbar/classes/class.ilToolbarGUI.php';
56  $bar = new ilToolbarGUI;
57  $bar->addButton($lng->txt('book_add_schedule'), $ilCtrl->getLinkTarget($this, 'create'));
58  $bar = $bar->getHTML();
59  }
60 
61  include_once 'Modules/BookingManager/classes/class.ilBookingSchedulesTableGUI.php';
62  $table = new ilBookingSchedulesTableGUI($this, 'render', $this->ref_id);
63  $tpl->setContent($bar.$table->getHTML());
64  }
65 
69  function create()
70  {
71  global $tpl, $ilCtrl, $ilTabs, $lng;
72 
73  $ilTabs->clearTargets();
74  $ilTabs->setBackTarget($lng->txt('book_back_to_list'), $ilCtrl->getLinkTarget($this, 'render'));
75 
76  $form = $this->initForm();
77  $tpl->setContent($form->getHTML());
78  }
79 
83  function edit()
84  {
85  global $tpl, $ilCtrl, $ilTabs, $lng;
86 
87  $ilTabs->clearTargets();
88  $ilTabs->setBackTarget($lng->txt('book_back_to_list'), $ilCtrl->getLinkTarget($this, 'render'));
89 
90  $form = $this->initForm('edit', (int)$_GET['schedule_id']);
91  $tpl->setContent($form->getHTML());
92  }
93 
100  function initForm($a_mode = "create", $id = NULL)
101  {
102  global $lng, $ilCtrl;
103 
104  $lng->loadLanguageModule("dateplaner");
105 
106  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
107 
108  $form_gui = new ilPropertyFormGUI();
109 
110  $title = new ilTextInputGUI($lng->txt("title"), "title");
111  $title->setRequired(true);
112  $title->setSize(40);
113  $title->setMaxLength(120);
114  $form_gui->addItem($title);
115 
116  /*
117  $type = new ilRadioGroupInputGUI($lng->txt("book_schedule_type"), "type");
118  $type->setRequired(true);
119  $form_gui->addItem($type);
120  $fix = new ilRadioOption($lng->txt("book_schedule_type_fix"), "fix");
121  $fix->setInfo($lng->txt("book_schedule_type_fix_info"));
122  $type->addOption($fix);
123  $flex = new ilRadioOption($lng->txt("book_schedule_type_flexible"), "flexible");
124  $flex->setInfo($lng->txt("book_schedule_type_flexible_info"));
125  $type->addOption($flex);
126 
127  $raster = new ilNumberInputGUI($lng->txt("book_schedule_raster"), "raster");
128  $raster->setRequired(true);
129  $raster->setInfo($lng->txt("book_schedule_raster_info"));
130  $raster->setMinValue(1);
131  $raster->setSize(3);
132  $raster->setMaxLength(3);
133  $raster->setSuffix($lng->txt("book_minutes"));
134  $flex->addSubItem($raster);
135 
136  $rent_min = new ilNumberInputGUI($lng->txt("book_schedule_rent_min"), "rent_min");
137  $rent_min->setInfo($lng->txt("book_schedule_rent_info"));
138  $rent_min->setMinValue(1);
139  $rent_min->setSize(3);
140  $rent_min->setMaxLength(3);
141  $flex->addSubItem($rent_min);
142 
143  $rent_max = new ilNumberInputGUI($lng->txt("book_schedule_rent_max"), "rent_max");
144  $rent_max->setInfo($lng->txt("book_schedule_rent_info"));
145  $rent_max->setMinValue(1);
146  $rent_max->setSize(3);
147  $rent_max->setMaxLength(3);
148  $flex->addSubItem($rent_max);
149 
150  $break = new ilNumberInputGUI($lng->txt("book_schedule_break"), "break");
151  $break->setInfo($lng->txt("book_schedule_break_info"));
152  $break->setMinValue(1);
153  $break->setSize(3);
154  $break->setMaxLength(3);
155  $flex->addSubItem($break);
156  */
157 
158  $definition = new ilCheckboxGroupInputGUI($lng->txt("book_schedule_days"), "days");
159  $definition->setInfo($lng->txt("book_schedule_days_info"));
160  $definition->setRequired(true);
161  $form_gui->addItem($definition);
162 
163  $days = array('mo', 'tu', 'we', 'th', 'fr', 'sa', 'su');
164  foreach($days as $day_id)
165  {
166  $day = new ilCheckboxOption($lng->txt(ucfirst($day_id)."_long"), $day_id);
167  $definition->addOption($day);
168 
169  for($loop = 1; $loop <= $this->slots_no; $loop++)
170  {
171  $hours[$day_id][$loop] = new ilTextInputGUI($lng->txt("book_schedule_slot")." ".$loop, $day_id."_slot[]");
172  $hours[$day_id][$loop]->setSize(14);
173  $hours[$day_id][$loop]->setMaxLength(14);
174  $day->addSubItem($hours[$day_id][$loop]);
175  }
176  }
177 
178  $deadline = new ilNumberInputGUI($lng->txt("book_deadline"), "deadline");
179  $deadline->setInfo($lng->txt("book_deadline_info"));
180  $deadline->setSuffix($lng->txt("book_hours"));
181  $deadline->setMinValue(0);
182  $deadline->setSize(3);
183  $deadline->setMaxLength(3);
184  $form_gui->addItem($deadline);
185 
186  if ($a_mode == "edit")
187  {
188  $form_gui->setTitle($lng->txt("book_edit_schedule"));
189 
190  $item = new ilHiddenInputGUI('schedule_id');
191  $item->setValue($id);
192  $form_gui->addItem($item);
193 
194  include_once 'Modules/BookingManager/classes/class.ilBookingSchedule.php';
195  $schedule = new ilBookingSchedule($id);
196  $title->setValue($schedule->getTitle());
197  $deadline->setValue($schedule->getDeadline());
198 
199  /*
200  if($schedule->getRaster())
201  {
202  $type->setValue("flexible");
203  $raster->setValue($schedule->getRaster());
204  $rent_min->setValue($schedule->getMinRental());
205  $rent_max->setValue($schedule->getMaxRental());
206  $break->setValue($schedule->getAutoBreak());
207  }
208  else
209  {
210  $type->setValue("fix");
211  }
212  */
213 
214  $def = $schedule->getDefinition();
215  $definition->setValue(array_keys($def));
216  foreach($def as $day_id => $slots)
217  {
218  foreach($slots as $idx => $slot)
219  {
220  if(isset($hours[$day_id][$idx+1]))
221  {
222  $hours[$day_id][$idx+1]->setValue($slot);
223  }
224  }
225  }
226 
227  $form_gui->addCommandButton("update", $lng->txt("save"));
228  }
229  else
230  {
231  $form_gui->setTitle($lng->txt("book_add_schedule"));
232  $form_gui->addCommandButton("save", $lng->txt("save"));
233  $form_gui->addCommandButton("render", $lng->txt("cancel"));
234  }
235  $form_gui->setFormAction($ilCtrl->getFormAction($this));
236 
237  return $form_gui;
238  }
239 
243  function save()
244  {
245  global $tpl, $lng;
246 
247  $form = $this->initForm();
248  if($form->checkInput())
249  {
250  include_once 'Modules/BookingManager/classes/class.ilBookingSchedule.php';
251  $obj = new ilBookingSchedule;
252  $this->formToObject($form, $obj);
253  $obj->save();
254 
255  ilUtil::sendSuccess($lng->txt("book_schedule_added"));
256  $this->render();
257  }
258  else
259  {
260  $form->setValuesByPost();
261  $this->setDefinitionFromPost($form);
262  $tpl->setContent($form->getHTML());
263  }
264  }
265 
269  function update()
270  {
271  global $tpl, $lng;
272 
273  $form = $this->initForm('edit', (int)$_POST['schedule_id']);
274  if($form->checkInput())
275  {
276  include_once 'Modules/BookingManager/classes/class.ilBookingSchedule.php';
277  $obj = new ilBookingSchedule((int)$_POST['schedule_id']);
278  $this->formToObject($form, $obj);
279  $obj->update();
280 
281  ilUtil::sendSuccess($lng->txt("book_schedule_updated"));
282  $this->render();
283  }
284  else
285  {
286  $form->setValuesByPost();
287  $this->setDefinitionFromPost($form);
288  $tpl->setContent($form->getHTML());
289  }
290  }
291 
297  protected function setDefinitionFromPost(ilPropertyFormGUI $form)
298  {
299  $days = $form->getInput("days");
300  if($days)
301  {
302  $days_group = $form->getItemByPostVar("days");
303  foreach($days_group->getOptions() as $option)
304  {
305  $days_fields[$option->getValue()] = $option;
306  }
307 
308  foreach($days as $day)
309  {
310  $slot = $form->getInput($day."_slot");
311  $subs = $days_fields[$day]->getSubItems();
312  if($slot[0])
313  {
314  $subs[0]->setValue($slot[0]);
315  }
316  if($slot[1])
317  {
318  $subs[1]->setValue($slot[1]);
319  }
320  }
321  }
322  }
323 
329  protected function formToObject($form, $schedule)
330  {
331  global $ilObjDataCache;
332 
333  $schedule->setTitle($form->getInput("title"));
334  $schedule->setPoolId($ilObjDataCache->lookupObjId($this->ref_id));
335  $schedule->setDeadline($form->getInput("deadline"));
336 
337  /*
338  if($form->getInput("type") == "flexible")
339  {
340  $schedule->setRaster($form->getInput("raster"));
341  $schedule->setMinRental($form->getInput("rent_min"));
342  $schedule->setMaxRental($form->getInput("rent_max"));
343  $schedule->setAutoBreak($form->getInput("break"));
344  }
345  else
346  {
347  $schedule->setRaster(NULL);
348  $schedule->setMinRental(NULL);
349  $schedule->setMaxRental(NULL);
350  $schedule->setAutoBreak(NULL);
351  }
352  */
353 
354  $definition = array();
355  foreach($form->getInput("days") as $day_id)
356  {
357  $day_slots = array();
358  foreach($form->getInput($day_id."_slot") as $slot)
359  {
360  if(trim($slot))
361  {
362  $fromto = explode("-", $slot);
363  if(sizeof($fromto) == 2)
364  {
365  $from = $this->parseTime($fromto[0]);
366  $to = $this->parseTime($fromto[1]);
367  if($from && $to)
368  {
369  $definition[$day_id][] = $from."-".$to;
370  }
371  }
372  }
373  }
374  if(!sizeof($definition[$day_id]))
375  {
376  $definition[$day_id] = array("00:00-23:59");
377  }
378  }
379 
380  // slots may not overlap
381  foreach($definition as $day_id => $slots)
382  {
383  $old = 0;
384  sort($slots);
385  foreach($slots as $idx => $slot)
386  {
387  $slot = explode('-', $slot);
388  $from = $slot[0];
389  $to = $slot[1];
390  if($from < $old)
391  {
392  $from = $old;
393  $slots[$idx] = $from.'-'.$to;
394  if($to < $from)
395  {
396  unset($slots[$idx]);
397  }
398  }
399  $old = $to;
400  }
401  $definition[$day_id] = $slots;
402  }
403 
404  $schedule->setDefinition($definition);
405  }
406 
411  protected function parseTime($raw)
412  {
413  $raw = strtolower(trim($raw));
414  $am = $pm = false;
415  $min = 0;
416  if(substr($raw, -2) == 'pm')
417  {
418  $pm = true;
419  $raw = substr($raw, 0, -2);
420  }
421  else if(substr($raw, -2) == 'am')
422  {
423  $am = true;
424  $raw = substr($raw, 0, -2);
425  }
426  if($colon = strpos($raw, ':'))
427  {
428  $min = (int)substr($raw, $colon+1);
429  $raw = substr($raw, 0, $colon);
430  }
431  $hours = (int)$raw;
432  if(!$min)
433  {
434  $min = "0";
435  }
436  if($pm && $hours < 12)
437  {
438  $hours += 12;
439  }
440  else if($am && $hours == 12)
441  {
442  $hours -= 12;
443  }
444  if($hours > 23 || $min > 59)
445  {
446  return false;
447  }
448  return str_pad($hours, 2, "0", STR_PAD_LEFT).":".
449  str_pad($min, 2, "0", STR_PAD_LEFT);
450  }
451 
455  function confirmDelete()
456  {
457  global $ilCtrl, $lng, $tpl, $ilTabs;
458 
459  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
460  $conf = new ilConfirmationGUI();
461  $conf->setFormAction($ilCtrl->getFormAction($this));
462  $conf->setHeaderText($lng->txt('book_confirm_delete'));
463 
464  include_once 'Modules/BookingManager/classes/class.ilBookingSchedule.php';
465  $type = new ilBookingSchedule((int)$_GET['schedule_id']);
466  $conf->addItem('schedule_id', (int)$_GET['schedule_id'], $type->getTitle());
467  $conf->setConfirm($lng->txt('delete'), 'delete');
468  $conf->setCancel($lng->txt('cancel'), 'render');
469 
470  $tpl->setContent($conf->getHTML());
471  }
472 
476  function delete()
477  {
478  global $ilCtrl, $lng;
479 
480  include_once 'Modules/BookingManager/classes/class.ilBookingSchedule.php';
481  $obj = new ilBookingSchedule((int)$_POST['schedule_id']);
482  $obj->delete();
483 
484  ilUtil::sendSuccess($lng->txt('book_schedule_deleted'), true);
485  $ilCtrl->redirect($this, 'render');
486  }
487 }
488 
489 ?>