summaryrefslogtreecommitdiffstats
path: root/apps/calendar/lib/calendar.php
blob: 1bd203151cf6bc9e145703a68c42585d6945323d (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
 * ownCloud - Calendar
 *
 * @author Jakob Sack
 * @copyright 2011 Jakob Sack mail@jakobsack.de
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*
 *
 * The following SQL statement is just a help for developers and will not be
 * executed!
 *
 * CREATE TABLE calendar_objects (
 *     id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
 *     calendarid INTEGER UNSIGNED NOT NULL,
 *     objecttype VARCHAR(40) NOT NULL,
 *     startdate DATETIME,
 *     enddate DATETIME,
 *     repeating INT(1),
 *     summary VARCHAR(255),
 *     calendardata TEXT,
 *     uri VARCHAR(100),
 *     lastmodified INT(11)
 * );
 * 
 * CREATE TABLE calendar_calendars (
 *     id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
 *     userid VARCHAR(255),
 *     displayname VARCHAR(100),
 *     uri VARCHAR(100),
 *     ctag INTEGER UNSIGNED NOT NULL DEFAULT '0',
 *     description TEXT,
 *     calendarorder INTEGER UNSIGNED NOT NULL DEFAULT '0',
 *     calendarcolor VARCHAR(10),
 *     timezone TEXT,
 *     components VARCHAR(20)
 * );
 */

/**
 * This class manages our calendars
 */
class OC_Calendar_Calendar{
	public static function allCalendars($uid){
		$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*calendar_calendars WHERE userid = ?' );
		$result = $stmt->execute(array($uid));
		
		$calendars = array();
		while( $row = $result->fetchRow()){
			$calendars[] = $row;
		}

		return $calendars;
	}
	
	public static function allCalendarsWherePrincipalURIIs($principaluri){
		$uid = self::extractUserID($principaluri);
		return self::allCalendars($uid);
	}

	public static function findCalendar($id){
		$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*calendar_calendars WHERE id = ?' );
		$result = $stmt->execute(array($id));

		return $result->fetchRow();
	}

	public static function addCalendar($userid,$name,$description,$components='VEVENT,VTODO',$timezone=null,$order=0,$color=null){
		$all = self::allCalendars($userid);
		$uris = array();
		foreach($all as $i){
			$uris[] = $i['uri'];
		}

		$uri = self::createURI($name, $uris );

		$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*calendar_calendars (userid,displayname,uri,ctag,description,calendarorder,calendarcolor,timezone,components) VALUES(?,?,?,?,?,?,?,?,?)' );
		$result = $stmt->execute(array($userid,$name,$uri,1,$description,$order,$color,$timezone,$components));

		return OC_DB::insertid();
	}

	public static function addCalendarFromDAVData($principaluri,$uri,$name,$description,$components,$timezone,$order,$color){
		$userid = self::extractUserID($principaluri);
		
		$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*calendar_calendars (userid,displayname,uri,ctag,description,calendarorder,calendarcolor,timezone,components) VALUES(?,?,?,?,?,?,?,?,?)' );
		$result = $stmt->execute(array($userid,$name,$uri,1,$description,$order,$color,$timezone,$components));

		return OC_DB::insertid();
	}

	public static function editCalendar($id,$name=null,$description=null,$components=null,$timezone=null,$order=null,$color=null){
		// Need these ones for checking uri
		$calendar = self::find($id);

		// Keep old stuff
		if(is_null($name)) $name = $calendar['name'];
		if(is_null($description)) $description = $calendar['description'];
		if(is_null($components)) $components = $calendar['components'];
		if(is_null($timezone)) $timezone = $calendar['timezone'];
		if(is_null($order)) $order = $calendar['order'];
		if(is_null($color)) $color = $calendar['color'];
		
		$stmt = OC_DB::prepare( 'UPDATE *PREFIX*calendar_calendars SET displayname=?,description=?,calendarorder=?,calendarcolor=?,timezone=?,components=?,ctag=ctag+1 WHERE id=?' );
		$result = $stmt->execute(array($name,$description,$order,$color,$timezone,$components,$id));

		return true;
	}

	public static function touchCalendar($id){
		$stmt = OC_DB::prepare( 'UPDATE *PREFIX*calendar_calendars SET ctag = ctag + 1 WHERE id = ?' );
		$stmt->execute(array($id));

		return true;
	}

	public static function deleteCalendar($id){
		$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*calendar_calendars WHERE id = ?' );
		$stmt->execute(array($id));
		
		$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*calendar_objects WHERE calendarid = ?' );
		$stmt->execute(array($id));

		return true;
	}

	public static function allCalendarObjects($id){
		$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*calendar_objects WHERE calendarid = ?' );
		$result = $stmt->execute(array($id));

		$calendarobjects = array();
		while( $row = $result->fetchRow()){
			$calendarobjects[] = $row;
		}

		return $calendarobjects;
	}
	
	public static function findCalendarObject($id){
		$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*calendar_objects WHERE id = ?' );
		$result = $stmt->execute(array($id));

		return $result->fetchRow();
	}

	public static function findCalendarObjectWhereDAVDataIs($cid,$uri){
		$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*calendar_objects WHERE calendarid = ? AND uri = ?' );
		$result = $stmt->execute(array($cid,$uri));

		return $result->fetchRow();
	}

	public static function addCalendarObject($id,$data){
		$object = Sabre_VObject_Reader::read($data);
		list($startdate,$enddate,$summary,$repeating,$uid) = self::extractData($object);

		$uri = null;
		if(is_null($uri)){
			$uid = self::createUID();
			$object->add('UID',$uid);
			$data = $object->serialize();
			$uri = $uid.'.ics';
		}
		else{
			$uri = $uid.'.ics';
		}

		$start = is_null($startdate)?null:$startdate->format('Y-m-d H:i:s');
		$end = is_null($enddate)?null:$enddate->format('Y-m-d H:i:s');
		$type = $object->name;
		$time = time();
		
		$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*calendar_objects (calendarid,objecttype,startdate,enddate,repeating,summary,calendardata,uri,lastmodified) VALUES(?,?,?,?,?,?,?,?,?)' );
		$result = $stmt->execute(array($id,$type,$start,$end,$repeating,$summary,$data,$uri,$time));

		self::touchCalendar($id);

		return OC_DB::insertid();
	}

	public static function addCalendarObjectFromDAVData($id,$uri,$data){
		$object = Sabre_VObject_Reader::read($data);
		list($startdate,$enddate,$summary,$repeating,$uid) = self::extractData($object);

		$start = is_null($startdate)?null:$startdate->format('Y-m-d H:i:s');
		$end = is_null($enddate)?null:$enddate->format('Y-m-d H:i:s');
		$type = $object->name;
		$time = time();

		$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*calendar_objects (calendarid,objecttype,startdate,enddate,repeating,summary,calendardata,uri,lastmodified) VALUES(?,?,?,?,?,?,?,?,?)' );
		// $result = $stmt->execute(array($id,$type,$start,$end,$repeating,$summary,$data,$uri,$time));
		$result = $stmt->execute(array($id,$type,'2011-08-17 14:00:00','2011-08-17 15:00:00',0,'baum',$data,$uri,$time));

		self::touchCalendar($id);

		return OC_DB::insertid();
	}

	public static function editCalendarObject($id, $data){
		$oldobject = self::findCard($id);
		
		$object = Sabre_VObject_Reader::read($data);
		list($startdate,$enddate,$summary,$repeating,$uid) = self::extractData($object);

		$stmt = OC_DB::prepare( 'UPDATE *PREFIX*calendar_objects SET objecttype=?,startdate=?,enddate=?,repeating=?,summary=?,calendardata=?, lastmodified = ? WHERE id = ?' );
		$result = $stmt->execute(array($object->name,(is_null($startdate)?null:$startdate->format('Y-m-d H:i:s')),(is_null($enddate)?null:$enddate->format('Y-m-d H:i:s')),$repeating,$summary,$data,time(),$id));

		self::touchCalendar($id);

		return true;
	}

	public static function editCalendarObjectFromDAVData($cid,$uri,$data){
		$oldobject = self::findCardWhereDAVDataIs($cid,$uri);
		
		$object = Sabre_VObject_Reader::read($data);
		list($startdate,$enddate,$summary,$repeating,$uid) = self::extractData($object);

		$stmt = OC_DB::prepare( 'UPDATE *PREFIX*calendar_objects SET objecttype=?,startdate=?,enddate=?,repeating=?,summary=?,calendardata=?, lastmodified = ? WHERE id = ?' );
		$result = $stmt->execute(array($object->name,(is_null($startdate)?null:$startdate->format('Y-m-d H:i:s')),(is_null($enddate)?null:$enddate->format('Y-m-d H:i:s')),$repeating,$summary,$data,time(),$oldobject['id']));

		self::touchCalendar($oldobject['calendarid']);

		return true;
	}
	
	public static function deleteCalendarObject($id){
		$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*calendar_objects WHERE id = ?' );
		$stmt->execute(array($id));

		return true;
	}

	public static function deleteCalendarObjectFromDAVData($cid,$uri){
		$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*calendar_objects WHERE calendarid = ? AND uri=?' );
		$stmt->execute(array($cid,$uri));

		return true;
	}
	
	public static function createURI($name,$existing){
		$name = strtolower($name);
		$newname = $name;
		$i = 1;
		while(in_array($newname,$existing)){
			$newname = $name.$i;
			$i = $i + 1;
		}
		return $newname;
	}

	public static function createUID(){
		return substr(md5(rand().time()),0,10);
	}
	
	public static function extractUserID($principaluri){
		list($prefix,$userid) = Sabre_DAV_URLUtil::splitPath($principaluri);
		return $userid;
	}

	protected static function extractData($object){
		$return = array(null,null,'',false,null);
		foreach($object->children as $property){
			if($property->name == 'DTSTART'){
				$return[0] = $property->getDateTime();
			}
			elseif($property->name == 'DTEND'){
				$return[1] = $property->getDateTime();
			}
			elseif($property->name == 'SUMMARY'){
				$return[2] = $property->value;
			}
			elseif($property->name == 'RRULE'){
				$return[3] = true;
			}
			elseif($property->name == 'UID'){
				$return[4] = $property->value;
			}
		}
		return $return;
	}
}