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