2011-09-15 12:14:47 +04:00
< ? php
/**
2011-09-24 00:59:24 +04:00
* Copyright ( c ) 2011 Jakob Sack < mail @ jakobsack . de >
* This file is licensed under the Affero General Public License version 3 or
* later .
* See the COPYING - README file .
2011-09-15 12:14:47 +04:00
*/
/**
2011-09-20 16:38:16 +04:00
* This class manages our calendar objects
2011-09-15 12:14:47 +04:00
*/
class OC_Calendar_Object {
/**
* @ brief Returns all objects of a calendar
* @ param integer $id
* @ return array
*
* The objects are associative arrays . You ' ll find the original vObject in
* [ 'calendardata' ]
*/
public static function all ( $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 ;
}
2011-10-17 21:58:56 +04:00
/**
* @ brief Returns all objects of a calendar between $start and $end
* @ param integer $id
* @ param DateTime $start
* @ param DateTime $end
* @ return array
*
* The objects are associative arrays . You ' ll find the original vObject
* in [ 'calendardata' ]
*/
public static function allInPeriod ( $id , $start , $end ){
$stmt = OC_DB :: prepare ( 'SELECT * FROM *PREFIX*calendar_objects WHERE calendarid = ?'
. ' AND ((startdate >= ? AND startdate <= ? AND repeating = 0)'
2011-12-01 00:13:26 +04:00
. ' OR (enddate >= ? AND enddate <= ? AND repeating = 0)'
2011-11-13 01:02:04 +04:00
. ' OR (startdate <= ? AND repeating = 1))' );
2011-10-17 21:58:56 +04:00
$start = self :: getUTCforMDB ( $start );
$end = self :: getUTCforMDB ( $end );
$result = $stmt -> execute ( array ( $id ,
2011-12-01 00:13:26 +04:00
$start , $end ,
2011-10-17 21:58:56 +04:00
$start , $end ,
2011-11-13 01:02:04 +04:00
$end ));
2011-10-17 21:58:56 +04:00
$calendarobjects = array ();
while ( $row = $result -> fetchRow ()){
$calendarobjects [] = $row ;
}
return $calendarobjects ;
}
2011-09-15 12:14:47 +04:00
/**
* @ brief Returns an object
* @ param integer $id
* @ return associative array
*/
public static function find ( $id ){
$stmt = OC_DB :: prepare ( 'SELECT * FROM *PREFIX*calendar_objects WHERE id = ?' );
$result = $stmt -> execute ( array ( $id ));
return $result -> fetchRow ();
}
/**
* @ brief finds an object by its DAV Data
* @ param integer $cid Calendar id
* @ param string $uri the uri ( 'filename' )
* @ return associative array
*/
public static function findWhereDAVDataIs ( $cid , $uri ){
$stmt = OC_DB :: prepare ( 'SELECT * FROM *PREFIX*calendar_objects WHERE calendarid = ? AND uri = ?' );
$result = $stmt -> execute ( array ( $cid , $uri ));
return $result -> fetchRow ();
}
/**
* @ brief Adds an object
* @ param integer $id Calendar id
* @ param string $data object
* @ return insertid
*/
public static function add ( $id , $data ){
2011-09-20 16:41:17 +04:00
$object = OC_VObject :: parse ( $data );
2012-04-13 20:36:20 +04:00
OC_Calendar_App :: loadCategoriesFromVCalendar ( $object );
2011-09-15 12:14:47 +04:00
list ( $type , $startdate , $enddate , $summary , $repeating , $uid ) = self :: extractData ( $object );
if ( is_null ( $uid )){
2012-03-08 00:29:23 +04:00
$object -> setUID ();
2011-09-15 12:14:47 +04:00
$data = $object -> serialize ();
}
$uri = 'owncloud-' . md5 ( $data . rand () . time ()) . '.ics' ;
$stmt = OC_DB :: prepare ( 'INSERT INTO *PREFIX*calendar_objects (calendarid,objecttype,startdate,enddate,repeating,summary,calendardata,uri,lastmodified) VALUES(?,?,?,?,?,?,?,?,?)' );
2012-04-11 18:21:45 +04:00
$stmt -> execute ( array ( $id , $type , $startdate , $enddate , $repeating , $summary , $data , $uri , time ()));
2011-09-15 12:14:47 +04:00
OC_Calendar_Calendar :: touchCalendar ( $id );
2011-10-29 13:40:48 +04:00
return OC_DB :: insertid ( '*PREFIX*calendar_objects' );
2011-09-15 12:14:47 +04:00
}
/**
* @ brief Adds an object with the data provided by sabredav
* @ param integer $id Calendar id
* @ param string $uri the uri the card will have
* @ param string $data object
* @ return insertid
*/
public static function addFromDAVData ( $id , $uri , $data ){
2011-09-20 16:41:17 +04:00
$object = OC_VObject :: parse ( $data );
2011-09-15 12:14:47 +04:00
list ( $type , $startdate , $enddate , $summary , $repeating , $uid ) = self :: extractData ( $object );
$stmt = OC_DB :: prepare ( 'INSERT INTO *PREFIX*calendar_objects (calendarid,objecttype,startdate,enddate,repeating,summary,calendardata,uri,lastmodified) VALUES(?,?,?,?,?,?,?,?,?)' );
2012-04-11 18:21:45 +04:00
$stmt -> execute ( array ( $id , $type , $startdate , $enddate , $repeating , $summary , $data , $uri , time ()));
2011-09-15 12:14:47 +04:00
OC_Calendar_Calendar :: touchCalendar ( $id );
2011-10-29 13:40:48 +04:00
return OC_DB :: insertid ( '*PREFIX*calendar_objects' );
2011-09-15 12:14:47 +04:00
}
/**
* @ brief edits an object
* @ param integer $id id of object
* @ param string $data object
* @ return boolean
*/
public static function edit ( $id , $data ){
$oldobject = self :: find ( $id );
2011-09-20 16:41:17 +04:00
$object = OC_VObject :: parse ( $data );
2012-04-13 20:36:20 +04:00
OC_Calendar_App :: loadCategoriesFromVCalendar ( $object );
2011-09-15 12:14:47 +04:00
list ( $type , $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 = ?' );
2012-04-11 18:21:45 +04:00
$stmt -> execute ( array ( $type , $startdate , $enddate , $repeating , $summary , $data , time (), $id ));
2011-09-15 12:14:47 +04:00
2011-09-23 12:50:32 +04:00
OC_Calendar_Calendar :: touchCalendar ( $oldobject [ 'calendarid' ]);
2011-09-15 12:14:47 +04:00
return true ;
}
/**
* @ brief edits an object with the data provided by sabredav
* @ param integer $id calendar id
* @ param string $uri the uri of the object
* @ param string $data object
* @ return boolean
*/
public static function editFromDAVData ( $cid , $uri , $data ){
$oldobject = self :: findWhereDAVDataIs ( $cid , $uri );
2011-09-20 16:41:17 +04:00
$object = OC_VObject :: parse ( $data );
2011-09-15 12:14:47 +04:00
list ( $type , $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 = ?' );
2012-04-11 18:21:45 +04:00
$stmt -> execute ( array ( $type , $startdate , $enddate , $repeating , $summary , $data , time (), $oldobject [ 'id' ]));
2011-09-15 12:14:47 +04:00
OC_Calendar_Calendar :: touchCalendar ( $oldobject [ 'calendarid' ]);
return true ;
}
/**
* @ brief deletes an object
* @ param integer $id id of object
* @ return boolean
*/
public static function delete ( $id ){
2011-09-23 12:50:32 +04:00
$oldobject = self :: find ( $id );
2011-09-15 12:14:47 +04:00
$stmt = OC_DB :: prepare ( 'DELETE FROM *PREFIX*calendar_objects WHERE id = ?' );
$stmt -> execute ( array ( $id ));
2011-09-23 12:50:32 +04:00
OC_Calendar_Calendar :: touchCalendar ( $oldobject [ 'calendarid' ]);
2011-09-15 12:14:47 +04:00
return true ;
}
/**
* @ brief deletes an object with the data provided by sabredav
* @ param integer $cid calendar id
* @ param string $uri the uri of the object
* @ return boolean
*/
public static function deleteFromDAVData ( $cid , $uri ){
$stmt = OC_DB :: prepare ( 'DELETE FROM *PREFIX*calendar_objects WHERE calendarid = ? AND uri=?' );
$stmt -> execute ( array ( $cid , $uri ));
2012-02-20 00:24:53 +04:00
OC_Calendar_Calendar :: touchCalendar ( $cid );
2011-09-15 12:14:47 +04:00
return true ;
}
2011-09-16 02:08:15 +04:00
public static function moveToCalendar ( $id , $calendarid ){
$stmt = OC_DB :: prepare ( 'UPDATE *PREFIX*calendar_objects SET calendarid=? WHERE id = ?' );
2012-04-11 18:21:45 +04:00
$stmt -> execute ( array ( $calendarid , $id ));
2011-09-16 02:08:15 +04:00
OC_Calendar_Calendar :: touchCalendar ( $id );
return true ;
}
2011-09-15 12:14:47 +04:00
/**
* @ brief Extracts data from a vObject - Object
* @ param Sabre_VObject $object
* @ return array
*
* [ type , start , end , summary , repeating , uid ]
*/
protected static function extractData ( $object ){
$return = array ( '' , null , null , '' , 0 , null );
// Child to use
$children = 0 ;
$use = null ;
2011-09-20 16:41:17 +04:00
foreach ( $object -> children as $property ){
2011-09-15 12:14:47 +04:00
if ( $property -> name == 'VEVENT' ){
$children ++ ;
$thisone = true ;
foreach ( $property -> children as & $element ){
if ( $element -> name == 'RECURRENCE-ID' ){
$thisone = false ;
}
} unset ( $element );
if ( $thisone ){
$use = $property ;
}
}
elseif ( $property -> name == 'VTODO' || $property -> name == 'VJOURNAL' ){
2011-09-15 13:26:13 +04:00
$return [ 0 ] = $property -> name ;
2011-09-15 12:14:47 +04:00
foreach ( $property -> children as & $element ){
2011-09-15 13:26:13 +04:00
if ( $element -> name == 'SUMMARY' ){
$return [ 3 ] = $element -> value ;
2011-09-15 12:14:47 +04:00
}
2011-09-15 13:26:13 +04:00
elseif ( $element -> name == 'UID' ){
$return [ 5 ] = $element -> value ;
2011-09-15 12:14:47 +04:00
}
};
// Only one VTODO or VJOURNAL per object
// (only one UID per object but a UID is required by a VTODO =>
// one VTODO per object)
break ;
}
2011-09-20 16:41:17 +04:00
}
2011-09-15 12:14:47 +04:00
// find the data
if ( ! is_null ( $use )){
$return [ 0 ] = $use -> name ;
2011-09-20 16:41:17 +04:00
foreach ( $use -> children as $property ){
2011-09-15 12:14:47 +04:00
if ( $property -> name == 'DTSTART' ){
$return [ 1 ] = self :: getUTCforMDB ( $property -> getDateTime ());
}
elseif ( $property -> name == 'DTEND' ){
$return [ 2 ] = self :: getUTCforMDB ( $property -> getDateTime ());
}
elseif ( $property -> name == 'SUMMARY' ){
$return [ 3 ] = $property -> value ;
}
elseif ( $property -> name == 'RRULE' ){
$return [ 4 ] = 1 ;
}
elseif ( $property -> name == 'UID' ){
$return [ 5 ] = $property -> value ;
}
2011-09-20 16:41:17 +04:00
}
2011-09-15 12:14:47 +04:00
}
// More than one child means reoccuring!
if ( $children > 1 ){
$return [ 4 ] = 1 ;
}
return $return ;
}
/**
* @ brief DateTime to UTC string
* @ param DateTime $datetime The date to convert
* @ returns date as YYYY - MM - DD hh : mm
*
* This function creates a date string that can be used by MDB2 .
* Furthermore it converts the time to UTC .
*/
protected static function getUTCforMDB ( $datetime ){
return date ( 'Y-m-d H:i' , $datetime -> format ( 'U' ) - $datetime -> getOffset ());
}
2011-09-29 01:16:32 +04:00
public static function getDTEndFromVEvent ( $vevent )
{
if ( $vevent -> DTEND ) {
$dtend = $vevent -> DTEND ;
} else {
$dtend = clone $vevent -> DTSTART ;
2012-03-01 23:50:00 +04:00
// clone creates a shallow copy, also clone DateTime
$dtend -> setDateTime ( clone $dtend -> getDateTime (), $dtend -> getDateType ());
2011-09-29 01:16:32 +04:00
if ( $vevent -> DURATION ){
$duration = strval ( $vevent -> DURATION );
$invert = 0 ;
if ( $duration [ 0 ] == '-' ){
$duration = substr ( $duration , 1 );
$invert = 1 ;
}
if ( $duration [ 0 ] == '+' ){
$duration = substr ( $duration , 1 );
}
$interval = new DateInterval ( $duration );
$interval -> invert = $invert ;
$dtend -> getDateTime () -> add ( $interval );
}
}
return $dtend ;
}
2011-09-15 23:20:42 +04:00
public static function getRepeatOptions ( $l10n )
{
return array (
'doesnotrepeat' => $l10n -> t ( 'Does not repeat' ),
'daily' => $l10n -> t ( 'Daily' ),
'weekly' => $l10n -> t ( 'Weekly' ),
'weekday' => $l10n -> t ( 'Every Weekday' ),
'biweekly' => $l10n -> t ( 'Bi-Weekly' ),
'monthly' => $l10n -> t ( 'Monthly' ),
2011-12-26 02:19:49 +04:00
'yearly' => $l10n -> t ( 'Yearly' )
2011-09-15 23:20:42 +04:00
);
}
2011-12-26 02:19:49 +04:00
public static function getEndOptions ( $l10n )
{
return array (
'never' => $l10n -> t ( 'never' ),
'count' => $l10n -> t ( 'by occurrences' ),
'date' => $l10n -> t ( 'by date' )
);
}
public static function getMonthOptions ( $l10n )
{
return array (
'monthday' => $l10n -> t ( 'by monthday' ),
'weekday' => $l10n -> t ( 'by weekday' )
);
}
public static function getWeeklyOptions ( $l10n )
{
return array (
'MO' => $l10n -> t ( 'Monday' ),
'TU' => $l10n -> t ( 'Tuesday' ),
'WE' => $l10n -> t ( 'Wednesday' ),
'TH' => $l10n -> t ( 'Thursday' ),
'FR' => $l10n -> t ( 'Friday' ),
'SA' => $l10n -> t ( 'Saturday' ),
'SU' => $l10n -> t ( 'Sunday' )
);
}
public static function getWeekofMonth ( $l10n )
{
return array (
'auto' => $l10n -> t ( 'events week of month' ),
'1' => $l10n -> t ( 'first' ),
'2' => $l10n -> t ( 'second' ),
'3' => $l10n -> t ( 'third' ),
'4' => $l10n -> t ( 'fourth' ),
'5' => $l10n -> t ( 'fifth' ),
'-1' => $l10n -> t ( 'last' )
);
}
public static function getByYearDayOptions (){
$return = array ();
foreach ( range ( 1 , 366 ) as $num ){
$return [( string ) $num ] = ( string ) $num ;
}
return $return ;
}
public static function getByMonthDayOptions (){
$return = array ();
foreach ( range ( 1 , 31 ) as $num ){
$return [( string ) $num ] = ( string ) $num ;
}
return $return ;
}
public static function getByMonthOptions ( $l10n ){
return array (
'1' => $l10n -> t ( 'January' ),
'2' => $l10n -> t ( 'February' ),
'3' => $l10n -> t ( 'March' ),
'4' => $l10n -> t ( 'April' ),
'5' => $l10n -> t ( 'May' ),
'6' => $l10n -> t ( 'June' ),
'7' => $l10n -> t ( 'July' ),
'8' => $l10n -> t ( 'August' ),
'9' => $l10n -> t ( 'September' ),
'10' => $l10n -> t ( 'October' ),
'11' => $l10n -> t ( 'November' ),
'12' => $l10n -> t ( 'December' )
);
}
public static function getYearOptions ( $l10n ){
return array (
2011-12-26 22:59:15 +04:00
'bydate' => $l10n -> t ( 'by events date' ),
2011-12-26 02:19:49 +04:00
'byyearday' => $l10n -> t ( 'by yearday(s)' ),
'byweekno' => $l10n -> t ( 'by weeknumber(s)' ),
'bydaymonth' => $l10n -> t ( 'by day and month' )
);
}
public static function getByWeekNoOptions (){
return range ( 1 , 52 );
}
2011-09-15 23:20:42 +04:00
public static function validateRequest ( $request )
{
$errnum = 0 ;
$errarr = array ( 'title' => 'false' , 'cal' => 'false' , 'from' => 'false' , 'fromtime' => 'false' , 'to' => 'false' , 'totime' => 'false' , 'endbeforestart' => 'false' );
if ( $request [ 'title' ] == '' ){
$errarr [ 'title' ] = 'true' ;
$errnum ++ ;
}
2011-09-23 00:22:52 +04:00
2011-09-15 23:20:42 +04:00
$fromday = substr ( $request [ 'from' ], 0 , 2 );
$frommonth = substr ( $request [ 'from' ], 3 , 2 );
$fromyear = substr ( $request [ 'from' ], 6 , 4 );
if ( ! checkdate ( $frommonth , $fromday , $fromyear )){
$errarr [ 'from' ] = 'true' ;
$errnum ++ ;
}
$allday = isset ( $request [ 'allday' ]);
if ( ! $allday && self :: checkTime ( urldecode ( $request [ 'fromtime' ]))) {
$errarr [ 'fromtime' ] = 'true' ;
$errnum ++ ;
}
$today = substr ( $request [ 'to' ], 0 , 2 );
$tomonth = substr ( $request [ 'to' ], 3 , 2 );
$toyear = substr ( $request [ 'to' ], 6 , 4 );
if ( ! checkdate ( $tomonth , $today , $toyear )){
$errarr [ 'to' ] = 'true' ;
$errnum ++ ;
}
2011-12-26 02:19:49 +04:00
if ( $request [ 'repeat' ] != 'doesnotrepeat' ){
if ( is_nan ( $request [ 'interval' ]) && $request [ 'interval' ] != '' ){
$errarr [ 'interval' ] = 'true' ;
2012-04-11 18:21:45 +04:00
$errnum ++ ;
2011-12-26 02:19:49 +04:00
}
if ( array_key_exists ( 'repeat' , $request ) && ! array_key_exists ( $request [ 'repeat' ], self :: getRepeatOptions ( OC_Calendar_App :: $l10n ))){
$errarr [ 'repeat' ] = 'true' ;
2012-04-11 18:21:45 +04:00
$errnum ++ ;
2011-12-26 02:19:49 +04:00
}
if ( array_key_exists ( 'advanced_month_select' , $request ) && ! array_key_exists ( $request [ 'advanced_month_select' ], self :: getMonthOptions ( OC_Calendar_App :: $l10n ))){
$errarr [ 'advanced_month_select' ] = 'true' ;
$errnum ++ ;
}
if ( array_key_exists ( 'advanced_year_select' , $request ) && ! array_key_exists ( $request [ 'advanced_year_select' ], self :: getYearOptions ( OC_Calendar_App :: $l10n ))){
$errarr [ 'advanced_year_select' ] = 'true' ;
$errnum ++ ;
}
if ( array_key_exists ( 'weekofmonthoptions' , $request ) && ! array_key_exists ( $request [ 'weekofmonthoptions' ], self :: getWeekofMonth ( OC_Calendar_App :: $l10n ))){
$errarr [ 'weekofmonthoptions' ] = 'true' ;
$errnum ++ ;
}
if ( $request [ 'end' ] != 'never' ){
if ( ! array_key_exists ( $request [ 'end' ], self :: getEndOptions ( OC_Calendar_App :: $l10n ))){
$errarr [ 'end' ] = 'true' ;
$errnum ++ ;
}
if ( $request [ 'end' ] == 'count' && is_nan ( $request [ 'byoccurrences' ])){
$errarr [ 'byoccurrences' ] = 'true' ;
$errnum ++ ;
}
if ( $request [ 'end' ] == 'date' ){
list ( $bydate_day , $bydate_month , $bydate_year ) = explode ( '-' , $request [ 'bydate' ]);
if ( ! checkdate ( $bydate_month , $bydate_day , $bydate_year )){
$errarr [ 'bydate' ] = 'true' ;
$errnum ++ ;
}
}
}
if ( array_key_exists ( 'weeklyoptions' , $request )){
foreach ( $request [ 'weeklyoptions' ] as $option ){
if ( ! in_array ( $option , self :: getWeeklyOptions ( OC_Calendar_App :: $l10n ))){
$errarr [ 'weeklyoptions' ] = 'true' ;
$errnum ++ ;
}
}
}
if ( array_key_exists ( 'byyearday' , $request )){
foreach ( $request [ 'byyearday' ] as $option ){
if ( ! array_key_exists ( $option , self :: getByYearDayOptions ())){
$errarr [ 'byyearday' ] = 'true' ;
$errnum ++ ;
}
}
}
if ( array_key_exists ( 'weekofmonthoptions' , $request )){
if ( is_nan (( double ) $request [ 'weekofmonthoptions' ])){
$errarr [ 'weekofmonthoptions' ] = 'true' ;
$errnum ++ ;
}
}
if ( array_key_exists ( 'bymonth' , $request )){
foreach ( $request [ 'bymonth' ] as $option ){
if ( ! in_array ( $option , self :: getByMonthOptions ( OC_Calendar_App :: $l10n ))){
$errarr [ 'bymonth' ] = 'true' ;
$errnum ++ ;
}
}
}
if ( array_key_exists ( 'byweekno' , $request )){
foreach ( $request [ 'byweekno' ] as $option ){
if ( ! array_key_exists ( $option , self :: getByWeekNoOptions ())){
$errarr [ 'byweekno' ] = 'true' ;
$errnum ++ ;
}
}
}
if ( array_key_exists ( 'bymonthday' , $request )){
foreach ( $request [ 'bymonthday' ] as $option ){
if ( ! array_key_exists ( $option , self :: getByMonthDayOptions ())){
$errarr [ 'bymonthday' ] = 'true' ;
$errnum ++ ;
}
}
}
}
2011-09-15 23:20:42 +04:00
if ( ! $allday && self :: checkTime ( urldecode ( $request [ 'totime' ]))) {
$errarr [ 'totime' ] = 'true' ;
$errnum ++ ;
}
if ( $today < $fromday && $frommonth == $tomonth && $fromyear == $toyear ){
$errarr [ 'endbeforestart' ] = 'true' ;
$errnum ++ ;
}
if ( $today == $fromday && $frommonth > $tomonth && $fromyear == $toyear ){
$errarr [ 'endbeforestart' ] = 'true' ;
$errnum ++ ;
}
if ( $today == $fromday && $frommonth == $tomonth && $fromyear > $toyear ){
$errarr [ 'endbeforestart' ] = 'true' ;
$errnum ++ ;
}
2011-10-25 21:59:23 +04:00
if ( ! $allday && $fromday == $today && $frommonth == $tomonth && $fromyear == $toyear ){
2011-09-15 23:20:42 +04:00
list ( $tohours , $tominutes ) = explode ( ':' , $request [ 'totime' ]);
list ( $fromhours , $fromminutes ) = explode ( ':' , $request [ 'fromtime' ]);
if ( $tohours < $fromhours ){
$errarr [ 'endbeforestart' ] = 'true' ;
$errnum ++ ;
}
if ( $tohours == $fromhours && $tominutes < $fromminutes ){
$errarr [ 'endbeforestart' ] = 'true' ;
$errnum ++ ;
}
}
if ( $errnum )
{
return $errarr ;
}
return false ;
}
protected static function checkTime ( $time )
{
list ( $hours , $minutes ) = explode ( ':' , $time );
return empty ( $time )
|| $hours < 0 || $hours > 24
|| $minutes < 0 || $minutes > 60 ;
}
public static function createVCalendarFromRequest ( $request )
{
2011-09-20 16:41:17 +04:00
$vcalendar = new OC_VObject ( 'VCALENDAR' );
2011-09-15 23:20:42 +04:00
$vcalendar -> add ( 'PRODID' , 'ownCloud Calendar' );
$vcalendar -> add ( 'VERSION' , '2.0' );
2011-09-20 16:41:17 +04:00
$vevent = new OC_VObject ( 'VEVENT' );
2011-09-15 23:20:42 +04:00
$vcalendar -> add ( $vevent );
2012-02-25 01:20:40 +04:00
$vevent -> setDateTime ( 'CREATED' , 'now' , Sabre_VObject_Property_DateTime :: UTC );
2011-09-16 02:08:15 +04:00
2011-09-20 16:41:17 +04:00
$vevent -> setUID ();
2011-09-15 23:20:42 +04:00
return self :: updateVCalendarFromRequest ( $request , $vcalendar );
}
public static function updateVCalendarFromRequest ( $request , $vcalendar )
{
$title = $request [ " title " ];
$location = $request [ " location " ];
2012-04-13 18:51:04 +04:00
$categories = $request [ " categories " ];
2011-09-15 23:20:42 +04:00
$allday = isset ( $request [ " allday " ]);
$from = $request [ " from " ];
$to = $request [ " to " ];
2011-10-25 21:59:23 +04:00
if ( ! $allday ){
$fromtime = $request [ 'fromtime' ];
$totime = $request [ 'totime' ];
}
2011-12-26 02:19:49 +04:00
$vevent = $vcalendar -> VEVENT ;
2011-09-15 23:20:42 +04:00
$description = $request [ " description " ];
2011-12-26 02:19:49 +04:00
$repeat = $request [ " repeat " ];
if ( $repeat != 'doesnotrepeat' ){
$rrule = '' ;
$interval = $request [ 'interval' ];
$end = $request [ 'end' ];
$byoccurrences = $request [ 'byoccurrences' ];
switch ( $repeat ){
case 'daily' :
$rrule .= 'FREQ=DAILY' ;
break ;
case 'weekly' :
$rrule .= 'FREQ=WEEKLY' ;
if ( array_key_exists ( 'weeklyoptions' , $request )){
$byday = '' ;
$daystrings = array_flip ( self :: getWeeklyOptions ( OC_Calendar_App :: $l10n ));
foreach ( $request [ 'weeklyoptions' ] as $days ){
if ( $byday == '' ){
$byday .= $daystrings [ $days ];
} else {
$byday .= ',' . $daystrings [ $days ];
}
}
$rrule .= ';BYDAY=' . $byday ;
}
break ;
case 'weekday' :
2012-01-05 20:09:01 +04:00
$rrule .= 'FREQ=WEEKLY' ;
2011-12-26 02:19:49 +04:00
$rrule .= ';BYDAY=MO,TU,WE,TH,FR' ;
break ;
case 'biweekly' :
$rrule .= 'FREQ=WEEKLY' ;
$interval = $interval * 2 ;
break ;
case 'monthly' :
$rrule .= 'FREQ=MONTHLY' ;
if ( $request [ 'advanced_month_select' ] == 'monthday' ){
break ;
} elseif ( $request [ 'advanced_month_select' ] == 'weekday' ){
if ( $request [ 'weekofmonthoptions' ] == 'auto' ){
2012-01-05 20:53:13 +04:00
list ( $_day , $_month , $_year ) = explode ( '-' , $from );
$weekofmonth = floor ( $_day / 7 );
2011-12-26 02:19:49 +04:00
} else {
$weekofmonth = $request [ 'weekofmonthoptions' ];
}
$days = array_flip ( self :: getWeeklyOptions ( OC_Calendar_App :: $l10n ));
$byday = '' ;
foreach ( $request [ 'weeklyoptions' ] as $day ){
if ( $byday == '' ){
$byday .= $weekofmonth . $days [ $day ];
} else {
$byday .= ',' . $weekofmonth . $days [ $day ];
}
}
$rrule .= ';BYDAY=' . $byday ;
}
break ;
case 'yearly' :
$rrule .= 'FREQ=YEARLY' ;
2011-12-26 22:59:15 +04:00
if ( $request [ 'advanced_year_select' ] == 'bydate' ){
} elseif ( $request [ 'advanced_year_select' ] == 'byyearday' ){
list ( $_day , $_month , $_year ) = explode ( '-' , $from );
$byyearday = date ( 'z' , mktime ( 0 , 0 , 0 , $_month , $_day , $_year )) + 1 ;
if ( array_key_exists ( 'byyearday' , $request )){
foreach ( $request [ 'byyearday' ] as $yearday ){
2011-12-26 02:19:49 +04:00
$byyearday .= ',' . $yearday ;
}
}
$rrule .= ';BYYEARDAY=' . $byyearday ;
} elseif ( $request [ 'advanced_year_select' ] == 'byweekno' ){
list ( $_day , $_month , $_year ) = explode ( '-' , $from );
$rrule .= ';BYDAY=' . strtoupper ( substr ( date ( 'l' , mktime ( 0 , 0 , 0 , $_month , $_day , $_year )), 0 , 2 ));
$byweekno = '' ;
foreach ( $request [ 'byweekno' ] as $weekno ){
if ( $byweekno == '' ){
$byweekno = $weekno ;
} else {
$byweekno .= ',' . $weekno ;
}
}
$rrule .= ';BYWEEKNO=' . $byweekno ;
} elseif ( $request [ 'advanced_year_select' ] == 'bydaymonth' ){
if ( array_key_exists ( 'weeklyoptions' , $request )){
$days = array_flip ( self :: getWeeklyOptions ( OC_Calendar_App :: $l10n ));
$byday = '' ;
foreach ( $request [ 'weeklyoptions' ] as $day ){
if ( $byday == '' ){
$byday .= $days [ $day ];
} else {
$byday .= ',' . $days [ $day ];
}
}
$rrule .= ';BYDAY=' . $byday ;
}
if ( array_key_exists ( 'bymonth' , $request )){
$monthes = array_flip ( self :: getByMonthOptions ( OC_Calendar_App :: $l10n ));
$bymonth = '' ;
foreach ( $request [ 'bymonth' ] as $month ){
if ( $bymonth == '' ){
$bymonth .= $monthes [ $month ];
} else {
$bymonth .= ',' . $monthes [ $month ];
}
}
$rrule .= ';BYMONTH=' . $bymonth ;
}
if ( array_key_exists ( 'bymonthday' , $request )){
$bymonthday = '' ;
foreach ( $request [ 'bymonthday' ] as $monthday ){
if ( $bymonthday == '' ){
$bymonthday .= $monthday ;
} else {
$bymonthday .= ',' . $monthday ;
}
}
$rrule .= ';BYMONTHDAY=' . $bymonthday ;
}
}
break ;
default :
break ;
}
if ( $interval != '' ){
$rrule .= ';INTERVAL=' . $interval ;
}
if ( $end == 'count' ){
$rrule .= ';COUNT=' . $byoccurrences ;
}
if ( $end == 'date' ){
list ( $bydate_day , $bydate_month , $bydate_year ) = explode ( '-' , $request [ 'bydate' ]);
$rrule .= ';UNTIL=' . $bydate_year . $bydate_month . $bydate_day ;
}
$vevent -> setString ( 'RRULE' , $rrule );
$repeat = " true " ;
} else {
$repeat = " false " ;
}
2011-09-15 23:20:42 +04:00
2012-02-25 01:20:40 +04:00
$vevent -> setDateTime ( 'LAST-MODIFIED' , 'now' , Sabre_VObject_Property_DateTime :: UTC );
$vevent -> setDateTime ( 'DTSTAMP' , 'now' , Sabre_VObject_Property_DateTime :: UTC );
2011-09-20 16:41:17 +04:00
$vevent -> setString ( 'SUMMARY' , $title );
2011-09-15 23:20:42 +04:00
if ( $allday ){
$start = new DateTime ( $from );
$end = new DateTime ( $to . ' +1 day' );
2012-02-25 01:20:40 +04:00
$vevent -> setDateTime ( 'DTSTART' , $start , Sabre_VObject_Property_DateTime :: DATE );
$vevent -> setDateTime ( 'DTEND' , $end , Sabre_VObject_Property_DateTime :: DATE );
2011-09-15 23:20:42 +04:00
} else {
2012-05-01 20:50:31 +04:00
$timezone = OC_Preferences :: getValue ( OCP\USER :: getUser (), 'calendar' , 'timezone' , date_default_timezone_get ());
2011-09-15 23:20:42 +04:00
$timezone = new DateTimeZone ( $timezone );
$start = new DateTime ( $from . ' ' . $fromtime , $timezone );
$end = new DateTime ( $to . ' ' . $totime , $timezone );
2012-02-25 01:20:40 +04:00
$vevent -> setDateTime ( 'DTSTART' , $start , Sabre_VObject_Property_DateTime :: LOCALTZ );
$vevent -> setDateTime ( 'DTEND' , $end , Sabre_VObject_Property_DateTime :: LOCALTZ );
2011-09-15 23:20:42 +04:00
}
2011-09-29 01:16:32 +04:00
unset ( $vevent -> DURATION );
2011-09-15 23:20:42 +04:00
2011-09-20 16:41:17 +04:00
$vevent -> setString ( 'LOCATION' , $location );
2011-12-10 00:56:03 +04:00
$vevent -> setString ( 'DESCRIPTION' , $description );
2012-04-13 18:51:04 +04:00
$vevent -> setString ( 'CATEGORIES' , $categories );
2011-09-15 23:20:42 +04:00
/* if ( $repeat == " true " ){
$vevent -> RRULE = $repeat ;
} */
return $vcalendar ;
}
2012-03-03 18:06:51 +04:00
2012-02-22 13:42:01 +04:00
public static function getowner ( $id ){
$event = self :: find ( $id );
$cal = OC_Calendar_Calendar :: find ( $event [ 'calendarid' ]);
return $cal [ 'userid' ];
}
2012-04-10 23:53:39 +04:00
public static function getCalendarid ( $id ){
$event = self :: find ( $id );
return $event [ 'calendarid' ];
}
2012-03-01 23:50:00 +04:00
}