planning.plugin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /*
  3. @name Planning
  4. @author Julien NARBONI <julien.narboni@sys1.fr>
  5. @link http://www.sys1.fr
  6. @licence Copyright Sys1
  7. @version 1.0.0
  8. @description Gestion du planning
  9. */
  10. //Déclaration d'un item de menu dans le menu principal
  11. function planning_menu(&$menuItems){
  12. global $_,$myUser;
  13. if(!$myUser->can('planning','read')) return;
  14. $menuItems[] = array(
  15. 'sort'=>5,
  16. 'url'=>'index.php?module=planning',
  17. 'label'=>'Planning',
  18. 'icon'=> 'fas fa-calendar-alt',
  19. 'color'=> '#9b59b6'
  20. );
  21. }
  22. //Cette fonction va generer une page quand on clique sur planning dans menu
  23. function planning_page(){
  24. global $_,$myUser;
  25. if(!isset($_['module']) || $_['module'] !='planning') return;
  26. User::check_access('planning','read');
  27. $page = !isset($_['page']) ? 'list' : $_['page'];
  28. $file = __DIR__.SLASH.'page.'.$page.'.php';
  29. if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
  30. require_once($file);
  31. }
  32. //Fonction executée lors de l'activation du plugin
  33. function planning_install($id){
  34. if($id != 'fr.sys1.planning') return;
  35. Entity::install(__DIR__);
  36. $type = new PlanningEventType();
  37. $type->editable = 0;
  38. $type->label = 'Rendez vous';
  39. $type->slug = 'rendez-vous';
  40. $type->color = '#8bc34a';
  41. $type->icon = 'far fa-calendar-check';
  42. $type->save();
  43. global $conf;
  44. $conf->put('planning_day_start','09:00');
  45. $conf->put('planning_day_end','17:00');
  46. $conf->put('planning_show_default',true);
  47. $conf->put('planning_allow_event_edit',true);
  48. $conf->put('planning_allow_share',true);
  49. }
  50. //Fonction executée lors de la désactivation du plugin
  51. function planning_uninstall($id){
  52. if($id != 'fr.sys1.planning') return;
  53. Entity::uninstall(__DIR__);
  54. }
  55. //Déclaration des sections de droits du plugin
  56. function planning_section(&$sections){
  57. $sections['planning'] = "Gestion des droits sur le plugin planning";
  58. }
  59. //cette fonction comprends toutes les actions du plugin qui ne nécessitent pas de vue html
  60. function planning_action(){
  61. require_once(__DIR__.SLASH.'action.php');
  62. }
  63. //Déclaration du menu de réglages
  64. function planning_menu_setting(&$settingMenu){
  65. global $_, $myUser;
  66. if(!$myUser->can('planning','configure')) return;
  67. $settingMenu[]= array(
  68. 'sort' =>1,
  69. 'url' => 'setting.php?section=planning',
  70. 'icon' => 'fas fa-angle-right',
  71. 'label' => 'Planning'
  72. );
  73. }
  74. //Déclaration des pages de réglages
  75. function planning_content_setting(){
  76. global $_;
  77. if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php'))
  78. require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php');
  79. }
  80. function planning_save_event(&$data){
  81. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  82. $event = is_null($data['id']) ? new PlanningEvent() : PlanningEvent::getById($data['id']);
  83. foreach($data as $key=>$value)
  84. $event->$key = $value;
  85. $event->save();
  86. $data['id'] = $event->id;
  87. }
  88. function planning_delete_event($id){
  89. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  90. PlanningEvent::deleteById($id);
  91. }
  92. function planning_dav($command){
  93. if(substr($command, 0,12)!='dav/planning') return;
  94. require_once('common.php');
  95. global $conf,$myUser,$_;
  96. if(!$myUser || $myUser->login==''){
  97. foreach(explode('&',parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY)) as $parameter){
  98. $infos = explode('=',$parameter);
  99. $_[$infos[0]] = $infos[1];
  100. }
  101. if(!isset($_['u']) ){
  102. header('HTTP/1.0 401 Unauthorized');
  103. exit;
  104. }
  105. $myUser = User::check($_['u'],$_['p']);
  106. if(!$myUser->connected()){
  107. header('HTTP/1.0 403 Unauthorized');
  108. echo 'Bad login';
  109. exit;
  110. }
  111. global $myFirm;
  112. if($myUser->superadmin == 1){
  113. foreach(Firm::loadAll() as $firm)
  114. $firms[$firm->id] = $firm;
  115. $myUser->setFirms($firms);
  116. }
  117. if(is_numeric($myUser->preference('default_firm')) && $myUser->haveFirm($myUser->preference('default_firm'))){
  118. $_SESSION['firm'] = serialize(Firm::getById($myUser->preference('default_firm')));
  119. } else if(count($myUser->firms)!=0){
  120. $_SESSION['firm'] = serialize(reset($myUser->firms));
  121. } else {
  122. throw new Exception('Ce compte n\'est actif sur aucun établissement, veuillez contacter l\'administrateur');
  123. }
  124. $myFirm = isset($_SESSION['firm']) ? unserialize($_SESSION['firm']) : new Firm();
  125. $_SESSION['currentUser'] = serialize($myUser);
  126. }
  127. require_once(__DIR__.SLASH.'CalDavServer.class.php');
  128. $projectPath = preg_replace('|https?\:\/\/'.$_SERVER['REMOTE_ADDR'].'|i', '', ROOT_URL);
  129. $server = new CalDavServer();
  130. $server->root = $projectPath.'/dav/planning/';
  131. $server->calendarLastUpdate = function($user,$calendar){
  132. global $myUser,$myFirm;
  133. require_once(__DIR__.SLASH.'Planning.class.php');
  134. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  135. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  136. $planning = Planning::load(array('slug'=>$calendar,'owner'=>$user));
  137. $lastUpdated = PlanningEvent::load(array(),array('updated DESC'));
  138. return !$lastUpdated ? 0 : $lastUpdated->updated;
  139. };
  140. $server->searchEvents = function($user,$calendar){
  141. global $myUser,$myFirm;
  142. require_once(__DIR__.SLASH.'Planning.class.php');
  143. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  144. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  145. $events = array();
  146. $planning = Planning::load(array('slug'=>$calendar,'owner'=>$user));
  147. if(!$planning) return $events;
  148. $userObject = User::byLogin($user);
  149. foreach(PlanningEvent::getAll($user,$userObject->ranks[$myFirm->id],array($planning->id),null,null) as $event){
  150. $row = $event->toArray();
  151. $row['type'] = $row['type']->label;
  152. $row['label'] = htmlspecialchars(html_entity_decode($row['label'],ENT_QUOTES,'UTF-8'));
  153. $row['description'] =htmlspecialchars(html_entity_decode($row['description'],ENT_QUOTES,'UTF-8'));
  154. $row['type'] =htmlspecialchars(html_entity_decode($row['type'],ENT_QUOTES,'UTF-8'));
  155. $row['street'] =htmlspecialchars(html_entity_decode($row['street'],ENT_QUOTES,'UTF-8'));
  156. $row['city'] =htmlspecialchars(html_entity_decode($row['city'],ENT_QUOTES,'UTF-8'));
  157. //$row['recurrence'] = 'FREQ=DAILY;INTERVAL=3;COUNT=10';
  158. $events[] = $row;
  159. }
  160. return $events;
  161. };
  162. $server->deleteEvent = function($user,$calendar,$event = null){
  163. global $myUser;
  164. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  165. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  166. $errors = PlanningEvent::removeAll($myUser->login,array($event));
  167. if(count($errors)>0) throw new Exception("Error Processing Request");
  168. };
  169. $server->saveEvent = function($user,$calendar,$event = null,$infos){
  170. global $myUser;
  171. require_once(__DIR__.SLASH.'Planning.class.php');
  172. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  173. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  174. $event = new PlanningEvent();
  175. if(strpos($infos->uid, '-')===false){
  176. $event = PlanningEvent::getById($infos->uid);
  177. $event = !$event ? new PlanningEvent():$event;
  178. }
  179. $planning = Planning::load(array('slug'=>$calendar,'owner'=>$user));
  180. if($planning->owner != $myUser->login){
  181. if(PlanningShare::rowCount(array('planning'=>$planning->id,'recipient'=>$myUser->login,'edit'=>1))==0)
  182. throw new Exception("Vous n'avez pas la permission d'éditer cet évenement");
  183. }
  184. $event->label = $infos->title;
  185. $event->planning = $planning->id;
  186. $event->startDate = $infos->start;
  187. $event->endDate = $infos->end;
  188. $event->street = $infos->location;
  189. $event->description = nl2br($infos->description);
  190. $event->save();
  191. };
  192. if($conf->get('planning_dav_log')=='1'){
  193. $server->log = function($type,$message,$isVerbose = false){
  194. file_put_contents(__DIR__.SLASH.'logs.txt','['.$type.'] : '.$message.PHP_EOL,FILE_APPEND);
  195. if(!$isVerbose) Log::put($message,'[CALDAV]['.$type.']');
  196. };
  197. }
  198. $server->start();
  199. }
  200. function planning_cron(){
  201. require_once(__DIR__.SLASH.'Planning.class.php');
  202. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  203. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  204. $query = 'SELECT e.*,t.label AS '.PlanningEventType::tableName().'_join_label FROM {{table}} e LEFT JOIN '.PlanningEventType::tableName().' t ON e.type=t.id WHERE notificationNumber!=? AND notificationState!=?';
  205. $data = array(0,PlanningEvent::NOTIFIED);
  206. $now = new DateTime();
  207. foreach(PlanningEvent::staticQuery($query,$data,true,1) as $event){
  208. $unity = 60 ;
  209. if($event->notificationUnity=='hour') $unity = 3600;
  210. if($event->notificationUnity=='day') $unity = 86400;
  211. $notificationDelay = $event->notificationNumber * $unity;
  212. $notificationDate = new DateTime();
  213. $notificationDate->setTimestamp($event->startDate - $notificationDelay);
  214. if($notificationDate > $now) continue;
  215. $type = $event->join('type');
  216. $html= 'Du <strong>'.date('d/m/Y H:i',$event->startDate).'</strong> au <strong>'.date('d/m/Y H:i',$event->endDate).'</strong>';
  217. if($event->street!='') $html.= ' au '.$event->street;
  218. $html.= '<br/><a class="btn btn-primary" href="index.php?module=planning&start='.date('Ymd',$event->startDate).'&event='.$event->id.'">Voir le rendez vous</a>';
  219. $recipients = array($event->creator);
  220. if($event->creator!=$event->updater) $recipients[] = $event->updater;
  221. // GESTION ENVOI NOTIFICATION
  222. Plugin::callHook('emit_notification',array(array(
  223. 'label' => $type->label.' : '.$event->label.' '.strtolower(relative_time($event->startDate)),
  224. 'html' => $html,
  225. 'type' => 'notice',
  226. 'meta' => array('link' => ROOT_URL.'/index.php?module=planning&start='.date('Ymd',$event->startDate).'&event='.$event->id),
  227. 'end' => strtotime('+2day'),
  228. 'recipients' => array_unique($recipients) // recipients contient login
  229. )
  230. ));
  231. $event->notificationState = PlanningEvent::NOTIFIED;
  232. $event->save();
  233. }
  234. }
  235. function planning_widget(&$widgets){
  236. global $myUser;
  237. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  238. $modelWidget = new DashboardWidget();
  239. $modelWidget->model = 'planning';
  240. $modelWidget->title = 'Planning';
  241. $modelWidget->icon = 'far fa-calendar-alt';
  242. $modelWidget->background = '#ff7979';
  243. $modelWidget->load = 'action.php?action=planning_widget_load';
  244. //$modelWidget->js = [Plugin::url().'/js/widget.js?v=1'];
  245. $modelWidget->css = [Plugin::url().'/css/widget.css?v=2'];
  246. $modelWidget->description = "Affiche vos 10 prochains rendez vous";
  247. $widgets[] = $modelWidget;
  248. }
  249. //Déclaration des settings de base
  250. Configuration::setting('planning',array(
  251. "Configuration générale",
  252. 'planning_day_start' => array("label"=>"Heure de début de journée","legend"=>"Heure ouvrée pour votre établissement au format HH:mm","type"=>"hour", "placeholder"=>"08:00"),
  253. 'planning_day_end' => array("label"=>"Heure de fin de journée","legend"=>"Heure ouvrée pour votre établissement au format HH:mm","type"=>"hour", "placeholder"=>"17:00"),
  254. 'planning_allow_event_edit' => array("label"=>"Autoriser l'édition d'évenement","type"=>"checkbox"),
  255. 'planning_show_default' => array("label"=>"Afficher le planning par défaut (général)","type"=>"checkbox"),
  256. 'planning_allow_share' => array("label"=>"Autoriser le partage de calendrier","type"=>"checkbox"),
  257. "CalDAV",
  258. 'planning_dav_log' => array("label"=>"Activer les logs caldav","legend"=>"Les logs caldav peuvent consommer de la performance","type"=>"checkbox")
  259. ));
  260. //Déclation des assets
  261. Plugin::addCss("/css/fullcalendar.min.css?v=1");
  262. //Plugin::addCss("/css/fullcalendar.print.min.css?v=1");
  263. Plugin::addJs("/js/moment.min.js?v=1");
  264. Plugin::addJs("/js/rrule.min.js?v=1");
  265. Plugin::addJs("/js/fullcalendar.min.js?v=1");
  266. Plugin::addJs("/js/locale-all.js?v=1");
  267. Plugin::addCss("/css/main.css?v=2");
  268. Plugin::addJs("/js/main.js?v=2");
  269. //Mapping hook / fonctions
  270. Plugin::addHook("install", "planning_install");
  271. Plugin::addHook("uninstall", "planning_uninstall");
  272. Plugin::addHook("section", "planning_section");
  273. Plugin::addHook("menu_main", "planning_menu");
  274. Plugin::addHook("page", "planning_page");
  275. Plugin::addHook("action", "planning_action");
  276. Plugin::addHook("menu_setting", "planning_menu_setting");
  277. Plugin::addHook("content_setting", "planning_content_setting");
  278. Plugin::addHook("save_planning_event", "planning_save_event");
  279. Plugin::addHook("delete_planning_event", "planning_delete_event");
  280. Plugin::addHook("rewrite", "planning_dav");
  281. Plugin::addHook("cron", "planning_cron");
  282. Plugin::addHook("widget", "planning_widget");
  283. ?>