2012-05-01 18:16:12 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Georg Ehrke <ownclouddev@georgswebsite.de>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This class manages the caching of repeating events
|
|
|
|
* Events will be cached for the current year ± 5 years
|
|
|
|
*/
|
|
|
|
class OC_Calendar_Repeat{
|
|
|
|
/*
|
|
|
|
* @brief returns the cache of an event
|
2012-05-18 13:48:18 +04:00
|
|
|
* @param (int) $id - id of the event
|
|
|
|
* @return (array)
|
2012-05-01 18:16:12 +04:00
|
|
|
*/
|
2012-05-18 13:48:18 +04:00
|
|
|
public static function get($id){
|
|
|
|
$stmt = OCP\DB::prepare('SELECT * FROM *PREFIX*calendar_repeat WHERE eventid = ?');
|
|
|
|
$result = $stmt->execute(array($id));
|
|
|
|
$return = array();
|
|
|
|
while($row = $result->fetchRow()){
|
|
|
|
$return[] = $row;
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2012-05-13 01:00:21 +04:00
|
|
|
/*
|
2012-05-13 01:16:11 +04:00
|
|
|
* @brief returns the cache of an event in a specific peroid
|
2012-05-18 13:48:18 +04:00
|
|
|
* @param (int) $id - id of the event
|
2012-06-15 13:39:19 +04:00
|
|
|
* @param (DateTime) $from - start for period in UTC
|
|
|
|
* @param (DateTime) $until - end for period in UTC
|
2012-05-18 13:48:18 +04:00
|
|
|
* @return (array)
|
2012-05-13 01:16:11 +04:00
|
|
|
*/
|
2012-05-18 13:48:18 +04:00
|
|
|
public static function get_inperiod($id, $from, $until){
|
2012-06-09 00:22:16 +04:00
|
|
|
$stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*calendar_repeat WHERE eventid = ?'
|
|
|
|
.' AND ((startdate >= ? AND startdate <= ?)'
|
2012-06-11 19:13:20 +04:00
|
|
|
.' OR (enddate >= ? AND enddate <= ?))');
|
2012-06-09 00:22:16 +04:00
|
|
|
$result = $stmt->execute(array($id,
|
2012-06-15 13:39:19 +04:00
|
|
|
OC_Calendar_Object::getUTCforMDB($from), OC_Calendar_Object::getUTCforMDB($until),
|
|
|
|
OC_Calendar_Object::getUTCforMDB($from), OC_Calendar_Object::getUTCforMDB($until)));
|
2012-06-09 00:22:16 +04:00
|
|
|
$return = array();
|
|
|
|
while($row = $result->fetchRow()){
|
|
|
|
$return[] = $row;
|
|
|
|
}
|
|
|
|
return $return;
|
2012-05-18 13:48:18 +04:00
|
|
|
}
|
2012-05-01 18:16:12 +04:00
|
|
|
/*
|
2012-05-13 01:16:11 +04:00
|
|
|
* @brief returns the cache of all repeating events of a calendar
|
2012-05-29 21:46:17 +04:00
|
|
|
* @param (int) $id - id of the calendar
|
|
|
|
* @return (array)
|
2012-05-01 18:16:12 +04:00
|
|
|
*/
|
2012-06-15 13:34:21 +04:00
|
|
|
public static function getCalendar($id){
|
2012-06-09 00:22:16 +04:00
|
|
|
$stmt = OCP\DB::prepare('SELECT * FROM *PREFIX*calendar_repeat WHERE calid = ?');
|
|
|
|
$result = $stmt->execute(array($id));
|
|
|
|
$return = array();
|
|
|
|
while($row = $result->fetchRow()){
|
|
|
|
$return[] = $row;
|
|
|
|
}
|
|
|
|
return $return;
|
2012-05-29 21:46:17 +04:00
|
|
|
}
|
2012-05-13 01:00:21 +04:00
|
|
|
/*
|
2012-05-13 01:16:11 +04:00
|
|
|
* @brief returns the cache of all repeating events of a calendar in a specific period
|
2012-05-29 21:46:17 +04:00
|
|
|
* @param (int) $id - id of the event
|
|
|
|
* @param (string) $from - start for period in UTC
|
|
|
|
* @param (string) $until - end for period in UTC
|
2012-06-09 00:22:16 +04:00
|
|
|
* @return (array)
|
2012-05-13 01:16:11 +04:00
|
|
|
*/
|
2012-06-15 13:34:21 +04:00
|
|
|
public static function getCalendar_inperiod($id, $from, $until){
|
2012-06-09 00:22:16 +04:00
|
|
|
$stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*calendar_repeat WHERE calid = ?'
|
|
|
|
.' AND ((startdate >= ? AND startdate <= ?)'
|
2012-06-11 19:13:20 +04:00
|
|
|
.' OR (enddate >= ? AND enddate <= ?))');
|
2012-06-09 00:22:16 +04:00
|
|
|
$result = $stmt->execute(array($id,
|
|
|
|
$from, $until,
|
|
|
|
$from, $until));
|
|
|
|
$return = array();
|
|
|
|
while($row = $result->fetchRow()){
|
|
|
|
$return[] = $row;
|
|
|
|
}
|
|
|
|
return $return;
|
2012-05-29 21:46:17 +04:00
|
|
|
}
|
2012-05-01 18:16:12 +04:00
|
|
|
/*
|
|
|
|
* @brief generates the cache the first time
|
2012-06-09 00:22:16 +04:00
|
|
|
* @param (int) id - id of the event
|
|
|
|
* @return (bool)
|
2012-05-01 18:16:12 +04:00
|
|
|
*/
|
2012-06-01 13:30:54 +04:00
|
|
|
public static function generate($id){
|
2012-06-11 17:36:48 +04:00
|
|
|
$event = OC_Calendar_Object::find($id);
|
2012-06-09 00:22:16 +04:00
|
|
|
if($event['repeating'] == 0){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$object = OC_VObject::parse($event['calendardata']);
|
2012-06-15 13:29:16 +04:00
|
|
|
$start = new DateTime('01-01-' . date('Y') . ' 00:00:00', new DateTimeZone('UTC'));
|
2012-06-09 00:22:16 +04:00
|
|
|
$start->modify('-5 years');
|
2012-06-15 13:29:16 +04:00
|
|
|
$end = new DateTime('31-12-' . date('Y') . ' 23:59:59', new DateTimeZone('UTC'));
|
2012-06-09 00:22:16 +04:00
|
|
|
$end->modify('+5 years');
|
|
|
|
$object->expand($start, $end);
|
2012-06-11 17:00:51 +04:00
|
|
|
foreach($object->getComponents() as $vevent){
|
2012-06-15 14:11:22 +04:00
|
|
|
if(!($vevent instanceof Sabre_VObject_Component_VEvent)){
|
2012-06-11 17:00:51 +04:00
|
|
|
continue;
|
|
|
|
}
|
2012-06-15 14:11:22 +04:00
|
|
|
$startenddate = OC_Calendar_Object::generateStartEndDate($vevent->DTSTART, OC_Calendar_Object::getDTEndFromVEvent($vevent), 'UTC');
|
2012-06-11 17:00:51 +04:00
|
|
|
$stmt = OCP\DB::prepare('INSERT INTO *PREFIX*calendar_repeat (eventid,calid,startdate,enddate) VALUES(?,?,?,?)');
|
2012-06-15 14:11:22 +04:00
|
|
|
$stmt->execute(array($id,OC_Calendar_Object::getCalendarid($id),$startenddate['start'],$startenddate['end']));
|
2012-06-11 17:00:51 +04:00
|
|
|
}
|
|
|
|
return true;
|
2012-06-01 13:30:54 +04:00
|
|
|
}
|
2012-05-13 01:16:11 +04:00
|
|
|
/*
|
|
|
|
* @brief generates the cache the first time for all repeating event of an calendar
|
2012-06-09 13:49:25 +04:00
|
|
|
* @param (int) id - id of the calendar
|
|
|
|
* @return (bool)
|
2012-05-13 01:16:11 +04:00
|
|
|
*/
|
2012-06-15 13:34:21 +04:00
|
|
|
public static function generateCalendar($id){
|
2012-06-09 13:49:25 +04:00
|
|
|
$allobjects = OC_Calendar_Object::all($id);
|
|
|
|
foreach($allobjects['id'] as $eventid){
|
|
|
|
self::generate($eventid);
|
|
|
|
}
|
|
|
|
return true;
|
2012-06-01 13:30:54 +04:00
|
|
|
}
|
2012-05-01 18:16:12 +04:00
|
|
|
/*
|
|
|
|
* @brief updates an event that is already cached
|
2012-06-09 13:49:25 +04:00
|
|
|
* @param (int) id - id of the event
|
|
|
|
* @return (bool)
|
2012-05-01 18:16:12 +04:00
|
|
|
*/
|
2012-06-01 13:30:54 +04:00
|
|
|
public static function update($id){
|
2012-06-09 13:49:25 +04:00
|
|
|
self::clean($id);
|
|
|
|
self::generate($id);
|
|
|
|
return true;
|
2012-06-01 13:30:54 +04:00
|
|
|
}
|
2012-05-13 01:16:11 +04:00
|
|
|
/*
|
|
|
|
* @brief updates all repating events of a calendar that are already cached
|
2012-06-09 13:49:25 +04:00
|
|
|
* @param (int) id - id of the calendar
|
|
|
|
* @return (bool)
|
2012-05-13 01:16:11 +04:00
|
|
|
*/
|
2012-06-15 13:34:21 +04:00
|
|
|
public static function updateCalendar($id){
|
|
|
|
self::cleanCalendar($id);
|
|
|
|
self::generateCalendar($id);
|
2012-06-09 13:49:25 +04:00
|
|
|
return true;
|
2012-06-01 13:30:54 +04:00
|
|
|
}
|
2012-05-01 18:16:12 +04:00
|
|
|
/*
|
|
|
|
* @brief checks if an event is already cached
|
2012-06-09 13:49:25 +04:00
|
|
|
* @param (int) id - id of the event
|
|
|
|
* @return (bool)
|
2012-05-01 18:16:12 +04:00
|
|
|
*/
|
2012-06-01 13:30:54 +04:00
|
|
|
public static function is_cached($id){
|
2012-06-11 19:13:20 +04:00
|
|
|
if(count(self::get($id)) != 0){
|
2012-06-09 13:49:25 +04:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-01 13:30:54 +04:00
|
|
|
}
|
2012-06-11 19:13:20 +04:00
|
|
|
/*
|
|
|
|
* @brief checks if an event is already cached in a specific period
|
|
|
|
* @param (int) id - id of the event
|
2012-06-15 13:39:19 +04:00
|
|
|
* @param (DateTime) $from - start for period in UTC
|
|
|
|
* @param (DateTime) $until - end for period in UTC
|
2012-06-11 19:13:20 +04:00
|
|
|
* @return (bool)
|
|
|
|
*/
|
|
|
|
public static function is_cached_inperiod($id, $start, $end){
|
|
|
|
if(count(self::get_inperiod($id, $start, $end)) != 0){
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-06-01 13:30:54 +04:00
|
|
|
/*
|
|
|
|
* @brief checks if a whole calendar is already cached
|
2012-06-09 13:49:25 +04:00
|
|
|
* @param (int) id - id of the calendar
|
|
|
|
* @return (bool)
|
2012-06-01 13:30:54 +04:00
|
|
|
*/
|
|
|
|
public static function is_calendar_cached($id){
|
2012-06-15 13:34:21 +04:00
|
|
|
$cachedevents = count(self::getCalendar($id));
|
2012-06-09 13:49:25 +04:00
|
|
|
$repeatingevents = 0;
|
|
|
|
$allevents = OC_Calendar_Object::all($id);
|
2012-06-11 23:43:24 +04:00
|
|
|
foreach($allevents as $event){
|
|
|
|
if($event['repeating'] === 1){
|
2012-06-09 13:49:25 +04:00
|
|
|
$repeatingevents++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($cachedevents < $repeatingevents){
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
return true;
|
|
|
|
}
|
2012-06-01 13:30:54 +04:00
|
|
|
}
|
2012-05-01 18:16:12 +04:00
|
|
|
/*
|
|
|
|
* @brief removes the cache of an event
|
2012-06-09 13:49:25 +04:00
|
|
|
* @param (int) id - id of the event
|
|
|
|
* @return (bool)
|
2012-05-01 18:16:12 +04:00
|
|
|
*/
|
2012-06-01 13:30:54 +04:00
|
|
|
public static function clean($id){
|
2012-06-09 13:49:25 +04:00
|
|
|
$stmt = OCP\DB::prepare('DELETE FROM *PREFIX*calendar_repeat WHERE eventid = ?');
|
|
|
|
$stmt->execute(array($id));
|
2012-06-01 13:30:54 +04:00
|
|
|
}
|
2012-05-01 18:16:12 +04:00
|
|
|
/*
|
|
|
|
* @brief removes the cache of all events of a calendar
|
2012-06-09 13:49:25 +04:00
|
|
|
* @param (int) id - id of the calendar
|
|
|
|
* @return (bool)
|
2012-05-01 18:16:12 +04:00
|
|
|
*/
|
2012-06-15 13:34:21 +04:00
|
|
|
public static function cleanCalendar($id){
|
2012-06-09 13:49:25 +04:00
|
|
|
$stmt = OCP\DB::prepare('DELETE FROM *PREFIX*calendar_repeat WHERE calid = ?');
|
|
|
|
$stmt->execute(array($id));
|
2012-06-01 13:30:54 +04:00
|
|
|
}
|
2012-05-29 21:46:17 +04:00
|
|
|
}
|