notification.plugin.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 $_;
  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.core.notification') return;
  40. Entity::install(__DIR__);
  41. require_once(__DIR__.SLASH.'UserNotification.class.php');
  42. require_once(__DIR__.SLASH.'Notification.class.php');
  43. $recipients = array();
  44. foreach (User::getAll(array('right'=>false)) as $user) {
  45. $recipients[] = $user->login;
  46. }
  47. // envoi premiere notif
  48. Notification::emit(
  49. array(
  50. 'label' => "Nouvelle fonctionnalité de notification !",
  51. 'html' => "Une nouvelle <strong>Notification</strong>
  52. à é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>",
  53. 'meta' => array(
  54. 'link' => ROOT_URL.'/index.php?module=notification',
  55. ),
  56. 'recipients' => $recipients
  57. )
  58. );
  59. }
  60. //Fonction executée lors de la désactivation du plugin
  61. function notification_uninstall($id){
  62. if($id != 'fr.core.notification') return;
  63. Entity::uninstall(__DIR__);
  64. }
  65. //Déclaration des sections de droits du plugin
  66. //Déclaration des sections de droits du plugin
  67. Right::register('notification',array('label'=>'Gestion des droits sur le plugin notification'));
  68. //Comprends toutes les actions du plugin qui ne nécessitent pas de vue html
  69. require_once(__DIR__.SLASH.'action.php');
  70. //Déclaration du menu de réglages
  71. function notification_menu_setting(&$settingMenu){
  72. global $myUser;
  73. if(!$myUser->can('notification','configure')) return;
  74. $settingMenu[]= array(
  75. 'sort' =>1,
  76. 'url' => 'setting.php?section=notification',
  77. 'icon' => 'fas fa-angle-right',
  78. 'label' => 'Notifications'
  79. );
  80. }
  81. function notification_menu_account(&$accountMenu){
  82. global $myUser;
  83. if(!$myUser->connected() || !$myUser->can('notification', 'read')) return;
  84. $accountMenu[]= array(
  85. 'sort' => 1,
  86. 'url' => 'account.php?section=notification',
  87. 'icon' => 'fas fa-angle-right',
  88. 'label' => 'Notifications',
  89. );
  90. }
  91. //Déclaration des pages de réglages
  92. function notification_content_setting(){
  93. global $_;
  94. if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php'))
  95. require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php');
  96. }
  97. //Déclaration des pages de réglages
  98. function notification_content_account(){
  99. global $_;
  100. if(file_exists(__DIR__.SLASH.'account.'.$_['section'].'.php'))
  101. require_once(__DIR__.SLASH.'account.'.$_['section'].'.php');
  102. }
  103. //Émission d'une notification
  104. function notification_emit_notification($params){
  105. require_once(__DIR__.SLASH.'Notification.class.php');
  106. Notification::emit($params);
  107. }
  108. function notification_methods(&$sendTypes){
  109. require_once(__DIR__.SLASH.'Notification.class.php');
  110. require_once(__DIR__.SLASH.'UserNotification.class.php');
  111. $sendTypes[] = array(
  112. 'slug' => 'mail',
  113. 'label' => 'Mail',
  114. 'explain' => 'Envoi par e-mail',
  115. 'icon' => 'far fa-envelope-open',
  116. 'callback' => function($recipient, $infos, $notification){
  117. if($infos['pinned'] == 1) return;
  118. if(!isset($recipient)) throw new Exception("Aucun destinataire n'a été renseigné pour l'envoi de mail");
  119. // ENVOI MAIL
  120. global $conf;
  121. $expeditor = $conf->get('notification_mail_from') != '' ? $conf->get('notification_mail_from'): 'Erp <no-reply@core.fr>';
  122. $reply = $conf->get('notification_mail_reply') != '' ? $conf->get('notification_mail_reply'): 'no-reply@core.fr';
  123. $mail = new Mail();
  124. $mail->expeditor = $expeditor;
  125. $mail->reply = $reply;
  126. $mail->title = $infos['label'];
  127. $mail->message = '<h3>'.$infos['label'].'</h3><p>'.$infos['html'].'</p>';
  128. $mail->message .= (isset($infos['meta']['link'])) ? '<a href="'.$infos['meta']['link'].'">Accéder à l\'outil</a>' : '';
  129. $usermail = User::byLogin($recipient);
  130. $mail->recipients['to'][] = $usermail->mail;
  131. $mail->send();
  132. }
  133. );
  134. $sendTypes[] = array(
  135. 'slug' => 'interface',
  136. 'label' => 'Interface',
  137. 'explain' => 'Visualisation via l\'ERP',
  138. 'icon' => 'far fa-bell',
  139. 'callback' => function($recipient, $infos, $notification){
  140. if($infos['pinned'] == 1) return;
  141. if(!isset($recipient))
  142. throw new Exception("Aucun destinataires n\'a été renseignés pour l\'envoi de notifications à travers l\'ERP");
  143. $userNotification = new UserNotification();
  144. $userNotification->notification = $notification->id;
  145. $userNotification->user = $recipient;
  146. $userNotification->read = 0;
  147. $userNotification->save();
  148. }
  149. );
  150. }
  151. function notification_cron($time){
  152. global $_;
  153. if(date('H:i', $time)!= '03:00' && !isset($_['force-notification'])) return;
  154. Plugin::need('notification/Notification');
  155. Notification::clear();
  156. }
  157. /*
  158. function notification_widget(&$widgets){
  159. Plugin::need('dashboard/DashboardWidget');
  160. $modelWidget = new DashboardWidget();
  161. $modelWidget->model = 'notification';
  162. $modelWidget->title = 'Notification';
  163. $modelWidget->options[] = array('function'=>'window.location = \'index.php?module=notification\';','icon'=>'fa-eye','title'=>'Toutes les notifications');
  164. $modelWidget->icon = 'fas fa-bell';
  165. $modelWidget->background = '#ffbe5c';
  166. $modelWidget->load = 'action.php?action=notification_widget_load';
  167. $modelWidget->configure = 'action.php?action=notification_widget_configure';
  168. $modelWidget->configure_callback = 'notification_widget_configure_save';
  169. $modelWidget->js = [Plugin::url().'/js/widget.js?v='.time()];
  170. $modelWidget->css = [Plugin::url().'/css/widget.css?v=1'.time()];
  171. $modelWidget->description = "Affiche les dernières alertes des catégories de notification sélectionnées";
  172. $widgets[] = $modelWidget;
  173. }
  174. */
  175. Plugin::addHook('widget',function(&$models){
  176. $model = new DashboardWidget();
  177. $model->icon = 'fas fa-bell';
  178. $model->headerBackground = '#ffbe5c';
  179. $model->description = 'Affiche un menu sélectionné';
  180. $model->label = 'Notification';
  181. $model->width = 3;
  182. $model->height = 3;
  183. $model->model = 'notification';
  184. $model->css = array(__DIR__.'/css/widget.css?v='.time());
  185. $model->js = array(__DIR__.'/js/widget.js?v='.time());
  186. $model->content = function(&$widget){
  187. global $myUser;
  188. User::check_access('notification','read');
  189. Plugin::need('dashboard/DashboardWidget');
  190. $widget->title = 'Dernières alertes';
  191. if(empty($widget->meta['label'])) $widget->meta['label'] = 'Bloc Menu';
  192. if(empty($widget->meta['url'])) $widget->meta['url'] = '';
  193. if(empty($widget->meta['color'])) $widget->meta['color'] = '#007bff';
  194. ob_start();
  195. require_once(__DIR__.SLASH.'Notification.class.php');
  196. require_once(__DIR__.SLASH.'UserNotification.class.php');
  197. $types = isset($widget->meta['types'])? explode(',',$widget->meta['types']) :array('announcement');
  198. $max = isset($widget->meta['notification-number'])?$widget->meta['notification-number']:5;
  199. $maxlength = isset($widget->meta['notification-length'])?$widget->meta['notification-length']:250 ;
  200. $userNotifications = UserNotification::byUser($myUser,array(
  201. 'limit' => $max,
  202. 'unread' => false,
  203. 'types' => $types,
  204. ));
  205. ?>
  206. <div class="notification-container w-100">
  207. <h5 class="text-uppercase text-center"><i class="far fa-bell"></i> Alertes</h5>
  208. <ul class="notification-widget-list">
  209. <?php foreach($userNotifications as $infos):
  210. $infos['created-relative'] = isset($_['synthesis']) ? relative_time($infos['created'], null, 7) : relative_time($infos['created'], null, 7, true);
  211. $meta = json_decode($infos['meta']);
  212. $infos['link'] = 'index.php?module=notification#'.$infos['id'];
  213. $type = Notification::types($infos['type']);
  214. ?>
  215. <li>
  216. <h5 onclick="document.location.href='<?php echo $infos['link']; ?>';" class="pointer mb-0 mt-1" style="color: <?php echo $type['color']; ?>;">
  217. <div><i title="<?php echo $type['label'] ?>" class="<?php echo $type['icon'] ?>"></i> <?php echo $infos['label']; ?></div>
  218. <small class="text-muted d-block"><i class="far fa-clock"></i> <?php echo $infos['created-relative']; ?></small>
  219. </h5>
  220. <p class=""><?php echo truncate($infos['html'],$maxlength,array('html'=>true)); ?></p>
  221. <a class="btn btn-link d-block w-100 text-center" href="<?php echo $infos['link']; ?>">+ d'informations</a>
  222. </li>
  223. <?php endforeach; ?>
  224. </ul>
  225. </div>
  226. <?php
  227. $widget->content = ob_get_clean();
  228. };
  229. $model->configure = function($widget){
  230. global $myUser;
  231. User::check_access('notification','read');
  232. require_once(__DIR__.SLASH.'Notification.class.php');
  233. ob_start();
  234. $selectedTypes = isset($widget->meta['types'])? explode(',',$widget->meta['types']) :array('announcement');
  235. $max = isset($widget->meta['notification-number'])?$widget->meta['notification-number']:5;
  236. $maxlength = isset($widget->meta['notification-length'])?$widget->meta['notification-length']:250 ;
  237. ?>
  238. <div id="navigation-widget-form">
  239. <div id="notification-widget-form">
  240. <div class="row">
  241. <div class="col-md-6 col-sm-12">
  242. <label for="notification-number">Nb de notifications affichées :</label>
  243. <input type="number" class="form-control mb-2" value="<?php echo $max; ?>" id="notification-number">
  244. </div>
  245. <div class="col-md-6 col-sm-12">
  246. <label for="notification-length">Nb de caractères affichés :</label>
  247. <input type="number" class="form-control mb-2" value="<?php echo $maxlength; ?>" id="notification-length">
  248. </div>
  249. <div class="col-md-12 col-sm-12">
  250. <label for="">Types de notifications à afficher :</label>
  251. <ul class="list-group" id="widget-notification-list">
  252. <?php foreach(Notification::types() as $slug=>$type): ?>
  253. <li class="list-group-item p-0">
  254. <label class="m-0 pointer p-2"><input type="checkbox" onchange="notification_widget_configure_save()"; <?php echo in_array($slug, $selectedTypes)?'checked="checked"':''; ?> value="<?php echo $slug; ?>" data-type="checkbox"><i class="<?php echo $type['icon']; ?>"></i> <?php echo $type['label']; ?> <small class="text-muted">- <?php echo $type['description']; ?></small></label>
  255. </li>
  256. <?php endforeach; ?>
  257. </ul>
  258. <input type="hidden" id="types" data-id="types" value="<?php echo implode(',',$selectedTypes);?>">
  259. </div>
  260. </div>
  261. </div>
  262. </div>
  263. <?php
  264. };
  265. $model->save = function($widget,$form){
  266. global $myUser,$_;
  267. User::check_access('notification','read');
  268. require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
  269. if(!isset($form['types'])) throw new Exception("Au moins un type de notification à afficher est requis");
  270. if(!isset($form['notification-number'])) $form['notification-number'] = 5;
  271. if(!isset($form['notification-length'])) $form['notification-length'] = 150;
  272. $widget->meta['types'] = $form['types'];
  273. $widget->meta['notification-number'] = $form['notification-number'];
  274. $widget->meta['notification-length'] = $form['notification-length'];
  275. $widget->save();
  276. $widget->save();
  277. };
  278. $models[$model->model] = $model;
  279. });
  280. //Déclaration des settings de base
  281. //Types possibles : text,select ( + "values"=> array('1'=>'Val 1'),password,checkbox. Un simple string définit une catégorie.
  282. Configuration::setting('notification',array(
  283. "E-mail",
  284. 'notification_mail_from' => array("label"=>"Envoi de la part de","legend"=>"Format: Nom &lt;email&gt;, (eg: MyFirm &lt;no-reply@kiss.fr&gt;)","type"=>"text",'placeholder'=>'eg: MyFirm <no-reply@kiss.fr>'),
  285. '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"),
  286. ));
  287. //Déclation des assets
  288. Plugin::addCss("/css/main.css");
  289. Plugin::addJs("/js/main.js");
  290. //Mapping hook / fonctions
  291. Plugin::addHook("install", "notification_install");
  292. Plugin::addHook("uninstall", "notification_uninstall");
  293. Plugin::addHook("login_header", "notification_menu");
  294. Plugin::addHook("page", "notification_page");
  295. Plugin::addHook("menu_account", "notification_menu_account");
  296. Plugin::addHook("content_account", "notification_content_account");
  297. Plugin::addHook("menu_setting", "notification_menu_setting");
  298. Plugin::addHook("content_setting", "notification_content_setting");
  299. Plugin::addHook("emit_notification", "notification_emit_notification");
  300. Plugin::addHook("notification_methods", "notification_methods");
  301. Plugin::addHook("cron", "notification_cron");
  302. ?>