notification.plugin.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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="if($(this).hasClass('notification-unread'))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.sys1.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(false) 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.sys1.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' => 1,
  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. global $conf;
  126. $expeditor = $conf->get('notification_mail_from') != '' ? $conf->get('notification_mail_from'): 'Sys1 <no-reply@sys1.fr>';
  127. $reply = $conf->get('notification_mail_reply') != '' ? $conf->get('notification_mail_reply'): 'no-reply@sys1.fr';
  128. $mail = new Mail();
  129. $mail->expeditor = $expeditor;
  130. $mail->reply = $reply;
  131. $mail->title = $infos['label'];
  132. $mail->message = '<h3>'.$infos['label'].'</h3><p>'.$infos['html'].'</p>';
  133. $mail->message .= (isset($infos['meta']['link'])) ? '<a href="'.$infos['meta']['link'].'">Accéder à l\'outil</a>' : '';
  134. $usermail = User::byLogin($recipient);
  135. $mail->recipients['to'][] = $usermail->mail;
  136. $mail->send();
  137. }
  138. );
  139. $sendTypes[] = array(
  140. 'slug' => 'interface',
  141. 'label' => 'Interface',
  142. 'explain' => 'Visualisation via l\'ERP',
  143. 'icon' => 'far fa-bell',
  144. 'callback' => function($recipient, $infos, $notification){
  145. if($infos['pinned'] == 1) return;
  146. if(!isset($recipient))
  147. throw new Exception("Aucun destinataires n\'a été renseignés pour l\'envoi de notifications à travers l\'ERP");
  148. $userNotification = new UserNotification();
  149. $userNotification->notification = $notification->id;
  150. $userNotification->user = $recipient;
  151. $userNotification->read = 0;
  152. $userNotification->save();
  153. }
  154. );
  155. }
  156. function notification_widget(&$widgets){
  157. global $myUser;
  158. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  159. $modelWidget = new DashboardWidget();
  160. $modelWidget->model = 'notification';
  161. $modelWidget->title = 'Notification';
  162. $modelWidget->options[] = array('function'=>'window.location = \'index.php?module=notification\';','icon'=>'fa-eye','title'=>'Toutes les notifications');
  163. $modelWidget->icon = 'fas fa-bell';
  164. $modelWidget->background = '#ffbe5c';
  165. $modelWidget->load = 'action.php?action=notification_widget_load';
  166. $modelWidget->configure = 'action.php?action=notification_widget_configure';
  167. $modelWidget->configure_callback = 'notification_widget_configure_save';
  168. $modelWidget->js = [Plugin::url().'/js/widget.js?v='.time()];
  169. $modelWidget->css = [Plugin::url().'/css/widget.css?v=1'.time()];
  170. $modelWidget->description = "Affiche les dernières alertes des catégories de notification sélectionnées";
  171. $widgets[] = $modelWidget;
  172. }
  173. //Déclaration des settings de base
  174. //Types possibles : text,select ( + "values"=> array('1'=>'Val 1'),password,checkbox. Un simple string définit une catégorie.
  175. Configuration::setting('notification',array(
  176. "E-mail",
  177. 'notification_mail_from' => array("label"=>"Envoi de la part de","legend"=>"Format: Nom &lt;email&gt;, (eg: Sys1 &lt;no-reply@sys1.fr&gt;)","type"=>"text",'placeholder'=>'eg: Sys1 <no-reply@sys1.fr>'),
  178. 'notification_mail_reply' => array("label"=>"Réponse au mail","legend"=>"Adresse e-mail unique (eg: me@domain.com)","type"=>"text", 'placeholder'=>"eg: me@domain.com"),
  179. ));
  180. //Déclation des assets
  181. Plugin::addCss("/css/main.css");
  182. Plugin::addJs("/js/main.js");
  183. //Mapping hook / fonctions
  184. Plugin::addHook("widget", "notification_widget");
  185. Plugin::addHook("install", "notification_install");
  186. Plugin::addHook("uninstall", "notification_uninstall");
  187. Plugin::addHook("section", "notification_section");
  188. Plugin::addHook("login_header", "notification_menu");
  189. Plugin::addHook("page", "notification_page");
  190. Plugin::addHook("action", "notification_action");
  191. Plugin::addHook("menu_account", "notification_menu_account");
  192. Plugin::addHook("content_account", "notification_content_account");
  193. Plugin::addHook("menu_setting", "notification_menu_setting");
  194. Plugin::addHook("content_setting", "notification_content_setting");
  195. Plugin::addHook("emit_notification", "notification_emit_notification");
  196. Plugin::addHook("notification_methods", "notification_methods");
  197. ?>