Notification.class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Define a notification.
  4. * @author Valentin CARRUESCO
  5. * @category Plugin
  6. * @license MIT
  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. global $myFirm;
  75. $sendTypes = array();
  76. Plugin::callHook("notification_methods", array(&$sendTypes));
  77. $default = array(
  78. 'label' => "Notification sans titre",
  79. 'html' => "",
  80. 'type' => "notice",
  81. 'pinned' => 0,
  82. 'meta' => array(),
  83. 'start' => time(),
  84. 'end' => strtotime('+3month'),
  85. 'recipients' => array()
  86. );
  87. $infos = array_merge($default,$infos);
  88. $notification = new Notification();
  89. $notification->label = $infos['label'];
  90. $notification->html = $infos['html'];
  91. $notification->type = $infos['type'];
  92. $notification->pinned = $infos['pinned'];
  93. $notification->meta = json_encode($infos['meta']);
  94. $notification->end = $infos['end'];
  95. $notification->start = $infos['start'];
  96. $notification->save();
  97. $users = User::getAll();
  98. $users_rights = array();
  99. foreach ($users as $user) {
  100. if(!is_object($myFirm)) continue;
  101. $ranks = $user->getRanksId($myFirm->id);
  102. if (isset($ranks)) $users_rights[$user->login] = $ranks;
  103. }
  104. // Traitement des rangs
  105. $recipients = array();
  106. foreach ($infos['recipients'] as $recipient) {
  107. if (is_numeric($recipient)) {
  108. $users = array_keys(array_filter($users_rights, function($v) use ($recipient) {
  109. return in_array($recipient, $v);
  110. }));
  111. $recipients = array_merge($users, $recipients);
  112. } else {
  113. $recipients[] = $recipient;
  114. }
  115. }
  116. $recipients = array_unique($recipients);
  117. // pour chaque destinataire, vérification des préférences
  118. foreach ($recipients as $recipient) {
  119. //$userPreferences = Notification::getNotificationPreferences($recipient);
  120. $userPreferences = Notification::settingPreferences($recipient);
  121. // pour chaque type d'envoi
  122. foreach ($sendTypes as $sendType) {
  123. if(!isset($userPreferences[$infos['type']]) || !isset($userPreferences[$infos['type']][$infos['type'].'_'.$sendType['slug']]) || !$userPreferences[$infos['type']][$infos['type'].'_'.$sendType['slug']]) continue;
  124. $sendType['callback']($recipient, $infos,$notification);
  125. }
  126. }
  127. }
  128. public static function clear(){
  129. require_once(__DIR__.SLASH.'UserNotification.class.php');
  130. $toDelete = array();
  131. //on fetch pour economiser de la mémoire
  132. foreach (Notification::staticQuery('SELECT id FROM {{table}} WHERE end < ?',array(time()))->fetchAll() as $notification) {
  133. $toDelete[] = $notification['id'];
  134. }
  135. Notification::delete(array('id:IN'=>$toDelete));
  136. UserNotification::delete(array('notification:IN'=>$toDelete));
  137. }
  138. public static function settingPreferences($user, $userPreferences=array()){
  139. global $conf;
  140. require_once(__DIR__.SLASH.'UserNotification.class.php');
  141. // récupération préférences par défault
  142. $defaultPreferences = array();
  143. foreach(Notification::types() as $slug => $type){
  144. $defaultMethods = array();
  145. foreach ($type['default_methods'] as $method => $state)
  146. $defaultMethods[$slug.'_'.$method] = $state;
  147. $defaultPreferences[$slug] = $defaultMethods;
  148. }
  149. if(empty($userPreferences)){
  150. // récupération préférences utilisateur
  151. $preferences = UserPreference::loadAll(array('user'=>$user, 'key'=>'notification_categories'));
  152. $userPreferences = array();
  153. foreach($preferences as $preference)
  154. $userPreferences = json_decode($preference->value, JSON_OBJECT_AS_ARRAY);
  155. }
  156. $notificationPreferences = array();
  157. foreach ($defaultPreferences as $slug => $type){
  158. $tab = array();
  159. $notificationPreferences[$slug] = $defaultPreferences[$slug];
  160. foreach ($type as $method => $state){
  161. if(isset($userPreferences[$slug][$method])){
  162. $tab[$method] = $userPreferences[$slug][$method];
  163. unset($userPreferences[$slug][$method]);
  164. }else{
  165. $tab[$method] = $conf->get($method) ? $conf->get($method) : $defaultPreferences[$slug][$method];
  166. }
  167. }
  168. $notificationPreferences[$slug] = $tab;
  169. }
  170. foreach($userPreferences as $slug=>$types){
  171. if(!isset($notificationPreferences[$slug])) $notificationPreferences[$slug] = array();
  172. if(!is_array($types)) continue;
  173. $notificationPreferences[$slug] = array_merge($notificationPreferences[$slug],$types);
  174. }
  175. return $notificationPreferences;
  176. }
  177. }
  178. ?>