calendar.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Copyright (c) 2011 Jakob Sack <mail@jakobsack.de>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /*
  9. *
  10. * The following SQL statement is just a help for developers and will not be
  11. * executed!
  12. *
  13. * CREATE TABLE calendar_objects (
  14. * id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  15. * calendarid INTEGER UNSIGNED NOT NULL,
  16. * objecttype VARCHAR(40) NOT NULL,
  17. * startdate DATETIME,
  18. * enddate DATETIME,
  19. * repeating INT(1),
  20. * summary VARCHAR(255),
  21. * calendardata TEXT,
  22. * uri VARCHAR(100),
  23. * lastmodified INT(11)
  24. * );
  25. *
  26. * CREATE TABLE calendar_calendars (
  27. * id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  28. * userid VARCHAR(255),
  29. * displayname VARCHAR(100),
  30. * uri VARCHAR(100),
  31. * active INTEGER UNSIGNED NOT NULL DEFAULT '0',
  32. * ctag INTEGER UNSIGNED NOT NULL DEFAULT '0',
  33. * calendarorder INTEGER UNSIGNED NOT NULL DEFAULT '0',
  34. * calendarcolor VARCHAR(10),
  35. * timezone TEXT,
  36. * components VARCHAR(20)
  37. * );
  38. */
  39. /**
  40. * This class manages our calendars
  41. */
  42. class OC_Calendar_Calendar{
  43. /**
  44. * @brief Returns the list of calendars for a specific user.
  45. * @param string $uid User ID
  46. * @param boolean $active Only return calendars with this $active state, default(=null) is don't care
  47. * @return array
  48. */
  49. public static function allCalendars($uid, $active=null){
  50. $values = array($uid);
  51. $active_where = '';
  52. if (!is_null($active) && $active){
  53. $active_where = ' AND active = ?';
  54. $values[] = $active;
  55. }
  56. $stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*calendar_calendars WHERE userid = ?' . $active_where );
  57. $result = $stmt->execute($values);
  58. $calendars = array();
  59. while( $row = $result->fetchRow()){
  60. $calendars[] = $row;
  61. }
  62. return $calendars;
  63. }
  64. /**
  65. * @brief Returns the list of calendars for a principal (DAV term of user)
  66. * @param string $principaluri
  67. * @return array
  68. */
  69. public static function allCalendarsWherePrincipalURIIs($principaluri){
  70. $uid = self::extractUserID($principaluri);
  71. return self::allCalendars($uid);
  72. }
  73. /**
  74. * @brief Gets the data of one calendar
  75. * @param integer $id
  76. * @return associative array
  77. */
  78. public static function find($id){
  79. $stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*calendar_calendars WHERE id = ?' );
  80. $result = $stmt->execute(array($id));
  81. return $result->fetchRow();
  82. }
  83. /**
  84. * @brief Creates a new calendar
  85. * @param string $userid
  86. * @param string $name
  87. * @param string $components Default: "VEVENT,VTODO,VJOURNAL"
  88. * @param string $timezone Default: null
  89. * @param integer $order Default: 1
  90. * @param string $color Default: null, format: '#RRGGBB(AA)'
  91. * @return insertid
  92. */
  93. public static function addCalendar($userid,$name,$components='VEVENT,VTODO,VJOURNAL',$timezone=null,$order=0,$color=null){
  94. $all = self::allCalendars($userid);
  95. $uris = array();
  96. foreach($all as $i){
  97. $uris[] = $i['uri'];
  98. }
  99. $uri = self::createURI($name, $uris );
  100. $stmt = OCP\DB::prepare( 'INSERT INTO *PREFIX*calendar_calendars (userid,displayname,uri,ctag,calendarorder,calendarcolor,timezone,components) VALUES(?,?,?,?,?,?,?,?)' );
  101. $result = $stmt->execute(array($userid,$name,$uri,1,$order,$color,$timezone,$components));
  102. return OCP\DB::insertid('*PREFIX*calendar_calendar');
  103. }
  104. /**
  105. * @brief Creates a new calendar from the data sabredav provides
  106. * @param string $principaluri
  107. * @param string $uri
  108. * @param string $name
  109. * @param string $components
  110. * @param string $timezone
  111. * @param integer $order
  112. * @param string $color format: '#RRGGBB(AA)'
  113. * @return insertid
  114. */
  115. public static function addCalendarFromDAVData($principaluri,$uri,$name,$components,$timezone,$order,$color){
  116. $userid = self::extractUserID($principaluri);
  117. $stmt = OCP\DB::prepare( 'INSERT INTO *PREFIX*calendar_calendars (userid,displayname,uri,ctag,calendarorder,calendarcolor,timezone,components) VALUES(?,?,?,?,?,?,?,?)' );
  118. $result = $stmt->execute(array($userid,$name,$uri,1,$order,$color,$timezone,$components));
  119. return OCP\DB::insertid('*PREFIX*calendar_calendars');
  120. }
  121. /**
  122. * @brief Edits a calendar
  123. * @param integer $id
  124. * @param string $name Default: null
  125. * @param string $components Default: null
  126. * @param string $timezone Default: null
  127. * @param integer $order Default: null
  128. * @param string $color Default: null, format: '#RRGGBB(AA)'
  129. * @return boolean
  130. *
  131. * Values not null will be set
  132. */
  133. public static function editCalendar($id,$name=null,$components=null,$timezone=null,$order=null,$color=null){
  134. // Need these ones for checking uri
  135. $calendar = self::find($id);
  136. // Keep old stuff
  137. if(is_null($name)) $name = $calendar['displayname'];
  138. if(is_null($components)) $components = $calendar['components'];
  139. if(is_null($timezone)) $timezone = $calendar['timezone'];
  140. if(is_null($order)) $order = $calendar['calendarorder'];
  141. if(is_null($color)) $color = $calendar['calendarcolor'];
  142. $stmt = OCP\DB::prepare( 'UPDATE *PREFIX*calendar_calendars SET displayname=?,calendarorder=?,calendarcolor=?,timezone=?,components=?,ctag=ctag+1 WHERE id=?' );
  143. $result = $stmt->execute(array($name,$order,$color,$timezone,$components,$id));
  144. return true;
  145. }
  146. /**
  147. * @brief Sets a calendar (in)active
  148. * @param integer $id
  149. * @param boolean $active
  150. * @return boolean
  151. */
  152. public static function setCalendarActive($id,$active){
  153. $stmt = OCP\DB::prepare( 'UPDATE *PREFIX*calendar_calendars SET active = ? WHERE id = ?' );
  154. $stmt->execute(array($active, $id));
  155. return true;
  156. }
  157. /**
  158. * @brief Updates ctag for calendar
  159. * @param integer $id
  160. * @return boolean
  161. */
  162. public static function touchCalendar($id){
  163. $stmt = OCP\DB::prepare( 'UPDATE *PREFIX*calendar_calendars SET ctag = ctag + 1 WHERE id = ?' );
  164. $stmt->execute(array($id));
  165. return true;
  166. }
  167. /**
  168. * @brief removes a calendar
  169. * @param integer $id
  170. * @return boolean
  171. */
  172. public static function deleteCalendar($id){
  173. $stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*calendar_calendars WHERE id = ?' );
  174. $stmt->execute(array($id));
  175. $stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*calendar_objects WHERE calendarid = ?' );
  176. $stmt->execute(array($id));
  177. return true;
  178. }
  179. /**
  180. * @brief Creates a URI for Calendar
  181. * @param string $name name of the calendar
  182. * @param array $existing existing calendar URIs
  183. * @return string uri
  184. */
  185. public static function createURI($name,$existing){
  186. $name = strtolower($name);
  187. $newname = $name;
  188. $i = 1;
  189. while(in_array($newname,$existing)){
  190. $newname = $name.$i;
  191. $i = $i + 1;
  192. }
  193. return $newname;
  194. }
  195. /**
  196. * @brief gets the userid from a principal path
  197. * @return string
  198. */
  199. public static function extractUserID($principaluri){
  200. list($prefix,$userid) = Sabre_DAV_URLUtil::splitPath($principaluri);
  201. return $userid;
  202. }
  203. public static function getCalendarColorOptions(){
  204. return array(
  205. '#ff0000', // "Red"
  206. '#b3dc6c', // "Green"
  207. '#ffff00', // "Yellow"
  208. '#808000', // "Olive"
  209. '#ffa500', // "Orange"
  210. '#ff7f50', // "Coral"
  211. '#ee82ee', // "Violet"
  212. '#9fc6e7', // "light blue"
  213. );
  214. }
  215. public static function getEventSourceInfo($calendar){
  216. return array(
  217. 'url' => OCP\Util::linkTo('calendar', 'ajax/events.php').'?calendar_id='.$calendar['id'],
  218. 'backgroundColor' => $calendar['calendarcolor'],
  219. 'borderColor' => '#888',
  220. 'textColor' => 'black',
  221. 'cache' => true,
  222. );
  223. }
  224. }