From df1ee89672dc2af1af8842da8e693fb8a696418c Mon Sep 17 00:00:00 2001 From: Julio Date: Wed, 18 Dec 2019 09:39:27 +0100 Subject: [PATCH] Add event id + notification UI improvemenets BT#16386 --- main/inc/lib/NotificationEvent.php | 63 ++++++++++++++++++- main/install/configuration.dist.php | 1 + main/notification_event/add.php | 8 +-- main/notification_event/edit.php | 3 +- main/template/default/layout/notification.tpl | 57 +++++++++-------- plugin/justification/Justification.php | 2 +- 6 files changed, 99 insertions(+), 35 deletions(-) diff --git a/main/inc/lib/NotificationEvent.php b/main/inc/lib/NotificationEvent.php index f003e814ef..d85b2cac04 100644 --- a/main/inc/lib/NotificationEvent.php +++ b/main/inc/lib/NotificationEvent.php @@ -12,6 +12,7 @@ class NotificationEvent extends Model 'persistent', 'day_diff', 'event_type', + 'event_id', ]; const ACCOUNT_EXPIRATION = 1; @@ -43,15 +44,67 @@ class NotificationEvent extends Model ]; } - public function getForm(FormValidator $form) + public function getForm(FormValidator $form, $data = []) { + $options = $this->getEventsForSelect(); + $form->addSelect('event_type', get_lang('EventType'), $options); + $form->freeze('event_type'); + + $eventType = $data['event_type']; + switch ($eventType) { + case self::JUSTIFICATION_EXPIRATION: + $plugin = Justification::create(); + $list = $plugin->getList(); + $list = array_column($list, 'name', 'id'); + $form->addSelect('event_id', get_lang('JustificationType'), $list); + break; + default: + break; + } + $form->freeze('event_id'); + + $form->addText('title', get_lang('Title')); $form->addTextarea('content', get_lang('Content')); $form->addText('link', get_lang('Link'), false); $form->addCheckBox('persistent', get_lang('Persistent')); $form->addNumeric('day_diff', get_lang('DayDiff'), false); + + return $form; + } + + public function getAddForm(FormValidator $form) + { $options = $this->getEventsForSelect(); - $form->addSelect('event_type', get_lang('EventType'), $options); + $eventType = $form->getSubmitValue('event_type'); + + $form->addSelect( + 'event_type', + get_lang('EventType'), + $options, + ['placeholder' => get_lang('SelectAnOption'), 'onchange' => 'document.add.submit()'] + ); + + if (!empty($eventType)) { + $form->freeze('event_type'); + $form->addText('title', get_lang('Title')); + $form->addTextarea('content', get_lang('Content')); + $form->addText('link', get_lang('Link'), false); + $form->addCheckBox('persistent', get_lang('Persistent')); + $form->addNumeric('day_diff', get_lang('DayDiff'), false); + + switch ($eventType) { + case self::JUSTIFICATION_EXPIRATION: + $plugin = Justification::create(); + $list = $plugin->getList(); + $list = array_column($list, 'name', 'id'); + $form->addSelect('event_id', get_lang('JustificationType'), $list); + break; + default: + break; + } + $form->addButtonSave(get_lang('Save')); + } return $form; } @@ -80,6 +133,7 @@ class NotificationEvent extends Model foreach ($events as $event) { $days = (int) $event['day_diff']; $checkIsRead = $event['persistent'] == 0 ? true : false; + $eventItemId = $event['event_id']; switch ($event['event_type']) { case self::ACCOUNT_EXPIRATION: @@ -112,6 +166,11 @@ class NotificationEvent extends Model if (empty($userJustification['date_validity'])) { continue; } + + if ($eventItemId != $userJustification['justification_document_id']) { + continue; + } + $showNotification = $this->showNotification($userJustification['date_validity'], $days); $id = 'id_'.self::JUSTIFICATION_EXPIRATION.'_event_'.$event['id'].'_'.$userJustification['id']; diff --git a/main/install/configuration.dist.php b/main/install/configuration.dist.php index 33b4c3878c..ad887e652b 100755 --- a/main/install/configuration.dist.php +++ b/main/install/configuration.dist.php @@ -1351,6 +1351,7 @@ id INT unsigned NOT NULL auto_increment PRIMARY KEY, day_diff INT, event_type VARCHAR(255) ); +ALTER TABLE notification_event ADD COLUMN event_id INT NULL; */ // create new user text extra field called 'notification_event' to save the persistent settings. // $_configuration['notification_event'] = false; diff --git a/main/notification_event/add.php b/main/notification_event/add.php index e1248c9ff4..2ae4fd3c15 100644 --- a/main/notification_event/add.php +++ b/main/notification_event/add.php @@ -13,14 +13,12 @@ $fields = []; $manager = new NotificationEvent(); $form = new FormValidator('add'); -$form = $manager->getForm($form); +$form = $manager->getAddForm($form); -$form->addButtonSave(get_lang('Save')); - -if ($form->validate()) { +if (isset($_POST) && isset($_POST['title']) && $form->validate()) { $values = $form->getSubmitValues(); $manager->save($values); - Display::addFlash(get_lang('Saved')); + Display::addFlash(Display::return_message(get_lang('Saved'))); $url = api_get_path(WEB_CODE_PATH).'notification_event/list.php?'; header('Location: '.$url); exit; diff --git a/main/notification_event/edit.php b/main/notification_event/edit.php index 3b8b9d341d..5b807d3f29 100644 --- a/main/notification_event/edit.php +++ b/main/notification_event/edit.php @@ -16,12 +16,11 @@ if (empty($id)) { $manager = new NotificationEvent(); $notification = $manager->get($id); - $tpl = new Template($tool); $fields = []; $form = new FormValidator('edit', 'post', api_get_self().'?id='.$id); -$form = $manager->getForm($form); +$form = $manager->getForm($form, $notification); $form->setDefaults($notification); $form->addButtonSave(get_lang('Update')); diff --git a/main/template/default/layout/notification.tpl b/main/template/default/layout/notification.tpl index 3c06f32f34..68f6fe5ef1 100644 --- a/main/template/default/layout/notification.tpl +++ b/main/template/default/layout/notification.tpl @@ -161,6 +161,34 @@ appNotifications.badgeLoadingMask(false); }); }, + loadNotificationArray: function () { + $('#notificationsContainer').html(""); + + var closeLink = '
'; + for (i = 0; i < count; i++) { + if (notifications[i]) { + var template = $('#notificationTemplate').html(); + template = template.replace("{id}", notifications[i].id); + template = template.replace("{link}", notifications[i].link); + template = template.replace("{title}", notifications[i].title); + template = template.replace("{content}", notifications[i].content); + template = template.replace("{event_text}", notifications[i].event_text); + + if (notifications[i].persistent == 1) { + template = template.replace("{close_link}", ''); + } else { + template = template.replace("{close_link}", closeLink); + } + $('#notificationsContainer').append(template); + } + } + + $('.notification-read').on('click', function (event) { + appNotifications.markAsRead(event, $(this)); + }); + appNotifications.loadingMask(false); + $("#notifications-dropdown").prop("disabled", false); + }, load: function () { appNotifications.loadingMask(true); $('#notificationsContainer').html(""); @@ -188,30 +216,7 @@ }); setTimeout(function () { - var closeLink = '
'; - for (i = 0; i < count; i++) { - if (notifications[i]) { - var template = $('#notificationTemplate').html(); - template = template.replace("{id}", notifications[i].id); - template = template.replace("{link}", notifications[i].link); - template = template.replace("{title}", notifications[i].title); - template = template.replace("{content}", notifications[i].content); - template = template.replace("{event_text}", notifications[i].event_text); - - if (notifications[i].persistent == 1) { - template = template.replace("{close_link}", ''); - } else { - template = template.replace("{close_link}", closeLink); - } - $('#notificationsContainer').append(template); - } - } - - $('.notification-read').on('click', function (event) { - appNotifications.markAsRead(event, $(this)); - }); - appNotifications.loadingMask(false); - $("#notifications-dropdown").prop("disabled", false); + appNotifications.loadNotificationArray(); }, 1000); }, markAsRead: function (event, elem) { @@ -227,6 +232,7 @@ $.ajax({ url: '{{ _p.web_main }}inc/ajax/message.ajax.php?a=mark_notification_as_read&id='+notificationId, success: function (data) { + console.log(notifications); notifications = $.grep(notifications, function(value) { if (notificationId == value.id) { return false; @@ -238,7 +244,8 @@ count--; console.log('count : ' + count); - appNotifications.loadAll(); + //appNotifications.loadAll(); + appNotifications.loadNotificationArray(); } }); }, diff --git a/plugin/justification/Justification.php b/plugin/justification/Justification.php index 0699aec485..8f22041200 100644 --- a/plugin/justification/Justification.php +++ b/plugin/justification/Justification.php @@ -57,7 +57,7 @@ class Justification extends Plugin public function getList() { - $sql = 'SELECT * FROM justification_document '; + $sql = 'SELECT * FROM justification_document ORDER BY name '; $query = Database::query($sql); return Database::store_result($query, 'ASSOC');