Notification.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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,$pinned;
  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. 'pinned' => 'int'
  21. );
  22. function __construct(){
  23. parent::__construct();
  24. $this->pinned = 0;
  25. }
  26. public static function types($key = null){
  27. global $conf;
  28. $types = array(
  29. 'notice' => array(
  30. 'label' =>'Information',
  31. 'color' =>'#007fdc',
  32. 'icon' =>'far fa-flag',
  33. 'description' =>"Notifications d'informations mineures",
  34. 'default_methods' => array(
  35. 'interface' => !empty($conf->get('notice_interface')) ? $conf->get('notice_interface') : true,
  36. 'mail' => !empty($conf->get('notice_mail')) ? $conf->get('notice_mail') : false
  37. )
  38. ),
  39. 'announcement' => array(
  40. 'label' =>'Annonce',
  41. 'color' =>'#30336b',
  42. 'icon' =>'fas fa-bullhorn',
  43. 'description' =>"Annonce générale",
  44. 'default_methods' => array(
  45. 'interface' => !empty($conf->get('announcement_interface')) ? $conf->get('announcement_interface') : true,
  46. 'mail' => !empty($conf->get('announcement_mail')) ? $conf->get('announcement_mail') : false
  47. )
  48. ),
  49. 'alert' => array(
  50. 'label' =>'Alerte',
  51. 'color' =>'#ff3100',
  52. 'icon' =>'fas fa-fire',
  53. 'description' =>"Notifications importantes ou critiques",
  54. 'default_methods' => array(
  55. 'interface' => !empty($conf->get('alert_interface')) ? $conf->get('alert_interface') : true,
  56. 'mail' => !empty($conf->get('alert_mail')) ? $conf->get('alert_mail') : false
  57. )
  58. )
  59. );
  60. Plugin::callHook('notification_type',array(&$types));
  61. return $key ? (isset($types[$key]) ? $types[$key] : null) : $types;
  62. }
  63. public static function categories(){
  64. $categories = array();
  65. foreach(Notification::types() as $slug => $type){
  66. $type['slug'] = $slug;
  67. if(!isset($type['category'])) $type['category'] = 'Général';
  68. if(!isset($categories[slugify($type['category'])])) $categories[slugify($type['category'])] = array('label'=>$type['category'],'items'=>array());
  69. $categories[slugify($type['category'])]['items'][] = $type;
  70. }
  71. return $categories;
  72. }
  73. public static function emit($infos){
  74. $sendTypes = array();
  75. Plugin::callHook("notification_methods", array(&$sendTypes));
  76. $default = array(
  77. 'label' => "Notification sans titre",
  78. 'html' => "",
  79. 'type' => "notice",
  80. 'pinned' => 0,
  81. 'meta' => array(),
  82. 'start' => time(),
  83. 'end' => strtotime('+3month'),
  84. 'recipients' => array()
  85. );
  86. $infos = array_merge($default,$infos);
  87. $notification = new Notification();
  88. $notification->label = $infos['label'];
  89. $notification->html = $infos['html'];
  90. $notification->type = $infos['type'];
  91. $notification->pinned = $infos['pinned'];
  92. $notification->meta = json_encode($infos['meta']);
  93. $notification->end = $infos['end'];
  94. $notification->start = $infos['start'];
  95. $notification->save();
  96. // pour chaque destinataire, vérification des préférences
  97. foreach ($infos['recipients'] as $recipient) {
  98. //$userPreferences = Notification::getNotificationPreferences($recipient);
  99. $userPreferences = Notification::settingPreferences($recipient);
  100. // pour chaque type d'envoi
  101. foreach ($sendTypes as $sendType) {
  102. if(!isset($userPreferences[$infos['type']]) || !isset($userPreferences[$infos['type']][$infos['type'].'_'.$sendType['slug']]) || !$userPreferences[$infos['type']][$infos['type'].'_'.$sendType['slug']]) continue;
  103. $sendType['callback']($recipient, $infos,$notification);
  104. }
  105. }
  106. }
  107. public static function clear(){
  108. foreach (Notification::loadAll(array('end:<'=>time())) as $notification) {
  109. UserNotification::delete(array('notification'=>$notification->id));
  110. Notification::deleteById($notification->id);
  111. }
  112. }
  113. public static function settingPreferences($user, $userPreferences=array()){
  114. global $conf;
  115. require_once(__DIR__.SLASH.'UserNotification.class.php');
  116. // récupération préférences par défault
  117. $defaultPreferences = array();
  118. foreach(Notification::types() as $slug => $type){
  119. $defaultMethods = array();
  120. foreach ($type['default_methods'] as $method => $state)
  121. $defaultMethods[$slug.'_'.$method] = $state;
  122. $defaultPreferences[$slug] = $defaultMethods;
  123. }
  124. if(empty($userPreferences)){
  125. // récupération préférences utilisateur
  126. $preferences = UserPreference::loadAll(array('user'=>$user, 'key'=>'notification_categories'));
  127. $userPreferences = array();
  128. foreach($preferences as $preference)
  129. $userPreferences = json_decode($preference->value, JSON_OBJECT_AS_ARRAY);
  130. }
  131. $notificationPreferences = array();
  132. foreach ($defaultPreferences as $slug => $type){
  133. $tab = array();
  134. $notificationPreferences[$slug] = $defaultPreferences[$slug];
  135. foreach ($type as $method => $state){
  136. if(isset($userPreferences[$slug][$method])){
  137. $tab[$method] = $userPreferences[$slug][$method];
  138. unset($userPreferences[$slug][$method]);
  139. }else{
  140. $tab[$method] = $conf->get($method) ? $conf->get($method) : $defaultPreferences[$slug][$method];
  141. }
  142. }
  143. $notificationPreferences[$slug] = $tab;
  144. }
  145. foreach($userPreferences as $slug=>$types){
  146. if(!isset($notificationPreferences[$slug])) $notificationPreferences[$slug] = array();
  147. if(!is_array($types)) continue;
  148. $notificationPreferences[$slug] = array_merge($notificationPreferences[$slug],$types);
  149. }
  150. return $notificationPreferences;
  151. }
  152. }
  153. ?>