ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  if (!$this->loadRecurrence()) {
89  return false;
90  }
91 
92  if ($_POST['frequence'] == 'NONE') {
93  return true;
94  }
95 
96  if (!isset($_POST['until_type']) or $_POST['until_type'] == self::REC_LIMITED) {
97  if ($_POST['count'] <= 0 or $_POST['count'] >= 100) {
98  $this->setAlert($lng->txt("cal_rec_err_limit"));
99  return false;
100  }
101  }
102 
103 
104  return true;
105  }
106 
112  protected function loadRecurrence()
113  {
114  if (!$this->getRecurrence() instanceof ilCalendarRecurrence) {
115  return false;
116  }
117 
118 
119  switch ($_POST['frequence']) {
120  case IL_CAL_FREQ_DAILY:
121  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
122  $this->getRecurrence()->setInterval((int) $_POST['count_DAILY']);
123  break;
124 
125  case IL_CAL_FREQ_WEEKLY:
126  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
127  $this->getRecurrence()->setInterval((int) $_POST['count_WEEKLY']);
128  if (is_array($_POST['byday_WEEKLY'])) {
129  $this->getRecurrence()->setBYDAY(ilUtil::stripSlashes(implode(',', $_POST['byday_WEEKLY'])));
130  }
131  break;
132 
133  case IL_CAL_FREQ_MONTHLY:
134  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
135  $this->getRecurrence()->setInterval((int) $_POST['count_MONTHLY']);
136  switch ((int) $_POST['subtype_MONTHLY']) {
137  case 0:
138  // nothing to do;
139  break;
140 
141  case 1:
142  switch ((int) $_POST['monthly_byday_day']) {
143  case 8:
144  // Weekday
145  $this->getRecurrence()->setBYSETPOS((int) $_POST['monthly_byday_num']);
146  $this->getRecurrence()->setBYDAY('MO,TU,WE,TH,FR');
147  break;
148 
149  case 9:
150  // Day of month
151  $this->getRecurrence()->setBYMONTHDAY((int) $_POST['monthly_byday_num']);
152  break;
153 
154  default:
155  $this->getRecurrence()->setBYDAY((int) $_POST['monthly_byday_num'] . $_POST['monthly_byday_day']);
156  break;
157  }
158  break;
159 
160  case 2:
161  $this->getRecurrence()->setBYMONTHDAY((int) $_POST['monthly_bymonthday']);
162  break;
163  }
164  break;
165 
166  case IL_CAL_FREQ_YEARLY:
167  $this->getRecurrence()->setFrequenceType($_POST['frequence']);
168  $this->getRecurrence()->setInterval((int) $_POST['count_YEARLY']);
169  switch ((int) $_POST['subtype_YEARLY']) {
170  case 0:
171  // nothing to do;
172  break;
173 
174  case 1:
175  $this->getRecurrence()->setBYMONTH((int) $_POST['yearly_bymonth_byday']);
176  $this->getRecurrence()->setBYDAY((int) $_POST['yearly_byday_num'] . $_POST['yearly_byday']);
177  break;
178 
179  case 2:
180  $this->getRecurrence()->setBYMONTH((int) $_POST['yearly_bymonth_by_monthday']);
181  $this->getRecurrence()->setBYMONTHDAY((int) $_POST['yearly_bymonthday']);
182  break;
183  }
184  break;
185  }
186 
187  // UNTIL
188  switch ((int) $_POST['until_type']) {
189  case 1:
190  $this->getRecurrence()->setFrequenceUntilDate(null);
191  // nothing to do
192  break;
193 
194  case 2:
195  $this->getRecurrence()->setFrequenceUntilDate(null);
196  $this->getRecurrence()->setFrequenceUntilCount((int) $_POST['count']);
197  break;
198 
199  case 3:
200  $dt = new ilDateTimeInputGUI('', 'until_end');
201  $dt->setRequired(true);
202  if ($dt->checkInput()) {
203  $this->getRecurrence()->setFrequenceUntilCount(0);
204  $this->getRecurrence()->setFrequenceUntilDate($dt->getDate());
205  } else {
206  return false;
207  }
208  break;
209  }
210 
211  return true;
212  }
213 
214 
222  public function setRecurrence($a_rec)
223  {
224  $this->recurrence = $a_rec;
225  }
226 
231  public function getRecurrence()
232  {
233  return $this->recurrence;
234  }
235 
241  public function allowUnlimitedRecurrences($a_status)
242  {
243  $this->allow_unlimited_recurrences = $a_status;
244  }
245 
251  {
253  }
254 
262  public function setEnabledSubForms($a_sub_forms)
263  {
264  $this->enabled_subforms = $a_sub_forms;
265  }
266 
273  public function getEnabledSubForms()
274  {
276  }
277 
285  public function insert($a_tpl)
286  {
287  $tpl = new ilTemplate('tpl.recurrence_input.html', true, true, 'Services/Calendar');
288 
289  $options = array('NONE' => $this->lng->txt('cal_no_recurrence'));
290  if (in_array(IL_CAL_FREQ_DAILY, $this->getEnabledSubForms())) {
291  $options[IL_CAL_FREQ_DAILY] = $this->lng->txt('cal_daily');
292  }
293  if (in_array(IL_CAL_FREQ_WEEKLY, $this->getEnabledSubForms())) {
294  $options[IL_CAL_FREQ_WEEKLY] = $this->lng->txt('cal_weekly');
295  }
296  if (in_array(IL_CAL_FREQ_MONTHLY, $this->getEnabledSubForms())) {
297  $options[IL_CAL_FREQ_MONTHLY] = $this->lng->txt('cal_monthly');
298  }
299  if (in_array(IL_CAL_FREQ_YEARLY, $this->getEnabledSubForms())) {
300  $options[IL_CAL_FREQ_YEARLY] = $this->lng->txt('cal_yearly');
301  }
302 
303  $tpl->setVariable('FREQUENCE', ilUtil::formSelect(
304  $this->recurrence->getFrequenceType(),
305  'frequence',
306  $options,
307  false,
308  true,
309  '',
310  '',
311  array('onchange' => 'ilHideFrequencies();','id' => 'il_recurrence_1')
312  ));
313 
314  $tpl->setVariable('TXT_EVERY', $this->lng->txt('cal_every'));
315 
316  // DAILY
317  if (in_array(IL_CAL_FREQ_DAILY, $this->getEnabledSubForms())) {
318  $tpl->setVariable('TXT_DAILY_FREQ_UNIT', $this->lng->txt('cal_day_s'));
319  $tpl->setVariable('COUNT_DAILY_VAL', $this->recurrence->getInterval());
320  }
321 
322  // WEEKLY
323  if (in_array(IL_CAL_FREQ_WEEKLY, $this->getEnabledSubForms())) {
324  $tpl->setVariable('TXT_WEEKLY_FREQ_UNIT', $this->lng->txt('cal_week_s'));
325  $tpl->setVariable('COUNT_WEEKLY_VAL', $this->recurrence->getInterval());
326  $this->buildWeekDaySelection($tpl);
327  }
328 
329  // MONTHLY
330  if (in_array(IL_CAL_FREQ_MONTHLY, $this->getEnabledSubForms())) {
331  $tpl->setVariable('TXT_MONTHLY_FREQ_UNIT', $this->lng->txt('cal_month_s'));
332  $tpl->setVariable('COUNT_MONTHLY_VAL', $this->recurrence->getInterval());
333  $tpl->setVariable('TXT_ON_THE', $this->lng->txt('cal_on_the'));
334  $tpl->setVariable('TXT_BYMONTHDAY', $this->lng->txt('cal_on_the'));
335  $tpl->setVariable('TXT_OF_THE_MONTH', $this->lng->txt('cal_of_the_month'));
338  }
339 
340  // YEARLY
341  if (in_array(IL_CAL_FREQ_YEARLY, $this->getEnabledSubForms())) {
342  $tpl->setVariable('TXT_YEARLY_FREQ_UNIT', $this->lng->txt('cal_year_s'));
343  $tpl->setVariable('COUNT_YEARLY_VAL', $this->recurrence->getInterval());
344  $tpl->setVariable('TXT_ON_THE', $this->lng->txt('cal_on_the'));
347  }
348 
349  // UNTIL
350  $this->buildUntilSelection($tpl);
351 
352  $a_tpl->setCurrentBlock("prop_custom");
353  $a_tpl->setVariable("CUSTOM_CONTENT", $tpl->get());
354  $a_tpl->parseCurrentBlock();
355  }
356 
363  protected function buildWeekDaySelection($tpl)
364  {
365  $days = array(0 => 'SU',1 => 'MO',2 => 'TU',3 => 'WE',4 => 'TH',5 => 'FR',6 => 'SA',7 => 'SU');
366 
367  $checked_days = array();
368  foreach ($this->recurrence->getBYDAYList() as $byday) {
369  if (in_array($byday, $days)) {
370  $checked_days[] = $byday;
371  }
372  }
373 
374  for ($i = (int) $this->user_settings->getWeekStart();$i < 7 + (int) $this->user_settings->getWeekStart();$i++) {
375  $tpl->setCurrentBlock('byday_simple');
376 
377  if (in_array($days[$i], $checked_days)) {
378  $tpl->setVariable('BYDAY_WEEKLY_CHECKED', 'checked="checked"');
379  }
380  $tpl->setVariable('TXT_ON', $this->lng->txt('cal_on'));
381  $tpl->setVariable('BYDAY_WEEKLY_VAL', $days[$i]);
382  $tpl->setVariable('TXT_DAY_SHORT', ilCalendarUtil::_numericDayToString($i, false));
383  $tpl->parseCurrentBlock();
384  }
385  }
386 
394  protected function buildMonthlyByDaySelection($tpl)
395  {
396  $byday_list = $this->recurrence->getBYDAYList();
397  $chosen_num_day = 1;
398  $chosen_day = 'MO';
399  $chosen = false;
400  foreach ($byday_list as $byday) {
401  if (preg_match('/^(-?\d)([A-Z][A-Z])/', $byday, $parsed) === 1) {
402  $chosen = true;
403  $chosen_num_day = $parsed[1];
404  $chosen_day = $parsed[2];
405  }
406  }
407  // check for last day
408  if (count($this->recurrence->getBYMONTHDAYList()) == 1) {
409  $bymonthday = $this->recurrence->getBYMONTHDAY();
410  if (in_array($bymonthday, array(1,2,3,4,5,-1))) {
411  $chosen = true;
412  $chosen_num_day = $bymonthday;
413  $chosen_day = 9;
414  }
415  }
416  // Check for first, second... last weekday
417  if (count($this->recurrence->getBYSETPOSList()) == 1) {
418  $bysetpos = $this->recurrence->getBYSETPOS();
419  if (in_array($bysetpos, array(1,2,3,4,5,-1))) {
420  if ($this->recurrence->getBYDAYList() == array('MO','TU','WE','TH','FR')) {
421  $chosen = true;
422  $chosen_num_day = $bysetpos;
423  $chosen_day = 8;
424  }
425  }
426  }
427 
428 
429 
430  if ($chosen) {
431  $tpl->setVariable('M_BYDAY_CHECKED', 'checked="checked"');
432  }
433 
434  $num_options = array(
435  1 => $this->lng->txt('cal_first'),
436  2 => $this->lng->txt('cal_second'),
437  3 => $this->lng->txt('cal_third'),
438  4 => $this->lng->txt('cal_fourth'),
439  5 => $this->lng->txt('cal_fifth'),
440  -1 => $this->lng->txt('cal_last'));
441 
442  $tpl->setVariable('SELECT_BYDAY_NUM_MONTHLY', ilUtil::formSelect(
443  $chosen_num_day,
444  'monthly_byday_num',
445  $num_options,
446  false,
447  true,
448  '',
449  '',
450  array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_1');")
451  ));
452 
453  $days = array(0 => 'SU',1 => 'MO',2 => 'TU',3 => 'WE',4 => 'TH',5 => 'FR',6 => 'SA',7 => 'SU');
454 
455  for ($i = (int) $this->user_settings->getWeekStart();$i < 7 + (int) $this->user_settings->getWeekStart();$i++) {
456  $days_select[$days[$i]] = ilCalendarUtil::_numericDayToString($i);
457  }
458  $days_select[8] = $this->lng->txt('cal_weekday');
459  $days_select[9] = $this->lng->txt('cal_day_of_month');
460  $tpl->setVariable('SEL_BYDAY_DAY_MONTHLY', ilUtil::formSelect(
461  $chosen_day,
462  'monthly_byday_day',
463  $days_select,
464  false,
465  true,
466  '',
467  '',
468  array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_1');")
469  ));
470  }
471 
480  {
481  $tpl->setVariable('TXT_IN', $this->lng->txt('cal_in'));
482 
483  $chosen_day = 1;
484  $chosen = false;
485  if (count($bymonthday = $this->recurrence->getBYMONTHDAYList()) == 1) {
486  foreach ($bymonthday as $mday) {
487  if ($mday > 0 and $mday < 32) {
488  $chosen = true;
489  $chosen_day = $mday;
490  }
491  }
492  }
493 
494  if ($chosen) {
495  $tpl->setVariable('M_BYMONTHDAY_CHECKED', 'checked="checked"');
496  }
497 
498  for ($i = 1; $i < 32;$i++) {
499  $options[$i] = $i;
500  }
501  $tpl->setVariable('SELECT_BYMONTHDAY', ilUtil::formSelect(
502  $chosen_day,
503  'monthly_bymonthday',
504  $options,
505  false,
506  true,
507  '',
508  '',
509  array('onchange' => "ilUpdateSubTypeSelection('sub_monthly_radio_2');")
510  ));
511  }
512 
521  {
522  $tpl->setVariable('TXT_Y_EVERY', $this->lng->txt('cal_every'));
523 
524 
525  $chosen = false;
526  $chosen_month = 1;
527  $chosen_day = 1;
528  foreach ($this->recurrence->getBYMONTHList() as $month) {
529  if ($this->recurrence->getBYMONTHDAYList()) {
530  $chosen_month = $month;
531  $chosen = true;
532  break;
533  }
534  }
535  foreach ($this->recurrence->getBYMONTHDAYList() as $day) {
536  $chosen_day = $day;
537  }
538 
539  for ($i = 1; $i < 32;$i++) {
540  $options[$i] = $i;
541  }
542  $tpl->setVariable('SELECT_BYMONTHDAY_NUM_YEARLY', ilUtil::formSelect(
543  $chosen_day,
544  'yearly_bymonthday',
545  $options,
546  false,
547  true,
548  '',
549  '',
550  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_2');")
551  ));
552 
553  $options = array();
554  for ($m = 1;$m < 13;$m++) {
556  }
557  $tpl->setVariable('SELECT_BYMONTH_YEARLY', ilUtil::formSelect(
558  $chosen_month,
559  'yearly_bymonth_by_monthday',
560  $options,
561  false,
562  true,
563  '',
564  '',
565  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_2');")
566  ));
567 
568 
569  if ($chosen) {
570  $tpl->setVariable('Y_BYMONTHDAY_CHECKED', 'checked="checked"');
571  }
572  }
573 
581  protected function buildYearlyByDaySelection($tpl)
582  {
583  $tpl->setVariable('TXT_ON_THE', $this->lng->txt('cal_on_the'));
584 
585  $chosen_num_day = 1;
586  $chosen_day = 'MO';
587  $chosen = false;
588  foreach ($this->recurrence->getBYDAYList() as $byday) {
589  if (preg_match('/^(-?\d)([A-Z][A-Z])/', $byday, $parsed) === 1) {
590  $chosen = true;
591  $chosen_num_day = $parsed[1];
592  $chosen_day = $parsed[2];
593  }
594  }
595 
596 
597  $num_options = array(
598  1 => $this->lng->txt('cal_first'),
599  2 => $this->lng->txt('cal_second'),
600  3 => $this->lng->txt('cal_third'),
601  4 => $this->lng->txt('cal_fourth'),
602  5 => $this->lng->txt('cal_fifth'),
603  -1 => $this->lng->txt('cal_last'));
604 
605  $tpl->setVariable('SELECT_BYDAY_NUM_YEARLY', ilUtil::formSelect(
606  $chosen_num_day,
607  'yearly_byday_num',
608  $num_options,
609  false,
610  true,
611  '',
612  '',
613  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")
614  ));
615 
616 
617  $days = array(0 => 'SU',1 => 'MO',2 => 'TU',3 => 'WE',4 => 'TH',5 => 'FR',6 => 'SA',7 => 'SU');
618  for ($i = (int) $this->user_settings->getWeekStart();$i < 7 + (int) $this->user_settings->getWeekStart();$i++) {
619  $days_select[$days[$i]] = ilCalendarUtil::_numericDayToString($i);
620  }
621  $tpl->setVariable('SELECT_BYDAY_DAY_YEARLY', ilUtil::formSelect(
622  $chosen_day,
623  'yearly_byday',
624  $days_select,
625  false,
626  true,
627  '',
628  '',
629  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")
630  ));
631 
632 
633  $chosen = false;
634  $chosen_month = 1;
635  foreach ($this->recurrence->getBYMONTHList() as $month) {
636  if ($this->recurrence->getBYMONTHDAYList()) {
637  $chosen_month = $month;
638  $chosen = true;
639  break;
640  }
641  }
642  $options = array();
643  for ($m = 1;$m < 13;$m++) {
645  }
646  $tpl->setVariable('SELECT_BYMONTH_BYDAY', ilUtil::formSelect(
647  $chosen_month,
648  'yearly_bymonth_byday',
649  $options,
650  false,
651  true,
652  '',
653  '',
654  array('onchange' => "ilUpdateSubTypeSelection('sub_yearly_radio_1');")
655  ));
656  }
657 
665  protected function buildUntilSelection($tpl)
666  {
667  if ($this->isUnlimitedRecurrenceAllowed()) {
668  $tpl->setVariable('TXT_NO_ENDING', $this->lng->txt('cal_no_ending'));
669  }
670 
671  $tpl->setVariable('TXT_UNTIL_CREATE', $this->lng->txt('cal_create'));
672  $tpl->setVariable('TXT_APPOINTMENTS', $this->lng->txt('cal_appointments'));
673 
674  $tpl->setVariable('VAL_COUNT', $this->recurrence->getFrequenceUntilCount() ?
675  $this->recurrence->getFrequenceUntilCount() :
676  2);
677 
678  if ($this->recurrence->getFrequenceUntilDate()) {
679  $tpl->setVariable('UNTIL_END_CHECKED', 'checked="checked"');
680  } elseif ($this->recurrence->getFrequenceUntilCount() or !$this->isUnlimitedRecurrenceAllowed()) {
681  $tpl->setVariable('UNTIL_COUNT_CHECKED', 'checked="checked"');
682  } else {
683  $tpl->setVariable('UNTIL_NO_CHECKED', 'checked="checked"');
684  }
685 
686  $tpl->setVariable('TXT_UNTIL_END', $this->lng->txt('cal_repeat_until'));
687  $dt = new ilDateTimeInputGUI('', 'until_end');
688  // no proper subform
689  // $dt->setRequired(true);
690  $dt->setDate(
691  $this->recurrence->getFrequenceUntilDate() ? $this->recurrence->getFrequenceUntilDate() : null
692  );
693  $tpl->setVariable('UNTIL_END_DATE', $dt->getTableFilterHTML());
694  }
695 }
const IL_CAL_FREQ_MONTHLY
__construct($a_title, $a_postvar)
Constructor.
static _numericDayToString($a_day, $a_long=true)
get
$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
Create styles array
The data for the language used.
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
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20