Notification.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Define a notification.
  4. * @author Valentin CARRUESCO
  5. * @category Plugin
  6. * @license copyright
  7. */
  8. class Notification extends Entity{
  9. public $id,$label,$html,$type,$meta,$end,$start;
  10. protected $TABLE_NAME = 'notification';
  11. public $fields =
  12. array(
  13. 'id' => 'key',
  14. 'label' => 'string',
  15. 'html' => 'longstring',
  16. 'type' => 'string',
  17. 'meta' => 'longstring',
  18. 'start' => 'date',
  19. 'end' => 'date'
  20. );
  21. public static function types($key = null){
  22. $types = array(
  23. 'notice' => array(
  24. 'label' =>'Information',
  25. 'color' =>'#007fdc',
  26. 'icon' =>'far fa-flag',
  27. 'description' =>"Notifications d'informations mineures",
  28. ),
  29. 'alert' => array(
  30. 'label' =>'Alerte',
  31. 'color' =>'#ff3100',
  32. 'icon' =>'fas fa-fire',
  33. 'description' =>"Notifications importantes ou critiques",
  34. )
  35. );
  36. Plugin::callHook('notification_type',array(&$types));
  37. return $key ? $types[$key] : $types;
  38. }
  39. public static function emit($infos,$recipients = array()){
  40. require_once(__DIR__.SLASH.'UserNotification.class.php');
  41. //Nettoyage des notifications périmées
  42. self::clear();
  43. $default = array(
  44. 'label' => "Notification sans titre",
  45. 'html' => "",
  46. 'type' => "notice",
  47. 'meta' => array(),
  48. 'start' => time(),
  49. 'end' => strtotime('+3month')
  50. );
  51. $infos = array_merge($default,$infos);
  52. $notification = new Notification();
  53. $notification->label = $infos['label'];
  54. $notification->html = $infos['html'];
  55. $notification->type = $infos['type'];
  56. $notification->meta = json_encode($infos['meta']);
  57. $notification->end = $infos['end'];
  58. $notification->start = $infos['start'];
  59. $notification->save();
  60. foreach($recipients as $recipient) {
  61. $userNotification = new UserNotification();
  62. $userNotification->notification = $notification->id;
  63. $userNotification->user = $recipient;
  64. $userNotification->read = 0;
  65. $userNotification->save();
  66. }
  67. $preferences = array();
  68. foreach(UserPreference::staticQuery("SELECT * FROM {{table}} up WHERE user IN ('".implode("','",$recipients)."') AND up.key IN('notification_send_mail','notification_categories')",array(),true) as $row){
  69. if(!isset($preferences[$row->user])) $preferences[$row->user] = array();
  70. $preferences[$row->user][$row->key] = $row->value;
  71. }
  72. foreach($preferences as $user => $preference){
  73. if(trim($preference['notification_send_mail']) == '') continue;
  74. if(trim($preference['notification_categories']) == '') continue;
  75. $categories = json_decode($preference['notification_categories'],true);
  76. if(!is_array($categories) || !in_array($notification->type, $categories)) continue;
  77. $mail = new Mail();
  78. $mail->expeditor = 'IdleCorp <no-reply@idleman.fr>';
  79. $mail->title = $notification->label;
  80. $mail->recipients['to'][] = $preference['notification_send_mail'];
  81. $mail->message = $notification->html;
  82. $mail->message .= '<br><a href="'.ROOT_URL.'/index.php?module=notification#'.$userNotification->id.'">Voir la notification</a>';
  83. $mail->send();
  84. }
  85. }
  86. public static function clear(){
  87. foreach (Notification::loadAll(array('end:<'=>time())) as $notification) {
  88. UserNotification::delete(array('notification'=>$notification->id));
  89. Notification::deleteById($notification->id);
  90. }
  91. }
  92. }
  93. ?>