owncalendar/lib/repeat.php

204 lines
6.2 KiB
PHP
Raw Normal View History

2012-05-01 14:16:12 +00: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.
*/
2012-06-30 15:08:00 +00:00
/**
2012-05-01 14:16:12 +00:00
* This class manages the caching of repeating events
* Events will be cached for the current year ± 5 years
*/
class OC_Calendar_Repeat{
2012-06-30 15:08:00 +00:00
/**
2012-05-01 14:16:12 +00:00
* @brief returns the cache of an event
2012-05-18 09:48:18 +00:00
* @param (int) $id - id of the event
2012-09-07 12:32:45 +00:00
* @return (array)
2012-05-01 14:16:12 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function get($id) {
2013-06-05 18:05:55 +00:00
$stmt = OCP\DB::prepare('SELECT * FROM `*PREFIX*clndr_repeat` WHERE `eventid` = ?');
2012-05-18 09:48:18 +00:00
$result = $stmt->execute(array($id));
$return = array();
2012-09-07 13:21:03 +00:00
while($row = $result->fetchRow()) {
2012-05-18 09:48:18 +00:00
$return[] = $row;
}
return $return;
}
2012-06-30 15:08:00 +00:00
/**
2012-05-12 21:16:11 +00:00
* @brief returns the cache of an event in a specific peroid
2012-05-18 09:48:18 +00:00
* @param (int) $id - id of the event
* @param (DateTime) $from - start for period in UTC
* @param (DateTime) $until - end for period in UTC
2012-05-18 09:48:18 +00:00
* @return (array)
2012-05-12 21:16:11 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function get_inperiod($id, $from, $until) {
2013-06-05 18:05:55 +00:00
$stmt = OCP\DB::prepare( 'SELECT * FROM `*PREFIX*clndr_repeat` WHERE `eventid` = ?'
.' AND ((`startdate` >= ? AND `startdate` <= ?)'
.' OR (`enddate` >= ? AND `enddate` <= ?))');
2012-06-08 20:22:16 +00:00
$result = $stmt->execute(array($id,
OC_Calendar_Object::getUTCforMDB($from), OC_Calendar_Object::getUTCforMDB($until),
OC_Calendar_Object::getUTCforMDB($from), OC_Calendar_Object::getUTCforMDB($until)));
2012-06-08 20:22:16 +00:00
$return = array();
2012-09-07 13:21:03 +00:00
while($row = $result->fetchRow()) {
2012-06-08 20:22:16 +00:00
$return[] = $row;
}
return $return;
2012-05-18 09:48:18 +00:00
}
2012-06-30 15:08:00 +00:00
/**
2012-05-12 21:16:11 +00:00
* @brief returns the cache of all repeating events of a calendar
* @param (int) $id - id of the calendar
2012-09-07 12:32:45 +00:00
* @return (array)
2012-05-01 14:16:12 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function getCalendar($id) {
2013-06-05 18:05:55 +00:00
$stmt = OCP\DB::prepare('SELECT * FROM `*PREFIX*clndr_repeat` WHERE `calid` = ?');
2012-06-08 20:22:16 +00:00
$result = $stmt->execute(array($id));
$return = array();
2012-09-07 13:21:03 +00:00
while($row = $result->fetchRow()) {
2012-06-08 20:22:16 +00:00
$return[] = $row;
}
return $return;
}
2012-06-30 15:08:00 +00:00
/**
2012-05-12 21:16:11 +00:00
* @brief returns the cache of all repeating events of a calendar in a specific period
* @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-08 20:22:16 +00:00
* @return (array)
2012-05-12 21:16:11 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function getCalendar_inperiod($id, $from, $until) {
2013-06-05 18:05:55 +00:00
$stmt = OCP\DB::prepare( 'SELECT * FROM `*PREFIX*clndr_repeat` WHERE `calid` = ?'
.' AND ((`startdate` >= ? AND `startdate` <= ?)'
.' OR (`enddate` >= ? AND `enddate` <= ?))');
2012-06-08 20:22:16 +00:00
$result = $stmt->execute(array($id,
$from, $until,
$from, $until));
$return = array();
2012-09-07 13:21:03 +00:00
while($row = $result->fetchRow()) {
2012-06-08 20:22:16 +00:00
$return[] = $row;
}
return $return;
}
2012-06-30 15:08:00 +00:00
/**
2012-05-01 14:16:12 +00:00
* @brief generates the cache the first time
2012-06-08 20:22:16 +00:00
* @param (int) id - id of the event
* @return (bool)
2012-05-01 14:16:12 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function generate($id) {
$event = OC_Calendar_Object::find($id);
2012-09-07 13:21:03 +00:00
if($event['repeating'] == 0) {
2012-06-08 20:22:16 +00:00
return false;
}
$object = OC_VObject::parse($event['calendardata']);
$start = new DateTime('01-01-' . date('Y') . ' 00:00:00', new DateTimeZone('UTC'));
2012-06-08 20:22:16 +00:00
$start->modify('-5 years');
$end = new DateTime('31-12-' . date('Y') . ' 23:59:59', new DateTimeZone('UTC'));
2012-06-08 20:22:16 +00:00
$end->modify('+5 years');
$object->expand($start, $end);
2012-09-07 13:21:03 +00:00
foreach($object->getComponents() as $vevent) {
if(!($vevent instanceof Sabre\VObject\Component\VEvent)) {
continue;
}
$startenddate = OC_Calendar_Object::generateStartEndDate($vevent->DTSTART, OC_Calendar_Object::getDTEndFromVEvent($vevent), ($vevent->DTSTART->getDateType() == Sabre\VObject\Property\DateTime::DATE)?true:false, 'UTC');
2013-06-05 18:05:55 +00:00
$stmt = OCP\DB::prepare('INSERT INTO `*PREFIX*clndr_repeat` (`eventid`,`calid`,`startdate`,`enddate`) VALUES(?,?,?,?)');
$stmt->execute(array($id,OC_Calendar_Object::getCalendarid($id),$startenddate['start'],$startenddate['end']));
}
return true;
2012-06-01 09:30:54 +00:00
}
2012-06-30 15:08:00 +00:00
/**
2012-05-12 21:16:11 +00:00
* @brief generates the cache the first time for all repeating event of an calendar
* @param (int) id - id of the calendar
* @return (bool)
2012-05-12 21:16:11 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function generateCalendar($id) {
$allobjects = OC_Calendar_Object::all($id);
2012-09-07 13:21:03 +00:00
foreach($allobjects as $event) {
self::generate($event['id']);
}
return true;
2012-06-01 09:30:54 +00:00
}
2012-06-30 15:08:00 +00:00
/**
2012-05-01 14:16:12 +00:00
* @brief updates an event that is already cached
* @param (int) id - id of the event
* @return (bool)
2012-05-01 14:16:12 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function update($id) {
self::clean($id);
self::generate($id);
return true;
2012-06-01 09:30:54 +00:00
}
2012-06-30 15:08:00 +00:00
/**
2012-05-12 21:16:11 +00:00
* @brief updates all repating events of a calendar that are already cached
* @param (int) id - id of the calendar
* @return (bool)
2012-05-12 21:16:11 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function updateCalendar($id) {
self::cleanCalendar($id);
self::generateCalendar($id);
return true;
2012-06-01 09:30:54 +00:00
}
2012-06-30 15:08:00 +00:00
/**
2012-05-01 14:16:12 +00:00
* @brief checks if an event is already cached
* @param (int) id - id of the event
* @return (bool)
2012-05-01 14:16:12 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function is_cached($id) {
if(count(self::get($id)) != 0) {
return true;
}else{
return false;
}
2012-06-01 09:30:54 +00:00
}
2012-06-30 15:08:00 +00:00
/**
2012-06-11 15:13:20 +00:00
* @brief checks if an event is already cached in a specific period
* @param (int) id - id of the event
* @param (DateTime) $from - start for period in UTC
* @param (DateTime) $until - end for period in UTC
2012-06-11 15:13:20 +00:00
* @return (bool)
*/
2012-09-07 13:21:03 +00:00
public static function is_cached_inperiod($id, $start, $end) {
if(count(self::get_inperiod($id, $start, $end)) != 0) {
2012-06-11 15:13:20 +00:00
return true;
}else{
return false;
}
}
2012-06-30 15:08:00 +00:00
/**
2012-06-01 09:30:54 +00:00
* @brief checks if a whole calendar is already cached
* @param (int) id - id of the calendar
* @return (bool)
2012-06-01 09:30:54 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function is_calendar_cached($id) {
$cachedevents = count(self::getCalendar($id));
$repeatingevents = 0;
$allevents = OC_Calendar_Object::all($id);
2012-09-07 13:21:03 +00:00
foreach($allevents as $event) {
if($event['repeating'] === 1) {
$repeatingevents++;
}
}
2012-09-07 13:21:03 +00:00
if($cachedevents < $repeatingevents) {
return false;
}else{
return true;
}
2012-06-01 09:30:54 +00:00
}
2012-06-30 15:08:00 +00:00
/**
2012-05-01 14:16:12 +00:00
* @brief removes the cache of an event
* @param (int) id - id of the event
* @return (bool)
2012-05-01 14:16:12 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function clean($id) {
2013-06-05 18:05:55 +00:00
$stmt = OCP\DB::prepare('DELETE FROM `*PREFIX*clndr_repeat` WHERE `eventid` = ?');
$stmt->execute(array($id));
2012-06-01 09:30:54 +00:00
}
2012-06-30 15:08:00 +00:00
/**
2012-05-01 14:16:12 +00:00
* @brief removes the cache of all events of a calendar
* @param (int) id - id of the calendar
* @return (bool)
2012-05-01 14:16:12 +00:00
*/
2012-09-07 13:21:03 +00:00
public static function cleanCalendar($id) {
2013-06-05 18:05:55 +00:00
$stmt = OCP\DB::prepare('DELETE FROM `*PREFIX*clndr_repeat` WHERE `calid` = ?');
$stmt->execute(array($id));
2012-06-01 09:30:54 +00:00
}
}