action.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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(array('right'=>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 {{table}} WHERE pinned=? ';
  40. $data = array(1);
  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. User::check_access('notification','read');
  76. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  77. $widget = DashboardWidget::current();
  78. $widget->title = 'Dernières alertes';
  79. ob_start();
  80. require_once(__DIR__.SLASH.'widget.php');
  81. $widget->content = ob_get_clean();
  82. echo json_encode($widget);
  83. break;
  84. case 'notification_widget_configure_save':
  85. Action::write(function(&$response){
  86. global $myUser,$_;
  87. User::check_access('notification','read');
  88. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  89. $widget = DashboardWidget::getById($_['id']);
  90. if(!isset($_['types'])) throw new Exception("Au moins un type de notification à afficher est requis");
  91. if(!isset($_['notification-number'])) $_['notification-number'] = 5;
  92. if(!isset($_['notification-length'])) $_['notification-length'] = 150;
  93. $widget->data('types',$_['types']);
  94. $widget->data('notification-number',$_['notification-number']);
  95. $widget->data('notification-length',$_['notification-length']);
  96. $widget->save();
  97. });
  98. break;
  99. case 'notification_widget_configure':
  100. global $myUser;
  101. User::check_access('notification','read');
  102. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  103. $widget = DashboardWidget::current();
  104. ob_start();
  105. require_once(__DIR__.SLASH.'widget.configure.php');
  106. $content = ob_get_clean();
  107. echo $content ;
  108. break;
  109. //Récuperation d'une liste de notification
  110. case 'notification_user_notification_search':
  111. Action::write(function(&$response){
  112. global $myUser,$_;
  113. if(!$myUser->connected()) return;
  114. if(!$myUser->can('notification','read')) return;
  115. require_once(__DIR__.SLASH.'UserNotification.class.php');
  116. require_once(__DIR__.SLASH.'Notification.class.php');
  117. $response['unread'] = 0;
  118. $unreads = UserNotification::byUser($myUser,array(
  119. 'limit' => isset($_['excerpt']) ? 5 : 100,
  120. 'unread' => isset($_['unread']) ? filter_var($_['unread'], FILTER_VALIDATE_BOOLEAN) : false,
  121. ));
  122. foreach ($unreads as $row) {
  123. $row['html'] = isset($_['excerpt']) ? truncate(str_replace('"', "'", html_entity_decode($row['html'])), 150, array('keepTags'=>false)) : $row['html'];
  124. $row['class'] = 'read';
  125. $row['readState'] = 'non lu';
  126. if($row['read']==0){
  127. $row['class'] = 'unread';
  128. $response['unread']++;
  129. $row['readState'] = 'lu';
  130. }
  131. if($row['pinned']==1) $row['class'] .=' pinned';
  132. $row['created-relative'] = isset($_['excerpt']) ? relative_time($row['created'], null, 7) : relative_time($row['created'], null, 7, true);
  133. $meta = json_decode($row['meta']);
  134. $row['link'] = isset($meta->link) ? $meta->link : 'index.php?module=notification#'.$row['id'];
  135. $response['rows'][] = $row;
  136. }
  137. $response['unread'] = UserNotification::get_unreads_count($myUser,'compact');
  138. });
  139. break;
  140. //Active/Désactive l'état lu
  141. case 'notification_usernotification_toggle_read':
  142. Action::write(function(&$response){
  143. global $myUser,$_;
  144. User::check_access('notification','read');
  145. require_once(__DIR__.SLASH.'UserNotification.class.php');
  146. $item = UserNotification::getById($_['id']);
  147. if($myUser->login != $item->user) throw new Exception("Permissions insuffisantes",403);
  148. $item->read = $item->read == true ? false : true;
  149. $item->save();
  150. $response['read'] = $item->read;
  151. $response['unread'] = UserNotification::get_unreads_count($myUser,'detailled');
  152. });
  153. break;
  154. case 'notification_user_notification_read_all':
  155. Action::write(function(&$response){
  156. global $myUser,$_;
  157. User::check_access('notification','read');
  158. require_once(__DIR__.SLASH.'UserNotification.class.php');
  159. foreach (json_decode($_['id']) as $id) {
  160. $item = UserNotification::getById($id);
  161. if($myUser->login != $item->user) throw new Exception("Permissions insuffisantes",403);
  162. $item->read = true;
  163. $item->save();
  164. $response['read'][$id] = $item->read;
  165. }
  166. $response['unread'] = UserNotification::get_unreads_count($myUser,'detailled');
  167. });
  168. break;
  169. //Suppression d'élement usernotification
  170. case 'notification_usernotification_delete':
  171. Action::write(function(&$response){
  172. global $myUser,$_;
  173. User::check_access('notification','delete');
  174. require_once(__DIR__.SLASH.'UserNotification.class.php');
  175. if(!$notification = UserNotification::getById($_['id'])) return;
  176. if($myUser->login != $notification->user) throw new Exception("Permissions insuffisantes",403);
  177. UserNotification::deleteById($_['id']);
  178. });
  179. break;
  180. case 'notification_usernotifications_delete':
  181. Action::write(function(&$response){
  182. global $myUser,$_;
  183. User::check_access('notification','delete');
  184. require_once(__DIR__.SLASH.'UserNotification.class.php');
  185. foreach (json_decode($_['id']) as $id) {
  186. $notification = UserNotification::getById($id);
  187. if($myUser->login != $notification->user) throw new Exception("Permissions insuffisantes",403);
  188. UserNotification::deleteById($id);
  189. }
  190. });
  191. break;
  192. //Sauvegarde des préférences de notifications
  193. case 'notification_user_save_preference':
  194. Action::write(function(&$response){
  195. global $myUser,$_;
  196. require_once(__DIR__.SLASH.'Notification.class.php');
  197. if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour enregistrer vos préférences", 401);
  198. foreach ($_['preferences'] as $method => $pref) {
  199. foreach ($pref as $key => $value){
  200. $_['preferences'][$method][$key] = ($value == 'true') ? true : false;
  201. }
  202. }
  203. $preferences = Notification::settingPreferences($myUser->login, $_['preferences']);
  204. $myUser->preference('notification_categories',json_encode($preferences));
  205. });
  206. break;
  207. //Sauvegarde des configurations de
  208. case 'notification_setting_save':
  209. Action::write(function(&$response){
  210. global $myUser,$_,$conf;
  211. User::check_access('notification','configure');
  212. foreach(Configuration::setting('notification') as $key=>$value){
  213. if(!is_array($value)) continue;
  214. $allowed[] = $key;
  215. }
  216. foreach ($_['fields'] as $key => $value){
  217. if(!in_array($key, $allowed)) continue;
  218. $value = html_entity_decode($value);
  219. if($key=='notification_mail_reply' && !check_mail($value) && !empty($value)) throw new Exception("Email invalide :".$value);
  220. $conf->put($key,$value);
  221. }
  222. foreach ($_['followings'] as $key => $value)
  223. $conf->put($key,$value);
  224. });
  225. break;
  226. }
  227. ?>