nextcloud/apps/calendar/lib/calendar.php

252 lines
7.1 KiB
PHP
Raw Normal View History

2011-08-11 13:22:07 +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-08-11 13:22:07 +04:00
*/
/*
*
* The following SQL statement is just a help for developers and will not be
* executed!
*
* CREATE TABLE calendar_objects (
* id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
* calendarid INTEGER UNSIGNED NOT NULL,
* objecttype VARCHAR(40) NOT NULL,
* startdate DATETIME,
* enddate DATETIME,
* repeating INT(1),
* summary VARCHAR(255),
* calendardata TEXT,
* uri VARCHAR(100),
* lastmodified INT(11)
* );
2011-09-16 00:45:58 +04:00
*
2011-08-11 13:22:07 +04:00
* CREATE TABLE calendar_calendars (
* id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
* userid VARCHAR(255),
* displayname VARCHAR(100),
* uri VARCHAR(100),
* active INTEGER UNSIGNED NOT NULL DEFAULT '0',
2011-08-11 13:22:07 +04:00
* ctag INTEGER UNSIGNED NOT NULL DEFAULT '0',
* calendarorder INTEGER UNSIGNED NOT NULL DEFAULT '0',
* calendarcolor VARCHAR(10),
* timezone TEXT,
* components VARCHAR(20)
* );
*/
/**
2011-08-22 18:59:16 +04:00
* This class manages our calendars
2011-08-11 13:22:07 +04:00
*/
class OC_Calendar_Calendar{
2011-09-16 00:45:58 +04:00
/**
* @brief Returns the list of calendars for a specific user.
* @param string $uid User ID
* @param boolean $active
* @return array
*
* TODO: what is active for?
*/
public static function allCalendars($uid, $active=null){
$values = array($uid);
$active_where = '';
2011-09-16 00:45:58 +04:00
if (!is_null($active) && $active){
$active_where = ' AND active = ?';
$values[] = $active;
}
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*calendar_calendars WHERE userid = ?' . $active_where );
$result = $stmt->execute($values);
2011-09-16 00:45:58 +04:00
2011-08-22 18:59:16 +04:00
$calendars = array();
2011-08-11 13:22:07 +04:00
while( $row = $result->fetchRow()){
2011-08-22 18:59:16 +04:00
$calendars[] = $row;
2011-08-11 13:22:07 +04:00
}
2011-08-22 18:59:16 +04:00
return $calendars;
2011-08-11 13:22:07 +04:00
}
2011-09-16 00:45:58 +04:00
/**
* @brief Returns the list of calendars for a principal (DAV term of user)
* @param string $principaluri
* @return array
*/
2011-08-22 18:59:16 +04:00
public static function allCalendarsWherePrincipalURIIs($principaluri){
2011-08-11 13:22:07 +04:00
$uid = self::extractUserID($principaluri);
return self::allCalendars($uid);
}
2011-09-16 00:45:58 +04:00
/**
* @brief Gets the data of one calendar
* @param integer $id
* @return associative array
*/
public static function find($id){
2011-08-11 13:22:07 +04:00
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*calendar_calendars WHERE id = ?' );
$result = $stmt->execute(array($id));
return $result->fetchRow();
}
2011-09-16 00:45:58 +04:00
/**
* @brief Creates a new calendar
* @param string $userid
* @param string $name
* @param string $components Default: "VEVENT,VTODO,VJOURNAL"
* @param string $timezone Default: null
* @param integer $order Default: 1
* @param string $color Default: null
* @return insertid
*/
2011-10-08 15:28:01 +04:00
public static function addCalendar($userid,$name,$components='VEVENT,VTODO,VJOURNAL',$timezone=null,$order=0,$color=null){
2011-08-22 18:59:16 +04:00
$all = self::allCalendars($userid);
2011-08-11 13:22:07 +04:00
$uris = array();
foreach($all as $i){
$uris[] = $i['uri'];
}
$uri = self::createURI($name, $uris );
2011-10-08 15:28:01 +04:00
$stmt = OC_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));
2011-08-11 13:22:07 +04:00
return OC_DB::insertid('*PREFIX*calendar_calendar');
2011-08-11 13:22:07 +04:00
}
2011-09-16 00:45:58 +04:00
/**
* @brief Creates a new calendar from the data sabredav provides
* @param string $principaluri
* @param string $uri
* @param string $name
* @param string $components
* @param string $timezone
* @param integer $order
* @param string $color
* @return insertid
*/
2011-10-08 15:28:01 +04:00
public static function addCalendarFromDAVData($principaluri,$uri,$name,$components,$timezone,$order,$color){
2011-08-11 13:22:07 +04:00
$userid = self::extractUserID($principaluri);
2011-09-16 00:45:58 +04:00
2011-10-08 15:28:01 +04:00
$stmt = OC_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));
2011-08-11 13:22:07 +04:00
return OC_DB::insertid('*PREFIX*calendar_calendars');
2011-08-11 13:22:07 +04:00
}
2011-09-16 00:45:58 +04:00
/**
* @brief Edits a calendar
* @param integer $id
* @param string $name Default: null
* @param string $components Default: null
* @param string $timezone Default: null
* @param integer $order Default: null
* @param string $color Default: null
* @return boolean
*
* Values not null will be set
*/
2011-10-08 15:28:01 +04:00
public static function editCalendar($id,$name=null,$components=null,$timezone=null,$order=null,$color=null){
2011-08-11 13:22:07 +04:00
// Need these ones for checking uri
$calendar = self::find($id);
2011-08-22 18:59:16 +04:00
// Keep old stuff
if(is_null($name)) $name = $calendar['displayname'];
2011-08-22 18:59:16 +04:00
if(is_null($components)) $components = $calendar['components'];
if(is_null($timezone)) $timezone = $calendar['timezone'];
if(is_null($order)) $order = $calendar['calendarorder'];
if(is_null($color)) $color = $calendar['calendarcolor'];
2011-09-16 00:45:58 +04:00
2011-10-08 15:28:01 +04:00
$stmt = OC_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));
2011-08-11 13:22:07 +04:00
return true;
}
2011-09-16 00:45:58 +04:00
/**
* @brief Sets a calendar (in)active
* @param integer $id
* @param boolean $active
* @return boolean
*/
public static function setCalendarActive($id,$active){
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*calendar_calendars SET active = ? WHERE id = ?' );
$stmt->execute(array($active, $id));
return true;
}
2011-09-16 00:45:58 +04:00
/**
* @brief Updates ctag for calendar
* @param integer $id
* @return boolean
*/
2011-08-11 13:22:07 +04:00
public static function touchCalendar($id){
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*calendar_calendars SET ctag = ctag + 1 WHERE id = ?' );
$stmt->execute(array($id));
return true;
}
2011-09-16 00:45:58 +04:00
/**
* @brief removes a calendar
* @param integer $id
* @return boolean
*/
2011-08-11 13:22:07 +04:00
public static function deleteCalendar($id){
$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*calendar_calendars WHERE id = ?' );
$stmt->execute(array($id));
2011-09-16 00:45:58 +04:00
2011-08-22 18:59:16 +04:00
$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*calendar_objects WHERE calendarid = ?' );
2011-08-11 13:22:07 +04:00
$stmt->execute(array($id));
return true;
}
2011-09-16 00:45:58 +04:00
/**
* @brief Creates a URI for Calendar
* @param string $name name of the calendar
* @param array $existing existing calendar URIs
* @return string uri
*/
2011-08-11 13:22:07 +04:00
public static function createURI($name,$existing){
$name = strtolower($name);
$newname = $name;
$i = 1;
while(in_array($newname,$existing)){
$newname = $name.$i;
$i = $i + 1;
}
return $newname;
}
2011-09-16 00:45:58 +04:00
/**
* @brief gets the userid from a principal path
* @return string
*/
2011-08-11 13:22:07 +04:00
public static function extractUserID($principaluri){
list($prefix,$userid) = Sabre_DAV_URLUtil::splitPath($principaluri);
return $userid;
}
2011-10-14 16:56:18 +04:00
public static function getCalendarColorOptions(){
return array(
'ff0000', // "Red"
'00ff00', // "Green"
'ffff00', // "Yellow"
'808000', // "Olive"
'ffa500', // "Orange"
'ff7f50', // "Coral"
'ee82ee', // "Violet"
'ecc255', // dark yellow
);
}
2011-10-19 21:07:56 +04:00
public static function getEventSourceInfo($calendar){
return array(
'url' => 'ajax/events.php?calendar_id='.$calendar['id'],
'backgroundColor' => '#'.$calendar['calendarcolor'],
'borderColor' => '#888',
'textColor' => 'black',
);
}
2011-08-11 13:22:07 +04:00
}