action.php 7.6 KB

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