notification.plugin.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. //Déclaration d'un item de menu dans le menu principal
  3. function notification_menu(){
  4. global $_,$myUser;
  5. if(!$myUser->can('notification','read')) return;
  6. ?>
  7. <div class="dropdown notification_menu ml-auto">
  8. <button class="btn btn-dark dropdown-toggle" type="button" id="dropdownNotification" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  9. <span class="badge badge-pill badge-danger notification-number hidden">-</span>
  10. <i class="fas fa-bell"></i>
  11. </button>
  12. <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownNotification"></div>
  13. </div>
  14. <div class="notification-template">
  15. <div data-id="{{id}}" class="dropdown-item notification-{{class}} notification-item" onclick="notification_user_notification_toggle_read(this,event,'.dropdown-item'); window.location='{{link}}';">
  16. <ul class="notification-item-options">
  17. <li onclick="notification_user_notification_toggle_read(this,event,'.dropdown-item')" title="Marquer comme lu" class="notification-read"><i class="fas fa-eye"></i></li>
  18. <li onclick="notification_user_notification_delete(this,event,'.dropdown-item')" title="Supprimer la notification" class="notification-delete btn-delete"><i class="fas fa-times"></i></li>
  19. </ul>
  20. <small>{{created-relative}}</small>
  21. <strong class="mt-2">{{label}}</strong>
  22. <p>{{{html}}}</p>
  23. </div>
  24. <div class="dropdown-divider" data-notification="{{id}}"></div>
  25. </div>
  26. <?php
  27. }
  28. //Cette fonction va generer une page quand on clique sur notification dans menu
  29. function notification_page(){
  30. global $_,$myUser;
  31. if(!isset($_['module']) || $_['module'] !='notification') return;
  32. $page = !isset($_['page']) ? 'list' : $_['page'];
  33. $file = __DIR__.SLASH.'page.'.$page.'.php';
  34. if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
  35. require_once($file);
  36. }
  37. //Fonction executée lors de l'activation du plugin
  38. function notification_install($id){
  39. if($id != 'fr.idleman.notification') return;
  40. Entity::install(__DIR__);
  41. global $myUser,$_;
  42. require_once(__DIR__.SLASH.'UserNotification.class.php');
  43. require_once(__DIR__.SLASH.'Notification.class.php');
  44. $recipients = array();
  45. foreach (User::getAll() as $user) {
  46. $recipients[] = $user->login;
  47. }
  48. // envoi premiere notif
  49. Notification::emit(
  50. array(
  51. 'label' => "Nouvelle fonctionnalité de notification !",
  52. 'html' => "Une nouvelle <strong>Notification</strong>
  53. à été ajoutée à votre espace.<br/> Vous pouvez consulter toutes vos notifications <a href='".ROOT_URL."/index.php?module=notification'>ici</a> et configurer vos <a href='".ROOT_URL."/account.php?section=notification'>préférences la.</a>",
  54. 'meta' => array(
  55. 'link' => ROOT_URL.'/index.php?module=notification',
  56. ),
  57. 'recipients' => $recipients
  58. )
  59. );
  60. }
  61. //Fonction executée lors de la désactivation du plugin
  62. function notification_uninstall($id){
  63. if($id != 'fr.idleman.notification') return;
  64. Entity::uninstall(__DIR__);
  65. }
  66. //Déclaration des sections de droits du plugin
  67. function notification_section(&$sections){
  68. $sections['notification'] = "Gestion des droits sur le plugin notification";
  69. }
  70. //Cette fonction comprends toutes les actions
  71. //du plugin qui ne nécessitent pas de vue html
  72. function notification_action(){
  73. require_once(__DIR__.SLASH.'action.php');
  74. }
  75. //Déclaration du menu de réglages
  76. function notification_menu_setting(&$settingMenu){
  77. global $_, $myUser;
  78. if(!$myUser->can('notification','configure')) return;
  79. $settingMenu[]= array(
  80. 'sort' =>1,
  81. 'url' => 'setting.php?section=notification',
  82. 'icon' => 'fas fa-angle-right',
  83. 'label' => 'Notifications'
  84. );
  85. }
  86. function notification_menu_account(&$accountMenu){
  87. global $_, $myUser;
  88. if(!$myUser->connected() || !$myUser->can('notification', 'read')) return;
  89. $accountMenu[]= array(
  90. 'sort' =>0,
  91. 'url' => 'account.php?section=notification',
  92. 'icon' => 'fas fa-angle-right',
  93. 'label' => 'Notifications',
  94. );
  95. }
  96. //Déclaration des pages de réglages
  97. function notification_content_setting(){
  98. global $_;
  99. if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php'))
  100. require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php');
  101. }
  102. //Déclaration des pages de réglages
  103. function notification_content_account(){
  104. global $_;
  105. if(file_exists(__DIR__.SLASH.'account.'.$_['section'].'.php'))
  106. require_once(__DIR__.SLASH.'account.'.$_['section'].'.php');
  107. }
  108. //Émission d'une notification
  109. function notification_emit_notification($params){
  110. require_once(__DIR__.SLASH.'Notification.class.php');
  111. Notification::emit($params);
  112. }
  113. function notification_methods(&$sendTypes){
  114. require_once(__DIR__.SLASH.'Notification.class.php');
  115. require_once(__DIR__.SLASH.'UserNotification.class.php');
  116. $sendTypes[] = array(
  117. 'slug' => 'mail',
  118. 'label' => 'Mail',
  119. 'explain' => 'Envoi par e-mail',
  120. 'icon' => 'far fa-envelope-open',
  121. 'callback' => function($recipient, $infos, $notification){
  122. if($infos['pinned'] == 1) return;
  123. if(!isset($recipient)) throw new Exception("Aucun destinataires n\'a été renseignés pour l\'envoi de mail");
  124. // ENVOI MAIL
  125. $mail = new Mail();
  126. $mail->expeditor = 'Sys1 <no-reply@idleman.fr>';
  127. $mail->title = $infos['label'];
  128. $mail->message = '<h3>'.$infos['label'].'</h3><p>'.$infos['html'].'</p>';
  129. $mail->message .= (isset($infos['meta']['link'])) ? '<a href="'.$infos['meta']['link'].'">Accéder à l\'ERP</a>' : '';
  130. $usermail = User::byLogin($recipient);
  131. $mail->recipients['to'][] = $usermail->mail;
  132. /* /!\ ralentissements /!\ */
  133. $test = $mail->send();
  134. }
  135. );
  136. $sendTypes[] = array(
  137. 'slug' => 'interface',
  138. 'label' => 'Interface',
  139. 'explain' => 'Visualisation via l\'ERP',
  140. 'icon' => 'far fa-bell',
  141. 'callback' => function($recipient, $infos, $notification){
  142. if($infos['pinned'] == 1) return;
  143. if(!isset($recipient))
  144. throw new Exception("Aucun destinataires n\'a été renseignés pour l\'envoi de notifications à travers l\'ERP");
  145. $userNotification = new UserNotification();
  146. $userNotification->notification = $notification->id;
  147. $userNotification->user = $recipient;
  148. $userNotification->read = 0;
  149. $userNotification->save();
  150. }
  151. );
  152. }
  153. function notification_widget(&$widgets){
  154. global $myUser;
  155. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  156. $modelWidget = new DashboardWidget();
  157. $modelWidget->model = 'notification';
  158. $modelWidget->title = 'Notification';
  159. $modelWidget->options[] = array('function'=>'window.location = \'index.php?module=notification\';','icon'=>'fa-eye','title'=>'Toutes les notifications');
  160. $modelWidget->icon = 'fas fa-bell';
  161. $modelWidget->background = '#fa8231';
  162. $modelWidget->load = 'action.php?action=notification_widget_load';
  163. $modelWidget->configure = 'action.php?action=notification_widget_configure';
  164. $modelWidget->configure_callback = 'notification_widget_configure_save';
  165. $modelWidget->js = [Plugin::url().'/js/widget.js?v='.time()];
  166. $modelWidget->css = [Plugin::url().'/css/widget.css?v=1'.time()];
  167. $modelWidget->description = "Affiche les dernières alertes d'une catégorie donnée";
  168. $widgets[] = $modelWidget;
  169. }
  170. //Déclation des assets
  171. Plugin::addCss("/css/main.css");
  172. Plugin::addJs("/js/main.js");
  173. //Mapping hook / fonctions
  174. Plugin::addHook("widget", "notification_widget");
  175. Plugin::addHook("install", "notification_install");
  176. Plugin::addHook("uninstall", "notification_uninstall");
  177. Plugin::addHook("section", "notification_section");
  178. Plugin::addHook("login_header", "notification_menu");
  179. Plugin::addHook("page", "notification_page");
  180. Plugin::addHook("action", "notification_action");
  181. Plugin::addHook("menu_account", "notification_menu_account");
  182. Plugin::addHook("content_account", "notification_content_account");
  183. Plugin::addHook("menu_setting", "notification_menu_setting");
  184. Plugin::addHook("content_setting", "notification_content_setting");
  185. Plugin::addHook("emit_notification", "notification_emit_notification");
  186. Plugin::addHook("notification_methods", "notification_methods");
  187. ?>