ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilRecurrenceInputGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 protected const int REC_LIMITED = 2;
30 protected const int REC_UNLIMITED = 1;
31
33 protected ilObjUser $user;
35
36 protected bool $allow_unlimited_recurrences = true;
37
38 protected array $enabled_subforms = array(
43 );
44
45 public function __construct(string $a_title, string $a_postvar)
46 {
47 global $DIC;
48
49 $DIC->ui()->mainTemplate()->addJavaScript("assets/js/recurrence_input.js");
50 $this->user = $DIC->user();
51 $this->user_settings = ilCalendarUserSettings::_getInstanceByUserId($this->user->getId());
52 $this->recurrence = new ilCalendarRecurrence();
53 parent::__construct($a_title, $a_postvar);
54 $this->lng->loadLanguageModule('dateplaner');
55 }
56
60 public function checkInput(): bool
61 {
62 global $DIC;
63
64 $lng = $DIC['lng'];
65
66 if (!$this->loadRecurrence()) {
67 return false;
68 }
69
71 return true;
72 }
73
74 if (
75 (
76 $this->getRecurrenceInputByTypeAsInt('until_type') === 0 ||
77 $this->getRecurrenceInputByTypeAsInt('until_type') == self::REC_LIMITED
78 ) &&
79 (
80 $this->getRecurrenceInputByTypeAsInt('count') <= 0 ||
81 $this->getRecurrenceInputByTypeAsInt('count') >= 100
82 )
83 ) {
84 $this->setAlert($this->lng->txt("cal_rec_err_limit"));
85 return false;
86 }
87 return true;
88 }
89
90 protected function getRecurrenceInputByTypeAsInt(string $input): int
91 {
92 if ($this->http->wrapper()->post()->has($input)) {
93 return $this->http->wrapper()->post()->retrieve(
94 $input,
95 $this->refinery->kindlyTo()->int()
96 );
97 }
98 return 0;
99 }
100
101 protected function getRecurrenceInputByTypeAsString(string $input): string
102 {
103 if ($this->http->wrapper()->post()->has($input)) {
104 return $this->http->wrapper()->post()->retrieve(
105 $input,
106 $this->refinery->kindlyTo()->string()
107 );
108 }
109 return '';
110 }
111
112 protected function loadRecurrence(): bool
113 {
114 if (!$this->getRecurrence() instanceof ilCalendarRecurrence) {
115 return false;
116 }
117 switch ($this->getRecurrenceInputByTypeAsString('frequence')) {
119 $this->getRecurrence()->setFrequenceType($this->getRecurrenceInputByTypeAsString('frequence'));
120 $this->getRecurrence()->setInterval($this->getRecurrenceInputByTypeAsInt('count_DAILY'));
121 break;
122
124 $this->getRecurrence()->setFrequenceType($this->getRecurrenceInputByTypeAsString('frequence'));
125 $this->getRecurrence()->setInterval($this->getRecurrenceInputByTypeAsInt('count_WEEKLY'));
126
127 $weekly_days = [];
128 if ($this->http->wrapper()->post()->has('byday_WEEKLY')) {
129 $weekly_days = $this->http->wrapper()->post()->retrieve(
130 'byday_WEEKLY',
131 $this->refinery->kindlyTo()->dictOf(
132 $this->refinery->kindlyTo()->string()
133 )
134 );
135 }
136 if ($weekly_days !== []) {
137 $this->getRecurrence()->setBYDAY(implode(',', $weekly_days));
138 }
139 break;
140
142 $this->getRecurrence()->setFrequenceType($this->getRecurrenceInputByTypeAsString('frequence'));
143 $this->getRecurrence()->setInterval($this->getRecurrenceInputByTypeAsInt('count_MONTHLY'));
144 switch ($this->getRecurrenceInputByTypeAsInt('subtype_MONTHLY')) {
145 case 0:
146 // nothing to do;
147 break;
148 case 1:
149 switch ($this->getRecurrenceInputByTypeAsString('monthly_byday_day')) {
150 case '8':
151 // Weekday
152 $this->getRecurrence()->setBYSETPOS($this->getRecurrenceInputByTypeAsString('monthly_byday_num'));
153 $this->getRecurrence()->setBYDAY('MO,TU,WE,TH,FR');
154 break;
155
156 case '9':
157 // Day of month
158 $this->getRecurrence()->setBYMONTHDAY($this->getRecurrenceInputByTypeAsString('monthly_byday_num'));
159 break;
160
161 default:
162 $this->getRecurrence()->setBYDAY(
163 $this->getRecurrenceInputByTypeAsString('monthly_byday_num') .
164 $this->getRecurrenceInputByTypeAsString('monthly_byday_day')
165 );
166 break;
167 }
168 break;
169
170 case 2:
171 $this->getRecurrence()->setBYMONTHDAY($this->getRecurrenceInputByTypeAsString('monthly_bymonthday'));
172 break;
173 }
174 break;
175
177 $this->getRecurrence()->setFrequenceType($this->getRecurrenceInputByTypeAsString('frequence'));
178 $this->getRecurrence()->setInterval($this->getRecurrenceInputByTypeAsInt('count_YEARLY'));
179 switch ($this->getRecurrenceInputByTypeAsInt('subtype_YEARLY')) {
180 case 0:
181 // nothing to do;
182 break;
183
184 case 1:
185 $this->getRecurrence()->setBYDAY(
186 $this->getRecurrenceInputByTypeAsString('yearly_byday_num') .
187 $this->getRecurrenceInputByTypeAsString('yearly_byday')
188 );
189 $this->getRecurrence()->setBYMONTH($this->getRecurrenceInputByTypeAsString('yearly_bymonth_byday'));
190 break;
191
192 case 2:
193 $this->getRecurrence()->setBYMONTH($this->getRecurrenceInputByTypeAsString('yearly_bymonthday'));
194 $this->getRecurrence()->setBYMONTHDAY($this->getRecurrenceInputByTypeAsString('yearly_bymonthday'));
195 break;
196 }
197 break;
198 }
199
200 // UNTIL
201 switch ($this->getRecurrenceInputByTypeAsInt('until_type')) {
202 case 1:
203 $this->getRecurrence()->setFrequenceUntilDate(null);
204 // nothing to do
205 break;
206
207 case 2:
208 $this->getRecurrence()->setFrequenceUntilDate(null);
209 $this->getRecurrence()->setFrequenceUntilCount($this->getRecurrenceInputByTypeAsInt('count'));
210 break;
211
212 case 3:
213 $dt = new ilDateTimeInputGUI('', 'until_end');
214 $dt->setRequired(true);
215 if ($dt->checkInput()) {
216 $this->getRecurrence()->setFrequenceUntilCount(0);
217 $this->getRecurrence()->setFrequenceUntilDate($dt->getDate());
218 } else {
219 return false;
220 }
221 break;
222 }
223 return true;
224 }
225
226 public function setRecurrence(ilCalendarRecurrence $a_rec): void
227 {
228 $this->recurrence = $a_rec;
229 }
230
232 {
233 return $this->recurrence;
234 }
235
239 public function allowUnlimitedRecurrences(bool $a_status): void
240 {
241 $this->allow_unlimited_recurrences = $a_status;
242 }
243
244 public function isUnlimitedRecurrenceAllowed(): bool
245 {
247 }
248
254 public function setEnabledSubForms(array $a_sub_forms): void
255 {
256 $this->enabled_subforms = $a_sub_forms;
257 }
258
259 public function getEnabledSubForms(): array
260 {
262 }
263
267 public function insert(ilTemplate $a_tpl): void
268 {
269 $tpl = new ilTemplate('tpl.recurrence_input.html', true, true, 'components/ILIAS/Calendar');
270
271 $options = array('NONE' => $this->lng->txt('cal_no_recurrence'));
273 $options[ilCalendarRecurrence::FREQ_DAILY] = $this->lng->txt('cal_daily');
274 }
276 $options[ilCalendarRecurrence::FREQ_WEEKLY] = $this->lng->txt('cal_weekly');
277 }
279 $options[ilCalendarRecurrence::FREQ_MONTHLY] = $this->lng->txt('cal_monthly');
280 }
282 $options[ilCalendarRecurrence::FREQ_YEARLY] = $this->lng->txt('cal_yearly');
283 }
284
285 $tpl->setVariable(
286 'FREQUENCE',
288 $this->recurrence->getFrequenceType(),
289 'frequence',
290 $options,
291 false,
292 true,
293 0,
294 '',
295 ['onchange' => 'ilHideFrequencies();', 'id' => 'il_recurrence_1']
296 )
297 );
298
299 $tpl->setVariable('TXT_EVERY', $this->lng->txt('cal_every'));
300
301 // DAILY
303 $tpl->setVariable('TXT_DAILY_FREQ_UNIT', $this->lng->txt('cal_day_s'));
304 $tpl->setVariable('COUNT_DAILY_VAL', $this->recurrence->getInterval());
305 }
306
307 // WEEKLY
309 $tpl->setVariable('TXT_WEEKLY_FREQ_UNIT', $this->lng->txt('cal_week_s'));
310 $tpl->setVariable('COUNT_WEEKLY_VAL', $this->recurrence->getInterval());
311 $this->buildWeekDaySelection($tpl);
312 }
313
314 // MONTHLY
316 $tpl->setVariable('TXT_MONTHLY_FREQ_UNIT', $this->lng->txt('cal_month_s'));
317 $tpl->setVariable('COUNT_MONTHLY_VAL', $this->recurrence->getInterval());
318 $tpl->setVariable('TXT_ON_THE', $this->lng->txt('cal_on_the'));
319 $tpl->setVariable('TXT_BYMONTHDAY', $this->lng->txt('cal_on_the'));
320 $tpl->setVariable('TXT_OF_THE_MONTH', $this->lng->txt('cal_of_the_month'));
321 $this->buildMonthlyByDaySelection($tpl);
323 }
324
325 // YEARLY
327 $tpl->setVariable('TXT_YEARLY_FREQ_UNIT', $this->lng->txt('cal_year_s'));
328 $tpl->setVariable('COUNT_YEARLY_VAL', $this->recurrence->getInterval());
329 $tpl->setVariable('TXT_ON_THE', $this->lng->txt('cal_on_the'));
331 $this->buildYearlyByDaySelection($tpl);
332 }
333
334 // UNTIL
335 $this->buildUntilSelection($tpl);
336
337 $a_tpl->setCurrentBlock("prop_custom");
338 $a_tpl->setVariable("CUSTOM_CONTENT", $tpl->get());
339 $a_tpl->parseCurrentBlock();
340 }
341
345 protected function buildWeekDaySelection(ilTemplate $tpl): void
346 {
347 $days = array(0 => 'SU', 1 => 'MO', 2 => 'TU', 3 => 'WE', 4 => 'TH', 5 => 'FR', 6 => 'SA', 7 => 'SU');
348
349 $checked_days = array();
350 foreach ($this->recurrence->getBYDAYList() as $byday) {
351 if (in_array($byday, $days)) {
352 $checked_days[] = $byday;
353 }
354 }
355
356 for ($i = $this->user_settings->getWeekStart(); $i < 7 + $this->user_settings->getWeekStart(); $i++) {
357 $tpl->setCurrentBlock('byday_simple');
358
359 if (in_array($days[$i], $checked_days)) {
360 $tpl->setVariable('BYDAY_WEEKLY_CHECKED', 'checked="checked"');
361 }
362 $tpl->setVariable('TXT_ON', $this->lng->txt('cal_on'));
363 $tpl->setVariable('DAY_COUNT', $i);
364 $tpl->setVariable('BYDAY_WEEKLY_VAL', $days[$i]);
365 $tpl->setVariable('TXT_DAY_SHORT', ilCalendarUtil::_numericDayToString($i, false));
366 $tpl->parseCurrentBlock();
367 }
368 }
369
373 protected function buildMonthlyByDaySelection(ilTemplate $tpl): void
374 {
375 $byday_list = $this->recurrence->getBYDAYList();
376 $chosen_num_day = 1;
377 $chosen_day = 'MO';
378 $chosen = false;
379 foreach ($byday_list as $byday) {
380 if (preg_match('/^(-?\d)([A-Z][A-Z])/', $byday, $parsed) === 1) {
381 $chosen = true;
382 $chosen_num_day = $parsed[1];
383 $chosen_day = $parsed[2];
384 }
385 }
386 // check for last day
387 if (count($this->recurrence->getBYMONTHDAYList()) == 1) {
388 $bymonthday = $this->recurrence->getBYMONTHDAY();
389 if (in_array($bymonthday, array(1, 2, 3, 4, 5, -1))) {
390 $chosen = true;
391 $chosen_num_day = $bymonthday;
392 $chosen_day = 9;
393 }
394 }
395 // Check for first, second... last weekday
396 if (count($this->recurrence->getBYSETPOSList()) == 1) {
397 $bysetpos = $this->recurrence->getBYSETPOS();
398 if (in_array($bysetpos, array(1, 2, 3, 4, 5, -1))) {
399 if ($this->recurrence->getBYDAYList() == array('MO', 'TU', 'WE', 'TH', 'FR')) {
400 $chosen = true;
401 $chosen_num_day = $bysetpos;
402 $chosen_day = 8;
403 }
404 }
405 }
406
407 if ($chosen) {
408 $tpl->setVariable('M_BYDAY_CHECKED', 'checked="checked"');
409 }
410
411 $num_options = array(
412 1 => $this->lng->txt('cal_first'),
413 2 => $this->lng->txt('cal_second'),
414 3 => $this->lng->txt('cal_third'),
415 4 => $this->lng->txt('cal_fourth'),
416 5 => $this->lng->txt('cal_fifth'),
417 -1 => $this->lng->txt('cal_last')
418 );
419
420 $tpl->setVariable('SELECT_BYDAY_NUM_MONTHLY', ilLegacyFormElementsUtil::formSelect(
421 $chosen_num_day,
422 'monthly_byday_num',
423 $num_options,
424 false,
425 true,
426 0,
427 '',
428 array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_1');")
429 ));
430
431 $days = array(0 => 'SU', 1 => 'MO', 2 => 'TU', 3 => 'WE', 4 => 'TH', 5 => 'FR', 6 => 'SA', 7 => 'SU');
432
433 for ($i = $this->user_settings->getWeekStart(); $i < 7 + $this->user_settings->getWeekStart(); $i++) {
434 $days_select[$days[$i]] = ilCalendarUtil::_numericDayToString($i);
435 }
436 $days_select[8] = $this->lng->txt('cal_weekday');
437 $days_select[9] = $this->lng->txt('cal_day_of_month');
438 $tpl->setVariable('SEL_BYDAY_DAY_MONTHLY', ilLegacyFormElementsUtil::formSelect(
439 $chosen_day,
440 'monthly_byday_day',
441 $days_select,
442 false,
443 true,
444 0,
445 '',
446 array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_1');")
447 ));
448 }
449
453 protected function buildMonthlyByMonthDaySelection(ilTemplate $tpl): void
454 {
455 $tpl->setVariable('TXT_IN', $this->lng->txt('cal_in'));
456
457 $chosen_day = 1;
458 $chosen = false;
459 if (count($bymonthday = $this->recurrence->getBYMONTHDAYList()) == 1) {
460 foreach ($bymonthday as $mday) {
461 if ($mday > 0 and $mday < 32) {
462 $chosen = true;
463 $chosen_day = $mday;
464 }
465 }
466 }
467
468 if ($chosen) {
469 $tpl->setVariable('M_BYMONTHDAY_CHECKED', 'checked="checked"');
470 }
471 $options = [];
472 for ($i = 1; $i < 32; $i++) {
473 $options[$i] = $i;
474 }
475 $tpl->setVariable('SELECT_BYMONTHDAY', ilLegacyFormElementsUtil::formSelect(
476 $chosen_day,
477 'monthly_bymonthday',
478 $options,
479 false,
480 true,
481 0,
482 '',
483 array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_2');")
484 ));
485 }
486
487 protected function buildYearlyByMonthDaySelection(ilTemplate $tpl): void
488 {
489 $tpl->setVariable('TXT_Y_EVERY', $this->lng->txt('cal_every'));
490
491 $chosen = false;
492 $chosen_month = 1;
493 $chosen_day = 1;
494 foreach ($this->recurrence->getBYMONTHList() as $month) {
495 if ($this->recurrence->getBYMONTHDAYList()) {
496 $chosen_month = $month;
497 $chosen = true;
498 break;
499 }
500 }
501 foreach ($this->recurrence->getBYMONTHDAYList() as $day) {
502 $chosen_day = $day;
503 }
504 $options = [];
505 for ($i = 1; $i < 32; $i++) {
506 $options[$i] = $i;
507 }
508 $tpl->setVariable('SELECT_BYMONTHDAY_NUM_YEARLY', ilLegacyFormElementsUtil::formSelect(
509 $chosen_day,
510 'yearly_bymonthday',
511 $options,
512 false,
513 true,
514 0,
515 '',
516 array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_2');")
517 ));
518
519 $options = array();
520 for ($m = 1; $m < 13; $m++) {
521 $options[$m] = ilCalendarUtil::_numericMonthToString($m);
522 }
523 $tpl->setVariable('SELECT_BYMONTH_YEARLY', ilLegacyFormElementsUtil::formSelect(
524 $chosen_month,
525 'yearly_bymonth_by_monthday',
526 $options,
527 false,
528 true,
529 0,
530 '',
531 array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_2');")
532 ));
533
534 if ($chosen) {
535 $tpl->setVariable('Y_BYMONTHDAY_CHECKED', 'checked="checked"');
536 }
537 }
538
539 protected function buildYearlyByDaySelection(ilTemplate $tpl): void
540 {
541 $tpl->setVariable('TXT_ON_THE', $this->lng->txt('cal_on_the'));
542
543 $chosen_num_day = 1;
544 $chosen_day = 'MO';
545 $chosen = false;
546 foreach ($this->recurrence->getBYDAYList() as $byday) {
547 if (preg_match('/^(-?\d)([A-Z][A-Z])/', $byday, $parsed) === 1) {
548 $chosen = true;
549 $chosen_num_day = $parsed[1];
550 $chosen_day = $parsed[2];
551 }
552 }
553
554 $num_options = array(
555 1 => $this->lng->txt('cal_first'),
556 2 => $this->lng->txt('cal_second'),
557 3 => $this->lng->txt('cal_third'),
558 4 => $this->lng->txt('cal_fourth'),
559 5 => $this->lng->txt('cal_fifth'),
560 -1 => $this->lng->txt('cal_last')
561 );
562
563 $tpl->setVariable('SELECT_BYDAY_NUM_YEARLY', ilLegacyFormElementsUtil::formSelect(
564 $chosen_num_day,
565 'yearly_byday_num',
566 $num_options,
567 false,
568 true,
569 0,
570 '',
571 array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")
572 ));
573
574 $days = array(0 => 'SU', 1 => 'MO', 2 => 'TU', 3 => 'WE', 4 => 'TH', 5 => 'FR', 6 => 'SA', 7 => 'SU');
575 $days_select = [];
576 for ($i = $this->user_settings->getWeekStart(); $i < 7 + $this->user_settings->getWeekStart(); $i++) {
577 $days_select[$days[$i]] = ilCalendarUtil::_numericDayToString($i);
578 }
579 $tpl->setVariable('SELECT_BYDAY_DAY_YEARLY', ilLegacyFormElementsUtil::formSelect(
580 $chosen_day,
581 'yearly_byday',
582 $days_select,
583 false,
584 true,
585 0,
586 '',
587 array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")
588 ));
589
590 $chosen = false;
591 $chosen_month = 1;
592 foreach ($this->recurrence->getBYMONTHList() as $month) {
593 if ($this->recurrence->getBYMONTHDAYList()) {
594 $chosen_month = $month;
595 $chosen = true;
596 break;
597 }
598 }
599 $options = array();
600 for ($m = 1; $m < 13; $m++) {
601 $options[$m] = ilCalendarUtil::_numericMonthToString($m);
602 }
603 $tpl->setVariable('SELECT_BYMONTH_BYDAY', ilLegacyFormElementsUtil::formSelect(
604 $chosen_month,
605 'yearly_bymonth_byday',
606 $options,
607 false,
608 true,
609 0,
610 '',
611 array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")
612 ));
613 }
614
618 protected function buildUntilSelection(ilTemplate $tpl): void
619 {
620 if ($this->isUnlimitedRecurrenceAllowed()) {
621 $tpl->setVariable('TXT_NO_ENDING', $this->lng->txt('cal_no_ending'));
622 }
623
624 $tpl->setVariable('TXT_UNTIL_CREATE', $this->lng->txt('cal_create'));
625 $tpl->setVariable('TXT_APPOINTMENTS', $this->lng->txt('cal_appointments'));
626
627 $tpl->setVariable('VAL_COUNT', $this->recurrence->getFrequenceUntilCount() ?:
628 2);
629
630 if ($this->recurrence->getFrequenceUntilDate()) {
631 $tpl->setVariable('UNTIL_END_CHECKED', 'checked="checked"');
632 } elseif ($this->recurrence->getFrequenceUntilCount() or !$this->isUnlimitedRecurrenceAllowed()) {
633 $tpl->setVariable('UNTIL_COUNT_CHECKED', 'checked="checked"');
634 } else {
635 $tpl->setVariable('UNTIL_NO_CHECKED', 'checked="checked"');
636 }
637
638 $tpl->setVariable('TXT_UNTIL_END', $this->lng->txt('cal_repeat_until'));
639 $dt = new ilDateTimeInputGUI('', 'until_end');
640 // no proper subform
641 // $dt->setRequired(true);
642 $dt->setDate(
643 $this->recurrence->getFrequenceUntilDate() ?: null
644 );
645 $tpl->setVariable('UNTIL_END_DATE', $dt->getTableFilterHTML());
646 }
647}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
Model of calendar entry recurrcences based on iCalendar-RFC-5545.
static _getInstanceByUserId(int $a_user_id)
static _numericDayToString(int $a_day, bool $a_long=true, ?ilLanguage $lng=null)
static _numericMonthToString(int $a_month, bool $a_long=true, ?ilLanguage $lng=null)
numeric month to string
This class represents a custom property in a property form.
This class represents a date/time property in a property form.
static formSelect( $selected, string $varname, array $options, bool $multiple=false, bool $direct_text=false, int $size=0, string $style_class="", array $attribs=[], bool $disabled=false)
Builds a select form field with options and shows the selected option first.
User class.
This class represents an input GUI for recurring events/appointments (course events or calendar appoi...
__construct(string $a_title, string $a_postvar)
getRecurrenceInputByTypeAsInt(string $input)
getRecurrenceInputByTypeAsString(string $input)
setEnabledSubForms(array $a_sub_forms)
set enabled subforms
buildMonthlyByMonthDaySelection(ilTemplate $tpl)
build monthly bymonthday selection
buildMonthlyByDaySelection(ilTemplate $tpl)
build monthly by day list (e.g second monday)
allowUnlimitedRecurrences(bool $a_status)
Allow unlimited recurrences.
buildYearlyByDaySelection(ilTemplate $tpl)
insert(ilTemplate $a_tpl)
@inheritDoc
buildUntilSelection(ilTemplate $tpl)
build selection for ending date
ilCalendarUserSettings $user_settings
buildYearlyByMonthDaySelection(ilTemplate $tpl)
ilCalendarRecurrence $recurrence
buildWeekDaySelection(ilTemplate $tpl)
build weekday checkboxes
setRecurrence(ilCalendarRecurrence $a_rec)
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26