action.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. case 'notification_send':
  5. Action::write(function(&$response){
  6. global $myUser,$_;
  7. require_once(__DIR__.SLASH.'Notification.class.php');
  8. User::check_access('notification','configure');
  9. if(!isset($_['label']) || empty($_['label'])) throw new Exception("Merci de spécifier le sujet de la notification");
  10. if(!isset($_['type']) || empty($_['type'])) throw new Exception("Merci de spécifier le type de la notification");
  11. if(!isset($_['recipients'])) $_['recipients'] = '';
  12. $recipients = explode(',',$_['recipients']);
  13. if($_['recipients']==''){
  14. $recipients = array();
  15. foreach (User::getAll(false) as $user)
  16. $recipients[] = $user->login;
  17. }
  18. $start = isset($_['start']) && $_['start']!='' ? timestamp_date($_['start']) : mktime(0,0,0) ;
  19. $end = isset($_['end']) && $_['end']!='' ? timestamp_date($_['end']) : strtotime('+3month',mktime(0,0,0));
  20. if($start>$end) throw new Exception("Dates de lancement et d'expiration incohérentes");
  21. Notification::emit(array(
  22. 'label' => $_['label'],
  23. 'html' => wysiwyg_filter(html_entity_decode($_['html'])),
  24. 'pinned' => $_['pinned'],
  25. 'type' => $_['type'],
  26. 'start' => $start,
  27. 'end' => $end,
  28. 'recipients' => $recipients
  29. ));
  30. });
  31. break;
  32. //Récuperation d'une liste de notification
  33. case 'notification_search':
  34. Action::write(function(&$response){
  35. global $myUser,$_;
  36. User::check_access('notification','read');
  37. require_once(__DIR__.SLASH.'Notification.class.php');
  38. // OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
  39. $query = 'SELECT * FROM '.Notification::tableName().' WHERE pinned=1 ';
  40. $data = array();
  41. //Recherche simple
  42. if(!empty($_['filters']['keyword'])){
  43. $query .= ' AND label LIKE ?';
  44. $data[] = '%'.$_['filters']['keyword'].'%';
  45. }
  46. //Recherche avancée
  47. if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('label','start','end','type'),$query,$data);
  48. //Tri des colonnes
  49. if(isset($_['sort'])) sort_secure_query($_['sort'],array('label','start','end','type'),$query,$data);
  50. //Pagination
  51. $response['pagination'] = Notification::paginate(20,(!empty($_['page'])?$_['page']:0),$query,$data);
  52. $notifications = Notification::staticQuery($query,$data,true);
  53. foreach($notifications as $notification){
  54. $row = $notification->toArray();
  55. $row['start'] = date('d/m/Y',$row['start']);
  56. $row['end'] = date('d/m/Y',$row['end']);
  57. $row['type'] = Notification::types($row['type']);
  58. $response['rows'][] = $row;
  59. }
  60. });
  61. break;
  62. //Suppression d'élement notification et de ses user notifications associés
  63. case 'notification_delete':
  64. Action::write(function(&$response){
  65. global $myUser,$_;
  66. require_once(__DIR__.SLASH.'UserNotification.class.php');
  67. require_once(__DIR__.SLASH.'Notification.class.php');
  68. User::check_access('notification','configure');
  69. UserNotification::delete(array('notification'=>$_['id']));
  70. Notification::deleteById($_['id']);
  71. });
  72. break;
  73. case 'notification_widget_load':
  74. global $myUser;
  75. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  76. $widget = DashboardWidget::current();
  77. $widget->title = 'Dernières alertes';
  78. ob_start();
  79. require_once(__DIR__.SLASH.'widget.php');
  80. $widget->content = ob_get_clean();
  81. echo json_encode($widget);
  82. break;
  83. case 'notification_widget_configure_save':
  84. Action::write(function(&$response){
  85. global $myUser,$_;
  86. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  87. $widget = DashboardWidget::getById($_['id']);
  88. if(!isset($_['types'])) throw new Exception("Au moins un type de notification à afficher est requis");
  89. if(!isset($_['notification-number'])) $_['notification-number'] = 5;
  90. if(!isset($_['notification-length'])) $_['notification-length'] = 150;
  91. $widget->data('types',$_['types']);
  92. $widget->data('notification-number',$_['notification-number']);
  93. $widget->data('notification-length',$_['notification-length']);
  94. $widget->save();
  95. });
  96. break;
  97. case 'notification_widget_configure':
  98. global $myUser;
  99. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  100. $widget = DashboardWidget::current();
  101. ob_start();
  102. require_once(__DIR__.SLASH.'widget.configure.php');
  103. $content = ob_get_clean();
  104. echo $content ;
  105. break;
  106. //Récuperation d'une liste de notification
  107. case 'notification_user_notification_search':
  108. Action::write(function(&$response){
  109. global $myUser,$_;
  110. if(!$myUser->connected()) return;
  111. if(!$myUser->can('notification','read')) return;
  112. require_once(__DIR__.SLASH.'UserNotification.class.php');
  113. require_once(__DIR__.SLASH.'Notification.class.php');
  114. $response['unread'] = 0;
  115. $unreads = UserNotification::byUser($myUser,array(
  116. 'limit' => isset($_['excerpt']) ? 5 : 100,
  117. 'unread' => isset($_['unread']) ? filter_var($_['unread'], FILTER_VALIDATE_BOOLEAN) : false,
  118. ));
  119. foreach ($unreads as $row) {
  120. $row['html'] = isset($_['excerpt']) ? truncate(str_replace('"', "'", html_entity_decode($row['html'])), 150, array('keepTags'=>false)) : $row['html'];
  121. $row['class'] = 'read';
  122. $row['readState'] = 'non lu';
  123. if($row['read']==0){
  124. $row['class'] = 'unread';
  125. $response['unread']++;
  126. $row['readState'] = 'lu';
  127. }
  128. if($row['pinned']==1) $row['class'] .=' pinned';
  129. $row['created-relative'] = isset($_['excerpt']) ? relative_time($row['created'], null, 7) : relative_time($row['created'], null, 7, true);
  130. $meta = json_decode($row['meta']);
  131. $row['link'] = isset($meta->link) ? $meta->link : 'index.php?module=notification#'.$row['id'];
  132. $response['rows'][] = $row;
  133. }
  134. $response['unread'] = UserNotification::get_unreads_count($myUser,'compact');
  135. });
  136. break;
  137. //Active/Désactive l'état lu
  138. case 'notification_usernotification_toggle_read':
  139. Action::write(function(&$response){
  140. global $myUser,$_;
  141. require_once(__DIR__.SLASH.'UserNotification.class.php');
  142. $item = UserNotification::getById($_['id']);
  143. if($myUser->login != $item->user) throw new Exception("Permissions insuffisantes",403);
  144. $item->read = $item->read == true ? false : true;
  145. $item->save();
  146. $response['read'] = $item->read;
  147. $response['unread'] = UserNotification::get_unreads_count($myUser,'detailled');
  148. });
  149. break;
  150. case 'notification_user_notification_read_all':
  151. Action::write(function(&$response){
  152. global $myUser,$_;
  153. require_once(__DIR__.SLASH.'UserNotification.class.php');
  154. foreach (json_decode($_['id']) as $id) {
  155. $item = UserNotification::getById($id);
  156. if($myUser->login != $item->user) throw new Exception("Permissions insuffisantes",403);
  157. $item->read = true;
  158. $item->save();
  159. $response['read'][$id] = $item->read;
  160. }
  161. $response['unread'] = UserNotification::get_unreads_count($myUser,'detailled');
  162. });
  163. break;
  164. //Suppression d'élement usernotification
  165. case 'notification_usernotification_delete':
  166. Action::write(function(&$response){
  167. global $myUser,$_;
  168. require_once(__DIR__.SLASH.'UserNotification.class.php');
  169. if(!$notification = UserNotification::getById($_['id'])) return;
  170. if($myUser->login != $notification->user) throw new Exception("Permissions insuffisantes",403);
  171. UserNotification::deleteById($_['id']);
  172. });
  173. break;
  174. case 'notification_usernotifications_delete':
  175. Action::write(function(&$response){
  176. global $myUser,$_;
  177. require_once(__DIR__.SLASH.'UserNotification.class.php');
  178. foreach (json_decode($_['id']) as $id) {
  179. $notification = UserNotification::getById($id);
  180. if($myUser->login != $notification->user) throw new Exception("Permissions insuffisantes",403);
  181. UserNotification::deleteById($id);
  182. }
  183. });
  184. break;
  185. //Sauvegarde des préférences de notifications
  186. case 'notification_user_save_preference':
  187. Action::write(function(&$response){
  188. global $myUser,$_;
  189. require_once(__DIR__.SLASH.'Notification.class.php');
  190. if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour enregistrer vos préférences", 401);
  191. foreach ($_['preferences'] as $method => $pref) {
  192. foreach ($pref as $key => $value){
  193. $_['preferences'][$method][$key] = ($value == 'true') ? true : false;
  194. }
  195. }
  196. $preferences = Notification::settingPreferences($myUser->login, $_['preferences']);
  197. $myUser->preference('notification_categories',json_encode($preferences));
  198. });
  199. break;
  200. //Sauvegarde des configurations de
  201. case 'notification_setting_save':
  202. Action::write(function(&$response){
  203. global $myUser,$_,$conf;
  204. User::check_access('notification','configure');
  205. foreach(Configuration::setting('notification') as $key=>$value){
  206. if(!is_array($value)) continue;
  207. $allowed[] = $key;
  208. }
  209. foreach ($_['fields'] as $key => $value){
  210. if(!in_array($key, $allowed)) continue;
  211. $value = html_entity_decode($value);
  212. if($key=='notification_mail_reply' && !check_mail($value) && !empty($value)) throw new Exception("Email invalide :".$value);
  213. $conf->put($key,$value);
  214. }
  215. foreach ($_['followings'] as $key => $value)
  216. $conf->put($key,$value);
  217. });
  218. break;
  219. }
  220. ?>