action.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. /** PLANNING **/
  5. //Récuperation d'une liste de planning
  6. case 'planning_search':
  7. Action::write(function(&$response){
  8. global $myUser,$_,$myFirm,$conf;
  9. User::check_access('planning','read');
  10. require_once(__DIR__.SLASH.'Planning.class.php');
  11. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  12. $rankIds = array();
  13. foreach ($myUser->ranks[$myFirm->id] as $rank) {
  14. if($rank->id==0) continue;
  15. $rankIds[] = $rank->id;
  16. }
  17. $sql = 'SELECT * FROM {{table}} p WHERE p.owner = ? OR p.id IN(SELECT s.planning FROM '.PlanningShare::tableName().' s WHERE (recipient = ? AND recipientEntity="user") ';
  18. if(count($rankIds)>0) $sql .= ' OR (recipient IN('.implode(',',$rankIds).') AND recipientEntity="rank" )';
  19. $sql .= ')';
  20. $plannings = Planning::staticQuery($sql,array($myUser->login,$myUser->login),true);
  21. $selectedPlannings = json_decode($myUser->preference('planning_selected_calendars'),true);
  22. $selectedPlannings = !is_array($selectedPlannings) ? array() : $selectedPlannings;
  23. if(count($plannings)==0){
  24. $item = Planning::provide();
  25. $item->label = 'Général';
  26. $item->color = '#17a2b8';
  27. $item->owner = $myUser->login;
  28. $item->type = 'local';
  29. $item->default = true;
  30. $item->slug = slugify($item->label);
  31. $item->save();
  32. $plannings[] = $item;
  33. }
  34. Plugin::callHook('planning_planning_search',array(&$plannings,&$selectedPlannings));
  35. usort($plannings, function($a,$b){
  36. global $myUser;
  37. if($a->owner == $myUser->login) return -1;
  38. return 1;
  39. });
  40. foreach($plannings as $planning){
  41. $row = $planning->toArray();
  42. if(in_array($planning->id, $selectedPlannings))
  43. $row['selected'] = true;
  44. if($conf->get('planning_show_default')!==true && $planning->default) continue;
  45. $row['editable'] = $planning->owner != $myUser->login ? false : true;
  46. $row['shared'] = $row['owner'] != $myUser->login;
  47. $row['ownerName'] = User::byLogin($row['owner'])->fullName();
  48. $response['rows'][] = $row;
  49. }
  50. });
  51. break;
  52. case 'planning_edit':
  53. global $myUser,$_;
  54. User::check_access('planning','edit');
  55. require_once(__DIR__.SLASH.'planning.modal.php');
  56. break;
  57. //Ajout ou modification d'élément planning
  58. case 'planning_save':
  59. Action::write(function(&$response){
  60. global $myUser,$_;
  61. User::check_access('planning','edit');
  62. require_once(__DIR__.SLASH.'Planning.class.php');
  63. $item = Planning::provide();
  64. if($item->id!=0 && $item->owner != $myUser->login && !$myUser->superadmin) throw new Exception("Seul le propriétaire de ce planning peut le modifier");
  65. $item->label = $_['planning-label'];
  66. $item->color = $_['planning-color'];
  67. $item->owner = $myUser->login;
  68. $item->type = 'local';
  69. $item->save();
  70. });
  71. break;
  72. //Suppression d'élement planning
  73. case 'planning_delete':
  74. Action::write(function(&$response){
  75. global $myUser,$_;
  76. User::check_access('planning','delete');
  77. require_once(__DIR__.SLASH.'Planning.class.php');
  78. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  79. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  80. $planning = Planning::provide();
  81. if(!$planning) throw new Exception("Planning inexistant");
  82. if($planning->default) throw new Exception("Vous ne pouvez pas supprimer le planning par défaut");
  83. if($planning->owner != $myUser->login && !$myUser->superadmin) throw new Exception("Vous ne pouvez pas supprimer un planning dont vous n'êtes pas le propriétaire");
  84. Planning::deleteById($planning->id);
  85. PlanningEvent::delete(array('planning'=>$planning->id));
  86. PlanningShare::delete(array('planning'=>$planning->id));
  87. });
  88. break;
  89. case 'planning_widget_load':
  90. global $myUser;
  91. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  92. $widget = DashboardWidget::current();
  93. $widget->title = 'Mes 10 prochains rendez vous';
  94. ob_start();
  95. require_once(__DIR__.SLASH.'widget.php');
  96. $widget->content = ob_get_clean();
  97. echo json_encode($widget);
  98. break;
  99. /** PLANNINGEVENT **/
  100. //Récuperation d'une liste de planningevent
  101. case 'planning_event_search':
  102. Action::write(function(&$response){
  103. global $myUser,$_,$myFirm,$conf;
  104. User::check_access('planning','read');
  105. require_once(__DIR__.SLASH.'Planning.class.php');
  106. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  107. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  108. if(!isset($_['plannings'])) return;
  109. $plannings = $_['plannings'];
  110. $start = strtotime($_['start']);
  111. $end = strtotime($_['end']);
  112. $events = array();
  113. foreach(array_merge(get_not_workable($start),get_not_workable($end)) as $time){
  114. if($time < $start || $time > $end) continue;
  115. $events[] = array(
  116. 'start' => date('Y-m-d\TH:i:s',$time),
  117. 'end' => date('Y-m-d\T23:59:59',$time),
  118. 'backgroundColor' => '#cecece',
  119. 'rendering' => 'background'
  120. );
  121. $events[] = array(
  122. 'start' => date('Y-m-d',$time),
  123. 'end' => date('Y-m-d',$time),
  124. 'backgroundColor' => '#cecece',
  125. 'allDay' => true,
  126. 'rendering' => 'background'
  127. );
  128. }
  129. if(isset($_['plannings']) && count($_['plannings'])!=0){
  130. $myUser->preference('planning_selected_calendars',json_encode($_['plannings']));
  131. $myUser->loadPreferences();
  132. $_SESSION['currentUser'] = serialize($myUser);
  133. foreach(PlanningEvent::getAll($myUser->login,$myUser->ranks[$myFirm->id],$plannings,$start,$end) as $event){
  134. if($event->startDate > $event->endDate) continue;
  135. $textColor = get_light($event->type->color) < 0.6 ? '#fefefefe': '#333333' ;
  136. $isAllDay = false;
  137. /*if($conf->get('planning_day_start')!='' && $conf->get('planning_day_end')!=''){
  138. if(date('H:i:s',$event->startDate) == $conf->get('planning_day_start').':00' && date('H:i:s',$event->endDate) == $conf->get('planning_day_end').':00') $isAllDay = true;
  139. }*/
  140. $events[] = array(
  141. 'id' => $event->id,
  142. 'title' => html_entity_decode($event->label,ENT_QUOTES,'UTF-8'),
  143. 'type' => $event->type->id,
  144. 'planning' => $event->planning->id,
  145. 'allDay' => $isAllDay,
  146. 'street' => $event->street,
  147. 'city' => $event->city,
  148. 'zip' => $event->zip,
  149. 'notificationNumber' => $event->notificationNumber,
  150. 'notificationUnity' => $event->notificationUnity,
  151. 'start' => date('Y-m-d\TH:i:s',$event->startDate),
  152. 'end' => date('Y-m-d\TH:i:s',$event->endDate),
  153. 'backgroundColor' => $event->type->color,
  154. 'underlineColor' => $event->planning->color,
  155. 'planningLabel' => $event->planning->label,
  156. 'borderColor' => 'transparent',
  157. 'editable' => $conf->get('planning_allow_event_edit') == true,
  158. 'icon' => $event->type->icon,
  159. 'textColor' => $textColor,
  160. 'description' => html_entity_decode(str_replace('\n',"\n",$event->description),ENT_QUOTES,'UTF-8'),
  161. 'location' => $event->street.' '.$event->zip.' '.$event->city,
  162. 'repeatOccurence' => $event->repeatOccurence,
  163. 'repeatUntil' => $event->repeatUntil,
  164. 'repeatYearlyMonth' => $event->repeatYearlyMonth,
  165. 'repeatYearlyNumber' => $event->repeatYearlyNumber,
  166. 'repeatMonthlyDay' => $event->repeatMonthlyDay,
  167. 'repeatMonthlyNumber'=> $event->repeatMonthlyNumber,
  168. 'repeatWeeklyDay' => explode(',',$event->repeatWeeklyDay),
  169. 'repeatDailyNumber' => $event->repeatDailyNumber,
  170. 'repeatType' => $event->repeatType
  171. );
  172. }
  173. }
  174. $response = $events;
  175. });
  176. break;
  177. //Ajout ou modification d'élément planningevent
  178. case 'planning_event_save':
  179. Action::write(function(&$response){
  180. global $myUser,$_;
  181. User::check_access('planning','edit');
  182. require_once(__DIR__.SLASH.'Planning.class.php');
  183. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  184. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  185. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  186. $item = PlanningEvent::provide('id',1);
  187. if(!$item) throw new Exception("Cet événement n'existe plus, merci de rafraîchir votre planning.");
  188. $planning = $item->id==0 ? Planning::getById($_['planning']) : $item->join('planning');
  189. if($planning->owner != $myUser->login){
  190. if(PlanningShare::rowCount(array('planning'=>$planning->id,'recipient'=>$myUser->login,'edit'=>1))==0)
  191. throw new Exception("Vous n'avez pas la permission d'éditer cet évenement", 403);
  192. }
  193. $start = explode('/',$_['startDate']);
  194. $end = explode('/',$_['endDate']);
  195. $startDate = mktime($_['startHour'],$_['startMinut'],0,$start[1],$start[0],$start[2]);
  196. $endDate = mktime($_['endHour'],$_['endMinut'],0,$end[1],$end[0],$end[2]);
  197. if($startDate > $endDate) throw new Exception("Dates incohérentes");
  198. $item->label = $_['label'];
  199. if(isset($_['planning']) && !empty($_['planning']) && $_['planning']!=0) $item->planning = $_['planning'];
  200. if(isset($_['type'])) $item->type = $_['type'];
  201. $item->startDate = $startDate;
  202. $item->endDate = $endDate;
  203. $item->description = $_['description'];
  204. $item->street = $_['street'];
  205. $item->city = $_['city'];
  206. $item->zip = $_['zip'];
  207. if(isset($_['event-repeat-type'])){
  208. $item->repeatType = $_['event-repeat-type'];
  209. if($_['repeatEndType'] == 'occurence')
  210. $item->repeatOccurence = $_['event-repeat-occurences'];
  211. if($_['repeatEndType'] == 'until')
  212. $item->repeatUntil = $_['event-repeat-until'];
  213. $item->repeatYearlyMonth = $_['event-repeat-yearly-month'];
  214. $item->repeatYearlyNumber = $_['event-repeat-yearly-number'];
  215. $item->repeatMonthlyDay = $_['event-repeat-monthly-day'];
  216. $item->repeatMonthlyNumber = $_['event-repeat-monthly-number'];
  217. if(isset($_['weeklyDay']) && is_array($_['weeklyDay'])) $item->repeatWeeklyDay = implode(',',$_['weeklyDay']) ;
  218. $item->repeatDailyNumber = $_['event-repeat-daily-number'];
  219. }
  220. if(isset($_['notificationNumber']) && !empty($_['notificationNumber'])){
  221. //Si les parametres de notifications ont changé on remet a zero l'etat de notification
  222. if($_['notificationNumber'] != $item->notificationNumber) $item->notificationState = '';
  223. $item->notificationNumber = $_['notificationNumber'];
  224. }
  225. if(isset($_['notificationUnity'])) $item->notificationUnity = $_['notificationUnity'];
  226. $item->save();
  227. });
  228. break;
  229. //Suppression d'élement planningevent
  230. case 'planning_event_delete':
  231. Action::write(function(&$response){
  232. global $myUser,$_;
  233. User::check_access('planning','delete');
  234. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  235. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  236. $events = PlanningEvent::removeAll($myUser->login,$_['events']);
  237. });
  238. break;
  239. /** EVENT TYPE **/
  240. //Récuperation d'une liste de planningeventtype
  241. case 'planning_event_type_search':
  242. Action::write(function(&$response){
  243. global $myUser,$_;
  244. User::check_access('planning','read');
  245. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  246. foreach(PlanningEventType::loadAll(array('state'=>PlanningEventType::ACTIVE),array('sort')) as $eventType){
  247. $row = $eventType->toArray();
  248. $row['textcolor'] = get_light($row['color'])> 0.5 ? '#333333':'#ffffff';
  249. $row['editable'] = !empty($row['editable']);
  250. $row['superadmin'] = $myUser->superadmin || $row['editable'];
  251. $response['rows'][] = $row;
  252. }
  253. });
  254. break;
  255. //Ajout ou modification d'élément planningeventtype
  256. case 'planning_event_type_save':
  257. Action::write(function(&$response){
  258. global $myUser,$_;
  259. User::check_access('planning','configure');
  260. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  261. $item = PlanningEventType::provide();
  262. if(!$item->editable && !$myUser->superadmin) throw new Exception("Item non éditable");
  263. $item->label = $_['label'];
  264. $item->icon = $_['icon'];
  265. $item->editable = $_['editable'];
  266. if(isset($_['parent'])) $item->parent = $_['parent'];
  267. $item->color = $_['color'];
  268. $item->slug = slugify($_['label']);
  269. $item->save();
  270. });
  271. break;
  272. //Récuperation ou edition d'élément planningeventtype
  273. case 'planning_event_type_edit':
  274. Action::write(function(&$response){
  275. global $myUser,$_;
  276. User::check_access('planning','configure');
  277. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  278. $response = PlanningEventType::getById($_['id']);
  279. });
  280. break;
  281. case 'planning_event_type_sort':
  282. Action::write(function(&$response){
  283. global $myUser,$_;
  284. User::check_access('planning','configure');
  285. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  286. foreach ($_['sort'] as $i=>$id) {
  287. PlanningEventType::change(array('sort'=>$i),array('id'=>$id));
  288. }
  289. });
  290. break;
  291. //Suppression d'élement planningeventtype
  292. case 'planning_event_type_delete':
  293. Action::write(function(&$response){
  294. global $myUser,$_;
  295. User::check_access('planning','configure');
  296. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  297. $item = PlanningEventType::getById($_['id']);
  298. $item->state = PlanningEventType::INACTIVE;
  299. $item->save();
  300. });
  301. break;
  302. //Sauvegarde des configurations de planning
  303. case 'planning_setting_save':
  304. Action::write(function(&$response){
  305. global $myUser,$_,$conf;
  306. User::check_access('planning','configure');
  307. foreach(Configuration::setting('planning') as $key=>$value){
  308. if(!is_array($value)) continue;
  309. $allowed[] = $key;
  310. }
  311. foreach ($_['fields'] as $key => $value) {
  312. if(in_array($key, $allowed))
  313. $conf->put($key,$value);
  314. }
  315. });
  316. break;
  317. /** PLANNINGSHARE **/
  318. //Récuperation d'une liste de planningshare
  319. case 'planning_share_search':
  320. Action::write(function(&$response){
  321. global $myUser,$_;
  322. User::check_access('planning','read');
  323. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  324. $shares = PlanningShare::loadAll(array('planning'=>$_['planning']));
  325. foreach($shares as $planningshare){
  326. $row = $planningshare->toArray();
  327. $row['edit'] = $row['edit']==0?null:$row['edit'];
  328. if($planningshare->recipientEntity=='rank'){
  329. $rank = Rank::getById($planningshare->recipient);
  330. $row['recipient'] = $rank->label;
  331. $row['recipientType'] = "Rang";
  332. }else{
  333. $row['recipient'] = User::byLogin($planningshare->recipient)->fullName();
  334. $row['recipientType'] = "Utilisateur";
  335. }
  336. $response['rows'][] = $row;
  337. }
  338. });
  339. break;
  340. //Ajout ou modification d'élément planningshare
  341. case 'planning_share_save':
  342. Action::write(function(&$response){
  343. global $myUser,$_;
  344. User::check_access('planning','edit');
  345. require_once(__DIR__.SLASH.'Planning.class.php');
  346. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  347. if(empty($_['recipient'])) throw new Exception("Vous devez préciser le destinataire du partage");
  348. //vérifie que ce destinataire n'a pas déja un partage sur ce planning
  349. if(PlanningShare::rowCount(array('planning'=>$_['planning'],'recipient'=>$_['recipient'])) >0) throw new Exception("Ce destinataire a déja un partage sur ce planning, veuillez supprimer l'ancien partage avant d'en créer un nouveau");
  350. $planning = Planning::getById($_['planning']);
  351. if(!$planning) throw new Exception("Planning inexistant");
  352. if($planning->owner != $myUser->login) throw new Exception("Vous n'êtes pas propriétaire du planning");
  353. if(!$_['read'] && !$_['edit']) throw new Exception("Vous devez cocher au moins un droit de planning");
  354. $item = PlanningShare::provide();
  355. $item->planning = $planning->id;
  356. $item->recipient = stripslashes($_['recipient']);
  357. $item->recipientEntity = $_['recipientEntity'];
  358. $item->read = $_['read'];
  359. $item->edit = $_['edit'];
  360. $item->save();
  361. });
  362. break;
  363. //Suppression d'élement planningshare
  364. case 'planning_share_delete':
  365. Action::write(function(&$response){
  366. global $myUser,$_;
  367. User::check_access('planning','delete');
  368. require_once(__DIR__.SLASH.'Planning.class.php');
  369. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  370. $item = PlanningShare::provide();
  371. $planning = Planning::getById($item->planning);
  372. if($planning->owner != $myUser->login) throw new Exception("Vous n'êtes pas propriétaire du planning");
  373. PlanningShare::deleteById($_['id']);
  374. });
  375. break;
  376. }
  377. ?>