caldav.plugin.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /*
  3. @name Caldav
  4. @author Valentin CARRUESCO <idleman@idleman.fr>
  5. @link http://blog.idleman.fr
  6. @licence CC by nc sa
  7. @version 1.0.0
  8. @description Plugin intégrant un serveur caldav et un calendrier mois/semaine/jour
  9. */
  10. function caldav_menu(&$menuItems){
  11. global $_;
  12. $menuItems[] = array('sort'=>10,'content'=>'<a href="index.php?module=caldav"><i class="fa fa-calendar"></i> Calendrier</a>');
  13. }
  14. function caldav_home($_){
  15. if(!isset($_['module']) || $_['module']!='caldav') return;
  16. global $myUser;
  17. try{
  18. $dbmanager = new Configuration();
  19. $calendarsQuery = $dbmanager->customQuery("SELECT * FROM ".ENTITY_PREFIX."plugin_caldav_calendars WHERE principaluri ='principals/".$myUser->getLogin()."'");
  20. $calendars =array();
  21. while($calendar = $calendarsQuery->fetchArray()):
  22. $calendars[] = $calendar;
  23. endwhile;
  24. if(count($calendars)==0) throw new Exception('Aucun calendrier créé pour votre compte, veuillez <a href="setting.php?section=caldav">créer un calendrier</a> avant de le consulter.');
  25. $_['calendar'] = isset($_['calendar']) ? $_['calendar']: $calendars[0]['uri'];
  26. $url = YANA_URL.'/plugins/caldav/calendars.php/calendars/'.$myUser->getLogin() .'/'.$_['calendar'];
  27. ?>
  28. <div style="width:300px;margin:20px auto;" >
  29. Calendrier : <select id="calendarSelect" onchange="window.location='index.php?module=caldav&calendar='+$(this).val();">
  30. <?php foreach($calendars as $calendar): ?>
  31. <option <?php echo $_['calendar'] ==$calendar['uri'] ?'selected="selected"':''; ?> value="<?php echo $calendar['uri']; ?>"><?php echo $calendar['displayname']; ?></option>
  32. <?php endforeach; ?>
  33. </select>
  34. </div>
  35. <div id='calendar'></div>
  36. <!-- Modal -->
  37. <div id="eventModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  38. <div class="modal-header">
  39. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  40. <h3 id="myModalLabel">Evenement</h3>
  41. </div>
  42. <div class="modal-body">
  43. <label>Libellé</label>
  44. <input type="text" id="label">
  45. <label label="startDay">Début</label>
  46. <input type="text" class="input-small date" id="startDay">
  47. <input type="text" class="input-mini" id="startHour">:
  48. <input type="text" class="input-mini" id="startMinut">
  49. <label label="endDay">Fin</label>
  50. <input type="text" class="input-small date" id="endDay">
  51. <input type="text" class="input-mini" id="endHour">
  52. <input type="text" class="input-mini" id="endMinut">
  53. <hr/>
  54. <label label="location">Lieu</label>
  55. <input type="text" class="input-large" id="location">
  56. <hr/>
  57. <div class="form-inline">
  58. <input id="alert" type="checkbox" id=""> Alerter <input id="alertNumber" type="text" class="input-mini">
  59. <select id="alertUnity" class="input-small">
  60. <option value="m">Minute(s)</option>
  61. <option value="h">Heure(s)</option>
  62. <option value="d">Jour(s)</option>
  63. </select> avant l'évenement.
  64. </div>
  65. </div>
  66. <div class="modal-footer">
  67. <button class="btn" data-dismiss="modal" aria-hidden="true">Fermer</button>
  68. <button class="btn btn-primary" onclick="caldav_save_event();">Enregistrer</button>
  69. </div>
  70. </div>
  71. <?php
  72. }catch(Exception $e){
  73. ?><div class="row"><div class="span12">
  74. <div class="alert alert-error fade in">
  75. <button type="button" class="close" data-dismiss="alert">×</button>
  76. <?php echo $e->getMessage(); ?></div>
  77. </div></div><?php
  78. }
  79. }
  80. function caldav_action(){
  81. global $_,$myUser;
  82. switch($_['action']){
  83. case 'caldav_get_events':
  84. Action::write(function($_,&$response){
  85. global $myUser;
  86. require_once('CalDavClient.class.php');
  87. $client = new CaldavClient();
  88. $client->host = YANA_URL.'/plugins/caldav/calendars.php/calendars';
  89. $client->login = $myUser->getLogin();
  90. $client->password = $myUser->getToken();
  91. $client->user = $myUser->getLogin();
  92. $client->calendar = $_['calendar'];
  93. $events = $client->get_events($_['calendar'],strtotime($_['start']),strtotime($_['end']));
  94. $response = array();
  95. foreach($events as $event):
  96. $response[] = array(
  97. 'id' => $event->ics,
  98. 'title' => $event->title,
  99. 'start' => date('Y-m-d\TH:i:s',$event->start),
  100. 'end' => date('Y-m-d\TH:i:s',$event->end),
  101. 'backgroundColor'=> '#94c655',
  102. 'borderColor' => '#78ab24',
  103. 'textColor' => '#ffffff'
  104. );
  105. endforeach;
  106. });
  107. break;
  108. case 'caldav_save_event':
  109. Action::write(function($_,&$response){
  110. global $myUser;
  111. require_once('CalDavClient.class.php');
  112. $client = new CaldavClient();
  113. $client->host = YANA_URL.'/plugins/caldav/calendars.php/calendars';
  114. $client->login = $myUser->getLogin();
  115. $client->password = $myUser->getToken();
  116. $client->user = $myUser->getLogin();
  117. $client->calendar = $_['calendar'];
  118. list($startDay,$startMonth,$startYear) = explode('/',$_['startDay']);
  119. list($endDay,$endMonth,$endYear) = explode('/',$_['endDay']);
  120. $start = mktime ($_['startHour'], $_['startMinut'], 0, $startMonth,$startDay, $startYear);
  121. $end = mktime ($_['endHour'], $_['endMinut'], 0, $endMonth,$endDay, $endYear);
  122. $event = new IcalEvent();
  123. $event->title = $_['label'];
  124. $event->description = $_['label'];
  125. $event->start = $start;
  126. $event->end = $end ;
  127. $event->categories = array('évenement');
  128. if($_['alert']=='1')
  129. $event->alarms = array($_['alertNumber'].$_['alertUnity']);
  130. if($_['location']!='')
  131. $event->location = $_['location'];
  132. $ics = isset($_['ics']) && !empty($_['ics']) ? $_['ics'] : null;
  133. $ics = $client->create_event($event,$ics);
  134. $event->id = $ics;
  135. $event->start = date('Y-m-d\TH:i:s',$start);
  136. $event->end = date('Y-m-d\TH:i:s',$end);
  137. $response['event'] = $event;
  138. });
  139. break;
  140. case 'caldav_delete_event':
  141. Action::write(function($_,&$response){
  142. global $myUser;
  143. require_once('CalDavClient.class.php');
  144. $client = new CaldavClient();
  145. $client->host = YANA_URL.'/plugins/caldav/calendars.php/calendars';
  146. $client->login = $myUser->getLogin();
  147. $client->password = $myUser->getToken();
  148. $client->user = $myUser->getLogin();
  149. $client->calendar = $_['calendar'];
  150. foreach($_['events'] as $ics){
  151. $ics = $client->delete_event($ics);
  152. }
  153. });
  154. break;
  155. case 'caldav_add_calendar':
  156. global $myUser;
  157. $dbmanager = new Configuration();
  158. $slug = Functions::slugify($_['label']);
  159. $principalQuery = $dbmanager->customQuery("SELECT * FROM ".ENTITY_PREFIX."plugin_caldav_principals WHERE uri='principalsr/".$myUser->getLogin()."' LIMIT 1");
  160. $principal = $principalQuery->fetchArray();
  161. if(!$principal){
  162. $query = "
  163. INSERT INTO ".ENTITY_PREFIX."plugin_caldav_principals (uri,email,displayname) VALUES ('principals/".$myUser->getLogin()."','". $myUser->getMail()."','".$myUser->getLogin()."');
  164. INSERT INTO ".ENTITY_PREFIX."plugin_caldav_principals (uri,email,displayname) VALUES ('principals/".$myUser->getLogin()."/calendar-proxy-read', null, null);
  165. INSERT INTO ".ENTITY_PREFIX."plugin_caldav_principals (uri,email,displayname) VALUES ('principals/".$myUser->getLogin()."/calendar-proxy-write', null, null);";
  166. $dbmanager->customExecute($query);
  167. }
  168. $query = "INSERT INTO ".ENTITY_PREFIX."plugin_caldav_calendars (principaluri, displayname, uri, synctoken, description, calendarorder, calendarcolor, timezone, components, transparent) VALUES ('principals/".$myUser->getLogin()."','". $_['label']."','". $slug."', 1, NULL, NULL, NULL, NULL, 'VEVENT,VTODO', '0');";
  169. $dbmanager->customExecute($query);
  170. header('location: setting.php?section=caldav');
  171. break;
  172. }
  173. }
  174. function caldav_setting_menu(){
  175. global $_;
  176. echo '<li '.(isset($_['section']) && $_['section']=='propise'?'class="active"':'').'><a href="setting.php?section=caldav"><i class="fa fa-angle-right"></i> Calendriers</a></li>';
  177. }
  178. function caldav_setting_page(){
  179. global $_,$myUser,$conf;
  180. if(isset($_['section']) && $_['section']=='caldav' ){
  181. if(!$myUser) throw new Exception('Vous devez être connecté pour effectuer cette action');
  182. $dbmanager = new Configuration();
  183. $calendars = $dbmanager->customQuery("SELECT * FROM ".ENTITY_PREFIX."plugin_caldav_calendars WHERE principaluri ='principals/".$myUser->getLogin()."'");
  184. ?>
  185. <div class="span9">
  186. <h1>Calendriers</h1>
  187. <p>Gestion des calendriers</p>
  188. <form action="action.php" method="POST">
  189. <input type="hidden" value="caldav_add_calendar" name="action">
  190. <h4>Créer un nouveau calendrier</h4>
  191. <label>Nom</label>
  192. <input type="text" name="label" id="label">
  193. <input type="submit" class="btn" value="Créer">
  194. </form>
  195. <hr/>
  196. <h4>Calendrier créés</h4>
  197. <table class="table">
  198. <thead>
  199. <tr>
  200. <th>Nom</th>
  201. <th>Adresse du synchronisation</th>
  202. </tr>
  203. </thead>
  204. <tbody>
  205. <?php while($calendar = $calendars->fetchArray()):
  206. $url = YANA_URL.'/plugins/caldav/calendars.php/calendars/'.str_replace('principals/','',$calendar['principaluri']) .'/'.$calendar['uri'];
  207. ?>
  208. <tr>
  209. <td>
  210. <?php echo $calendar['displayname']; ?>
  211. </td>
  212. <td>
  213. <a href="<?php echo $url;?>"><?php echo $url ?></a>
  214. </td>
  215. </tr>
  216. <?php endwhile; ?>
  217. </tbody>
  218. </table>
  219. </div>
  220. <?php
  221. }
  222. }
  223. Plugin::addJs('/js/moment.min.js');
  224. Plugin::addJs('/js/fullcalendar.min.js');
  225. Plugin::addJs('/js/locale-all.js');
  226. Plugin::addJs('/js/main.js');
  227. Plugin::addCss('/css/main.css',true);
  228. Plugin::addCss('/css/fullcalendar.min.css');
  229. Plugin::addHook("menubar_pre_home", "caldav_menu");
  230. Plugin::addHook("home", "caldav_home");
  231. Plugin::addHook("action_post_case", "caldav_action");
  232. Plugin::addHook("setting_menu", "caldav_setting_menu");
  233. Plugin::addHook("setting_bloc", "caldav_setting_page");
  234. ?>