action.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. <?php
  2. /** PLANNING **/
  3. //Récuperation d'une liste de planning
  4. Action::register('planning_search',function(&$response){
  5. global $myUser,$_,$myFirm,$conf;
  6. User::check_access('planning','read');
  7. require_once(__DIR__.SLASH.'Planning.class.php');
  8. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  9. $rankIds = array();
  10. foreach ($myUser->ranks[$myFirm->id] as $rank) {
  11. if($rank->id==0) continue;
  12. $rankIds[] = $rank->id;
  13. }
  14. $sql = 'SELECT * FROM {{table}} p WHERE p.owner = ? OR p.id IN(SELECT s.planning FROM '.PlanningShare::tableName().' s WHERE (recipient = ? AND recipientEntity="user") ';
  15. if(count($rankIds)>0) $sql .= ' OR (recipient IN('.implode(',',$rankIds).') AND recipientEntity="rank" )';
  16. $sql .= ')';
  17. $plannings = Planning::staticQuery($sql,array($myUser->login,$myUser->login),true);
  18. $selectedPlannings = json_decode($myUser->preference('planning_selected_calendars'),true);
  19. $selectedPlannings = !is_array($selectedPlannings) ? array() : $selectedPlannings;
  20. $selectedPlannings = array_filter($selectedPlannings);
  21. if(count($plannings)==0){
  22. $item = Planning::provide();
  23. $item->label = 'Général';
  24. $item->color = '#17a2b8';
  25. $item->owner = $myUser->login;
  26. $item->type = 'local';
  27. $item->default = true;
  28. $item->slug = slugify($item->label);
  29. $item->save();
  30. $plannings[] = $item;
  31. }
  32. Plugin::callHook('planning_planning_search',array(&$plannings,&$selectedPlannings));
  33. usort($plannings, function($a,$b){
  34. global $myUser;
  35. if($a->owner == $myUser->login) return -1;
  36. return 1;
  37. });
  38. foreach($plannings as $planning){
  39. $row = $planning->toArray();
  40. if(in_array($planning->id, $selectedPlannings) || ($planning->default && count($selectedPlannings) ==0 ) )
  41. $row['selected'] = true;
  42. if((int)$conf->get('planning_show_default')!==1 && $planning->default) continue;
  43. $row['editable'] = $planning->owner != $myUser->login ? false : true;
  44. $row['shared'] = $row['owner'] != $myUser->login;
  45. $row['ownerName'] = User::byLogin($row['owner'])->fullName();
  46. $response['rows'][] = $row;
  47. }
  48. });
  49. Action::register('planning_edit',function(&$response){
  50. global $myUser,$_;
  51. User::check_access('planning','edit');
  52. require_once(__DIR__.SLASH.'planning.modal.php');
  53. exit();
  54. });
  55. //Ajout ou modification d'élément planning
  56. Action::register('planning_save',function(&$response){
  57. global $myUser,$_;
  58. User::check_access('planning','edit');
  59. require_once(__DIR__.SLASH.'Planning.class.php');
  60. $item = Planning::provide();
  61. if($item->id!=0 && $item->owner != $myUser->login && !$myUser->superadmin) throw new Exception("Seul le propriétaire de ce planning peut le modifier");
  62. $item->label = $_['planning-label'];
  63. $item->color = $_['planning-color'];
  64. $item->owner = $myUser->login;
  65. $item->type = 'local';
  66. $item->save();
  67. });
  68. //Suppression d'élement planning
  69. Action::register('planning_delete',function(&$response){
  70. global $myUser,$_;
  71. User::check_access('planning','delete');
  72. require_once(__DIR__.SLASH.'Planning.class.php');
  73. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  74. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  75. $planning = Planning::provide();
  76. if(!$planning) throw new Exception("Planning inexistant");
  77. if($planning->default) throw new Exception("Vous ne pouvez pas supprimer le planning par défaut");
  78. 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");
  79. Planning::deleteById($planning->id);
  80. PlanningEvent::delete(array('planning'=>$planning->id));
  81. PlanningShare::delete(array('planning'=>$planning->id));
  82. });
  83. Action::register('planning_widget_load',function(&$response){
  84. global $myUser;
  85. User::check_access('planning','read');
  86. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  87. $widget = DashboardWidget::current();
  88. $widget->title = 'Mes 10 prochains rendez vous';
  89. ob_start();
  90. require_once(__DIR__.SLASH.'widget.php');
  91. $widget->content = ob_get_clean();
  92. echo json_encode($widget);
  93. exit();
  94. });
  95. /** PLANNINGEVENT **/
  96. //Récuperation d'une liste de planningevent
  97. Action::register('planning_event_search',function(&$response){
  98. global $myUser,$_,$myFirm,$conf;
  99. User::check_access('planning','read');
  100. require_once(__DIR__.SLASH.'Planning.class.php');
  101. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  102. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  103. require_once(__DIR__.SLASH.'PlanningEventResource.class.php');
  104. if(!isset($_['plannings'])) return;
  105. $_['plannings'] = explode(',',$_['plannings']);
  106. $plannings = $_['plannings'];
  107. $start = strtotime($_['start']);
  108. $end = strtotime($_['end']);
  109. $events = array();
  110. foreach(array_merge(get_not_workable($start),get_not_workable($end)) as $time){
  111. if($time < $start || $time > $end) continue;
  112. $events[] = array(
  113. 'start' => date('Y-m-d\TH:i:s',$time),
  114. 'end' => date('Y-m-d\T23:59:59',$time),
  115. 'backgroundColor' => '#cecece',
  116. 'display' => 'background'
  117. );
  118. $events[] = array(
  119. 'start' => date('Y-m-d',$time),
  120. 'end' => date('Y-m-d',$time),
  121. 'backgroundColor' => '#cecece',
  122. 'allDay' => true,
  123. 'display' => 'background'
  124. );
  125. }
  126. $allowedPlannings = null;
  127. //en vue équipe on ne selectionne que les planning equipe des equipiers sélectionnés.
  128. if($_['view']=='teammate'){
  129. $plannings = array();
  130. foreach(Planning::loadAll(array('owner:IN'=>implode(",",$_['teammates']), 'slug'=>Planning::TEAMMATE_SLUG)) as $planning){
  131. $plannings[]= $planning->id;
  132. }
  133. $allowedPlannings = $plannings;
  134. }
  135. if(isset($_['plannings']) && count($_['plannings'])!=0){
  136. $myUser->preference('planning_selected_calendars',json_encode($_['plannings']));
  137. $myUser->loadPreferences();
  138. $_SESSION['currentUser'] = serialize($myUser);
  139. foreach(PlanningEvent::getAll($myUser->login,$myUser->ranks[$myFirm->id],$plannings,$start,$end,$allowedPlannings) as $event){
  140. if($event->startDate > $event->endDate) continue;
  141. $textColor = get_light($event->type->color) < 0.6 ? '#fefefefe': '#333333' ;
  142. $isAllDay = false;
  143. $eventLine = array(
  144. 'id' => $event->id,
  145. 'title' => html_entity_decode($event->label,ENT_QUOTES,'UTF-8'),
  146. 'type' => $event->type->id,
  147. 'planning' => $event->planning->id,
  148. 'planningOwner' => $event->planning->owner,
  149. 'planningDefault' => $event->planning->default,
  150. 'allDay' => $isAllDay,
  151. 'street' => $event->street,
  152. 'group' => $event->group,
  153. 'city' => $event->city,
  154. 'zip' => $event->zip,
  155. 'notificationNumber' => $event->notificationNumber,
  156. 'notificationUnity' => $event->notificationUnity,
  157. 'start' => date('Y-m-d\TH:i:s',$event->startDate),
  158. 'end' => date('Y-m-d\TH:i:s',$event->endDate),
  159. 'backgroundColor' => $event->type->color,
  160. 'borderColor' => $event->type->color,
  161. 'textColor' => $textColor,
  162. //'display' => 'block', a décommenter pour avoir la couleur plutot en fond et pas en point
  163. 'underlineColor' => $event->planning->color, //todo, couleur planning a reporter quelque part sur l'event
  164. 'planningLabel' => $event->planning->label,
  165. 'editable' => $conf->get('planning_allow_event_edit') == true,
  166. 'icon' => $event->type->icon,
  167. 'description' => html_entity_decode(str_replace('\n',"\n",$event->description),ENT_QUOTES,'UTF-8'),
  168. 'location' => $event->street.' '.$event->zip.' '.$event->city,
  169. 'resources' => $event->resources,
  170. 'repeatOccurence' => $event->repeatOccurence,
  171. 'repeatUntil' => $event->repeatUntil,
  172. 'repeatYearlyMonth' => $event->repeatYearlyMonth,
  173. 'repeatYearlyNumber' => $event->repeatYearlyNumber,
  174. 'repeatMonthlyDay' => $event->repeatMonthlyDay,
  175. 'repeatMonthlyNumber'=> $event->repeatMonthlyNumber,
  176. 'repeatWeeklyDay' => explode(',',$event->repeatWeeklyDay),
  177. 'repeatDailyNumber' => $event->repeatDailyNumber,
  178. 'repeatType' => $event->repeatType
  179. );
  180. //gestion de l'affichage récurrent
  181. if($event->repeatType != 'never'){
  182. switch($event->repeatType){
  183. case 'daily':
  184. $eventLine['rrule'] = array(
  185. 'dtstart' => date('Y-m-d\TH:i:s',$event->startDate),
  186. 'freq' => 'daily'
  187. );
  188. if(!empty($event->repeatDailyNumber)) $eventLine['rrule']['interval'] =(int) $event->repeatDailyNumber;
  189. break;
  190. case 'weekly':
  191. $eventLine['rrule'] = array(
  192. 'dtstart' => date('Y-m-d\TH:i:s',$event->startDate),
  193. 'freq' => 'weekly'
  194. );
  195. if(!empty($event->repeatWeeklyDay)){
  196. foreach (explode(',',$event->repeatWeeklyDay) as $key => $value) {
  197. $eventLine['rrule']['byweekday'][] = (int) $value -1;
  198. }
  199. }
  200. break;
  201. case 'monthly':
  202. $eventLine['rrule'] = array(
  203. 'dtstart' => date('Y-m-d\TH:i:s',$event->startDate),
  204. 'freq' => 'monthly'
  205. );
  206. if(!empty($event->repeatMonthlyNumber))
  207. $eventLine['rrule']['byweekday'] = array( array('n'=> (int)$event->repeatMonthlyNumber,'weekday'=> (int)$event->repeatMonthlyDay -1));
  208. break;
  209. case 'yearly':
  210. $eventLine['rrule'] = array(
  211. 'dtstart' => date('Y-m-d\TH:i:s',$event->startDate),
  212. 'freq' => 'yearly'
  213. );
  214. if(!empty($event->repeatYearlyNumber)){
  215. $eventLine['rrule']['interval'] = 1;
  216. $eventLine['rrule']['bymonth'] = [(int)$event->repeatYearlyMonth];
  217. $eventLine['rrule']['bymonthday'] = [(int)$event->repeatYearlyNumber];
  218. }
  219. break;
  220. }
  221. if(!empty($event->repeatUntil)) $eventLine['rrule']['until'] = date('Y-m-d',$event->repeatUntil);
  222. if(!empty($event->repeatOccurence)) $eventLine['rrule']['count'] = $event->repeatOccurence;
  223. $eventLine['duration'] = '02:00';
  224. }
  225. $events[] = $eventLine;
  226. }
  227. }
  228. Plugin::callHook('planning_event_search',array(&$events));
  229. $response = $events;
  230. });
  231. Action::register('planning_event_move',function(&$response){
  232. global $myUser,$_;
  233. User::check_access('planning','edit');
  234. require_once(__DIR__.SLASH.'Planning.class.php');
  235. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  236. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  237. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  238. $item = PlanningEvent::getById($_['event']['id'],1);
  239. if(!$item) throw new Exception("Cet événement n'existe plus, merci de rafraîchir votre planning.");
  240. $planning = $item->join('planning');
  241. if($planning->owner != $myUser->login && !$myUser->can('planning','configure')){
  242. if(PlanningShare::rowCount(array('planning'=>$planning->id,'recipient'=>$myUser->login,'edit'=>1))==0)
  243. throw new Exception("Vous n'avez pas la permission d'éditer cet évenement", 403);
  244. }
  245. $start = explode('/',$_['event']['startDate']);
  246. $end = explode('/',$_['event']['endDate']);
  247. //date_default_timezone_set('Europe/Paris');
  248. $startDate = mktime($_['event']['startHour'],$_['event']['startMinut'],0,$start[1],$start[0],$start[2]);
  249. $endDate = mktime($_['event']['endHour'],$_['event']['endMinut'],0,$end[1],$end[0],$end[2]);
  250. if($_['view']=='teammate'){
  251. //Si l'évenement est bougé sur un equipier n'ayant pas de planning equipe, on le créé a la volée
  252. if($planning->owner != $_['event']['teammate']){
  253. $userPlanning = Planning::load(array('owner'=>$_['event']['teammate'],'slug'=>Planning::TEAMMATE_SLUG));
  254. if(!$userPlanning){
  255. $userPlanning = new Planning();
  256. $userPlanning->slug = Planning::TEAMMATE_SLUG;
  257. $userPlanning->owner = $_['event']['teammate'];
  258. $userPlanning->color = '#8bc34a';
  259. $userPlanning->label = 'Equipe';
  260. $userPlanning->save();
  261. }
  262. $item->planning = $userPlanning->id;
  263. }
  264. }
  265. $item->startDate = $startDate;
  266. $item->endDate = $endDate;
  267. $item->save();
  268. });
  269. Action::register('planning_event_resource_state',function(&$response){
  270. global $myUser,$_;
  271. User::check_access('planning','read');
  272. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  273. require_once(__DIR__.SLASH.'PlanningEventResource.class.php');
  274. $start = explode('/',$_['startDate']);
  275. $end = explode('/',$_['endDate']);
  276. $startDate = mktime($_['startHour'],$_['startMinut'],0,$start[1],$start[0],$start[2]);
  277. $endDate = mktime($_['endHour'],$_['endMinut'],0,$end[1],$end[0],$end[2]);
  278. $query = 'SELECT e.*,r.resource as resource FROM '.PlanningEventResource::tableName().' r LEFT JOIN {{table}} e ON r.event=e.id WHERE
  279. (e.startDate BETWEEN ? AND ?) OR (e.endDate BETWEEN ? AND ?) OR (e.startDate < ? AND e.endDate > ?)
  280. AND r.resource IN ('.implode(',',array_fill(0, count($_['resources']), '?')).') ';
  281. $data = array(
  282. $startDate,$endDate,$startDate,$endDate,$startDate,$endDate
  283. );
  284. foreach ($_['resources'] as $resource) $data[] = $resource;
  285. if(!empty($_['event'])){
  286. $query.= ' AND e.id!=? ';
  287. $data[] = $_['event'];
  288. }
  289. $response['rows'] = array();
  290. foreach(PlanningEvent::staticQuery($query,$data,true) as $booked){
  291. if(isset($_['event']) && $booked->id == $_['event']) continue;
  292. if(!isset($response['rows'][$booked->foreign('resource')])) $response['rows'][$booked->foreign('resource')] = array();
  293. $response['rows'][$booked->foreign('resource')][] = $booked->toArray();
  294. }
  295. });
  296. //Ajout ou modification d'élément planningevent
  297. Action::register('planning_event_save',function(&$response){
  298. global $myUser,$_;
  299. User::check_access('planning','edit');
  300. require_once(__DIR__.SLASH.'Planning.class.php');
  301. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  302. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  303. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  304. require_once(__DIR__.SLASH.'PlanningEventResource.class.php');
  305. $item = PlanningEvent::provide('id',1);
  306. if(!$item) throw new Exception("Cet événement n'existe plus, merci de rafraîchir votre planning.");
  307. $start = explode('/',$_['startDate']);
  308. $end = explode('/',$_['endDate']);
  309. $startDate = mktime($_['startHour'],$_['startMinut'],0,$start[1],$start[0],$start[2]);
  310. $endDate = mktime($_['endHour'],$_['endMinut'],0,$end[1],$end[0],$end[2]);
  311. //On ne peut pas avoir une date/heure de fin strictement similaire a la date de début (fullcalendar plante) on ajoute une minute si c'est le cas
  312. if($startDate == $endDate ) $endDate += 60;
  313. if($startDate > $endDate) throw new Exception("Dates incohérentes");
  314. $plannings = array();
  315. //Si on est en vue equipe
  316. if($_['view']=='teammate'){
  317. //on récupere les plannings d'équipe qui correspondent aux logins concernés par l'évenement
  318. $plannings = Planning::loadAll(array('owner:IN'=>$_['user'],'slug'=>Planning::TEAMMATE_SLUG));
  319. //Pour chaque login, on vérifie qu'un planning equipe existe, on le créé le cas échéant
  320. foreach (explode(',',$_['user']) as $user) {
  321. $planningExists = false;
  322. foreach ($plannings as $planning) {
  323. if($planning->owner == $user) $planningExists = true;
  324. }
  325. if(!$planningExists){
  326. $planning = new Planning();
  327. $planning->slug = Planning::TEAMMATE_SLUG;
  328. $planning->owner = $user;
  329. $planning->color = '#8bc34a';
  330. $planning->label = 'Equipe';
  331. $planning->save();
  332. $plannings[] = $planning;
  333. }
  334. }
  335. //Pour toutes les autres vues, on gère le planning de manière classique
  336. }else{
  337. $planning = $item->id==0 ? Planning::getById($_['planning']) : $item->join('planning');
  338. if($planning->owner != $myUser->login){
  339. if(PlanningShare::rowCount(array('planning'=>$planning->id,'recipient'=>$myUser->login,'edit'=>1))==0)
  340. throw new Exception("Vous n'avez pas la permission d'éditer cet évenement", 403);
  341. }
  342. }
  343. $item->label = $_['label'];
  344. if(!empty($_['planning'])) $item->planning = $_['planning'];
  345. if(isset($_['type'])) $item->type = $_['type'];
  346. $item->startDate = $startDate;
  347. $item->endDate = $endDate;
  348. $item->description = $_['description'];
  349. $item->street = $_['street'];
  350. $item->city = $_['city'];
  351. $item->zip = $_['zip'];
  352. if(isset($_['event-repeat-type'])){
  353. $item->repeatType = $_['event-repeat-type'];
  354. if($_['repeatEndType'] == 'occurence')
  355. $item->repeatOccurence = $_['event-repeat-occurences'];
  356. if($_['repeatEndType'] == 'until')
  357. $item->repeatUntil = $_['event-repeat-until'];
  358. $item->repeatYearlyMonth = $_['event-repeat-yearly-month'];
  359. $item->repeatYearlyNumber = $_['event-repeat-yearly-number'];
  360. $item->repeatMonthlyDay = $_['event-repeat-monthly-day'];
  361. $item->repeatMonthlyNumber = $_['event-repeat-monthly-number'];
  362. if(isset($_['weeklyDay']) && is_array($_['weeklyDay'])) $item->repeatWeeklyDay = implode(',',$_['weeklyDay']) ;
  363. $item->repeatDailyNumber = $_['event-repeat-daily-number'];
  364. $item->repeatUntil = !empty($_['event-repeat-until']) ? timestamp_date($_['event-repeat-until']) : '';
  365. $item->repeatOccurence = !empty($_['event-repeat-occurences']) ? $_['event-repeat-occurences'] : '';
  366. }
  367. if(isset($_['notificationNumber']) && !empty($_['notificationNumber'])){
  368. //Si les parametres de notifications ont changé on remet a zero l'etat de notification
  369. if($_['notificationNumber'] != $item->notificationNumber) $item->notificationState = '';
  370. $item->notificationNumber = $_['notificationNumber'];
  371. }
  372. if(isset($_['notificationUnity'])) $item->notificationUnity = $_['notificationUnity'];
  373. $item->save();
  374. PlanningEventResource::delete(array('event'=>$item->id));
  375. //on ajoute les reservation de ressources
  376. if(!empty($_['resources'])){
  377. foreach ($_['resources'] as $id) {
  378. $resource = new PlanningEventResource();
  379. $resource->resource = $id;
  380. $resource->event = $item->id;
  381. $resource->save();
  382. }
  383. }
  384. //on ajoute des copie de cet évenements aux autres planings equipiers concernés
  385. foreach ($plannings as $planning) {
  386. if($item->planning == $planning->id) continue;
  387. $clonedItem = clone($item);
  388. $clonedItem->id = null;
  389. $clonedItem->planning = $planning->id;
  390. $clonedItem->save();
  391. }
  392. });
  393. //Suppression d'élement planningevent
  394. Action::register('planning_event_delete',function(&$response){
  395. global $myUser,$_;
  396. User::check_access('planning','delete');
  397. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  398. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  399. $events = PlanningEvent::removeAll($myUser->login,$_['events']);
  400. });
  401. Action::register('planning_group_events_search',function(&$response){
  402. global $myUser,$_;
  403. User::check_access('planning','delete');
  404. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  405. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  406. $response['rows'] = PlanningEvent::loadAll(array('group'=>$_['group']));
  407. });
  408. Action::register('planning_subtype_search',function(&$response){
  409. global $myUser,$_;
  410. User::check_access('planning','read');
  411. require_once(__DIR__.SLASH.'PlanningEvent.class.php');
  412. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  413. $response['rows'] = array();
  414. foreach (PlanningEventType::loadAll(array('parent'=>$_['type'])) as $type) {
  415. $response['rows'][] = $type->toArray();
  416. }
  417. });
  418. /** TEAMMATE **/
  419. Action::register('planning_teammate_save',function(&$response){
  420. global $myUser,$_;
  421. User::check_access('planning','read');
  422. $myUser->preference('planning_teammate',implode(',',$_['teammates']));
  423. $myUser->loadPreferences();
  424. $response['teammates'] = array();
  425. $teammates = $myUser->preference('planning_teammate')!='' ? explode(',',$myUser->preference('planning_teammate')) : array();
  426. foreach ($teammates as $login) {
  427. $user = User::byLogin($login);
  428. $user->password = '';
  429. $row = $user->toArray();
  430. $row['avatar'] = $user->getAvatar();
  431. $response['teammates'][] = $row;
  432. }
  433. });
  434. Action::register('planning_teammate_sort',function(&$response){
  435. global $myUser,$_;
  436. User::check_access('planning','read');
  437. $myUser->preference('planning_teammate',implode(',',$_['sort']));
  438. $myUser->loadPreferences();
  439. });
  440. /** EVENT TYPE **/
  441. //Récuperation d'une liste de planningeventtype
  442. Action::register('planning_event_type_search',function(&$response){
  443. global $myUser,$_;
  444. User::check_access('planning','read');
  445. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  446. foreach(PlanningEventType::loadAll(array('state'=>PlanningEventType::ACTIVE),array('sort')) as $eventType){
  447. $row = $eventType->toArray();
  448. $row['textcolor'] = get_light($row['color'])> 0.5 ? '#333333':'#ffffff';
  449. $row['editable'] = !empty($row['editable']);
  450. $row['superadmin'] = $myUser->superadmin || $row['editable'];
  451. $row['editableOptions'] = $myUser->superadmin || $row['editable'];
  452. $row['class'] = !empty($eventType->parent) ? 'planning-line-child' : '';
  453. $response['rows'][] = $row;
  454. }
  455. });
  456. Action::register('planning_event_type_save',function(&$response){
  457. global $myUser,$_,$conf;
  458. User::check_access('planning','configure');
  459. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  460. if(!isset($_['items'])) return $response;
  461. $existingTypes = array();
  462. foreach (PlanningEventType::loadAll() as $existingType) {
  463. $existingTypes[$existingType->id] = $existingType;
  464. }
  465. $o = 0;
  466. foreach ($_['items'] as $i => $item){
  467. $type = !empty($item['id']) ? $existingTypes[$item['id']] : new PlanningEventType();
  468. if($type->id!=0 && !$type->editable && !$myUser->superadmin) throw new Exception("Item non éditable");
  469. $type->sort = $o;
  470. $type->label = $item['label'];
  471. $type->icon = $item['icon'];
  472. $type->editable = $item['editable'] == '1';
  473. $type->color = $item['color'];
  474. $type->slug = slugify($item['label']);
  475. $type->save();
  476. unset($existingTypes[$item['id']]);
  477. $o++;
  478. if(isset($item['childs'])){
  479. foreach($item['childs'] as $u=>$subItem){
  480. $subtype = !empty($subItem['id']) ? $existingTypes[$subItem['id']] : new PlanningEventType();
  481. if($subtype->id!=0 && !$subtype->editable && !$myUser->superadmin) throw new Exception("Item non éditable");
  482. $subtype->label = $subItem['label'];
  483. $subtype->sort = $o;
  484. $subtype->icon = $subItem['icon'];
  485. $subtype->editable = $subItem['editable'] == '1';
  486. $subtype->parent = $type->id;
  487. $subtype->color = $subItem['color'];
  488. $subtype->slug = slugify($subItem['label']);
  489. $subtype->save();
  490. unset($existingTypes[$subItem['id']]);
  491. $o++;
  492. }
  493. }
  494. }
  495. //Suppression logique des types
  496. foreach($existingTypes as $existingType){
  497. $existingType->state = PlanningEventType::INACTIVE;
  498. $existingType->save();
  499. }
  500. });
  501. //Récuperation ou edition d'élément planningeventtype
  502. Action::register('planning_event_type_edit',function(&$response){
  503. global $myUser,$_;
  504. User::check_access('planning','configure');
  505. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  506. $response = PlanningEventType::getById($_['id']);
  507. });
  508. Action::register('planning_event_type_sort',function(&$response){
  509. global $myUser,$_;
  510. User::check_access('planning','configure');
  511. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  512. foreach ($_['sort'] as $i=>$id) {
  513. PlanningEventType::change(array('sort'=>$i),array('id'=>$id));
  514. }
  515. });
  516. //Suppression d'élement planningeventtype
  517. Action::register('planning_event_type_delete',function(&$response){
  518. global $myUser,$_;
  519. User::check_access('planning','configure');
  520. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  521. $item = PlanningEventType::getById($_['id']);
  522. $item->state = PlanningEventType::INACTIVE;
  523. $item->save();
  524. });
  525. //Sauvegarde des configurations de planning
  526. Action::register('planning_setting_save',function(&$response){
  527. global $myUser,$_,$conf;
  528. User::check_access('planning','configure');
  529. foreach(Configuration::setting('planning') as $key=>$value){
  530. if(!is_array($value)) continue;
  531. $allowed[] = $key;
  532. }
  533. foreach ($_['fields'] as $key => $value) {
  534. if(in_array($key, $allowed))
  535. $conf->put($key,$value);
  536. }
  537. });
  538. /** PLANNINGSHARE **/
  539. //Récuperation d'une liste de planningshare
  540. Action::register('planning_share_search',function(&$response){
  541. global $myUser,$_;
  542. User::check_access('planning','read');
  543. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  544. $shares = PlanningShare::loadAll(array('planning'=>$_['planning']));
  545. foreach($shares as $planningshare){
  546. $row = $planningshare->toArray();
  547. $row['edit'] = $row['edit']==0?null:$row['edit'];
  548. if($planningshare->recipientEntity=='rank'){
  549. $rank = Rank::getById($planningshare->recipient);
  550. $row['recipient'] = $rank->label;
  551. $row['recipientType'] = "Rang";
  552. }else{
  553. $row['recipient'] = User::byLogin($planningshare->recipient)->fullName();
  554. $row['recipientType'] = "Utilisateur";
  555. }
  556. $response['rows'][] = $row;
  557. }
  558. });
  559. //Ajout ou modification d'élément planningshare
  560. Action::register('planning_share_save',function(&$response){
  561. global $myUser,$_;
  562. User::check_access('planning','edit');
  563. require_once(__DIR__.SLASH.'Planning.class.php');
  564. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  565. if(empty($_['recipient'])) throw new Exception("Vous devez préciser le destinataire du partage");
  566. //vérifie que ce destinataire n'a pas déja un partage sur ce planning
  567. 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");
  568. $planning = Planning::getById($_['planning']);
  569. if(!$planning) throw new Exception("Planning inexistant");
  570. if($planning->owner != $myUser->login) throw new Exception("Vous n'êtes pas propriétaire du planning");
  571. if(!$_['read'] && !$_['edit']) throw new Exception("Vous devez cocher au moins un droit de planning");
  572. $item = PlanningShare::provide();
  573. $item->planning = $planning->id;
  574. $item->recipient = stripslashes($_['recipient']);
  575. $item->recipientEntity = $_['recipientEntity'];
  576. $item->read = $_['read'];
  577. $item->edit = $_['edit'];
  578. $item->save();
  579. });
  580. //Suppression d'élement planningshare
  581. Action::register('planning_share_delete',function(&$response){
  582. global $myUser,$_;
  583. User::check_access('planning','delete');
  584. require_once(__DIR__.SLASH.'Planning.class.php');
  585. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  586. $item = PlanningShare::provide();
  587. $planning = Planning::getById($item->planning);
  588. if($planning->owner != $myUser->login) throw new Exception("Vous n'êtes pas propriétaire du planning");
  589. PlanningShare::deleteById($_['id']);
  590. });
  591. ?>