Notification.class.php 4.1 KB

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