aboutsummaryrefslogtreecommitdiff
path: root/export_events.php
blob: 88c5822e743aefc55b07ee40096b02b08532bf5c (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

// Load Elgg engine
require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");

if(!is_plugin_enabled("event_connector"))
{	
	register_error(elgg_echo('event_connector:activation:failed'));
	forward();
}

//Load iCal class library
require_once("vendors/iCalcreator.class.php");
require_once("vendors/iCalUtilityFunctions.class.php");

$event = '';

$event_calendar_first_date = trim(get_plugin_setting('first_date', 'event_calendar'));
$event_calendar_last_date = trim(get_plugin_setting('last_date', 'event_calendar'));

$original_start_date = get_input('start_date',date('Y-m-d'));
if ( $event_calendar_first_date && ($original_start_date < $event_calendar_first_date) ) {
	$original_start_date = $event_calendar_first_date;
}
if ( $event_calendar_last_date && ($original_start_date > $event_calendar_last_date) ) {
	$original_start_date = $event_calendar_first_date;
}

// the default interval is one month
$day = 60*60*24;
$week = 7*$day;
$month = 31*$day;

$mode = trim(get_input('mode',''));

if ($mode == "day") {
	$start_date = $original_start_date;
	$end_date = $start_date;
	$start_ts = strtotime($start_date);
	$end_ts = strtotime($end_date)+$day-1;
} else if ($mode == "week") {
	$start_ts = strtotime($original_start_date);
	$start_ts -= date("w",$start_ts)*$day;
	$end_ts = $start_ts + 6*$day;
	$start_date = date('Y-m-d',$start_ts);
	$end_date = date('Y-m-d',$end_ts);
} else {
	$start_ts = strtotime($original_start_date);
	$month = date('m',$start_ts);
	$year = date('Y',$start_ts);
	$start_date = $year.'-'.$month.'-1';
	$end_date = $year.'-'.$month.'-'.getLastDayOfMonth($month,$year);
}

if ($event_calendar_first_date && ($start_date < $event_calendar_first_date))
	$start_date = $event_calendar_first_date;

if ($event_calendar_last_date && ($end_date > $event_calendar_last_date))
	$end_date = $event_calendar_last_date;

$start_ts = strtotime($start_date);

if ($mode == "day") {
	$end_ts = strtotime($end_date)+$day-1;
} else if ($mode == "week") {
	$end_ts = $start_ts + 6*$day;
} else {
	$end_ts = strtotime($end_date);
}

$group_guid = (int) get_input('group_guid',0);


$offset = (int) get_input('offset',0);
$limit = 10;
$filter = get_input('filter','all');
$region = get_input('region','-');
$events = array();
if ($filter == 'all') {
	$count = event_calendar_get_events_between($start_ts,$end_ts,true,$limit,$offset,$group_guid,$region);
	$events = event_calendar_get_events_between($start_ts,$end_ts,false,$limit,$offset,$group_guid,$region);
} else if ($filter == 'friends') {
	$user_guid = get_loggedin_userid();
	$count = event_calendar_get_events_for_friends_between($start_ts,$end_ts,true,$limit,$offset,$user_guid,$group_guid,$region);
	$events = event_calendar_get_events_for_friends_between($start_ts,$end_ts,false,$limit,$offset,$user_guid,$group_guid,$region);	
} else if ($filter == 'mine') {
	$user_guid = get_loggedin_userid();
	$count = event_calendar_get_events_for_user_between($start_ts,$end_ts,true,$limit,$offset,$user_guid,$group_guid,$region);
	$events = event_calendar_get_events_for_user_between($start_ts,$end_ts,false,$limit,$offset,$user_guid,$group_guid,$region);	
}

if(!$events) {		
	register_error(elgg_echo('event_connector:no_event'));
	forward($_SERVER['HTTP_REFERER']);
}

$timezone = get_plugin_setting('timezone', 'event_connector');

$config = array( 'UNIQUE_ID' => 'human-connect.com', 'FILENAME'=> 'ElggCalendar.ics', 'TZID' => $timezone );
$v = new vcalendar($config);

$v->setProperty( 'method', 'PUBLISH' );
$v->setProperty( "X-WR-TIMEZONE", "Europe/Paris" );
$v->setProperty( "calscale", "GREGORIAN" );
$v->setProperty( "version", "2.0" );

if(isloggedin())
	$v->setProperty( "X-WR-CALNAME", get_loggedin_user()->username. "Calendar" );
else
	$v->setProperty( "X-WR-CALNAME", "Elgg Calendar" );

iCalUtilityFunctions::createTimezone(&$v, $timezone);

foreach($events as $event){
	//set default beginning and ending time
	$hb = 8; $he = 18;
	$mb = $me = $sb = $se = 0;
	if($event->start_time) {
		$hb= (int)($event->start_time/60);
		$mb = $event->start_time%60;
	}
	
	if($event->end_time) {
		$he = (int)($event->end_time/60);
		$me = $event->end_time%60;
	}
	
	$vevent = $v->newComponent('vevent');
	
	if (isloggedin()) {
		if (event_calendar_has_personal_event($event_id,$_SESSION['user']->getGUID()))
			$confirmed = true;
	}
	if(!isset($event->end_date)) $event->end_date = $event->start_date;
	$start = array( 'year'=>date('Y', $event->start_date), 'month'=>date('m', $event->start_date), 'day'=>date('d', $event->start_date), 'hour'=>$hb, 'min'=>$mb, 'sec'=>$sb);
	$vevent->setProperty( 'dtstart', $start );
	$end = array( 'year'=>date('Y', $event->end_date), 'month'=>date('m', $event->end_date), 'day'=>date('d', $event->end_date), 'hour'=>$he, 'min'=>$me, 'sec'=>$se );
	$vevent->setProperty( 'dtend', $end );
	$vevent->setProperty( 'LOCATION', $event->venue );
	$vevent->setProperty( 'LAST_MODIFIED', $event->time_updated );
	$vevent->setProperty( 'summary', $event->title );
	$description = isset($event->description) && $event->description != "" ? $event->description : null;
	if(!$description  && $event->long_description)
		$description = $event->long_description;
	$vevent->setProperty( 'description',  $description);
	//doesn't display correctly in iCal
	//$vevent->setOrganizer(get_entity($event->owner_guid)->username.':MAILTO:'.get_entity($event->owner_guid)->email);
		
}

	$v->returnCalendar();
	
	
function getLastDayOfMonth($month,$year) {
	return idate('d', mktime(0, 0, 0, ($month + 1), 0, $year));
}