]> git.datanom.net - webcal.git/blob - caldav/vevent.class.php
Initial upload
[webcal.git] / caldav / vevent.class.php
1 <?php
2 /* $Id$ */
3
4 require_once 'awl/iCalendar.php';
5 require_once 'caldavresource.class.php';
6 require_once 'icomponent.class.php';
7 require_once 'rruleparser.class.php';
8
9 class VEvent extends IComponent {
10
11 private $rulesParser;
12
13 function __construct($etag, $url, VTYPE $type, iCalendar $item, $new) {
14 parent::__construct($etag, $url, $type, $item, $new);
15 $this->rulesParser = new RRuleParser();
16 }
17
18 function isActive($start, $end) {
19 $res = FALSE;
20 if (!($start && $end))
21 return TRUE;
22 if (! CaldavRessource::isDateTime($start) ||
23 ! CaldavRessource::isDateTime($end))
24 throw new Exception(
25 "[$start,$end] Invalid CalDAV DateTime format");
26 $event = $this->getBaseComponent();
27 if ($start && !$end) {
28 if (CaldavRessource::datecmp(
29 $start, $event->GetPValue('DTSTART')) < 0)
30 $res = TRUE;
31 }
32 else {
33 if (CaldavRessource::datecmp(
34 $start, $event->GetPValue('DTSTART')) < 0 &&
35 CaldavRessource::datecmp(
36 $end, $event->GetPValue('DTEND')) > 0)
37 $res = TRUE;
38 }
39 return $res;
40 }
41
42 function getActiveDates($range_start = NULL, $range_end = NULL) {
43 $res = array();
44 $event = $this->getBaseComponent();
45 //print_r($event);
46 $start = $event->GetPValue('DTSTART');
47 $end = $event->GetPValue('DTEND');
48 //print "$start:$end<br/>";
49 if (! ($start && $end))
50 return $res;
51 $rrule = $event->GetPValue('RRULE');
52 if ($rrule) {
53 $this->rulesParser->setRule($rrule, $start, $end);
54 //print $this->rulesParser->__toString()."\n";
55 $res = $this->rulesParser->getEventDates(
56 $range_start, $range_end);
57 //print_r($res);
58 }
59 else {
60 if ($this->isActive($range_start, $range_end))
61 array_push($res, $start);
62 }
63 //var_dump($res);
64 return $res;
65 }
66
67 function getRRule() {
68 return $this->rulesParser;
69 }
70
71 function getAlarm() {
72 $alarm = $this->getComponent(VTYPE::VALARM);
73 // print_r($alarm);
74 if ($alarm)
75 $alarm = $alarm[0];
76 return $alarm;
77 }
78
79 function setProperty($name, $value) {
80 $component = $this->getBaseComponent();
81 $properties = $component->GetProperties();
82 $match = FALSE;
83 $update = FALSE;
84
85 if (count($properties) > 0) {
86 foreach ($properties as $property) {
87 //echo "B: " . $property->Name(). ":" . $property->Value() . "<br/>";
88 $test1 = explode(';', $name);
89 $test2 = explode(';', $property->Name());
90 if (strcasecmp($test1[0], $test2[0]) === 0) {
91 if (strcmp($property->Value(), $value) !== 0) {
92 $property->Value($value);
93 //echo "B: " . $property->Name(). ":" . $property->Value() . "<br/>";
94 $update = TRUE;
95 }
96 $match = TRUE;
97 }
98 }
99 }
100 if ($match == FALSE) {
101 $component->AddProperty(strtoupper($name), $value);
102 $update = TRUE;
103 }
104 else {
105 if ($update)
106 $component->SetProperties($properties);
107 }
108 if ($update) {
109 $this->addDefault($component);
110 $this->setDirty();
111 }
112 //$properties = $component->GetProperties();
113 //foreach ($properties as $property) {
114 // echo "A: " . $property->Name(). ":" . $property->Value() . "<br/>";
115 //}
116 //echo "<br/>";
117 //exit;
118 }
119
120 private function AddDefault(iCalComponent $component) {
121 $properties = $component->GetProperties();;
122 $now = gmdate("Ymd\THis\Z");
123 $a = array(1,1,1);
124 foreach ($properties as $property) {
125 //echo "D: " . $property->Name(). ":" . $property->Value() . "<br/>";
126 if (strcasecmp('DTSTAMP', $property->Name()) === 0) {
127 $property->Value($now);
128 $a[0] = 0;
129 }
130 if (strcasecmp('LAST-MODIFIED', $property->Name()) === 0) {
131 $property->Value($now);
132 $a[1] = 0;
133 }
134 if (strcasecmp('X-WEBCAL-GENERATION', $property->Name()) === 0) {
135 $property->Value('1');
136 $a[2] = 0;
137 }
138 }
139 for ($i = 0; $i < count($a); $i++) {
140 //echo $i.':'.$a[$i]."<br/>";
141 if ($a[$i]) {
142 switch ($i) {
143 case 0: $c['DTSTAMP'] = $now; break;
144 case 1: $c['LAST-MODIFIED'] = $now; break;
145 case 2: $c['X-WEBCAL-GENERATION'] = 1; break;
146 default: continue;
147 }
148 $key = key($c);
149 $val = $c[$key];
150 $component->AddProperty($key, $val);
151 $c = NULL;
152 }
153 }
154 }
155 }
This page took 0.073761 seconds and 6 git commands to generate.