ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCalendarAppointmentGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
33 include_once('./Services/Calendar/classes/class.ilTimeZone.php');
34 include_once('./Services/Calendar/classes/class.ilDateTime.php');
35 
37 {
38  protected $seed = null;
39  protected $default_fulltime = true;
40 
41  protected $app = null;
42  protected $rec = null;
43  protected $timezone = null;
44 
45  protected $tpl;
46  protected $lng;
47  protected $ctrl;
48 
56  public function __construct(ilDate $seed,$a_appointment_id = 0)
57  {
58  global $ilCtrl,$lng;
59 
60  $this->lng = $lng;
61  $lng->loadLanguageModule('dateplaner');
62  $this->ctrl = $ilCtrl;
63 
64  $this->initTimeZone();
65  $this->initSeed($seed);
66  $this->initAppointment($a_appointment_id);
67  }
68 
76  public function executeCommand()
77  {
78  global $ilUser, $ilSetting,$tpl;
79 
80  $next_class = $this->ctrl->getNextClass($this);
81  switch($next_class)
82  {
83 
84  default:
85  $cmd = $this->ctrl->getCmd("add");
86  $this->$cmd();
87  break;
88  }
89  return true;
90  }
91 
99  protected function cancel()
100  {
101  $this->ctrl->returnToParent($this);
102  }
103 
111  protected function initForm($a_mode)
112  {
113  global $ilUser,$tpl;
114 
115  include_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
116  include_once('./Services/Calendar/classes/class.ilCalendarRecurrenceGUI.php');
117  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
118  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
119  include_once('./Services/Calendar/classes/class.ilCalendarCategory.php');
120 
121  $this->form = new ilPropertyFormGUI();
122 
123  include_once('./Services/YUI/classes/class.ilYuiUtil.php');
125 
126  switch($a_mode)
127  {
128  case 'create':
129  $this->form->setTitle($this->lng->txt('cal_new_app'));
130  $this->ctrl->saveParameter($this,array('seed'));
131  $this->form->setFormAction($this->ctrl->getFormAction($this));
132  $this->form->addCommandButton('save',$this->lng->txt('cal_add_appointment'));
133  $this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
134  break;
135 
136  case 'edit':
137  $this->form->setTitle($this->lng->txt('cal_edit_appointment'));
138  $this->ctrl->saveParameter($this,array('seed','app_id'));
139  $this->form->setFormAction($this->ctrl->getFormAction($this));
140  $this->form->addCommandButton('update',$this->lng->txt('save'));
141  $this->form->addCommandButton('askDelete',$this->lng->txt('delete'));
142  $this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
143  break;
144  }
145  // title
146  $title = new ilTextInputGUI($this->lng->txt('title'),'title');
147  $title->setValue($this->app->getTitle());
148  $title->setRequired(true);
149  $title->setMaxLength(128);
150  $title->setSize(32);
151  $this->form->addItem($title);
152 
153  // calendar selection
154  $calendar = new ilSelectInputGUI($this->lng->txt('cal_category_selection'),'calendar');
155  if($_POST['category'])
156  {
157  $calendar->setValue((int) $_POST['calendar']);
158  }
159  elseif($a_mode == 'edit')
160  {
161  $ass = new ilCalendarCategoryAssignments($this->app->getEntryId());
162  $cat = $ass->getFirstAssignment();
163  $calendar->setValue($cat);
164  }
165  elseif(isset($_GET['ref_id']))
166  {
167  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
168  $obj_cal = ilObject::_lookupObjId($_GET['ref_id']);
169  $calendar->setValue(ilCalendarCategories::_lookupCategoryIdByObjId($obj_cal));
170  }
171  $calendar->setRequired(true);
172  $cats = ilCalendarCategories::_getInstance($ilUser->getId());
173  $calendar->setOptions($cats->prepareCategoriesOfUserForSelection());
174  $this->form->addItem($calendar);
175 
176  $tpl->addJavaScript('./Services/Calendar/js/toggle_appointment_time.js');
177  $fullday = new ilCheckboxInputGUI($this->lng->txt('cal_fullday'),'fullday');
178  $fullday->setChecked($this->app->isFullday() ? true : false);
179  $fullday->setOptionTitle($this->lng->txt('cal_fullday_title'));
180  $fullday->setAdditionalAttributes('onchange="ilToggleAppointmentTime(this);"');
181  $this->form->addItem($fullday);
182 
183  $start = new ilDateTimeInputGUI($this->lng->txt('cal_start'),'start');
184  $start->setDate($this->app->getStart());
185  $start->setShowTime(true);
186  $start->setMinuteStepSize(5);
187  $fullday->addSubItem($start);
188 
189  $end = new ilDateTimeInputGUI($this->lng->txt('cal_end'),'end');
190  $end->setDate($this->app->getEnd());
191  $end->setShowTime(true);
192  $end->setMinuteStepSize(5);
193  $fullday->addSubItem($end);
194 
195  // Recurrence
196  include_once('./Services/Calendar/classes/Form/class.ilRecurrenceInputGUI.php');
197  $rec = new ilRecurrenceInputGUI($this->lng->txt('cal_recurrences'),'frequence');
198  $rec->setRecurrence($this->rec);
199  $this->form->addItem($rec);
200 
201 
202  $where = new ilTextInputGUI($this->lng->txt('cal_where'),'location');
203  $where->setValue($this->app->getLocation());
204  $where->setMaxLength(128);
205  $where->setSize(32);
206  $this->form->addItem($where);
207 
208  $desc = new ilTextAreaInputGUI($this->lng->txt('description'),'description');
209  $desc->setValue($this->app->getDescription());
210  $desc->setCols(3);
211  $this->form->addItem($desc);
212 
213  }
214 
215 
222  protected function add()
223  {
224  global $tpl;
225 
226  $this->initForm('create');
227  $tpl->setContent($this->form->getHTML());
228  }
229 
235  protected function save()
236  {
237  global $ilErr;
238 
239  $this->load();
240 
241  if($this->app->validate())
242  {
243  if(!(int) $_POST['calendar'])
244  {
245  $cat_id = $this->createDefaultCalendar();
246  }
247  else
248  {
249  $cat_id = (int) $_POST['calendar'];
250  }
251 
252  $this->app->save();
253  $this->rec->setEntryId($this->app->getEntryId());
254  $this->saveRecurrenceSettings();
255 
256  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
257  $ass = new ilCalendarCategoryAssignments($this->app->getEntryId());
258  $ass->addAssignment($cat_id);
259 
260  ilUtil::sendInfo($this->lng->txt('cal_created_appointment'));
261  $this->ctrl->returnToParent($this);
262  }
263  else
264  {
265  ilUtil::sendInfo($ilErr->getMessage());
266  }
267  $this->add();
268  }
269 
277  protected function edit()
278  {
279  global $tpl,$ilUser,$ilErr;
280 
281  include_once('./Services/Calendar/classes/class.ilCalendarCategory.php');
282  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
283  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
284 
285  $cat_id = ilCalendarCategoryAssignments::_lookupCategory($this->app->getEntryId());
286  $cats = ilCalendarCategories::_getInstance($ilUser->getId());
287 
288  if(!$cats->isVisible($cat_id))
289  {
290  $ilErr->raiseError($this->lng->txt('permission_denied'),$ilErr->WARNING);
291  return false;
292  }
293  if(!$cats->isEditable($cat_id) or $this->app->isAutoGenerated())
294  {
295  $this->showInfoScreen();
296  return true;
297  }
298 
299  $this->initForm('edit');
300  $tpl->setContent($this->form->getHTML());
301  }
302 
309  protected function showInfoScreen()
310  {
311  global $tpl,$ilUser;
312 
313  include_once("./Services/InfoScreen/classes/class.ilInfoScreenGUI.php");
314  $info = new ilInfoScreenGUI($this);
315  $info->setFormAction($this->ctrl->getFormAction($this));
316 
317  $info->addSection($this->lng->txt('cal_details'));
318 
319  // Appointment
320  $info->addProperty($this->lng->txt('appointment'),
322  $this->app->getStart(),
323  $this->app->getEnd()));
324  $info->addProperty($this->lng->txt('title'),$this->app->getPresentationTitle());
325 
326  // Description
327  if(strlen($desc = $this->app->getDescription()))
328  {
329  $info->addProperty($this->lng->txt('description'),$desc);
330  }
331 
332  // Location
333  if(strlen($loc = $this->app->getLocation()))
334  {
335  $info->addProperty($this->lng->txt('cal_where'),$loc);
336  }
337 
338  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
339  $cat_id = ilCalendarCategoryAssignments::_lookupCategory($this->app->getEntryId());
340  $category = new ilCalendarCategory($cat_id);
341 
342  if($category->getType() == ilCalendarCategory::TYPE_OBJ)
343  {
344  $info->addSection($this->lng->txt('additional_info'));
345 
346  $cat_info = ilCalendarCategories::_getInstance()->getCategoryInfo($cat_id);
347  $refs = ilObject::_getAllReferences($cat_info['obj_id']);
348 
349  include_once('classes/class.ilLink.php');
350  $href = ilLink::_getStaticLink(current($refs),ilObject::_lookupType($cat_info['obj_id']),true);
351  $info->addProperty($this->lng->txt('perma_link'),'<a class="small" href="'.$href.'" target="_top">'.$href.'</a>');
352  }
353 
354  $tpl->setContent($info->getHTML());
355  }
356 
363  protected function update()
364  {
365  global $ilErr;
366 
367  $this->load();
368 
369  if($this->app->validate())
370  {
371  if(!(int) $_POST['calendar'])
372  {
373  $cat_id = $this->createDefaultCalendar();
374  }
375  else
376  {
377  $cat_id = (int) $_POST['calendar'];
378  }
379 
380 
381  $this->app->update();
382  $this->saveRecurrenceSettings();
383 
384  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
385  $ass = new ilCalendarCategoryAssignments($this->app->getEntryId());
386  $ass->deleteAssignments();
387  $ass->addAssignment($cat_id);
388 
389  ilUtil::sendInfo($this->lng->txt('settings_saved'));
390  $this->ctrl->returnToParent($this);
391  }
392  else
393  {
394  ilUtil::sendInfo($ilErr->getMessage());
395  }
396 
397  $this->edit();
398 
399  }
400 
407  protected function askDelete()
408  {
409  global $tpl;
410 
411  include_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
412 
413  $this->ctrl->saveParameter($this,array('seed','app_id'));
414 
415  $confirm = new ilConfirmationGUI();
416  $confirm->setFormAction($this->ctrl->getFormAction($this));
417  $confirm->setHeaderText($this->lng->txt('cal_delete_app_sure'));
418  $confirm->setCancel($this->lng->txt('cancel'),'edit');
419  $confirm->setConfirm($this->lng->txt('delete'),'delete');
420  $confirm->addItem('appointments[]',$this->app->getEntryId(),$this->app->getTitle());
421  $tpl->setContent($confirm->getHTML());
422 
423  }
424 
432  protected function delete()
433  {
434  foreach($_POST['appointments'] as $app_id)
435  {
436  $app = new ilCalendarEntry($app_id);
437  $app->delete();
438 
439  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
441  }
442  ilUtil::sendInfo($this->lng->txt('cal_deleted_app'),true);
443  $this->ctrl->returnToParent($this);
444  }
445 
453  protected function initTimeZone()
454  {
455  global $ilUser;
456 
457  $this->timezone = $ilUser->getTimeZone();
458  }
459 
467  protected function initSeed(ilDate $seed)
468  {
469  if(!isset($_GET['hour']))
470  {
471  $this->seed = clone $seed;
472  $this->default_fulltime = true;
473  }
474  else
475  {
476  if((int) $_GET['hour'] < 10)
477  {
478  $time = '0'.(int) $_GET['hour'].':00:00';
479  }
480  else
481  {
482  $time = (int) $_GET['hour'].':00:00';
483  }
484  $this->seed = new ilDateTime($seed->get(IL_CAL_DATE).' '.$time,IL_CAL_DATETIME,$this->timezone);
485  $this->default_fulltime = false;
486  }
487 
488  }
489 
497  protected function initAppointment($a_app_id = 0)
498  {
499  include_once('./Services/Calendar/classes/class.ilCalendarEntry.php');
500  include_once('./Services/Calendar/classes/class.ilCalendarRecurrences.php');
501  $this->app = new ilCalendarEntry($a_app_id);
502 
503  if(!$a_app_id)
504  {
505  $start = clone $this->seed;
506  $this->app->setStart($start);
507 
508  $seed_end = clone $this->seed;
509  if($this->default_fulltime)
510  {
511  #$seed_end->increment(IL_CAL_DAY,1);
512  }
513  else
514  {
515  $seed_end->increment(IL_CAL_HOUR,2);
516  }
517  $this->app->setEnd($seed_end);
518  $this->app->setFullday($this->default_fulltime);
519 
520  $this->rec = new ilCalendarRecurrence();
521  }
522  else
523  {
524  $this->rec = ilCalendarRecurrences::_getFirstRecurrence($this->app->getEntryId());
525  }
526 
527  }
528 
536  protected function load()
537  {
538  $this->app->setTitle(ilUtil::stripSlashes($_POST['title']));
539  $this->app->setLocation(ilUtil::stripSlashes($_POST['location']));
540  $this->app->setDescription(ilUtil::stripSlashes($_POST['description']));
541  $this->app->setTitle(ilUtil::stripSlashes($_POST['title']));
542  $this->app->setFullday(isset($_POST['fullday']) ? true : false);
543 
544  if($this->app->isFullday())
545  {
546  $start = new ilDate($_POST['start']['date']['y'].'-'.$_POST['start']['date']['m'].'-'.$_POST['start']['date']['d'],
547  IL_CAL_DATE);
548  $this->app->setStart($start);
549 
550  $end = new ilDate($_POST['end']['date']['y'].'-'.$_POST['end']['date']['m'].'-'.$_POST['end']['date']['d'],
551  IL_CAL_DATE);
552  $this->app->setEnd($end);
553  }
554  else
555  {
556  $start_dt['year'] = (int) $_POST['start']['date']['y'];
557  $start_dt['mon'] = (int) $_POST['start']['date']['m'];
558  $start_dt['mday'] = (int) $_POST['start']['date']['d'];
559  $start_dt['hours'] = (int) $_POST['start']['time']['h'];
560  $start_dt['minutes'] = (int) $_POST['start']['time']['m'];
561  $start = new ilDateTime($start_dt,IL_CAL_FKT_GETDATE,$this->timezone);
562  $this->app->setStart($start);
563 
564  $end_dt['year'] = (int) $_POST['end']['date']['y'];
565  $end_dt['mon'] = (int) $_POST['end']['date']['m'];
566  $end_dt['mday'] = (int) $_POST['end']['date']['d'];
567  $end_dt['hours'] = (int) $_POST['end']['time']['h'];
568  $end_dt['minutes'] = (int) $_POST['end']['time']['m'];
569  $end = new ilDateTime($end_dt,IL_CAL_FKT_GETDATE,$this->timezone);
570  $this->app->setEnd($end);
571  }
572  $this->loadRecurrenceSettings();
573  }
574 
581  protected function loadRecurrenceSettings()
582  {
583  $this->rec->reset();
584 
585  switch($_POST['frequence'])
586  {
587  case IL_CAL_FREQ_DAILY:
588  $this->rec->setFrequenceType($_POST['frequence']);
589  $this->rec->setInterval((int) $_POST['count_DAILY']);
590  break;
591 
592  case IL_CAL_FREQ_WEEKLY:
593  $this->rec->setFrequenceType($_POST['frequence']);
594  $this->rec->setInterval((int) $_POST['count_WEEKLY']);
595  if(is_array($_POST['byday_WEEKLY']))
596  {
597  $this->rec->setBYDAY(ilUtil::stripSlashes(implode(',',$_POST['byday_WEEKLY'])));
598  }
599  break;
600 
601  case IL_CAL_FREQ_MONTHLY:
602  $this->rec->setFrequenceType($_POST['frequence']);
603  $this->rec->setInterval((int) $_POST['count_MONTHLY']);
604  switch((int) $_POST['subtype_MONTHLY'])
605  {
606  case 0:
607  // nothing to do;
608  break;
609 
610  case 1:
611  switch((int) $_POST['monthly_byday_day'])
612  {
613  case 8:
614  // Weekday
615  $this->rec->setBYSETPOS((int) $_POST['monthly_byday_num']);
616  $this->rec->setBYDAY('MO,TU,WE,TH,FR');
617  break;
618 
619  case 9:
620  // Day of month
621  $this->rec->setBYMONTHDAY((int) $_POST['monthly_byday_num']);
622  break;
623 
624  default:
625  $this->rec->setBYDAY((int) $_POST['monthly_byday_num'].$_POST['monthly_byday_day']);
626  break;
627  }
628  break;
629 
630  case 2:
631  $this->rec->setBYMONTHDAY((int) $_POST['monthly_bymonthday']);
632  break;
633  }
634  break;
635 
636  case IL_CAL_FREQ_YEARLY:
637  $this->rec->setFrequenceType($_POST['frequence']);
638  $this->rec->setInterval((int) $_POST['count_YEARLY']);
639  switch((int) $_POST['subtype_YEARLY'])
640  {
641  case 0:
642  // nothing to do;
643  break;
644 
645  case 1:
646  $this->rec->setBYMONTH((int) $_POST['yearly_bymonth_byday']);
647  $this->rec->setBYDAY((int) $_POST['yearly_byday_num'].$_POST['yearly_byday']);
648  break;
649 
650  case 2:
651  $this->rec->setBYMONTH((int) $_POST['yearly_bymonth_by_monthday']);
652  $this->rec->setBYMONTHDAY((int) $_POST['yearly_bymonthday']);
653  break;
654  }
655  break;
656  }
657 
658  // UNTIL
659  switch((int) $_POST['until_type'])
660  {
661  case 1:
662  // nothing to do
663  break;
664 
665  case 2:
666  $this->rec->setFrequenceUntilCount((int) $_POST['count']);
667  break;
668  }
669 
670  }
671 
679  protected function saveRecurrenceSettings()
680  {
681  switch($_POST['frequence'])
682  {
683  case 'NONE':
684  // No recurrence => delete if there is an recurrence rule
685  if($this->rec->getRecurrenceId())
686  {
687  $this->rec->delete();
688  }
689  break;
690 
691  default:
692  if($this->rec->getRecurrenceId())
693  {
694  $this->rec->update();
695  }
696  else
697  {
698  $this->rec->save();
699  }
700  break;
701  }
702  }
703 
710  protected function createDefaultCalendar()
711  {
712  global $ilUser,$lng;
713 
714  $cat = new ilCalendarCategory();
715  $cat->setColor(ilCalendarCategory::DEFAULT_COLOR);
716  $cat->setType(ilCalendarCategory::TYPE_USR);
717  $cat->setTitle($this->lng->txt('cal_default_calendar'));
718  $cat->setObjId($ilUser->getId());
719  return $cat->add();
720  }
721 
722 }
723 ?>