ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $DIC;
65 
66  $lng = $DIC['lng'];
67  $tpl = $DIC['tpl'];
68  $ilUser = $DIC['ilUser'];
69 
70  $this->lng = $lng;
71  $this->lng->loadLanguageModule('dateplaner');
72 
73  $this->user_settings = ilCalendarUserSettings::_getInstanceByUserId($ilUser->getId());
74  $tpl->addJavascript("./Services/Calendar/js/recurrence_input.js");
75 
76  $this->setRecurrence(new ilCalendarRecurrence());
77 
78  parent::__construct($a_title, $a_postvar);
79  }
80 
88  public function checkInput()
89  {
90  global $DIC;
91 
92  $lng = $DIC['lng'];
93 
94  if (!$this->loadRecurrence()) {
95  return false;
96  }
97 
98  if ($_POST['frequence'] == 'NONE') {
99  return true;
100  }
101 
102  if (!isset($_POST['until_type']) or $_POST['until_type'] == self::REC_LIMITED) {
103  if ($_POST['count'] <= 0 or $_POST['count'] >= 100) {
104  $this->setAlert($lng->txt("cal_rec_err_limit"));
105  return false;
106  }
107  }
108 
109 
110  return true;
111  }
112 
118  protected function loadRecurrence()
119  {
120  if (!$this->getRecurrence() instanceof ilCalendarRecurrence) {
121  return false;
122  }
123 
124 
125  switch ($_POST['frequence']) {
126  case IL_CAL_FREQ_DAILY:
127  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
128  $this->getRecurrence()->setInterval((int) $_POST['count_DAILY']);
129  break;
130 
131  case IL_CAL_FREQ_WEEKLY:
132  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
133  $this->getRecurrence()->setInterval((int) $_POST['count_WEEKLY']);
134  if (is_array($_POST['byday_WEEKLY'])) {
135  $this->getRecurrence()->setBYDAY(ilUtil::stripSlashes(implode(',', $_POST['byday_WEEKLY'])));
136  }
137  break;
138 
139  case IL_CAL_FREQ_MONTHLY:
140  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
141  $this->getRecurrence()->setInterval((int) $_POST['count_MONTHLY']);
142  switch ((int) $_POST['subtype_MONTHLY']) {
143  case 0:
144  // nothing to do;
145  break;
146 
147  case 1:
148  switch ((int) $_POST['monthly_byday_day']) {
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  case 0:
177  // nothing to do;
178  break;
179 
180  case 1:
181  $this->getRecurrence()->setBYMONTH((int) $_POST['yearly_bymonth_byday']);
182  $this->getRecurrence()->setBYDAY((int) $_POST['yearly_byday_num'] . $_POST['yearly_byday']);
183  break;
184 
185  case 2:
186  $this->getRecurrence()->setBYMONTH((int) $_POST['yearly_bymonth_by_monthday']);
187  $this->getRecurrence()->setBYMONTHDAY((int) $_POST['yearly_bymonthday']);
188  break;
189  }
190  break;
191  }
192 
193  // UNTIL
194  switch ((int) $_POST['until_type']) {
195  case 1:
196  $this->getRecurrence()->setFrequenceUntilDate(null);
197  // nothing to do
198  break;
199 
200  case 2:
201  $this->getRecurrence()->setFrequenceUntilDate(null);
202  $this->getRecurrence()->setFrequenceUntilCount((int) $_POST['count']);
203  break;
204 
205  case 3:
206  $dt = new ilDateTimeInputGUI('', 'until_end');
207  $dt->setRequired(true);
208  if ($dt->checkInput()) {
209  $this->getRecurrence()->setFrequenceUntilCount(0);
210  $this->getRecurrence()->setFrequenceUntilDate($dt->getDate());
211  } else {
212  return false;
213  }
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  $options[IL_CAL_FREQ_DAILY] = $this->lng->txt('cal_daily');
298  }
299  if (in_array(IL_CAL_FREQ_WEEKLY, $this->getEnabledSubForms())) {
300  $options[IL_CAL_FREQ_WEEKLY] = $this->lng->txt('cal_weekly');
301  }
302  if (in_array(IL_CAL_FREQ_MONTHLY, $this->getEnabledSubForms())) {
303  $options[IL_CAL_FREQ_MONTHLY] = $this->lng->txt('cal_monthly');
304  }
305  if (in_array(IL_CAL_FREQ_YEARLY, $this->getEnabledSubForms())) {
306  $options[IL_CAL_FREQ_YEARLY] = $this->lng->txt('cal_yearly');
307  }
308 
309  $tpl->setVariable('FREQUENCE', ilUtil::formSelect(
310  $this->recurrence->getFrequenceType(),
311  'frequence',
312  $options,
313  false,
314  true,
315  '',
316  '',
317  array('onchange' => 'ilHideFrequencies();','id' => 'il_recurrence_1')
318  ));
319 
320  $tpl->setVariable('TXT_EVERY', $this->lng->txt('cal_every'));
321 
322  // DAILY
323  if (in_array(IL_CAL_FREQ_DAILY, $this->getEnabledSubForms())) {
324  $tpl->setVariable('TXT_DAILY_FREQ_UNIT', $this->lng->txt('cal_day_s'));
325  $tpl->setVariable('COUNT_DAILY_VAL', $this->recurrence->getInterval());
326  }
327 
328  // WEEKLY
329  if (in_array(IL_CAL_FREQ_WEEKLY, $this->getEnabledSubForms())) {
330  $tpl->setVariable('TXT_WEEKLY_FREQ_UNIT', $this->lng->txt('cal_week_s'));
331  $tpl->setVariable('COUNT_WEEKLY_VAL', $this->recurrence->getInterval());
332  $this->buildWeekDaySelection($tpl);
333  }
334 
335  // MONTHLY
336  if (in_array(IL_CAL_FREQ_MONTHLY, $this->getEnabledSubForms())) {
337  $tpl->setVariable('TXT_MONTHLY_FREQ_UNIT', $this->lng->txt('cal_month_s'));
338  $tpl->setVariable('COUNT_MONTHLY_VAL', $this->recurrence->getInterval());
339  $tpl->setVariable('TXT_ON_THE', $this->lng->txt('cal_on_the'));
340  $tpl->setVariable('TXT_BYMONTHDAY', $this->lng->txt('cal_on_the'));
341  $tpl->setVariable('TXT_OF_THE_MONTH', $this->lng->txt('cal_of_the_month'));
344  }
345 
346  // YEARLY
347  if (in_array(IL_CAL_FREQ_YEARLY, $this->getEnabledSubForms())) {
348  $tpl->setVariable('TXT_YEARLY_FREQ_UNIT', $this->lng->txt('cal_year_s'));
349  $tpl->setVariable('COUNT_YEARLY_VAL', $this->recurrence->getInterval());
350  $tpl->setVariable('TXT_ON_THE', $this->lng->txt('cal_on_the'));
353  }
354 
355  // UNTIL
356  $this->buildUntilSelection($tpl);
357 
358  $a_tpl->setCurrentBlock("prop_custom");
359  $a_tpl->setVariable("CUSTOM_CONTENT", $tpl->get());
360  $a_tpl->parseCurrentBlock();
361  }
362 
369  protected function buildWeekDaySelection($tpl)
370  {
371  $days = array(0 => 'SU',1 => 'MO',2 => 'TU',3 => 'WE',4 => 'TH',5 => 'FR',6 => 'SA',7 => 'SU');
372 
373  $checked_days = array();
374  foreach ($this->recurrence->getBYDAYList() as $byday) {
375  if (in_array($byday, $days)) {
376  $checked_days[] = $byday;
377  }
378  }
379 
380  for ($i = (int) $this->user_settings->getWeekStart();$i < 7 + (int) $this->user_settings->getWeekStart();$i++) {
381  $tpl->setCurrentBlock('byday_simple');
382 
383  if (in_array($days[$i], $checked_days)) {
384  $tpl->setVariable('BYDAY_WEEKLY_CHECKED', 'checked="checked"');
385  }
386  $tpl->setVariable('TXT_ON', $this->lng->txt('cal_on'));
387  $tpl->setVariable('DAY_COUNT', $i);
388  $tpl->setVariable('BYDAY_WEEKLY_VAL', $days[$i]);
389  $tpl->setVariable('TXT_DAY_SHORT', ilCalendarUtil::_numericDayToString($i, false));
390  $tpl->parseCurrentBlock();
391  }
392  }
393 
401  protected function buildMonthlyByDaySelection($tpl)
402  {
403  $byday_list = $this->recurrence->getBYDAYList();
404  $chosen_num_day = 1;
405  $chosen_day = 'MO';
406  $chosen = false;
407  foreach ($byday_list as $byday) {
408  if (preg_match('/^(-?\d)([A-Z][A-Z])/', $byday, $parsed) === 1) {
409  $chosen = true;
410  $chosen_num_day = $parsed[1];
411  $chosen_day = $parsed[2];
412  }
413  }
414  // check for last day
415  if (count($this->recurrence->getBYMONTHDAYList()) == 1) {
416  $bymonthday = $this->recurrence->getBYMONTHDAY();
417  if (in_array($bymonthday, array(1,2,3,4,5,-1))) {
418  $chosen = true;
419  $chosen_num_day = $bymonthday;
420  $chosen_day = 9;
421  }
422  }
423  // Check for first, second... last weekday
424  if (count($this->recurrence->getBYSETPOSList()) == 1) {
425  $bysetpos = $this->recurrence->getBYSETPOS();
426  if (in_array($bysetpos, array(1,2,3,4,5,-1))) {
427  if ($this->recurrence->getBYDAYList() == array('MO','TU','WE','TH','FR')) {
428  $chosen = true;
429  $chosen_num_day = $bysetpos;
430  $chosen_day = 8;
431  }
432  }
433  }
434 
435 
436 
437  if ($chosen) {
438  $tpl->setVariable('M_BYDAY_CHECKED', 'checked="checked"');
439  }
440 
441  $num_options = array(
442  1 => $this->lng->txt('cal_first'),
443  2 => $this->lng->txt('cal_second'),
444  3 => $this->lng->txt('cal_third'),
445  4 => $this->lng->txt('cal_fourth'),
446  5 => $this->lng->txt('cal_fifth'),
447  -1 => $this->lng->txt('cal_last'));
448 
449  $tpl->setVariable('SELECT_BYDAY_NUM_MONTHLY', ilUtil::formSelect(
450  $chosen_num_day,
451  'monthly_byday_num',
452  $num_options,
453  false,
454  true,
455  '',
456  '',
457  array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_1');")
458  ));
459 
460  $days = array(0 => 'SU',1 => 'MO',2 => 'TU',3 => 'WE',4 => 'TH',5 => 'FR',6 => 'SA',7 => 'SU');
461 
462  for ($i = (int) $this->user_settings->getWeekStart();$i < 7 + (int) $this->user_settings->getWeekStart();$i++) {
463  $days_select[$days[$i]] = ilCalendarUtil::_numericDayToString($i);
464  }
465  $days_select[8] = $this->lng->txt('cal_weekday');
466  $days_select[9] = $this->lng->txt('cal_day_of_month');
467  $tpl->setVariable('SEL_BYDAY_DAY_MONTHLY', ilUtil::formSelect(
468  $chosen_day,
469  'monthly_byday_day',
470  $days_select,
471  false,
472  true,
473  '',
474  '',
475  array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_1');")
476  ));
477  }
478 
487  {
488  $tpl->setVariable('TXT_IN', $this->lng->txt('cal_in'));
489 
490  $chosen_day = 1;
491  $chosen = false;
492  if (count($bymonthday = $this->recurrence->getBYMONTHDAYList()) == 1) {
493  foreach ($bymonthday as $mday) {
494  if ($mday > 0 and $mday < 32) {
495  $chosen = true;
496  $chosen_day = $mday;
497  }
498  }
499  }
500 
501  if ($chosen) {
502  $tpl->setVariable('M_BYMONTHDAY_CHECKED', 'checked="checked"');
503  }
504 
505  for ($i = 1; $i < 32;$i++) {
506  $options[$i] = $i;
507  }
508  $tpl->setVariable('SELECT_BYMONTHDAY', ilUtil::formSelect(
509  $chosen_day,
510  'monthly_bymonthday',
511  $options,
512  false,
513  true,
514  '',
515  '',
516  array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_2');")
517  ));
518  }
519 
528  {
529  $tpl->setVariable('TXT_Y_EVERY', $this->lng->txt('cal_every'));
530 
531 
532  $chosen = false;
533  $chosen_month = 1;
534  $chosen_day = 1;
535  foreach ($this->recurrence->getBYMONTHList() as $month) {
536  if ($this->recurrence->getBYMONTHDAYList()) {
537  $chosen_month = $month;
538  $chosen = true;
539  break;
540  }
541  }
542  foreach ($this->recurrence->getBYMONTHDAYList() as $day) {
543  $chosen_day = $day;
544  }
545 
546  for ($i = 1; $i < 32;$i++) {
547  $options[$i] = $i;
548  }
549  $tpl->setVariable('SELECT_BYMONTHDAY_NUM_YEARLY', ilUtil::formSelect(
550  $chosen_day,
551  'yearly_bymonthday',
552  $options,
553  false,
554  true,
555  '',
556  '',
557  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_2');")
558  ));
559 
560  $options = array();
561  for ($m = 1;$m < 13;$m++) {
563  }
564  $tpl->setVariable('SELECT_BYMONTH_YEARLY', ilUtil::formSelect(
565  $chosen_month,
566  'yearly_bymonth_by_monthday',
567  $options,
568  false,
569  true,
570  '',
571  '',
572  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_2');")
573  ));
574 
575 
576  if ($chosen) {
577  $tpl->setVariable('Y_BYMONTHDAY_CHECKED', 'checked="checked"');
578  }
579  }
580 
588  protected function buildYearlyByDaySelection($tpl)
589  {
590  $tpl->setVariable('TXT_ON_THE', $this->lng->txt('cal_on_the'));
591 
592  $chosen_num_day = 1;
593  $chosen_day = 'MO';
594  $chosen = false;
595  foreach ($this->recurrence->getBYDAYList() as $byday) {
596  if (preg_match('/^(-?\d)([A-Z][A-Z])/', $byday, $parsed) === 1) {
597  $chosen = true;
598  $chosen_num_day = $parsed[1];
599  $chosen_day = $parsed[2];
600  }
601  }
602 
603 
604  $num_options = array(
605  1 => $this->lng->txt('cal_first'),
606  2 => $this->lng->txt('cal_second'),
607  3 => $this->lng->txt('cal_third'),
608  4 => $this->lng->txt('cal_fourth'),
609  5 => $this->lng->txt('cal_fifth'),
610  -1 => $this->lng->txt('cal_last'));
611 
612  $tpl->setVariable('SELECT_BYDAY_NUM_YEARLY', ilUtil::formSelect(
613  $chosen_num_day,
614  'yearly_byday_num',
615  $num_options,
616  false,
617  true,
618  '',
619  '',
620  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")
621  ));
622 
623 
624  $days = array(0 => 'SU',1 => 'MO',2 => 'TU',3 => 'WE',4 => 'TH',5 => 'FR',6 => 'SA',7 => 'SU');
625  for ($i = (int) $this->user_settings->getWeekStart();$i < 7 + (int) $this->user_settings->getWeekStart();$i++) {
626  $days_select[$days[$i]] = ilCalendarUtil::_numericDayToString($i);
627  }
628  $tpl->setVariable('SELECT_BYDAY_DAY_YEARLY', ilUtil::formSelect(
629  $chosen_day,
630  'yearly_byday',
631  $days_select,
632  false,
633  true,
634  '',
635  '',
636  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")
637  ));
638 
639 
640  $chosen = false;
641  $chosen_month = 1;
642  foreach ($this->recurrence->getBYMONTHList() as $month) {
643  if ($this->recurrence->getBYMONTHDAYList()) {
644  $chosen_month = $month;
645  $chosen = true;
646  break;
647  }
648  }
649  $options = array();
650  for ($m = 1;$m < 13;$m++) {
652  }
653  $tpl->setVariable('SELECT_BYMONTH_BYDAY', ilUtil::formSelect(
654  $chosen_month,
655  'yearly_bymonth_byday',
656  $options,
657  false,
658  true,
659  '',
660  '',
661  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")
662  ));
663  }
664 
672  protected function buildUntilSelection($tpl)
673  {
674  if ($this->isUnlimitedRecurrenceAllowed()) {
675  $tpl->setVariable('TXT_NO_ENDING', $this->lng->txt('cal_no_ending'));
676  }
677 
678  $tpl->setVariable('TXT_UNTIL_CREATE', $this->lng->txt('cal_create'));
679  $tpl->setVariable('TXT_APPOINTMENTS', $this->lng->txt('cal_appointments'));
680 
681  $tpl->setVariable('VAL_COUNT', $this->recurrence->getFrequenceUntilCount() ?
682  $this->recurrence->getFrequenceUntilCount() :
683  2);
684 
685  if ($this->recurrence->getFrequenceUntilDate()) {
686  $tpl->setVariable('UNTIL_END_CHECKED', 'checked="checked"');
687  } elseif ($this->recurrence->getFrequenceUntilCount() or !$this->isUnlimitedRecurrenceAllowed()) {
688  $tpl->setVariable('UNTIL_COUNT_CHECKED', 'checked="checked"');
689  } else {
690  $tpl->setVariable('UNTIL_NO_CHECKED', 'checked="checked"');
691  }
692 
693  $tpl->setVariable('TXT_UNTIL_END', $this->lng->txt('cal_repeat_until'));
694  $dt = new ilDateTimeInputGUI('', 'until_end');
695  // no proper subform
696  // $dt->setRequired(true);
697  $dt->setDate(
698  $this->recurrence->getFrequenceUntilDate() ? $this->recurrence->getFrequenceUntilDate() : null
699  );
700  $tpl->setVariable('UNTIL_END_DATE', $dt->getTableFilterHTML());
701  }
702 }
const IL_CAL_FREQ_MONTHLY
__construct($a_title, $a_postvar)
Constructor.
static _numericDayToString($a_day, $a_long=true)
get
global $DIC
Definition: saml.php:7
$tpl
Definition: ilias.php:10
loadRecurrence()
load recurrence settings protected
static _getInstanceByUserId($a_user_id)
get singleton instance
This class represents an input GUI for recurring events/appointments (course events or calendar appoi...
buildUntilSelection($tpl)
build selection for ending date
setRecurrence($a_rec)
set recurrence object
allowUnlimitedRecurrences($a_status)
Allow unlimited recurrences.
static _numericMonthToString($a_month, $a_long=true)
numeric month to string
setAlert($a_alert)
Set Alert Text.
buildMonthlyByDaySelection($tpl)
build monthly by day list (e.g second monday)
This class represents a date/time property in a property form.
getEnabledSubForms()
get enabled subforms
buildMonthlyByMonthDaySelection($tpl)
build monthly bymonthday selection
const IL_CAL_FREQ_DAILY
Model of calendar entry recurrcences.
const IL_CAL_FREQ_YEARLY
buildYearlyByMonthDaySelection($tpl)
protected
special template class to simplify handling of ITX/PEAR
$ilUser
Definition: imgupload.php:18
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
buildWeekDaySelection($tpl)
build weekday checkboxes
isUnlimitedRecurrenceAllowed()
Check if unlimited recurrence is allowed.
This class represents a custom property in a property form.
static formSelect( $selected, $varname, $options, $multiple=false, $direct_text=false, $size="0", $style_class="", $attribs="", $disabled=false)
Builds a select form field with options and shows the selected option first.
$i
Definition: disco.tpl.php:19
setEnabledSubForms($a_sub_forms)
set enabled subforms
$_POST["username"]
const IL_CAL_FREQ_WEEKLY