aboutsummaryrefslogtreecommitdiffstats
path: root/apps/calendar/ajax/events.php
blob: 687b8e38d44f70043a7cd9fc2f3ae03959983d24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
 * Copyright (c) 2011 Georg Ehrke <ownclouddev at georgswebsite dot de>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

require_once ('../../../lib/base.php');
require_once('../../../3rdparty/when/When.php');

OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('calendar');

$start = DateTime::createFromFormat('U', $_GET['start']);
$end = DateTime::createFromFormat('U', $_GET['end']);

if($_GET['calendar_id'] == 'shared_rw' || $_GET['calendar_id'] == 'shared_r'){
	$calendars = OC_Calendar_Share::allSharedwithuser(OC_USER::getUser(), OC_Calendar_Share::CALENDAR, 1, ($_GET['calendar_id'] == 'shared_rw')?'rw':'r');
	$events = array();
	foreach($calendars as $calendar){
		$calendarevents = OC_Calendar_Object::allInPeriod($calendar['calendarid'], $start, $end);
		$events = array_merge($events, $calendarevents);
	}
}else{
	$calendar = OC_Calendar_Calendar::find($_GET['calendar_id']);
	if($calendar['userid'] != OC_User::getUser()){
		OC_JSON::error();
		exit;
	}
	$events = OC_Calendar_Object::allInPeriod($_GET['calendar_id'], $start, $end);
	OC_Response::enableCaching(0);
	OC_Response::setETagHeader($calendar['ctag']);
}

$events = OC_Calendar_Object::allInPeriod($_GET['calendar_id'], $start, $end);
$user_timezone = OC_Preferences::getValue(OC_USER::getUser(), 'calendar', 'timezone', date_default_timezone_get());
$return = array();
foreach($events as $event){
	$object = OC_VObject::parse($event['calendardata']);
	$vevent = $object->VEVENT;

	$return_event = OC_Calendar_App::prepareForOutput($event, $vevent);

	$dtstart = $vevent->DTSTART;
	$dtend = OC_Calendar_Object::getDTEndFromVEvent($vevent);
	$start_dt = $dtstart->getDateTime();
	$end_dt = $dtend->getDateTime();
	if ($dtstart->getDateType() == Sabre_VObject_Element_DateTime::DATE){
		$return_event['allDay'] = true;
	}else{
		$return_event['allDay'] = false;
		$start_dt->setTimezone(new DateTimeZone($user_timezone));
		$end_dt->setTimezone(new DateTimeZone($user_timezone));
	}
	if($event['repeating'] == 1){
		$duration = (double) $end_dt->format('U') - (double) $start_dt->format('U');
		$r = new When();
		$r->recur($start_dt)->rrule((string) $vevent->RRULE);
		while($result = $r->next()){
			if($result < $start){
				continue;
			}
			if($result > $end){
				break;
			}
			if($return_event['allDay'] == true){
				$return_event['start'] = $result->format('Y-m-d');
				$return_event['end'] = date('Y-m-d', $result->format('U') + --$duration);
			}else{
				$return_event['start'] = $result->format('Y-m-d H:i:s');
				$return_event['end'] = date('Y-m-d H:i:s', $result->format('U') + $duration);
			}
			$return[] = $return_event;
		}
	}else{
		if($return_event['allDay'] == true){
			$return_event['start'] = $start_dt->format('Y-m-d');
			$end_dt->modify('-1 sec');
			$return_event['end'] = $end_dt->format('Y-m-d');
		}else{
			$return_event['start'] = $start_dt->format('Y-m-d H:i:s');
			$return_event['end'] = $end_dt->format('Y-m-d H:i:s');
		}
		$return[] = $return_event;
	}
}
OC_JSON::encodedPrint($return);
?>