Merge branch 'master' into calendar_export
This commit is contained in:
commit
40e7b56783
|
@ -1,9 +0,0 @@
|
|||
License
|
||||
|
||||
Copyright (c) 2010 Thomas Planer
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -1,731 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Name: When
|
||||
* Author: Thomas Planer <tplaner@gmail.com>
|
||||
* Location: http://github.com/tplaner/When
|
||||
* Created: September 2010
|
||||
* Description: Determines the next date of recursion given an iCalendar "rrule" like pattern.
|
||||
* Requirements: PHP 5.3+ - makes extensive use of the Date and Time library (http://us2.php.net/manual/en/book.datetime.php)
|
||||
*/
|
||||
class When
|
||||
{
|
||||
protected $frequency;
|
||||
|
||||
protected $start_date;
|
||||
protected $try_date;
|
||||
|
||||
protected $end_date;
|
||||
|
||||
protected $gobymonth;
|
||||
protected $bymonth;
|
||||
|
||||
protected $gobyweekno;
|
||||
protected $byweekno;
|
||||
|
||||
protected $gobyyearday;
|
||||
protected $byyearday;
|
||||
|
||||
protected $gobymonthday;
|
||||
protected $bymonthday;
|
||||
|
||||
protected $gobyday;
|
||||
protected $byday;
|
||||
|
||||
protected $gobysetpos;
|
||||
protected $bysetpos;
|
||||
|
||||
protected $suggestions;
|
||||
|
||||
protected $count;
|
||||
protected $counter;
|
||||
|
||||
protected $goenddate;
|
||||
|
||||
protected $interval;
|
||||
|
||||
protected $wkst;
|
||||
|
||||
protected $valid_week_days;
|
||||
protected $valid_frequency;
|
||||
|
||||
/**
|
||||
* __construct
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->frequency = null;
|
||||
|
||||
$this->gobymonth = false;
|
||||
$this->bymonth = range(1,12);
|
||||
|
||||
$this->gobymonthday = false;
|
||||
$this->bymonthday = range(1,31);
|
||||
|
||||
$this->gobyday = false;
|
||||
// setup the valid week days (0 = sunday)
|
||||
$this->byday = range(0,6);
|
||||
|
||||
$this->gobyyearday = false;
|
||||
$this->byyearday = range(0,366);
|
||||
|
||||
$this->gobysetpos = false;
|
||||
$this->bysetpos = range(1,366);
|
||||
|
||||
$this->gobyweekno = false;
|
||||
// setup the range for valid weeks
|
||||
$this->byweekno = range(0,54);
|
||||
|
||||
$this->suggestions = array();
|
||||
|
||||
// this will be set if a count() is specified
|
||||
$this->count = 0;
|
||||
// how many *valid* results we returned
|
||||
$this->counter = 0;
|
||||
|
||||
// max date we'll return
|
||||
$this->end_date = new DateTime('9999-12-31');
|
||||
|
||||
// the interval to increase the pattern by
|
||||
$this->interval = 1;
|
||||
|
||||
// what day does the week start on? (0 = sunday)
|
||||
$this->wkst = 0;
|
||||
|
||||
$this->valid_week_days = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
|
||||
|
||||
$this->valid_frequency = array('SECONDLY', 'MINUTELY', 'HOURLY', 'DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime|string $start_date of the recursion - also is the first return value.
|
||||
* @param string $frequency of the recrusion, valid frequencies: secondly, minutely, hourly, daily, weekly, monthly, yearly
|
||||
*/
|
||||
public function recur($start_date, $frequency = "daily")
|
||||
{
|
||||
try
|
||||
{
|
||||
if(is_object($start_date))
|
||||
{
|
||||
$this->start_date = clone $start_date;
|
||||
}
|
||||
else
|
||||
{
|
||||
// timestamps within the RFC have a 'Z' at the end of them, remove this.
|
||||
$start_date = trim($start_date, 'Z');
|
||||
$this->start_date = new DateTime($start_date);
|
||||
}
|
||||
|
||||
$this->try_date = clone $this->start_date;
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
throw new InvalidArgumentException('Invalid start date DateTime: ' . $e);
|
||||
}
|
||||
|
||||
$this->freq($frequency);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function freq($frequency)
|
||||
{
|
||||
if(in_array(strtoupper($frequency), $this->valid_frequency))
|
||||
{
|
||||
$this->frequency = strtoupper($frequency);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidArgumentException('Invalid frequency type.');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// accepts an rrule directly
|
||||
public function rrule($rrule)
|
||||
{
|
||||
// strip off a trailing semi-colon
|
||||
$rrule = trim($rrule, ";");
|
||||
|
||||
$parts = explode(";", $rrule);
|
||||
|
||||
foreach($parts as $part)
|
||||
{
|
||||
list($rule, $param) = explode("=", $part);
|
||||
|
||||
$rule = strtoupper($rule);
|
||||
$param = strtoupper($param);
|
||||
|
||||
switch($rule)
|
||||
{
|
||||
case "FREQ":
|
||||
$this->frequency = $param;
|
||||
break;
|
||||
case "UNTIL":
|
||||
$this->until($param);
|
||||
break;
|
||||
case "COUNT":
|
||||
$this->count($param);
|
||||
break;
|
||||
case "INTERVAL":
|
||||
$this->interval($param);
|
||||
break;
|
||||
case "BYDAY":
|
||||
$params = explode(",", $param);
|
||||
$this->byday($params);
|
||||
break;
|
||||
case "BYMONTHDAY":
|
||||
$params = explode(",", $param);
|
||||
$this->bymonthday($params);
|
||||
break;
|
||||
case "BYYEARDAY":
|
||||
$params = explode(",", $param);
|
||||
$this->byyearday($params);
|
||||
break;
|
||||
case "BYWEEKNO":
|
||||
$params = explode(",", $param);
|
||||
$this->byweekno($params);
|
||||
break;
|
||||
case "BYMONTH":
|
||||
$params = explode(",", $param);
|
||||
$this->bymonth($params);
|
||||
break;
|
||||
case "BYSETPOS":
|
||||
$params = explode(",", $param);
|
||||
$this->bysetpos($params);
|
||||
break;
|
||||
case "WKST":
|
||||
$this->wkst($param);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
//max number of items to return based on the pattern
|
||||
public function count($count)
|
||||
{
|
||||
$this->count = (int)$count;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// how often the recurrence rule repeats
|
||||
public function interval($interval)
|
||||
{
|
||||
$this->interval = (int)$interval;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// starting day of the week
|
||||
public function wkst($day)
|
||||
{
|
||||
switch($day)
|
||||
{
|
||||
case 'SU':
|
||||
$this->wkst = 0;
|
||||
break;
|
||||
case 'MO':
|
||||
$this->wkst = 1;
|
||||
break;
|
||||
case 'TU':
|
||||
$this->wkst = 2;
|
||||
break;
|
||||
case 'WE':
|
||||
$this->wkst = 3;
|
||||
break;
|
||||
case 'TH':
|
||||
$this->wkst = 4;
|
||||
break;
|
||||
case 'FR':
|
||||
$this->wkst = 5;
|
||||
break;
|
||||
case 'SA':
|
||||
$this->wkst = 6;
|
||||
break;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// max date
|
||||
public function until($end_date)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(is_object($end_date))
|
||||
{
|
||||
$this->end_date = clone $end_date;
|
||||
}
|
||||
else
|
||||
{
|
||||
// timestamps within the RFC have a 'Z' at the end of them, remove this.
|
||||
$end_date = trim($end_date, 'Z');
|
||||
$this->end_date = new DateTime($end_date);
|
||||
}
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
throw new InvalidArgumentException('Invalid end date DateTime: ' . $e);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function bymonth($months)
|
||||
{
|
||||
if(is_array($months))
|
||||
{
|
||||
$this->gobymonth = true;
|
||||
$this->bymonth = $months;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function bymonthday($days)
|
||||
{
|
||||
if(is_array($days))
|
||||
{
|
||||
$this->gobymonthday = true;
|
||||
$this->bymonthday = $days;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function byweekno($weeks)
|
||||
{
|
||||
$this->gobyweekno = true;
|
||||
|
||||
if(is_array($weeks))
|
||||
{
|
||||
$this->byweekno = $weeks;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function bysetpos($days)
|
||||
{
|
||||
$this->gobysetpos = true;
|
||||
|
||||
if(is_array($days))
|
||||
{
|
||||
$this->bysetpos = $days;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function byday($days)
|
||||
{
|
||||
$this->gobyday = true;
|
||||
|
||||
if(is_array($days))
|
||||
{
|
||||
$this->byday = array();
|
||||
foreach($days as $day)
|
||||
{
|
||||
$len = strlen($day);
|
||||
|
||||
$as = '+';
|
||||
|
||||
// 0 mean no occurence is set
|
||||
$occ = 0;
|
||||
|
||||
if($len == 3)
|
||||
{
|
||||
$occ = substr($day, 0, 1);
|
||||
}
|
||||
if($len == 4)
|
||||
{
|
||||
$as = substr($day, 0, 1);
|
||||
$occ = substr($day, 1, 1);
|
||||
}
|
||||
|
||||
if($as == '-')
|
||||
{
|
||||
$occ = '-' . $occ;
|
||||
}
|
||||
else
|
||||
{
|
||||
$occ = '+' . $occ;
|
||||
}
|
||||
|
||||
$day = substr($day, -2, 2);
|
||||
switch($day)
|
||||
{
|
||||
case 'SU':
|
||||
$this->byday[] = $occ . 'SU';
|
||||
break;
|
||||
case 'MO':
|
||||
$this->byday[] = $occ . 'MO';
|
||||
break;
|
||||
case 'TU':
|
||||
$this->byday[] = $occ . 'TU';
|
||||
break;
|
||||
case 'WE':
|
||||
$this->byday[] = $occ . 'WE';
|
||||
break;
|
||||
case 'TH':
|
||||
$this->byday[] = $occ . 'TH';
|
||||
break;
|
||||
case 'FR':
|
||||
$this->byday[] = $occ . 'FR';
|
||||
break;
|
||||
case 'SA':
|
||||
$this->byday[] = $occ . 'SA';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function byyearday($days)
|
||||
{
|
||||
$this->gobyyearday = true;
|
||||
|
||||
if(is_array($days))
|
||||
{
|
||||
$this->byyearday = $days;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// this creates a basic list of dates to "try"
|
||||
protected function create_suggestions()
|
||||
{
|
||||
switch($this->frequency)
|
||||
{
|
||||
case "YEARLY":
|
||||
$interval = 'year';
|
||||
break;
|
||||
case "MONTHLY":
|
||||
$interval = 'month';
|
||||
break;
|
||||
case "WEEKLY":
|
||||
$interval = 'week';
|
||||
break;
|
||||
case "DAILY":
|
||||
$interval = 'day';
|
||||
break;
|
||||
case "HOURLY":
|
||||
$interval = 'hour';
|
||||
break;
|
||||
case "MINUTELY":
|
||||
$interval = 'minute';
|
||||
break;
|
||||
case "SECONDLY":
|
||||
$interval = 'second';
|
||||
break;
|
||||
}
|
||||
|
||||
$month_day = $this->try_date->format('j');
|
||||
$month = $this->try_date->format('n');
|
||||
$year = $this->try_date->format('Y');
|
||||
|
||||
$timestamp = $this->try_date->format('H:i:s');
|
||||
|
||||
if($this->gobysetpos)
|
||||
{
|
||||
if($this->try_date == $this->start_date)
|
||||
{
|
||||
$this->suggestions[] = clone $this->try_date;
|
||||
}
|
||||
else
|
||||
{
|
||||
if($this->gobyday)
|
||||
{
|
||||
foreach($this->bysetpos as $_pos)
|
||||
{
|
||||
$tmp_array = array();
|
||||
$_mdays = range(1, date('t',mktime(0,0,0,$month,1,$year)));
|
||||
foreach($_mdays as $_mday)
|
||||
{
|
||||
$date_time = new DateTime($year . '-' . $month . '-' . $_mday . ' ' . $timestamp);
|
||||
|
||||
$occur = ceil($_mday / 7);
|
||||
|
||||
$day_of_week = $date_time->format('l');
|
||||
$dow_abr = strtoupper(substr($day_of_week, 0, 2));
|
||||
|
||||
// set the day of the month + (positive)
|
||||
$occur = '+' . $occur . $dow_abr;
|
||||
$occur_zero = '+0' . $dow_abr;
|
||||
|
||||
// set the day of the month - (negative)
|
||||
$total_days = $date_time->format('t') - $date_time->format('j');
|
||||
$occur_neg = '-' . ceil(($total_days + 1)/7) . $dow_abr;
|
||||
|
||||
$day_from_end_of_month = $date_time->format('t') + 1 - $_mday;
|
||||
|
||||
if(in_array($occur, $this->byday) || in_array($occur_zero, $this->byday) || in_array($occur_neg, $this->byday))
|
||||
{
|
||||
$tmp_array[] = clone $date_time;
|
||||
}
|
||||
}
|
||||
|
||||
if($_pos > 0)
|
||||
{
|
||||
$this->suggestions[] = clone $tmp_array[$_pos - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->suggestions[] = clone $tmp_array[count($tmp_array) + $_pos];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif($this->gobyyearday)
|
||||
{
|
||||
foreach($this->byyearday as $_day)
|
||||
{
|
||||
if($_day >= 0)
|
||||
{
|
||||
$_day--;
|
||||
|
||||
$_time = strtotime('+' . $_day . ' days', mktime(0, 0, 0, 1, 1, $year));
|
||||
$this->suggestions[] = new Datetime(date('Y-m-d', $_time) . ' ' . $timestamp);
|
||||
}
|
||||
else
|
||||
{
|
||||
$year_day_neg = 365 + $_day;
|
||||
$leap_year = $this->try_date->format('L');
|
||||
if($leap_year == 1)
|
||||
{
|
||||
$year_day_neg = 366 + $_day;
|
||||
}
|
||||
|
||||
$_time = strtotime('+' . $year_day_neg . ' days', mktime(0, 0, 0, 1, 1, $year));
|
||||
$this->suggestions[] = new Datetime(date('Y-m-d', $_time) . ' ' . $timestamp);
|
||||
}
|
||||
}
|
||||
}
|
||||
// special case because for years you need to loop through the months too
|
||||
elseif($this->gobyday && $interval == "year")
|
||||
{
|
||||
foreach($this->bymonth as $_month)
|
||||
{
|
||||
// this creates an array of days of the month
|
||||
$_mdays = range(1, date('t',mktime(0,0,0,$_month,1,$year)));
|
||||
foreach($_mdays as $_mday)
|
||||
{
|
||||
$date_time = new DateTime($year . '-' . $_month . '-' . $_mday . ' ' . $timestamp);
|
||||
|
||||
// get the week of the month (1, 2, 3, 4, 5, etc)
|
||||
$week = $date_time->format('W');
|
||||
|
||||
if($date_time >= $this->start_date && in_array($week, $this->byweekno))
|
||||
{
|
||||
$this->suggestions[] = clone $date_time;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif($interval == "day")
|
||||
{
|
||||
$this->suggestions[] = clone $this->try_date;
|
||||
}
|
||||
elseif($interval == "week")
|
||||
{
|
||||
$this->suggestions[] = clone $this->try_date;
|
||||
|
||||
if($this->gobyday)
|
||||
{
|
||||
$week_day = $this->try_date->format('w');
|
||||
|
||||
$days_in_month = $this->try_date->format('t');
|
||||
|
||||
$overflow_count = 1;
|
||||
$_day = $month_day;
|
||||
|
||||
$run = true;
|
||||
while($run)
|
||||
{
|
||||
$_day++;
|
||||
if($_day <= $days_in_month)
|
||||
{
|
||||
$tmp_date = new DateTime($year . '-' . $month . '-' . $_day . ' ' . $timestamp);
|
||||
}
|
||||
else
|
||||
{
|
||||
//$tmp_month = $month+1;
|
||||
$tmp_date = new DateTime($year . '-' . $month . '-' . $overflow_count . ' ' . $timestamp);
|
||||
$tmp_date->modify('+1 month');
|
||||
$overflow_count++;
|
||||
}
|
||||
|
||||
$week_day = $tmp_date->format('w');
|
||||
|
||||
if($this->try_date == $this->start_date)
|
||||
{
|
||||
if($week_day == $this->wkst)
|
||||
{
|
||||
$this->try_date = clone $tmp_date;
|
||||
$this->try_date->modify('-7 days');
|
||||
$run = false;
|
||||
}
|
||||
}
|
||||
|
||||
if($week_day != $this->wkst)
|
||||
{
|
||||
$this->suggestions[] = clone $tmp_date;
|
||||
}
|
||||
else
|
||||
{
|
||||
$run = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif($this->gobyday && $interval == "month")
|
||||
{
|
||||
$_mdays = range(1, date('t',mktime(0,0,0,$month,1,$year)));
|
||||
foreach($_mdays as $_mday)
|
||||
{
|
||||
$date_time = new DateTime($year . '-' . $month . '-' . $_mday . ' ' . $timestamp);
|
||||
|
||||
// get the week of the month (1, 2, 3, 4, 5, etc)
|
||||
$week = $date_time->format('W');
|
||||
|
||||
if($date_time >= $this->start_date && in_array($week, $this->byweekno))
|
||||
{
|
||||
$this->suggestions[] = clone $date_time;
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif($this->gobymonth)
|
||||
{
|
||||
foreach($this->bymonth as $_month)
|
||||
{
|
||||
$date_time = new DateTime($year . '-' . $_month . '-' . $month_day . ' ' . $timestamp);
|
||||
|
||||
if($date_time >= $this->start_date)
|
||||
{
|
||||
$this->suggestions[] = clone $date_time;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->suggestions[] = clone $this->try_date;
|
||||
}
|
||||
|
||||
if($interval == "month")
|
||||
{
|
||||
|
||||
$this->try_date->modify('first day of next month');
|
||||
if((int) date('t', $this->try_date->format('U')) > (int) $this->start_date->format('j')){
|
||||
$this->try_date->modify('+' . (int) $this->start_date->format('j') - 1 . ' day');
|
||||
}else{
|
||||
$this->try_date->modify('+' . (int) date('t', $this->try_date->format('U')) - 1 . ' day');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->try_date->modify($this->interval . ' ' . $interval);
|
||||
}
|
||||
}
|
||||
|
||||
protected function valid_date($date)
|
||||
{
|
||||
$year = $date->format('Y');
|
||||
$month = $date->format('n');
|
||||
$day = $date->format('j');
|
||||
|
||||
$year_day = $date->format('z') + 1;
|
||||
|
||||
$year_day_neg = -366 + $year_day;
|
||||
$leap_year = $date->format('L');
|
||||
if($leap_year == 1)
|
||||
{
|
||||
$year_day_neg = -367 + $year_day;
|
||||
}
|
||||
|
||||
// this is the nth occurence of the date
|
||||
$occur = ceil($day / 7);
|
||||
|
||||
$week = $date->format('W');
|
||||
|
||||
$day_of_week = $date->format('l');
|
||||
$dow_abr = strtoupper(substr($day_of_week, 0, 2));
|
||||
|
||||
// set the day of the month + (positive)
|
||||
$occur = '+' . $occur . $dow_abr;
|
||||
$occur_zero = '+0' . $dow_abr;
|
||||
|
||||
// set the day of the month - (negative)
|
||||
$total_days = $date->format('t') - $date->format('j');
|
||||
$occur_neg = '-' . ceil(($total_days + 1)/7) . $dow_abr;
|
||||
|
||||
$day_from_end_of_month = $date->format('t') + 1 - $day;
|
||||
|
||||
if(in_array($month, $this->bymonth) &&
|
||||
(in_array($occur, $this->byday) || in_array($occur_zero, $this->byday) || in_array($occur_neg, $this->byday)) &&
|
||||
in_array($week, $this->byweekno) &&
|
||||
(in_array($day, $this->bymonthday) || in_array(-$day_from_end_of_month, $this->bymonthday)) &&
|
||||
(in_array($year_day, $this->byyearday) || in_array($year_day_neg, $this->byyearday)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// return the next valid DateTime object which matches the pattern and follows the rules
|
||||
public function next()
|
||||
{
|
||||
// check the counter is set
|
||||
if($this->count !== 0)
|
||||
{
|
||||
if($this->counter >= $this->count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// create initial set of suggested dates
|
||||
if(count($this->suggestions) === 0)
|
||||
{
|
||||
$this->create_suggestions();
|
||||
}
|
||||
|
||||
// loop through the suggested dates
|
||||
while(count($this->suggestions) > 0)
|
||||
{
|
||||
// get the first one on the array
|
||||
$try_date = array_shift($this->suggestions);
|
||||
|
||||
// make sure the date doesn't exceed the max date
|
||||
if($try_date > $this->end_date)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// make sure it falls within the allowed days
|
||||
if($this->valid_date($try_date) === true)
|
||||
{
|
||||
$this->counter++;
|
||||
return $try_date;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we might be out of suggested days, so load some more
|
||||
if(count($this->suggestions) === 0)
|
||||
{
|
||||
$this->create_suggestions();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2012 Georg Ehrke <georg@ownCloud.com>
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later.
|
||||
* See the COPYING-README file.
|
||||
*/
|
||||
OCP\JSON::checkLoggedIn();
|
||||
OCP\JSON::checkAppEnabled('calendar');
|
||||
$calendars = OC_Calendar_Calendar::allCalendars(OCP\USER::getUser());
|
||||
foreach($calendars as $calendar){
|
||||
OC_Calendar_Repeat::cleancalendar($calendar['id']);
|
||||
OC_Calendar_Repeat::generatecalendar($calendar['id']);
|
||||
}
|
||||
OCP\JSON::success();
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2012 Georg Ehrke <georg@ownCloud.com>
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later.
|
||||
* See the COPYING-README file.
|
||||
*/
|
||||
OCP\JSON::checkLoggedIn();
|
||||
OCP\JSON::checkAppEnabled('calendar');
|
||||
$calendars = OC_Calendar_Calendar::allCalendars(OCP\USER::getUser());
|
||||
$allcached = true;
|
||||
foreach($calendars as $calendar){
|
||||
if(!OC_Calendar_Repeat::is_calendar_cached($calendar['id'])){
|
||||
$allcached = false;
|
||||
}
|
||||
}
|
||||
$l = new OC_L10N('calendar');
|
||||
if(!$allcached){
|
||||
OCP\JSON::error(array('message'=>'Not all calendars are completely cached', 'l10n'=>$l->t('Not all calendars are completely cached')));
|
||||
}else{
|
||||
OCP\JSON::success(array('message'=>'Everything seems to be completely cached', 'l10n'=>$l->t('Everything seems to be completely cached')));
|
||||
}
|
|
@ -5,10 +5,6 @@
|
|||
* later.
|
||||
* See the COPYING-README file.
|
||||
*/
|
||||
|
||||
|
||||
require_once('when/When.php');
|
||||
|
||||
OCP\JSON::checkLoggedIn();
|
||||
OCP\JSON::checkAppEnabled('calendar');
|
||||
session_write_close();
|
||||
|
|
|
@ -5,10 +5,16 @@ OC::$CLASSPATH['OC_Calendar_Calendar'] = 'apps/calendar/lib/calendar.php';
|
|||
OC::$CLASSPATH['OC_Calendar_Object'] = 'apps/calendar/lib/object.php';
|
||||
OC::$CLASSPATH['OC_Calendar_Hooks'] = 'apps/calendar/lib/hooks.php';
|
||||
OC::$CLASSPATH['OC_Connector_Sabre_CalDAV'] = 'apps/calendar/lib/connector_sabre.php';
|
||||
OC::$CLASSPATH['OC_Calendar_Repeat'] = 'apps/calendar/lib/repeat.php';
|
||||
OC::$CLASSPATH['OC_Calendar_Share'] = 'apps/calendar/lib/share.php';
|
||||
OC::$CLASSPATH['OC_Search_Provider_Calendar'] = 'apps/calendar/lib/search.php';
|
||||
OC::$CLASSPATH['OC_Calendar_Export'] = 'apps/calendar/lib/export.php';
|
||||
OCP\Util::connectHook('OC_User', 'post_deleteUser', 'OC_Calendar_Hooks', 'deleteUser');
|
||||
OCP\Util::connectHook('OC_Calendar', 'addEvent', 'OC_Calendar_Repeat', 'generate');
|
||||
OCP\Util::connectHook('OC_Calendar', 'editEvent', 'OC_Calendar_Repeat', 'update');
|
||||
OCP\Util::connectHook('OC_Calendar', 'deleteEvent', 'OC_Calendar_Repeat', 'clean');
|
||||
OCP\Util::connectHook('OC_Calendar', 'moveEvent', 'OC_Calendar_Repeat', 'update');
|
||||
OCP\Util::connectHook('OC_Calendar', 'deleteCalendar', 'OC_Calendar_Repeat', 'cleanCalendar');
|
||||
OCP\Util::addscript('calendar','loader');
|
||||
OCP\Util::addscript("3rdparty", "chosen/chosen.jquery.min");
|
||||
OCP\Util::addStyle("3rdparty", "chosen/chosen");
|
||||
|
|
|
@ -290,4 +290,56 @@
|
|||
|
||||
</table>
|
||||
|
||||
<table>
|
||||
|
||||
<name>*dbprefix*calendar_repeat</name>
|
||||
|
||||
<declaration>
|
||||
|
||||
<field>
|
||||
<name>id</name>
|
||||
<type>integer</type>
|
||||
<default>0</default>
|
||||
<notnull>true</notnull>
|
||||
<autoincrement>1</autoincrement>
|
||||
<unsigned>true</unsigned>
|
||||
<length>4</length>
|
||||
</field>
|
||||
|
||||
<field>
|
||||
<name>eventid</name>
|
||||
<type>integer</type>
|
||||
<default>0</default>
|
||||
<notnull>true</notnull>
|
||||
<unsigned>true</unsigned>
|
||||
<length>4</length>
|
||||
</field>
|
||||
|
||||
<field>
|
||||
<name>calid</name>
|
||||
<type>integer</type>
|
||||
<default>0</default>
|
||||
<notnull>true</notnull>
|
||||
<unsigned>true</unsigned>
|
||||
<length>4</length>
|
||||
</field>
|
||||
|
||||
<field>
|
||||
<name>startdate</name>
|
||||
<type>timestamp</type>
|
||||
<default>0000-00-00 00:00:00</default>
|
||||
<notnull>false</notnull>
|
||||
</field>
|
||||
|
||||
<field>
|
||||
<name>enddate</name>
|
||||
<type>timestamp</type>
|
||||
<default>0000-00-00 00:00:00</default>
|
||||
<notnull>false</notnull>
|
||||
</field>
|
||||
|
||||
</declaration>
|
||||
|
||||
</table>
|
||||
|
||||
</database>
|
||||
|
|
|
@ -15,3 +15,10 @@ if (version_compare($installedVersion, '0.2.1', '<')) {
|
|||
$r = $stmt->execute(array($color,$id));
|
||||
}
|
||||
}
|
||||
if (version_compare($installedVersion, '0.5', '<')) {
|
||||
$calendars = OC_Calendar_Calendar::allCalendars(OCP\USER::getUser());
|
||||
foreach($calendars as $calendar){
|
||||
OC_Calendar_Repeat::cleanCalendar($calendar['id']);
|
||||
OC_Calendar_Repeat::generateCalendar($calendar['id']);
|
||||
}
|
||||
}
|
|
@ -1 +1 @@
|
|||
0.4.1
|
||||
0.5
|
||||
|
|
|
@ -44,4 +44,24 @@ $(document).ready(function(){
|
|||
$('#' + jsondata.firstday).attr('selected',true);
|
||||
$('#firstday').chosen();
|
||||
});
|
||||
$('#cleancalendarcache').click(function(){
|
||||
$.getJSON(OC.filePath('calendar', 'ajax/cache', 'rescan.php'), function(){
|
||||
calendarcachecheck();
|
||||
});
|
||||
});
|
||||
calendarcachecheck();
|
||||
});
|
||||
function calendarcachecheck(){
|
||||
$.getJSON(OC.filePath('calendar', 'ajax/cache', 'status.php'), function(jsondata, status) {
|
||||
$('#cleancalendarcache').attr('title', jsondata.l10n.text);
|
||||
if(jsondata.status == 'success'){
|
||||
$('#cleancalendarcache').css('background', '#90EE90');
|
||||
$('#cleancalendarcache').css('color', '#333');
|
||||
$('#cleancalendarcache').css('text-shadow', '#fff 0 1px 0');
|
||||
}else{
|
||||
$('#cleancalendarcache').css('background', '#DC143C');
|
||||
$('#cleancalendarcache').css('color', '#FFFFFF');
|
||||
$('#cleancalendarcache').css('text-shadow', '0px 0px 0px #fff, 0px 0px #fff');
|
||||
}
|
||||
});
|
||||
}
|
|
@ -116,8 +116,7 @@ class OC_Calendar_App{
|
|||
* @brief returns the default categories of ownCloud
|
||||
* @return (array) $categories
|
||||
*/
|
||||
protected static function getDefaultCategories()
|
||||
{
|
||||
protected static function getDefaultCategories(){
|
||||
return array(
|
||||
self::$l10n->t('Birthday'),
|
||||
self::$l10n->t('Business'),
|
||||
|
@ -152,8 +151,7 @@ class OC_Calendar_App{
|
|||
* @brief returns the categories of the vcategories object
|
||||
* @return (array) $categories
|
||||
*/
|
||||
public static function getCategoryOptions()
|
||||
{
|
||||
public static function getCategoryOptions(){
|
||||
$categories = self::getVCategories()->categories();
|
||||
return $categories;
|
||||
}
|
||||
|
@ -359,94 +357,57 @@ class OC_Calendar_App{
|
|||
/*
|
||||
* @brief generates the output for an event which will be readable for our js
|
||||
* @param (mixed) $event - event object / array
|
||||
* @param (int) $start - unixtimestamp of start
|
||||
* @param (int) $end - unixtimestamp of end
|
||||
* @param (int) $start - DateTime object of start
|
||||
* @param (int) $end - DateTime object of end
|
||||
* @return (array) $output - readable output
|
||||
*/
|
||||
public static function generateEventOutput($event, $start, $end){
|
||||
$output = array();
|
||||
|
||||
if(isset($event['calendardata'])){
|
||||
$object = OC_VObject::parse($event['calendardata']);
|
||||
$vevent = $object->VEVENT;
|
||||
}else{
|
||||
$vevent = $event['vevent'];
|
||||
}
|
||||
|
||||
$return = array();
|
||||
$id = $event['id'];
|
||||
$allday = ($vevent->DTSTART->getDateType() == Sabre_VObject_Element_DateTime::DATE)?true:false;
|
||||
$last_modified = @$vevent->__get('LAST-MODIFIED');
|
||||
$lastmodified = ($last_modified)?$last_modified->getDateTime()->format('U'):0;
|
||||
|
||||
$output = array('id'=>(int)$event['id'],
|
||||
'title' => ($event['summary']!=NULL || $event['summary'] != '')?$event['summary']: self::$l10n->t('unnamed'),
|
||||
'description' => isset($vevent->DESCRIPTION)?$vevent->DESCRIPTION->value:'',
|
||||
'lastmodified'=>$lastmodified);
|
||||
|
||||
$dtstart = $vevent->DTSTART;
|
||||
$start_dt = $dtstart->getDateTime();
|
||||
$dtend = OC_Calendar_Object::getDTEndFromVEvent($vevent);
|
||||
$end_dt = $dtend->getDateTime();
|
||||
|
||||
if ($dtstart->getDateType() == Sabre_VObject_Element_DateTime::DATE){
|
||||
$output['allDay'] = true;
|
||||
$staticoutput = array('id'=>(int)$event['id'],
|
||||
'title' => htmlspecialchars(($event['summary']!=NULL || $event['summary'] != '')?$event['summary']: self::$l10n->t('unnamed')),
|
||||
'description' => isset($vevent->DESCRIPTION)?htmlspecialchars($vevent->DESCRIPTION->value):'',
|
||||
'lastmodified'=>$lastmodified,
|
||||
'allDay'=>$allday);
|
||||
if(OC_Calendar_Object::isrepeating($id) && OC_Calendar_Repeat::is_cached_inperiod($event['id'], $start, $end)){
|
||||
$cachedinperiod = OC_Calendar_Repeat::get_inperiod($id, $start, $end);
|
||||
foreach($cachedinperiod as $cachedevent){
|
||||
$dynamicoutput = array();
|
||||
if($allday){
|
||||
$start_dt = new DateTime($cachedevent['startdate'], new DateTimeZone('UTC'));
|
||||
$end_dt = new DateTime($cachedevent['enddate'], new DateTimeZone('UTC'));
|
||||
$dynamicoutput['start'] = $start_dt->format('Y-m-d');
|
||||
$dynamicoutput['end'] = $end_dt->format('Y-m-d');
|
||||
}else{
|
||||
$output['allDay'] = false;
|
||||
$start_dt = new DateTime($cachedevent['startdate'], new DateTimeZone('UTC'));
|
||||
$end_dt = new DateTime($cachedevent['enddate'], new DateTimeZone('UTC'));
|
||||
$start_dt->setTimezone(new DateTimeZone(self::$tz));
|
||||
$end_dt->setTimezone(new DateTimeZone(self::$tz));
|
||||
$dynamicoutput['start'] = $start_dt->format('Y-m-d H:i:s');
|
||||
$dynamicoutput['end'] = $end_dt->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
// Handle exceptions to recurring events
|
||||
$exceptionDateObjects = $vevent->select('EXDATE');
|
||||
$exceptionDateMap = Array();
|
||||
foreach ($exceptionDateObjects as $exceptionObject) {
|
||||
foreach($exceptionObject->getDateTimes() as $datetime) {
|
||||
$ts = $datetime->getTimestamp();
|
||||
$exceptionDateMap[idate('Y',$ts)][idate('m', $ts)][idate('d', $ts)] = true;
|
||||
$return[] = array_merge($staticoutput, $dynamicoutput);
|
||||
}
|
||||
}else{
|
||||
if(OC_Calendar_Object::isrepeating($id)){
|
||||
$object->expand($start, $end);
|
||||
}
|
||||
|
||||
$return = array();
|
||||
if($event['repeating'] == 1){
|
||||
$duration = (double) $end_dt->format('U') - (double) $start_dt->format('U');
|
||||
$r = new When();
|
||||
$r->recur($start_dt)->rrule((string) $vevent->RRULE);
|
||||
/*$r = new iCal_Repeat_Generator(array('RECUR' => $start_dt,
|
||||
* 'RRULE' => (string)$vevent->RRULE
|
||||
* 'RDATE' => (string)$vevent->RDATE
|
||||
* 'EXRULE' => (string)$vevent->EXRULE
|
||||
* 'EXDATE' => (string)$vevent->EXDATE));*/
|
||||
while($result = $r->next()){
|
||||
if($result < $start){
|
||||
foreach($object->getComponents() as $singleevent){
|
||||
if(!($singleevent instanceof Sabre_VObject_Component_VEvent)){
|
||||
continue;
|
||||
}
|
||||
if($result > $end){
|
||||
break;
|
||||
$dynamicoutput = OC_Calendar_Object::generateStartEndDate($singleevent->DTSTART, OC_Calendar_Object::getDTEndFromVEvent($singleevent), $allday, self::$tz);
|
||||
$return[] = array_merge($staticoutput, $dynamicoutput);
|
||||
}
|
||||
// Check for exceptions to recurring events
|
||||
$ts = $result->getTimestamp();
|
||||
if (isset($exceptionDateMap[idate('Y',$ts)][idate('m', $ts)][idate('d', $ts)])) {
|
||||
continue;
|
||||
}
|
||||
unset($ts);
|
||||
|
||||
if($output['allDay'] == true){
|
||||
$output['start'] = $result->format('Y-m-d');
|
||||
$output['end'] = date('Y-m-d', $result->format('U') + --$duration);
|
||||
}else{
|
||||
$output['start'] = $result->format('Y-m-d H:i:s');
|
||||
$output['end'] = date('Y-m-d H:i:s', $result->format('U') + $duration);
|
||||
}
|
||||
$return[] = $output;
|
||||
}
|
||||
}else{
|
||||
if($output['allDay'] == true){
|
||||
$output['start'] = $start_dt->format('Y-m-d');
|
||||
$end_dt->modify('-1 sec');
|
||||
$output['end'] = $end_dt->format('Y-m-d');
|
||||
}else{
|
||||
$output['start'] = $start_dt->format('Y-m-d H:i:s');
|
||||
$output['end'] = $end_dt->format('Y-m-d H:i:s');
|
||||
}
|
||||
$return[] = $output;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
|
|
@ -109,7 +109,10 @@ class OC_Calendar_Calendar{
|
|||
$stmt = OCP\DB::prepare( 'INSERT INTO *PREFIX*calendar_calendars (userid,displayname,uri,ctag,calendarorder,calendarcolor,timezone,components) VALUES(?,?,?,?,?,?,?,?)' );
|
||||
$result = $stmt->execute(array($userid,$name,$uri,1,$order,$color,$timezone,$components));
|
||||
|
||||
return OCP\DB::insertid('*PREFIX*calendar_calendars');
|
||||
$insertid = OCP\DB::insertid('*PREFIX*calendar_calendars');
|
||||
OCP\Util::emitHook('OC_Calendar', 'addCalendar', $insertid);
|
||||
|
||||
return $insertid;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -129,7 +132,10 @@ class OC_Calendar_Calendar{
|
|||
$stmt = OCP\DB::prepare( 'INSERT INTO *PREFIX*calendar_calendars (userid,displayname,uri,ctag,calendarorder,calendarcolor,timezone,components) VALUES(?,?,?,?,?,?,?,?)' );
|
||||
$result = $stmt->execute(array($userid,$name,$uri,1,$order,$color,$timezone,$components));
|
||||
|
||||
return OCP\DB::insertid('*PREFIX*calendar_calendars');
|
||||
$insertid = OCP\DB::insertid('*PREFIX*calendar_calendars');
|
||||
OCP\Util::emitHook('OC_Calendar', 'addCalendar', $insertid);
|
||||
|
||||
return $insertid;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -158,6 +164,7 @@ class OC_Calendar_Calendar{
|
|||
$stmt = OCP\DB::prepare( 'UPDATE *PREFIX*calendar_calendars SET displayname=?,calendarorder=?,calendarcolor=?,timezone=?,components=?,ctag=ctag+1 WHERE id=?' );
|
||||
$result = $stmt->execute(array($name,$order,$color,$timezone,$components,$id));
|
||||
|
||||
OCP\Util::emitHook('OC_Calendar', 'editCalendar', $id);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -198,6 +205,7 @@ class OC_Calendar_Calendar{
|
|||
$stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*calendar_objects WHERE calendarid = ?' );
|
||||
$stmt->execute(array($id));
|
||||
|
||||
OCP\Util::emitHook('OC_Calendar', 'deleteCalendar', $id);
|
||||
if(count(self::allCalendars()) == 0) {
|
||||
self::addCalendar(OCP\USER::getUser(),'Default calendar');
|
||||
}
|
||||
|
|
|
@ -108,7 +108,7 @@ class OC_Calendar_Object{
|
|||
$object_id = OCP\DB::insertid('*PREFIX*calendar_objects');
|
||||
|
||||
OC_Calendar_Calendar::touchCalendar($id);
|
||||
|
||||
OCP\Util::emitHook('OC_Calendar', 'addEvent', $object_id);
|
||||
return $object_id;
|
||||
}
|
||||
|
||||
|
@ -128,7 +128,7 @@ class OC_Calendar_Object{
|
|||
$object_id = OCP\DB::insertid('*PREFIX*calendar_objects');
|
||||
|
||||
OC_Calendar_Calendar::touchCalendar($id);
|
||||
|
||||
OCP\Util::emitHook('OC_Calendar', 'addEvent', $object_id);
|
||||
return $object_id;
|
||||
}
|
||||
|
||||
|
@ -149,6 +149,7 @@ class OC_Calendar_Object{
|
|||
$stmt->execute(array($type,$startdate,$enddate,$repeating,$summary,$data,time(),$id));
|
||||
|
||||
OC_Calendar_Calendar::touchCalendar($oldobject['calendarid']);
|
||||
OCP\Util::emitHook('OC_Calendar', 'editEvent', $id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -170,6 +171,7 @@ class OC_Calendar_Object{
|
|||
$stmt->execute(array($type,$startdate,$enddate,$repeating,$summary,$data,time(),$oldobject['id']));
|
||||
|
||||
OC_Calendar_Calendar::touchCalendar($oldobject['calendarid']);
|
||||
OCP\Util::emitHook('OC_Calendar', 'editEvent', $oldobject['id']);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -184,6 +186,7 @@ class OC_Calendar_Object{
|
|||
$stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*calendar_objects WHERE id = ?' );
|
||||
$stmt->execute(array($id));
|
||||
OC_Calendar_Calendar::touchCalendar($oldobject['calendarid']);
|
||||
OCP\Util::emitHook('OC_Calendar', 'deleteEvent', $id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -195,9 +198,11 @@ class OC_Calendar_Object{
|
|||
* @return boolean
|
||||
*/
|
||||
public static function deleteFromDAVData($cid,$uri){
|
||||
$oldobject = self::findWhereDAVDataIs($cid, $uri);
|
||||
$stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*calendar_objects WHERE calendarid = ? AND uri=?' );
|
||||
$stmt->execute(array($cid,$uri));
|
||||
OC_Calendar_Calendar::touchCalendar($cid);
|
||||
OCP\Util::emitHook('OC_Calendar', 'deleteEvent', $oldobject['id']);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -207,6 +212,7 @@ class OC_Calendar_Object{
|
|||
$stmt->execute(array($calendarid,$id));
|
||||
|
||||
OC_Calendar_Calendar::touchCalendar($id);
|
||||
OCP\Util::emitHook('OC_Calendar', 'moveEvent', $id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -294,12 +300,11 @@ class OC_Calendar_Object{
|
|||
* This function creates a date string that can be used by MDB2.
|
||||
* Furthermore it converts the time to UTC.
|
||||
*/
|
||||
protected static function getUTCforMDB($datetime){
|
||||
public static function getUTCforMDB($datetime){
|
||||
return date('Y-m-d H:i', $datetime->format('U') - $datetime->getOffset());
|
||||
}
|
||||
|
||||
public static function getDTEndFromVEvent($vevent)
|
||||
{
|
||||
public static function getDTEndFromVEvent($vevent){
|
||||
if ($vevent->DTEND) {
|
||||
$dtend = $vevent->DTEND;
|
||||
}else{
|
||||
|
@ -796,4 +801,29 @@ class OC_Calendar_Object{
|
|||
$event = self::find($id);
|
||||
return $event['calendarid'];
|
||||
}
|
||||
|
||||
public static function isrepeating($id){
|
||||
$event = self::find($id);
|
||||
return ($event['repeating'] == 1)?true:false;
|
||||
}
|
||||
|
||||
public static function generateStartEndDate($dtstart, $dtend, $allday, $tz){
|
||||
$start_dt = $dtstart->getDateTime();
|
||||
$end_dt = $dtend->getDateTime();
|
||||
$return = array();
|
||||
if($allday){
|
||||
$return['start'] = $start_dt->format('Y-m-d');
|
||||
$end_dt->modify('-1 minute');
|
||||
while($start_dt->format('U') >= $end_dt->format('U')){
|
||||
$end_dt->modify('+1 day');
|
||||
}
|
||||
$return['end'] = $end_dt->format('Y-m-d');
|
||||
}else{
|
||||
$start_dt->setTimezone(new DateTimeZone($tz));
|
||||
$end_dt->setTimezone(new DateTimeZone($tz));
|
||||
$return['start'] = $start_dt->format('Y-m-d H:i:s');
|
||||
$return['end'] = $end_dt->format('Y-m-d H:i:s');
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,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*calendar_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*calendar_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*calendar_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*calendar_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_Element_DateTime::DATE)?true:false, 'UTC');
|
||||
$stmt = OCP\DB::prepare('INSERT INTO *PREFIX*calendar_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*calendar_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*calendar_repeat WHERE calid = ?');
|
||||
$stmt->execute(array($id));
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
|
||||
* Copyright (c) 2011 Georg Ehrke <ownclouddev at georgswebsite dot de>
|
||||
* Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de>
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later.
|
||||
* See the COPYING-README file.
|
||||
|
@ -44,6 +44,10 @@
|
|||
</select>
|
||||
</td></tr>
|
||||
|
||||
<tr><td><label for="" class="bold"><?php echo $l->t('Cache');?></label></td><td>
|
||||
<input id="cleancalendarcache" type="button" class="button" value="<?php echo $l->t('Clear cache for repeating events');?>">
|
||||
</td></tr>
|
||||
|
||||
</table>
|
||||
|
||||
<?php echo $l->t('Calendar CalDAV syncing addresses'); ?> (<a href="http://owncloud.org/synchronisation/" target="_blank"><?php echo $l->t('more info'); ?></a>)
|
||||
|
|
Loading…
Reference in New Issue