3 class AnimexxCalSourcePrivate extends AnimexxCalSource
9 public static function getNamespace()
11 return CALDAV_NAMESPACE_PRIVATE;
18 public function getPermissionsCalendar($user)
20 if ($user == $this->calendarDb->uid) return array("read"=> true, "write"=> true);
21 return array("read"=> false, "write"=> false);
26 * @param string $item_uri
27 * @param string $recurrence_uri
28 * @param null|array $item_arr
31 public function getPermissionsItem($user, $item_uri, $recurrence_uri, $item_arr = null)
33 $cal_perm = $this->getPermissionsCalendar($user);
34 if (!$cal_perm["read"]) return array("read"=> false, "write"=> false);
35 if (!$cal_perm["write"]) array("read"=> true, "write"=> false);
37 if ($item_arr === null) {
38 $x = q("SELECT `permission_edit` FROM %s%sjqcalendar WHERE `namespace` = %d AND `namespace_id` = %d AND `ical_uri` = '%s' AND `ical_recurr_uri` = '%s'",
39 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $this->getNamespace(), $this->namespace_id, dbesc($item_uri), dbesc($recurrence_uri)
41 if (!$x || count($x) == 0) return array("read"=> false, "write"=> false);
42 return array("read"=> true, "write"=> ($x[0]["permission_edit"]));
44 return array("read"=> true, "write"=> ($item_arr["permission_edit"]));
51 * @throws Sabre_DAV_Exception_NotFound
53 public function removeItem($uri){
54 $obj_alt = q("SELECT * FROM %s%sjqcalendar WHERE namespace = %d AND namespace_id = %d AND ical_uri = '%s'",
55 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $this->getNamespace(), $this->namespace_id, dbesc($uri));
57 if (count($obj_alt) == 0) throw new Sabre_DAV_Exception_NotFound("Not found");
59 $calendarBackend = new Sabre_CalDAV_Backend_Std();
60 $calendarBackend->deleteCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $obj_alt[0]["ical_uri"]);
67 * @param string $subject
69 * @param string $description
70 * @param string $location
72 * @param string $timezone
73 * @param bool $notification
74 * @param null $notification_type
75 * @param null $notification_value
76 * @throws Sabre_DAV_Exception_NotFound
77 * @throws Sabre_DAV_Exception_Conflict
79 public function updateItem($uri, $start, $end, $subject = "", $allday = false, $description = "", $location = "", $color = null, $timezone = "", $notification = true, $notification_type = null, $notification_value = null)
83 $usr_id = IntVal($this->calendarDb->uid);
85 $old = q("SELECT * FROM %s%sjqcalendar WHERE `uid` = %d AND `namespace` = %d AND `namespace_id` = %d AND `ical_uri` = '%s'",
86 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $usr_id, $this->getNamespace(), $this->namespace_id, dbesc($uri));
87 if (count($old) == 0) throw new Sabre_DAV_Exception_NotFound("Not Found 1");
88 $old_obj = new DBClass_friendica_jqcalendar($old[0]);
90 $calendarBackend = new Sabre_CalDAV_Backend_Std();
91 $obj = $calendarBackend->getCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $old_obj->ical_uri);
92 if (!$obj) throw new Sabre_DAV_Exception_NotFound("Not Found 2");
95 $v->setConfig('unique_id', $a->get_hostname());
97 $v->setMethod('PUBLISH');
98 $v->setProperty("x-wr-calname", "AnimexxCal");
99 $v->setProperty("X-WR-CALDESC", "Animexx Calendar");
100 $v->setProperty("X-WR-TIMEZONE", $a->timezone);
102 $obj["calendardata"] = icalendar_sanitize_string($obj["calendardata"]);
104 $v->parse($obj["calendardata"]);
105 /** @var $vevent vevent */
106 $vevent = $v->getComponent('vevent');
108 if (trim($vevent->getProperty('uid')) . ".ics" != $old_obj->ical_uri)
109 throw new Sabre_DAV_Exception_Conflict("URI != URI: " . $old_obj->ical_uri . " vs. " . trim($vevent->getProperty("uid")));
111 if ($end["year"] < $start["year"] ||
112 ($end["year"] == $start["year"] && $end["month"] < $start["month"]) ||
113 ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] < $start["day"]) ||
114 ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] < $start["hour"]) ||
115 ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] == $start["hour"] && $end["minute"] < $start["minute"]) ||
116 ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] == $start["hour"] && $end["minute"] == $start["minute"] && $end["second"] < $start["second"])
119 if ($end["hour"] < 23) $end["hour"]++;
120 } // DTEND muss <= DTSTART
122 if ($start["hour"] == 0 && $start["minute"] == 0 && $end["hour"] == 23 && $end["minute"] == 59) {
127 $vevent->setDtstart($start["year"], $start["month"], $start["day"], FALSE, FALSE, FALSE, FALSE, array("VALUE"=> "DATE"));
128 $end = mktime(0, 0, 0, $end["month"], $end["day"], $end["year"]) + 3600 * 24;
130 // If a DST change occurs on the current day
131 $end += date("Z", ($end - 3600*24)) - date("Z", $end);
133 $vevent->setDtend(date("Y", $end), date("m", $end), date("d", $end), FALSE, FALSE, FALSE, FALSE, array("VALUE"=> "DATE"));
135 $vevent->setDtstart($start["year"], $start["month"], $start["day"], $start["hour"], $start["minute"], $start["second"], FALSE, array("VALUE"=> "DATE-TIME"));
136 $vevent->setDtend($end["year"], $end["month"], $end["day"], $end["hour"], $end["minute"], $end["second"], FALSE, array("VALUE"=> "DATE-TIME"));
139 if ($subject != "") {
140 $vevent->setProperty('LOCATION', $location);
141 $vevent->setProperty('summary', $subject);
142 $vevent->setProperty('description', $description);
144 if (!is_null($color) && $color >= 0) $vevent->setProperty("X-ANIMEXX-COLOR", $color);
146 if (!$notification || $notification_type != null) {
147 $vevent->deleteComponent("VALARM");
150 $valarm = new valarm();
153 ($notification_type == "year" ? $notification_value : 0),
154 ($notification_type == "month" ? $notification_value : 0),
155 ($notification_type == "day" ? $notification_value : 0),
156 ($notification_type == "week" ? $notification_value : 0),
157 ($notification_type == "hour" ? $notification_value : 0),
158 ($notification_type == "minute" ? $notification_value : 0),
159 ($notification_type == "minute" ? $notification_value : 0),
161 ($notification_value > 0)
163 $valarm->setProperty("ACTION", "DISPLAY");
164 $valarm->setProperty("DESCRIPTION", $subject);
166 $vevent->setComponent($valarm);
171 $v->deleteComponent("vevent");
172 $v->setComponent($vevent, trim($vevent->getProperty("uid")));
173 $ical = $v->createCalendar();
175 $calendarBackend->updateCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $old_obj->ical_uri, $ical);
179 * @param array $start
181 * @param string $subject
182 * @param bool $allday
183 * @param string $description
184 * @param string $location
186 * @param string $timezone
187 * @param bool $notification
188 * @param null $notification_type
189 * @param null $notification_value
190 * @return array|string
192 public function addItem($start, $end, $subject, $allday = false, $description = "", $location = "", $color = null,
193 $timezone = "", $notification = true, $notification_type = null, $notification_value = null)
197 $v = new vcalendar();
198 $v->setConfig('unique_id', $a->get_hostname());
200 $v->setProperty('method', 'PUBLISH');
201 $v->setProperty("x-wr-calname", "AnimexxCal");
202 $v->setProperty("X-WR-CALDESC", "Animexx Calendar");
203 $v->setProperty("X-WR-TIMEZONE", $a->timezone);
205 $vevent = dav_create_vevent($start, $end, $allday);
206 $vevent->setLocation(icalendar_sanitize_string($location));
207 $vevent->setSummary(icalendar_sanitize_string($subject));
208 $vevent->setDescription(icalendar_sanitize_string($description));
210 if (!is_null($color) && $color >= 0) $vevent->setProperty("X-ANIMEXX-COLOR", $color);
212 if ($notification && $notification_type == null) {
214 $notification_type = "hour";
215 $notification_value = 24;
217 $notification_type = "minute";
218 $notification_value = 60;
222 $valarm = new valarm();
225 ($notification_type == "year" ? $notification_value : 0),
226 ($notification_type == "month" ? $notification_value : 0),
227 ($notification_type == "day" ? $notification_value : 0),
228 ($notification_type == "week" ? $notification_value : 0),
229 ($notification_type == "hour" ? $notification_value : 0),
230 ($notification_type == "minute" ? $notification_value : 0),
231 ($notification_type == "second" ? $notification_value : 0),
233 ($notification_value > 0)
235 $valarm->setAction("DISPLAY");
236 $valarm->setDescription($subject);
238 $vevent->setComponent($valarm);
242 $v->setComponent($vevent);
243 $ical = $v->createCalendar();
244 $obj_id = trim($vevent->getProperty("UID"));
246 $calendarBackend = new Sabre_CalDAV_Backend_Std();
247 $calendarBackend->createCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $obj_id . ".ics", $ical);
249 return $obj_id . ".ics";
252 private function jqcal2wdcal($row, $usr_id, $base_path) {
253 $evo = new DBClass_friendica_jqcalendar($row);
254 $not = q("SELECT COUNT(*) num FROM %s%snotifications WHERE `ical_uri` = '%s' AND `ical_recurr_uri` = '%s'",
255 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($row["ical_uri"]), $row["ical_recurr_uri"]
257 $editable = $this->getPermissionsItem($usr_id, $row["ical_uri"], $row["ical_recurr_uri"], $row);
258 $recurring = (is_null($evo->RecurringRule) || $evo->RecurringRule == "" || $evo->RecurringRule == "NULL" ? 0 : 1);
260 $end = wdcal_mySql2PhpTime($evo->EndTime);
261 if ($evo->IsAllDayEvent) $end -= 1;
264 "uri" => $evo->ical_uri,
265 "subject" => escape_tags($evo->Subject),
266 "start" => wdcal_mySql2PhpTime($evo->StartTime),
268 "is_allday" => $evo->IsAllDayEvent,
270 "is_recurring" => $recurring,
271 "color" => (is_null($evo->Color) || $evo->Color == "" ? $this->calendarDb->calendarcolor : $evo->Color),
272 "is_editable" => ($editable ? 1 : 0),
273 "is_editable_quick" => ($editable && !$recurring ? 1 : 0),
274 "location" => $evo->Location,
276 "has_notification" => ($not[0]["num"] > 0 ? 1 : 0),
277 "url_detail" => $base_path . $evo->ical_uri . "/",
278 "url_edit" => $base_path . $evo->ical_uri . "/edit/",
279 "special_type" => "",
287 * @param string $base_path
290 public function listItemsByRange($sd, $ed, $base_path)
293 $usr_id = IntVal($this->calendarDb->uid);
295 $von = wdcal_php2MySqlTime($sd);
296 $bis = wdcal_php2MySqlTime($ed);
298 // @TODO Events, die früher angefangen haben, aber noch andauern
299 $evs = q("SELECT * FROM %s%sjqcalendar WHERE `uid` = %d AND `namespace` = %d AND `namespace_id` = %d AND `starttime` between '%s' and '%s'",
300 CALDAV_SQL_DB, CALDAV_SQL_PREFIX,
301 $usr_id, $this->getNamespace(), $this->namespace_id, dbesc($von), dbesc($bis));
304 foreach ($evs as $row) $events[] = $this->jqcal2wdcal($row, $usr_id, $base_path);
311 * @throws Sabre_DAV_Exception_NotFound
314 public function getItemByUri($uri)
316 $usr_id = IntVal($this->calendarDb->uid);
317 $evs = q("SELECT * FROM %s%sjqcalendar WHERE `uid` = %d AND `namespace` = %d AND `namespace_id` = %d AND `ical_uri` = '%s'",
318 CALDAV_SQL_DB, CALDAV_SQL_PREFIX,
319 $usr_id, $this->getNamespace(), $this->namespace_id, dbesc($uri));
320 if (count($evs) == 0) throw new Sabre_DAV_Exception_NotFound();
321 return $this->jqcal2wdcal($evs[0], $usr_id);
329 public function getItemDetailRedirect($uri) {
330 return "/dav/wdcal/$uri/edit/";