action.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. //Récuperation d'une liste de notification
  66. Action::register('notification_user_notification_search',function(&$response){
  67. global $myUser,$_;
  68. if(!$myUser->connected()) return;
  69. if(!$myUser->can('notification','read')) return;
  70. require_once(__DIR__.SLASH.'UserNotification.class.php');
  71. require_once(__DIR__.SLASH.'Notification.class.php');
  72. $response['unread'] = 0;
  73. $unreads = UserNotification::byUser($myUser,array(
  74. 'limit' => isset($_['excerpt']) ? 5 : 100,
  75. 'unread' => isset($_['unread']) ? filter_var($_['unread'], FILTER_VALIDATE_BOOLEAN) : false,
  76. ))->fetchAll();
  77. $response['unread'] = 0;
  78. foreach ($unreads as $row) {
  79. $row['html'] = isset($_['excerpt']) ? truncate(str_replace('"', "'", html_entity_decode($row['html'])), 150, array('keepTags'=>false)) : $row['html'];
  80. $row['class'] = 'read';
  81. $row['readState'] = 'non lu';
  82. if($row['read']==0){
  83. $row['class'] = 'unread';
  84. $response['unread']++;
  85. $row['readState'] = 'lu';
  86. }
  87. if($row['pinned']==1) $row['class'] .=' pinned';
  88. $row['created-relative'] = isset($_['excerpt']) ? relative_time($row['created'], null, 7) : relative_time($row['created'], null, 7, true);
  89. $meta = json_decode($row['meta']);
  90. $row['link'] = isset($meta->link) ? $meta->link : 'index.php?module=notification#'.$row['id'];
  91. $response['rows'][] = $row;
  92. }
  93. $response['unread'] = $response['unread'] > 5 ? '5+' : $response['unread'];
  94. });
  95. //Active/Désactive l'état lu
  96. Action::register('notification_usernotification_toggle_read',function(&$response){
  97. global $myUser,$_;
  98. User::check_access('notification','read');
  99. require_once(__DIR__.SLASH.'UserNotification.class.php');
  100. $item = UserNotification::getById($_['id']);
  101. if($myUser->login != $item->user) throw new Exception("Permissions insuffisantes",403);
  102. $item->read = $item->read == true ? false : true;
  103. $item->save();
  104. $response['read'] = $item->read;
  105. $response['unread'] = UserNotification::get_unreads_count($myUser,'detailled');
  106. });
  107. Action::register('notification_user_notification_read_all',function(&$response){
  108. global $myUser,$_;
  109. User::check_access('notification','read');
  110. require_once(__DIR__.SLASH.'UserNotification.class.php');
  111. foreach (json_decode($_['id']) as $id) {
  112. $item = UserNotification::getById($id);
  113. if($myUser->login != $item->user) throw new Exception("Permissions insuffisantes",403);
  114. $item->read = true;
  115. $item->save();
  116. $response['read'][$id] = $item->read;
  117. }
  118. $response['unread'] = UserNotification::get_unreads_count($myUser,'detailled');
  119. });
  120. //Suppression d'élement usernotification
  121. Action::register('notification_usernotification_delete',function(&$response){
  122. global $myUser,$_;
  123. User::check_access('notification','delete');
  124. require_once(__DIR__.SLASH.'UserNotification.class.php');
  125. if(!$notification = UserNotification::getById($_['id'])) return;
  126. if($myUser->login != $notification->user) throw new Exception("Permissions insuffisantes",403);
  127. UserNotification::deleteById($_['id']);
  128. });
  129. Action::register('notification_usernotifications_delete',function(&$response){
  130. global $myUser,$_;
  131. User::check_access('notification','delete');
  132. require_once(__DIR__.SLASH.'UserNotification.class.php');
  133. foreach (json_decode($_['id']) as $id) {
  134. $notification = UserNotification::getById($id);
  135. if($myUser->login != $notification->user) throw new Exception("Permissions insuffisantes",403);
  136. UserNotification::deleteById($id);
  137. }
  138. });
  139. //Sauvegarde des préférences de notifications
  140. Action::register('notification_user_save_preference',function(&$response){
  141. global $myUser,$_;
  142. require_once(__DIR__.SLASH.'Notification.class.php');
  143. if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour enregistrer vos préférences", 401);
  144. foreach ($_['preferences'] as $method => $pref) {
  145. foreach ($pref as $key => $value){
  146. $_['preferences'][$method][$key] = ($value == 'true') ? true : false;
  147. }
  148. }
  149. $preferences = Notification::settingPreferences($myUser->login, $_['preferences']);
  150. $myUser->preference('notification_categories',json_encode($preferences));
  151. });
  152. //Sauvegarde des configurations de
  153. Action::register('notification_setting_save',function(&$response){
  154. global $myUser,$_,$conf;
  155. User::check_access('notification','configure');
  156. foreach(Configuration::setting('notification') as $key=>$value){
  157. if(!is_array($value)) continue;
  158. $allowed[] = $key;
  159. }
  160. foreach ($_['fields'] as $key => $value){
  161. if(!in_array($key, $allowed)) continue;
  162. $value = html_entity_decode($value);
  163. if($key=='notification_mail_reply' && !check_mail($value) && !empty($value)) throw new Exception("Email invalide :".$value);
  164. $conf->put($key,$value);
  165. }
  166. foreach ($_['followings'] as $key => $value)
  167. $conf->put($key,$value);
  168. });
  169. ?>