action.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. //Récuperation d'une liste de notification
  5. case 'notification_user_notification_search':
  6. Action::write(function(&$response){
  7. global $myUser,$_;
  8. if (!$myUser->connected()) return;
  9. if(!$myUser->can('notification','read')) return;
  10. require_once(__DIR__.SLASH.'UserNotification.class.php');
  11. require_once(__DIR__.SLASH.'Notification.class.php');
  12. $response['unread'] = 0;
  13. $query = 'SELECT *, un.id as id FROM {{table}} un LEFT JOIN '.Notification::tableName().' n ON un.notification=n.id WHERE un.user=? AND (n.start<? OR n.start IS NULL) AND (n.end>? OR n.end IS NULL) '.(isset($_['synthesis']) ? 'AND (un.read = 0)' : '').' ORDER BY n.created DESC LIMIT '.(isset($_['synthesis']) ? 5 : 100);
  14. foreach($userNotifications = UserNotification::staticQuery($query,array($myUser->login,time(),time())) as $infos){
  15. $infos['class'] = 'read';
  16. $infos['readState'] = 'non lu';
  17. if($infos['read']==0){
  18. $infos['class'] = 'unread';
  19. $response['unread']++;
  20. $infos['readState'] = 'lu';
  21. }
  22. $infos['created-relative'] = isset($_['synthesis']) ? relative_time($infos['created'], null, 7) : relative_time($infos['created'], null, 7, true);
  23. $meta = json_decode($infos['meta']);
  24. $infos['link'] = isset($meta->link) ? $meta->link : 'index.php?module=notification#'.$infos['id'];
  25. // if(isset($_['synthesis']))
  26. // $infos['html'] = truncate($infos['html'],80);
  27. $response['rows'][] = $infos;
  28. }
  29. // $query = 'SELECT COUNT(*) unread FROM {{table}} un LEFT JOIN '.Notification::tableName().' n ON un.notification=n.id WHERE un.user=? AND (n.start<? OR n.start IS NULL) AND (n.end>? OR n.end IS NULL) AND (un.read = 0) ORDER BY n.created DESC';
  30. // $result = UserNotification::staticQuery($query, array($myUser->login,time(),time()))->fetch();
  31. // $response['unread'] = $result['unread'] > 5 ? '5+' : $result['unread'];
  32. });
  33. break;
  34. //Active/Désactive l'état lu
  35. case 'notification_usernotification_toggle_read':
  36. Action::write(function(&$response){
  37. global $myUser,$_;
  38. require_once(__DIR__.SLASH.'UserNotification.class.php');
  39. $item = UserNotification::getById($_['id']);
  40. if($myUser->login != $item->user) throw new Exception("Permissions insuffisantes",403);
  41. $item->read = $item->read == true ? false : true;
  42. $item->save();
  43. $response['read'] = $item->read;
  44. });
  45. break;
  46. //Suppression d'élement usernotification
  47. case 'notification_usernotification_delete':
  48. Action::write(function(&$response){
  49. global $myUser,$_;
  50. require_once(__DIR__.SLASH.'UserNotification.class.php');
  51. $notification = UserNotification::getById($_['id']);
  52. if($myUser->login != $notification->user) throw new Exception("Permissions insuffisantes",403);
  53. UserNotification::deleteById($_['id']);
  54. });
  55. break;
  56. //Sauvegarde des préférences de notifications
  57. case 'notification_user_save_preference':
  58. Action::write(function(&$response){
  59. global $myUser,$_;
  60. if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour enregistrer vos préférences");
  61. if (trim($_['notification_send_mail']) != '' && !check_mail($_['notification_send_mail'])) throw new Exception("Format de l'adresse mail incorrect");
  62. $myUser->preference('notification_send_mail',$_['notification_send_mail']);
  63. if(!isset($_['categories'])) $_['categories'] = array();
  64. $myUser->preference('notification_categories',json_encode($_['categories']));
  65. });
  66. break;
  67. }
  68. ?>