Blame view
sources/apps/calendar/lib/repeat.php
6.18 KB
|
d1bafeea1
|
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 |
<?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
* @param (int) $id - id of the event
* @return (array)
*/
public static function get($id) {
$stmt = OCP\DB::prepare('SELECT * FROM `*PREFIX*clndr_repeat` WHERE `eventid` = ?');
$result = $stmt->execute(array($id));
$return = array();
while($row = $result->fetchRow()) {
$return[] = $row;
}
return $return;
}
/**
* @brief returns the cache of an event in a specific peroid
* @param (int) $id - id of the event
* @param (DateTime) $from - start for period in UTC
* @param (DateTime) $until - end for period in UTC
* @return (array)
*/
public static function get_inperiod($id, $from, $until) {
$stmt = OCP\DB::prepare( 'SELECT * FROM `*PREFIX*clndr_repeat` WHERE `eventid` = ?'
.' AND ((`startdate` >= ? AND `startdate` <= ?)'
.' OR (`enddate` >= ? AND `enddate` <= ?))');
$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)));
$return = array();
while($row = $result->fetchRow()) {
$return[] = $row;
}
return $return;
}
/**
* @brief returns the cache of all repeating events of a calendar
* @param (int) $id - id of the calendar
* @return (array)
*/
public static function getCalendar($id) {
$stmt = OCP\DB::prepare('SELECT * FROM `*PREFIX*clndr_repeat` WHERE `calid` = ?');
$result = $stmt->execute(array($id));
$return = array();
while($row = $result->fetchRow()) {
$return[] = $row;
}
return $return;
}
/**
* @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
* @return (array)
*/
public static function getCalendar_inperiod($id, $from, $until) {
$stmt = OCP\DB::prepare( 'SELECT * FROM `*PREFIX*clndr_repeat` WHERE `calid` = ?'
.' AND ((`startdate` >= ? AND `startdate` <= ?)'
.' OR (`enddate` >= ? AND `enddate` <= ?))');
$result = $stmt->execute(array($id,
$from, $until,
$from, $until));
$return = array();
while($row = $result->fetchRow()) {
$return[] = $row;
}
return $return;
}
/**
* @brief generates the cache the first time
* @param (int) id - id of the event
* @return (bool)
*/
public static function generate($id) {
$event = OC_Calendar_Object::find($id);
if($event['repeating'] == 0) {
return false;
}
$object = OC_VObject::parse($event['calendardata']);
$start = new DateTime('01-01-' . date('Y') . ' 00:00:00', new DateTimeZone('UTC'));
$start->modify('-5 years');
$end = new DateTime('31-12-' . date('Y') . ' 23:59:59', new DateTimeZone('UTC'));
$end->modify('+5 years');
$object->expand($start, $end);
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');
$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;
}
/**
* @brief generates the cache the first time for all repeating event of an calendar
* @param (int) id - id of the calendar
* @return (bool)
*/
public static function generateCalendar($id) {
$allobjects = OC_Calendar_Object::all($id);
foreach($allobjects as $event) {
self::generate($event['id']);
}
return true;
}
/**
* @brief updates an event that is already cached
* @param (int) id - id of the event
* @return (bool)
*/
public static function update($id) {
self::clean($id);
self::generate($id);
return true;
}
/**
* @brief updates all repating events of a calendar that are already cached
* @param (int) id - id of the calendar
* @return (bool)
*/
public static function updateCalendar($id) {
self::cleanCalendar($id);
self::generateCalendar($id);
return true;
}
/**
* @brief checks if an event is already cached
* @param (int) id - id of the event
* @return (bool)
*/
public static function is_cached($id) {
if(count(self::get($id)) != 0) {
return true;
}else{
return false;
}
}
/**
* @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
* @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;
}
}
/**
* @brief checks if a whole calendar is already cached
* @param (int) id - id of the calendar
* @return (bool)
*/
public static function is_calendar_cached($id) {
$cachedevents = count(self::getCalendar($id));
$repeatingevents = 0;
$allevents = OC_Calendar_Object::all($id);
foreach($allevents as $event) {
if($event['repeating'] === 1) {
$repeatingevents++;
}
}
if($cachedevents < $repeatingevents) {
return false;
}else{
return true;
}
}
/**
* @brief removes the cache of an event
* @param (int) id - id of the event
* @return (bool)
*/
public static function clean($id) {
$stmt = OCP\DB::prepare('DELETE FROM `*PREFIX*clndr_repeat` WHERE `eventid` = ?');
$stmt->execute(array($id));
}
/**
* @brief removes the cache of all events of a calendar
* @param (int) id - id of the calendar
* @return (bool)
*/
public static function cleanCalendar($id) {
$stmt = OCP\DB::prepare('DELETE FROM `*PREFIX*clndr_repeat` WHERE `calid` = ?');
$stmt->execute(array($id));
}
}
|