ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCalendarRecurrence.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 include_once('./Services/Calendar/classes/class.ilDate.php');
25 
36 define('IL_CAL_FREQ_DAILY',ilCalendarRecurrence::FREQ_DAILY);
37 define('IL_CAL_FREQ_WEEKLY',ilCalendarRecurrence::FREQ_WEEKLY);
38 define('IL_CAL_FREQ_MONTHLY',ilCalendarRecurrence::FREQ_MONTHLY);
39 define('IL_CAL_FREQ_YEARLY',ilCalendarRecurrence::FREQ_YEARLY);
40 
42 {
43  const REC_RECURRENCE = 0;
44  const REC_EXCLUSION = 1;
45 
46  const FREQ_DAILY = 'DAILY';
47  const FREQ_WEEKLY = 'WEEKLY';
48  const FREQ_MONTHLY = 'MONTHLY';
49  const FREQ_YEARLY = 'YEARLY';
50 
51 
52  protected $db;
53 
54  private $recurrence_id;
55  private $cal_id;
57 
58  private $freq_type;
59  private $freq_until_type;
60  private $freq_until_date = null;
62 
63  private $interval;
64  private $byday;
65  private $byweekno;
66  private $bymonth;
67  private $bymonthday;
68  private $byyearday;
69  private $bysetpos;
70  private $weekstart;
71 
72  private $timezone = 'Europe/Berlin';
73 
81  public function __construct($a_rec_id = 0)
82  {
83  global $ilDB;
84 
85  $this->db = $ilDB;
86  $this->recurrence_id = $a_rec_id;
87  if($a_rec_id)
88  {
89  $this->read();
90  }
91  }
92 
101  public static function _delete($a_cal_id)
102  {
103  global $ilDB;
104 
105  $query = "DELETE FROM cal_recurrence_rules ".
106  "WHERE cal_id = ".$ilDB->quote($a_cal_id)." ";
107  $res = $ilDB->query($query);
108  }
109 
116  public function reset()
117  {
118  $this->setBYDAY('');
119  $this->setBYMONTHDAY('');
120  $this->setBYMONTH('');
121  $this->setBYSETPOS('');
122  $this->setBYWEEKNO('');
123  $this->setBYYEARDAY('');
124  $this->setFrequenceType('');
125  $this->setInterval(1);
126  $this->setFrequenceUntilCount(0);
127 
128  return true;
129  }
130 
137  public function getRecurrenceId()
138  {
139  return $this->recurrence_id;
140  }
141 
142 
150  public function setEntryId($a_id)
151  {
152  $this->cal_id = $a_id;
153  }
154 
162  public function setRecurrence($a_type)
163  {
164  $this->recurrence_type = $a_type;
165  }
166 
174  public function isRecurrence()
175  {
176  return $this->recurrence_type == self::REC_RECURRENCE;
177  }
178 
186  public function setFrequenceType($a_type)
187  {
188  $this->freq_type = $a_type;
189  }
190 
197  public function getFrequenceType()
198  {
199  return $this->freq_type;
200  }
201 
208  public function getFrequenceUntilDate()
209  {
210  return is_object($this->freq_until_date) ? $this->freq_until_date : null;
211  }
212 
219  public function setFrequenceUntilDate(ilDateTime $a_date)
220  {
221  $this->freq_until_date = $a_date;
222  }
223 
231  public function setFrequenceUntilCount($a_count)
232  {
233  $this->freq_until_count = $a_count;
234  }
235 
243  public function getFrequenceUntilCount()
244  {
246  }
247 
255  public function setInterval($a_interval)
256  {
257  $this->interval = $a_interval;
258  }
259 
266  public function getInterval()
267  {
268  return $this->interval ? $this->interval : 1;
269  }
270 
278  public function setBYDAY($a_byday)
279  {
280  $this->byday = $a_byday;
281  }
282 
289  public function getBYDAY()
290  {
291  return $this->byday;
292  }
293 
300  public function getBYDAYList()
301  {
302  if(!trim($this->getBYDAY()))
303  {
304  return array();
305  }
306  foreach(explode(',',$this->getBYDAY()) as $byday)
307  {
308  $bydays[] = trim($byday);
309  }
310  return $bydays ? $bydays : array();
311  }
312 
320  public function setBYWEEKNO($a_byweekno)
321  {
322  $this->byweekno = $a_byweekno;
323  }
324 
331  public function getBYWEEKNOList()
332  {
333  if(!trim($this->getBYWEEKNO()))
334  {
335  return array();
336  }
337  foreach(explode(',',$this->getBYWEEKNO()) as $week_num)
338  {
339  $weeks[] = (int) $week_num;
340  }
341  return $weeks ? $weeks : array();
342  }
343 
344 
351  public function getBYWEEKNO()
352  {
353  return $this->byweekno;
354  }
355 
363  public function setBYMONTH($a_by)
364  {
365  $this->bymonth = $a_by;
366  }
367 
374  public function getBYMONTH()
375  {
376  return $this->bymonth;
377  }
378 
385  public function getBYMONTHList()
386  {
387  if(!trim($this->getBYMONTH()))
388  {
389  return array();
390  }
391  foreach(explode(',',$this->getBYMONTH()) as $month_num)
392  {
393  $months[] = (int) $month_num;
394  }
395  return $months ? $months : array();
396  }
397 
405  public function setBYMONTHDAY($a_by)
406  {
407  $this->bymonthday = $a_by;
408  }
409 
416  public function getBYMONTHDAY()
417  {
418  return $this->bymonthday;
419  }
420 
426  public function getBYMONTHDAYList()
427  {
428  if(!trim($this->getBYMONTHDAY()))
429  {
430  return array();
431  }
432  foreach(explode(',',$this->getBYMONTHDAY()) as $month_num)
433  {
434  $months[] = (int) $month_num;
435  }
436  return $months ? $months : array();
437 
438  }
439 
440 
448  public function setBYYEARDAY($a_by)
449  {
450  $this->byyearday = $a_by;
451  }
452 
459  public function getBYYEARDAY()
460  {
461  return $this->byyearday;
462  }
463 
470  public function getBYYEARDAYList()
471  {
472  if(!trim($this->getBYYEARDAY()))
473  {
474  return array();
475  }
476  foreach(explode(',',$this->getBYYEARDAY()) as $year_day)
477  {
478  $days[] = (int) $year_day;
479  }
480  return $days ? $days : array();
481  }
482 
490  public function setBYSETPOS($a_by)
491  {
492  $this->bysetpos = $a_by;
493  }
494 
501  public function getBYSETPOS()
502  {
503  return $this->bysetpos;
504  }
505 
512  public function getBYSETPOSList()
513  {
514  if(!trim($this->getBYSETPOS()))
515  {
516  return array();
517  }
518  foreach(explode(',',$this->getBYSETPOS()) as $pos)
519  {
520  $positions[] = (int) $pos;
521  }
522  return $positions ? $positions : array();
523  }
524 
525 
533  public function setWeekstart($a_start)
534  {
535  $this->weekstart = $a_start;
536  }
537 
544  public function getWeekstart()
545  {
546  return $this->weekstart;
547  }
548 
555  public function getTimeZone()
556  {
557  return $this->timezone;
558  }
559 
567  public function setTimeZone($a_tz)
568  {
569  $this->timezone = $a_tz;
570  }
571 
578  public function validate()
579  {
581  if(!in_array($this->getFrequenceType(),$valid_frequences))
582  {
583  return false;
584  }
585  if($this->getFrequenceUntilCount() < 0)
586  {
587  return false;
588  }
589  if($this->getInterval() <= 0)
590  {
591  return false;
592  }
593  return true;
594  }
595 
596 
603  public function save()
604  {
605  $until_date = is_null($this->getFrequenceUntilDate()) ?
606  0 :
607  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
608 
609  $query = "INSERT INTO cal_recurrence_rules ".
610  "SET cal_id = ".$this->db->quote($this->cal_id).", ".
611  "cal_recurrence = 1,".
612  "freq_type = ".$this->db->quote($this->getFrequenceType()).", ".
613  "freq_until_date = ".$this->db->quote($until_date).", ".
614  "freq_until_count = ".$this->db->quote($this->getFrequenceUntilCount()).", ".
615  "intervall = ".$this->db->quote($this->getInterval()).", ".
616  "byday = ".$this->db->quote($this->getBYDAY()).", ".
617  "byweekno = ".$this->db->quote($this->getBYWEEKNO()).", ".
618  "bymonth = ".$this->db->quote($this->getBYMONTH()).", ".
619  "bymonthday = ".$this->db->quote($this->getBYMONTHDAY()).", ".
620  "byyearday = ".$this->db->quote($this->getBYYEARDAY()).", ".
621  "bysetpos = ".$this->db->quote($this->getBYSETPOS()).", ".
622  "weekstart = ".$this->db->quote($this->getWeekstart())." ";
623  $res = $this->db->query($query);
624  $this->recurrence_id = $this->db->getLastInsertId();
625  return true;
626  }
627 
634  public function update()
635  {
636  $until_date = is_null($this->getFrequenceUntilDate()) ?
637  0 :
638  $this->getFrequenceUntilDate()->get(IL_CAL_DATETIME,'','UTC');
639 
640  $query = "UPDATE cal_recurrence_rules SET ".
641  "cal_id = ".$this->db->quote($this->cal_id).", ".
642  "cal_recurrence = 1,".
643  "freq_type = ".$this->db->quote($this->getFrequenceType()).", ".
644  "freq_until_date = ".$this->db->quote($until_date).", ".
645  "freq_until_count = ".$this->db->quote($this->getFrequenceUntilCount()).", ".
646  "intervall = ".$this->db->quote($this->getInterval()).", ".
647  "byday = ".$this->db->quote($this->getBYDAY()).", ".
648  "byweekno = ".$this->db->quote($this->getBYWEEKNO()).", ".
649  "bymonth = ".$this->db->quote($this->getBYMONTH()).", ".
650  "bymonthday = ".$this->db->quote($this->getBYMONTHDAY()).", ".
651  "byyearday = ".$this->db->quote($this->getBYYEARDAY()).", ".
652  "bysetpos = ".$this->db->quote($this->getBYSETPOS()).", ".
653  "weekstart = ".$this->db->quote($this->getWeekstart())." ".
654  "WHERE rule_id = ".$this->db->quote($this->recurrence_id)." ";
655  $this->db->query($query);
656  return true;
657  }
658 
665  public function delete()
666  {
667  $query = "DELETE FROM cal_recurrence_rules ".
668  "WHERE rule_id = ".$this->db->quote($this->recurrence_id);
669  $this->db->query($query);
670  return true;
671  }
672 
679  private function read()
680  {
681  $query = "SELECT * FROM cal_recurrence_rules ".
682  "WHERE rule_id = ".$this->db->quote($this->recurrence_id)." ";
683  $res = $this->db->query($query);
684  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
685  {
686  $this->cal_id = $row->cal_id;
687  $this->recurrence_type = $row->cal_recurrence;
688  $this->freq_type = $row->freq_type;
689 
690  if($row->freq_until_date != '0000-00-00 00:00:00')
691  {
692  $this->freq_until_date = new ilDateTime($row->freq_until_date,IL_CAL_DATETIME,'UTC');
693  }
694  $this->freq_until_count = $row->freq_until_count;
695  $this->interval = $row->intervall;
696  $this->byday = $row->byday;
697  $this->byweekno = $row->byweekno;
698  $this->bymonth = $row->bymonth;
699  $this->bymonthday = $row->bymonthday;
700  $this->byyearday = $row->byyearday;
701  $this->bysetpos = $row->bysetpos;
702  $this->weekstart = $row->week_start;
703  }
704 
705  }
706 }
707 
708 
709 ?>