ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilRecurrenceInputGUI.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 
24 
25 include_once('./Services/Calendar/classes/class.ilCalendarUserSettings.php');
26 include_once './Services/Calendar/classes/class.ilCalendarRecurrence.php';
27 
38 {
39  const REC_LIMITED = 2;
40  const REC_UNLIMITED = 1;
41 
42  protected $lng;
43 
44  protected $recurrence;
45  protected $user_settings;
46 
47  protected $allow_unlimited_recurrences = true;
48 
49  protected $enabled_subforms = array(
54 
62  public function __construct($a_title, $a_postvar)
63  {
64  global $lng,$tpl,$ilUser;
65 
66  $this->lng = $lng;
67  $this->lng->loadLanguageModule('dateplaner');
68 
69  $this->user_settings = ilCalendarUserSettings::_getInstanceByUserId($ilUser->getId());
70  $tpl->addJavascript("./Services/Calendar/js/recurrence_input.js");
71 
72  $this->setRecurrence(new ilCalendarRecurrence());
73 
74  parent::__construct($a_title,$a_postvar);
75  }
76 
84  public function checkInput()
85  {
86  global $lng;
87 
88  $this->loadRecurrence();
89 
90  if($_POST['frequence'] == 'NONE')
91  {
92  return true;
93  }
94 
95  if(!isset($_POST['until_type']) or $_POST['until_type'] == REC_LIMITED)
96  {
97  if($_POST['count'] <= 0 or $_POST['count'] >= 100)
98  {
99  $this->setAlert($lng->txt("cal_rec_err_limit"));
100  return false;
101  }
102  }
103 
104 
105  return true;
106  }
107 
113  protected function loadRecurrence()
114  {
115  if(!$this->getRecurrence() instanceof ilCalendarRecurrence)
116  {
117  return false;
118  }
119 
120 
121  switch($_POST['frequence'])
122  {
123  case IL_CAL_FREQ_DAILY:
124  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
125  $this->getRecurrence()->setInterval((int) $_POST['count_DAILY']);
126  break;
127 
128  case IL_CAL_FREQ_WEEKLY:
129  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
130  $this->getRecurrence()->setInterval((int) $_POST['count_WEEKLY']);
131  if(is_array($_POST['byday_WEEKLY']))
132  {
133  $this->getRecurrence()->setBYDAY(ilUtil::stripSlashes(implode(',',$_POST['byday_WEEKLY'])));
134  }
135  break;
136 
137  case IL_CAL_FREQ_MONTHLY:
138  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
139  $this->getRecurrence()->setInterval((int) $_POST['count_MONTHLY']);
140  switch((int) $_POST['subtype_MONTHLY'])
141  {
142  case 0:
143  // nothing to do;
144  break;
145 
146  case 1:
147  switch((int) $_POST['monthly_byday_day'])
148  {
149  case 8:
150  // Weekday
151  $this->getRecurrence()->setBYSETPOS((int) $_POST['monthly_byday_num']);
152  $this->getRecurrence()->setBYDAY('MO,TU,WE,TH,FR');
153  break;
154 
155  case 9:
156  // Day of month
157  $this->getRecurrence()->setBYMONTHDAY((int) $_POST['monthly_byday_num']);
158  break;
159 
160  default:
161  $this->getRecurrence()->setBYDAY((int) $_POST['monthly_byday_num'].$_POST['monthly_byday_day']);
162  break;
163  }
164  break;
165 
166  case 2:
167  $this->getRecurrence()->setBYMONTHDAY((int) $_POST['monthly_bymonthday']);
168  break;
169  }
170  break;
171 
172  case IL_CAL_FREQ_YEARLY:
173  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
174  $this->getRecurrence()->setInterval((int) $_POST['count_YEARLY']);
175  switch((int) $_POST['subtype_YEARLY'])
176  {
177  case 0:
178  // nothing to do;
179  break;
180 
181  case 1:
182  $this->getRecurrence()->setBYMONTH((int) $_POST['yearly_bymonth_byday']);
183  $this->getRecurrence()->setBYDAY((int) $_POST['yearly_byday_num'].$_POST['yearly_byday']);
184  break;
185 
186  case 2:
187  $this->getRecurrence()->setBYMONTH((int) $_POST['yearly_bymonth_by_monthday']);
188  $this->getRecurrence()->setBYMONTHDAY((int) $_POST['yearly_bymonthday']);
189  break;
190  }
191  break;
192  }
193 
194  // UNTIL
195  switch((int) $_POST['until_type'])
196  {
197  case 1:
198  $this->getRecurrence()->setFrequenceUntilDate(null);
199  // nothing to do
200  break;
201 
202  case 2:
203  $this->getRecurrence()->setFrequenceUntilDate(null);
204  $this->getRecurrence()->setFrequenceUntilCount((int) $_POST['count']);
205  break;
206 
207  case 3:
208  $end_dt['year'] = (int) $_POST['until_end']['date']['y'];
209  $end_dt['mon'] = (int) $_POST['until_end']['date']['m'];
210  $end_dt['mday'] = (int) $_POST['until_end']['date']['d'];
211 
212  $this->getRecurrence()->setFrequenceUntilCount(0);
213  $this->getRecurrence()->setFrequenceUntilDate(new ilDate($end_dt,IL_CAL_FKT_GETDATE,$this->timezone));
214  break;
215  }
216 
217  return true;
218  }
219 
220 
228  public function setRecurrence($a_rec)
229  {
230  $this->recurrence = $a_rec;
231  }
232 
237  public function getRecurrence()
238  {
239  return $this->recurrence;
240  }
241 
247  public function allowUnlimitedRecurrences($a_status)
248  {
249  $this->allow_unlimited_recurrences = $a_status;
250  }
251 
257  {
259  }
260 
268  public function setEnabledSubForms($a_sub_forms)
269  {
270  $this->enabled_subforms = $a_sub_forms;
271  }
272 
279  public function getEnabledSubForms()
280  {
282  }
283 
291  public function insert($a_tpl)
292  {
293  $tpl = new ilTemplate('tpl.recurrence_input.html',true,true,'Services/Calendar');
294 
295  $options = array('NONE' => $this->lng->txt('cal_no_recurrence'));
296  if(in_array(IL_CAL_FREQ_DAILY, $this->getEnabledSubForms()))
297  {
298  $options[IL_CAL_FREQ_DAILY] = $this->lng->txt('cal_daily');
299  }
300  if(in_array(IL_CAL_FREQ_WEEKLY, $this->getEnabledSubForms()))
301  {
302  $options[IL_CAL_FREQ_WEEKLY] = $this->lng->txt('cal_weekly');
303  }
304  if(in_array(IL_CAL_FREQ_MONTHLY, $this->getEnabledSubForms()))
305  {
306  $options[IL_CAL_FREQ_MONTHLY] = $this->lng->txt('cal_monthly');
307  }
308  if(in_array(IL_CAL_FREQ_YEARLY, $this->getEnabledSubForms()))
309  {
310  $options[IL_CAL_FREQ_YEARLY] = $this->lng->txt('cal_yearly');
311  }
312 
313  $tpl->setVariable('FREQUENCE',ilUtil::formSelect(
314  $this->recurrence->getFrequenceType(),
315  'frequence',
316  $options,
317  false,
318  true,
319  '',
320  '',
321  array('onchange' => 'ilHideFrequencies();','id' => 'il_recurrence_1')));
322 
323  $tpl->setVariable('TXT_EVERY',$this->lng->txt('cal_every'));
324 
325  // DAILY
326  if(in_array(IL_CAL_FREQ_DAILY, $this->getEnabledSubForms()))
327  {
328  $tpl->setVariable('TXT_DAILY_FREQ_UNIT',$this->lng->txt('cal_day_s'));
329  $tpl->setVariable('COUNT_DAILY_VAL',$this->recurrence->getInterval());
330  }
331 
332  // WEEKLY
333  if(in_array(IL_CAL_FREQ_WEEKLY, $this->getEnabledSubForms()))
334  {
335  $tpl->setVariable('TXT_WEEKLY_FREQ_UNIT',$this->lng->txt('cal_week_s'));
336  $tpl->setVariable('COUNT_WEEKLY_VAL',$this->recurrence->getInterval());
337  $this->buildWeekDaySelection($tpl);
338  }
339 
340  // MONTHLY
341  if(in_array(IL_CAL_FREQ_MONTHLY, $this->getEnabledSubForms()))
342  {
343  $tpl->setVariable('TXT_MONTHLY_FREQ_UNIT',$this->lng->txt('cal_month_s'));
344  $tpl->setVariable('COUNT_MONTHLY_VAL',$this->recurrence->getInterval());
345  $tpl->setVariable('TXT_ON_THE',$this->lng->txt('cal_on_the'));
346  $tpl->setVariable('TXT_BYMONTHDAY',$this->lng->txt('cal_on_the'));
347  $tpl->setVariable('TXT_OF_THE_MONTH',$this->lng->txt('cal_of_the_month'));
350  }
351 
352  // YEARLY
353  if(in_array(IL_CAL_FREQ_YEARLY, $this->getEnabledSubForms()))
354  {
355  $tpl->setVariable('TXT_YEARLY_FREQ_UNIT',$this->lng->txt('cal_year_s'));
356  $tpl->setVariable('COUNT_YEARLY_VAL',$this->recurrence->getInterval());
357  $tpl->setVariable('TXT_ON_THE',$this->lng->txt('cal_on_the'));
360  }
361 
362  // UNTIL
363  $this->buildUntilSelection($tpl);
364 
365  $a_tpl->setCurrentBlock("prop_custom");
366  $a_tpl->setVariable("CUSTOM_CONTENT", $tpl->get());
367  $a_tpl->parseCurrentBlock();
368 
369  }
370 
377  protected function buildWeekDaySelection($tpl)
378  {
379  $days = array(0 => 'SU',1 => 'MO',2 => 'TU',3 => 'WE',4 => 'TH',5 => 'FR',6 => 'SA',7 => 'SU');
380 
381  $checked_days = array();
382  foreach($this->recurrence->getBYDAYList() as $byday)
383  {
384  if(in_array($byday,$days))
385  {
386  $checked_days[] = $byday;
387  }
388  }
389 
390  for($i = (int) $this->user_settings->getWeekStart();$i < 7 + (int) $this->user_settings->getWeekStart();$i++)
391  {
392  $tpl->setCurrentBlock('byday_simple');
393 
394  if(in_array($days[$i],$checked_days))
395  {
396  $tpl->setVariable('BYDAY_WEEKLY_CHECKED','checked="checked"');
397  }
398  $tpl->setVariable('TXT_ON',$this->lng->txt('cal_on'));
399  $tpl->setVariable('BYDAY_WEEKLY_VAL',$days[$i]);
400  $tpl->setVariable('TXT_DAY_SHORT',ilCalendarUtil::_numericDayToString($i,false));
401  $tpl->parseCurrentBlock();
402  }
403 
404  }
405 
413  protected function buildMonthlyByDaySelection($tpl)
414  {
415  $byday_list = $this->recurrence->getBYDAYList();
416  $chosen_num_day = 1;
417  $chosen_day = 'MO';
418  $chosen = false;
419  foreach($byday_list as $byday)
420  {
421  if(preg_match('/^(-?\d)([A-Z][A-Z])/',$byday,$parsed) === 1)
422  {
423  $chosen = true;
424  $chosen_num_day = $parsed[1];
425  $chosen_day = $parsed[2];
426  }
427  }
428  // check for last day
429  if(count($this->recurrence->getBYMONTHDAYList()) == 1)
430  {
431  $bymonthday = $this->recurrence->getBYMONTHDAY();
432  if(in_array($bymonthday,array(1,2,3,4,5,-1)))
433  {
434  $chosen = true;
435  $chosen_num_day = $bymonthday;
436  $chosen_day = 9;
437  }
438  }
439  // Check for first, second... last weekday
440  if(count($this->recurrence->getBYSETPOSList()) == 1)
441  {
442  $bysetpos = $this->recurrence->getBYSETPOS();
443  if(in_array($bysetpos,array(1,2,3,4,5,-1)))
444  {
445  if($this->recurrence->getBYDAYList() == array('MO','TU','WE','TH','FR'))
446  {
447  $chosen = true;
448  $chosen_num_day = $bysetpos;
449  $chosen_day = 8;
450  }
451  }
452  }
453 
454 
455 
456  if($chosen)
457  {
458  $tpl->setVariable('M_BYDAY_CHECKED','checked="checked"');
459  }
460 
461  $num_options = array(
462  1 => $this->lng->txt('cal_first'),
463  2 => $this->lng->txt('cal_second'),
464  3 => $this->lng->txt('cal_third'),
465  4 => $this->lng->txt('cal_fourth'),
466  5 => $this->lng->txt('cal_fifth'),
467  -1 => $this->lng->txt('cal_last'));
468 
469  $tpl->setVariable('SELECT_BYDAY_NUM_MONTHLY',ilUtil::formSelect(
470  $chosen_num_day,
471  'monthly_byday_num',
472  $num_options,
473  false,
474  true,
475  '',
476  '',
477  array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_1');")));
478 
479  $days = array(0 => 'SU',1 => 'MO',2 => 'TU',3 => 'WE',4 => 'TH',5 => 'FR',6 => 'SA',7 => 'SU');
480 
481  for($i = (int) $this->user_settings->getWeekStart();$i < 7 + (int) $this->user_settings->getWeekStart();$i++)
482  {
483  $days_select[$days[$i]] = ilCalendarUtil::_numericDayToString($i);
484  }
485  $days_select[8] = $this->lng->txt('cal_weekday');
486  $days_select[9] = $this->lng->txt('cal_day_of_month');
487  $tpl->setVariable('SEL_BYDAY_DAY_MONTHLY',ilUtil::formSelect(
488  $chosen_day,
489  'monthly_byday_day',
490  $days_select,
491  false,
492  true,
493  '',
494  '',
495  array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_1');")));
496  }
497 
506  {
507  $tpl->setVariable('TXT_IN',$this->lng->txt('cal_in'));
508 
509  $chosen_day = 1;
510  $chosen = false;
511  if(count($bymonthday = $this->recurrence->getBYMONTHDAYList()) == 1)
512  {
513  foreach($bymonthday as $mday)
514  {
515  if($mday > 0 and $mday < 32)
516  {
517  $chosen = true;
518  $chosen_day = $mday;
519  }
520  }
521  }
522 
523  if($chosen)
524  {
525  $tpl->setVariable('M_BYMONTHDAY_CHECKED','checked="checked"');
526  }
527 
528  for($i = 1; $i < 32;$i++)
529  {
530  $options[$i] = $i;
531  }
532  $tpl->setVariable('SELECT_BYMONTHDAY',ilUtil::formSelect(
533  $chosen_day,
534  'monthly_bymonthday',
535  $options,
536  false,
537  true,
538  '',
539  '',
540  array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_2');")));
541  }
542 
551  {
552  $tpl->setVariable('TXT_Y_EVERY',$this->lng->txt('cal_every'));
553 
554 
555  $chosen = false;
556  $chosen_month = 1;
557  $chosen_day = 1;
558  foreach($this->recurrence->getBYMONTHList() as $month)
559  {
560  if($this->recurrence->getBYMONTHDAYList())
561  {
562  $chosen_month = $month;
563  $chosen = true;
564  break;
565  }
566  }
567  foreach($this->recurrence->getBYMONTHDAYList() as $day)
568  {
569  $chosen_day = $day;
570  }
571 
572  for($i = 1; $i < 32;$i++)
573  {
574  $options[$i] = $i;
575  }
576  $tpl->setVariable('SELECT_BYMONTHDAY_NUM_YEARLY',ilUtil::formSelect(
577  $chosen_day,
578  'yearly_bymonthday',
579  $options,
580  false,
581  true,
582 
583  '',
584  '',
585  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_2');")));
586 
587  $options = array();
588  for($m = 1;$m < 13;$m++)
589  {
590  $options[$m] = ilCalendarUtil::_numericMonthToString($m);
591  }
592  $tpl->setVariable('SELECT_BYMONTH_YEARLY',ilUtil::formSelect(
593  $chosen_month,
594  'yearly_bymonth_by_monthday',
595  $options,
596  false,
597  true,
598  '',
599  '',
600  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_2');")));
601 
602 
603  if($chosen)
604  {
605  $tpl->setVariable('Y_BYMONTHDAY_CHECKED','checked="checked"');
606  }
607 
608  }
609 
617  protected function buildYearlyByDaySelection($tpl)
618  {
619  $tpl->setVariable('TXT_ON_THE',$this->lng->txt('cal_on_the'));
620 
621  $chosen_num_day = 1;
622  $chosen_day = 'MO';
623  $chosen = false;
624  foreach($this->recurrence->getBYDAYList() as $byday)
625  {
626  if(preg_match('/^(-?\d)([A-Z][A-Z])/',$byday,$parsed) === 1)
627  {
628  $chosen = true;
629  $chosen_num_day = $parsed[1];
630  $chosen_day = $parsed[2];
631  }
632  }
633 
634 
635  $num_options = array(
636  1 => $this->lng->txt('cal_first'),
637  2 => $this->lng->txt('cal_second'),
638  3 => $this->lng->txt('cal_third'),
639  4 => $this->lng->txt('cal_fourth'),
640  5 => $this->lng->txt('cal_fifth'),
641  -1 => $this->lng->txt('cal_last'));
642 
643  $tpl->setVariable('SELECT_BYDAY_NUM_YEARLY',ilUtil::formSelect(
644  $chosen_num_day,
645  'yearly_byday_num',
646  $num_options,
647  false,
648  true,
649  '',
650  '',
651  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")));
652 
653 
654  $days = array(0 => 'SU',1 => 'MO',2 => 'TU',3 => 'WE',4 => 'TH',5 => 'FR',6 => 'SA',7 => 'SU');
655  for($i = (int) $this->user_settings->getWeekStart();$i < 7 + (int) $this->user_settings->getWeekStart();$i++)
656  {
657  $days_select[$days[$i]] = ilCalendarUtil::_numericDayToString($i);
658  }
659  $tpl->setVariable('SELECT_BYDAY_DAY_YEARLY',ilUtil::formSelect(
660  $chosen_day,
661  'yearly_byday',
662  $days_select,
663  false,
664  true,
665  '',
666  '',
667  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")));
668 
669 
670  $chosen = false;
671  $chosen_month = 1;
672  foreach($this->recurrence->getBYMONTHList() as $month)
673  {
674  if($this->recurrence->getBYMONTHDAYList())
675  {
676  $chosen_month = $month;
677  $chosen = true;
678  break;
679  }
680  }
681  $options = array();
682  for($m = 1;$m < 13;$m++)
683  {
684  $options[$m] = ilCalendarUtil::_numericMonthToString($m);
685  }
686  $tpl->setVariable('SELECT_BYMONTH_BYDAY',ilUtil::formSelect(
687  $chosen_month,
688  'yearly_bymonth_byday',
689  $options,
690  false,
691  true,
692  '',
693  '',
694  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")));
695 
696  }
697 
705  protected function buildUntilSelection($tpl)
706  {
707 
708  if($this->isUnlimitedRecurrenceAllowed())
709  {
710  $tpl->setVariable('TXT_NO_ENDING',$this->lng->txt('cal_no_ending'));
711  }
712 
713  $tpl->setVariable('TXT_UNTIL_CREATE',$this->lng->txt('cal_create'));
714  $tpl->setVariable('TXT_APPOINTMENTS',$this->lng->txt('cal_appointments'));
715 
716  $tpl->setVariable('VAL_COUNT',$this->recurrence->getFrequenceUntilCount() ?
717  $this->recurrence->getFrequenceUntilCount() :
718  2);
719 
720  if($this->recurrence->getFrequenceUntilDate())
721  {
722  $tpl->setVariable('UNTIL_END_CHECKED','checked="checked"');
723  }
724  elseif($this->recurrence->getFrequenceUntilCount() or !$this->isUnlimitedRecurrenceAllowed())
725  {
726  $tpl->setVariable('UNTIL_COUNT_CHECKED','checked="checked"');
727  }
728  else
729  {
730  $tpl->setVariable('UNTIL_NO_CHECKED','checked="checked"');
731  }
732 
733  $tpl->setVariable('TXT_UNTIL_END',$this->lng->txt('cal_repeat_until'));
734  $dt = new ilDateTimeInputGUI('','until_end');
735  $dt->setDate(
736  $this->recurrence->getFrequenceUntilDate() ? $this->recurrence->getFrequenceUntilDate() : new ilDate(time(),IL_CAL_UNIX));
737  $tpl->setVariable('UNTIL_END_DATE',$dt->getTableFilterHTML());
738  }
739 }
740 ?>