action.php 9.3 KB

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