From 00c1ae616fea884db071c5f96bd42155277674e7 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Tue, 25 Oct 2022 14:38:16 +0200 Subject: [PATCH] new: [inboxes] Added `severity` level and `message` and removed `description` column --- .../20221025000000_inbox_severity.php | 45 +++++++++++++++++++ .../InboxProcessors/GenericInboxProcessor.php | 9 +++- .../GenericOutboxProcessor.php | 1 - src/Controller/InboxController.php | 4 +- src/Controller/OutboxController.php | 4 +- src/Model/Entity/AppModel.php | 7 +++ src/Model/Entity/Inbox.php | 7 ++- src/Model/Entity/Outbox.php | 7 ++- src/Model/Table/InboxTable.php | 32 ++++++++----- src/Model/Table/OutboxTable.php | 5 +++ src/Utility/UI/Notification.php | 4 +- templates/Inbox/index.php | 23 ++++++---- templates/Inbox/view.php | 4 +- templates/Outbox/index.php | 23 ++++++---- templates/Outbox/view.php | 4 +- 15 files changed, 137 insertions(+), 42 deletions(-) create mode 100644 config/Migrations/20221025000000_inbox_severity.php diff --git a/config/Migrations/20221025000000_inbox_severity.php b/config/Migrations/20221025000000_inbox_severity.php new file mode 100644 index 0000000..d452856 --- /dev/null +++ b/config/Migrations/20221025000000_inbox_severity.php @@ -0,0 +1,45 @@ +table('inbox')->hasColumn('severity'); + if (!$exists) { + $this->table('inbox') + ->addColumn('severity', 'integer', [ + 'null' => false, + 'default' => 0, + 'signed' => false, + 'length' => 10, + ]) + ->renameColumn('comment', 'message') + ->removeColumn('description') + ->update(); + $this->table('outbox') + ->addColumn('severity', 'integer', [ + 'null' => false, + 'default' => 0, + 'signed' => false, + 'length' => 10, + ]) + ->renameColumn('comment', 'message') + ->removeColumn('description') + ->update(); + } + } +} diff --git a/libraries/default/InboxProcessors/GenericInboxProcessor.php b/libraries/default/InboxProcessors/GenericInboxProcessor.php index a212fd6..fd2e09d 100644 --- a/libraries/default/InboxProcessors/GenericInboxProcessor.php +++ b/libraries/default/InboxProcessors/GenericInboxProcessor.php @@ -19,9 +19,12 @@ class GenericInboxProcessor protected $validator; protected $processingTemplate = '/genericTemplates/confirm'; protected $processingTemplatesDirectory = ROOT . '/libraries/default/InboxProcessors/templates'; + protected $defaultSeverity; + protected $severity; public function __construct($registerActions=false) { $this->Inbox = TableRegistry::getTableLocator()->get('Inbox'); + $this->defaultSeverity = $this->Inbox::SEVERITY_INFO; if ($registerActions) { $this->registerActionInProcessor(); } @@ -54,6 +57,10 @@ class GenericInboxProcessor { return $this->description ?? ''; } + public function getSeverity() + { + return $this->severity ?? $this->defaultSeverity; + } protected function getProcessingTemplatePath() { @@ -192,7 +199,7 @@ class GenericInboxProcessor { $requestData['scope'] = $this->scope; $requestData['action'] = $this->action; - $requestData['description'] = $this->description; + $requestData['severity'] = $this->getSeverity(); $request = $this->generateRequest($requestData); $savedRequest = $this->Inbox->createEntry($request); return $this->genActionResult( diff --git a/libraries/default/OutboxProcessors/GenericOutboxProcessor.php b/libraries/default/OutboxProcessors/GenericOutboxProcessor.php index c230465..98df5f3 100644 --- a/libraries/default/OutboxProcessors/GenericOutboxProcessor.php +++ b/libraries/default/OutboxProcessors/GenericOutboxProcessor.php @@ -193,7 +193,6 @@ class GenericOutboxProcessor $user_id = Router::getRequest()->getSession()->read('Auth.id'); $requestData['scope'] = $this->scope; $requestData['action'] = $this->action; - $requestData['description'] = $this->description; $requestData['user_id'] = $user_id; $request = $this->generateRequest($requestData); $savedRequest = $this->Outbox->createEntry($request); diff --git a/src/Controller/InboxController.php b/src/Controller/InboxController.php index 715d060..39f906a 100644 --- a/src/Controller/InboxController.php +++ b/src/Controller/InboxController.php @@ -16,8 +16,8 @@ use Cake\Http\Exception\ForbiddenException; class InboxController extends AppController { - public $filterFields = ['scope', 'action', 'title', 'origin', 'comment']; - public $quickFilterFields = ['scope', 'action', ['title' => true], ['comment' => true]]; + public $filterFields = ['scope', 'action', 'title', 'origin', 'message']; + public $quickFilterFields = ['scope', 'action', ['title' => true], ['message' => true]]; public $containFields = ['Users']; public $paginate = [ diff --git a/src/Controller/OutboxController.php b/src/Controller/OutboxController.php index 90db5ba..ad0033c 100644 --- a/src/Controller/OutboxController.php +++ b/src/Controller/OutboxController.php @@ -16,8 +16,8 @@ use Cake\Http\Exception\ForbiddenException; class OutboxController extends AppController { - public $filterFields = ['scope', 'action', 'title', 'comment']; - public $quickFilterFields = ['scope', 'action', ['title' => true], ['comment' => true]]; + public $filterFields = ['scope', 'action', 'title', 'message']; + public $quickFilterFields = ['scope', 'action', ['title' => true], ['message' => true]]; public $containFields = ['Users']; public function beforeFilter(EventInterface $event) diff --git a/src/Model/Entity/AppModel.php b/src/Model/Entity/AppModel.php index a94e78a..bc9815d 100644 --- a/src/Model/Entity/AppModel.php +++ b/src/Model/Entity/AppModel.php @@ -2,7 +2,9 @@ namespace App\Model\Entity; +use App\Model\Table\AppTable; use Cake\ORM\Entity; +use Cake\ORM\TableRegistry; class AppModel extends Entity { @@ -33,6 +35,11 @@ class AppModel extends Entity return $this->_accessibleOnNew ?? []; } + public function table(): AppTable + { + return TableRegistry::get($this->getSource()); + } + public function rearrangeForAPI(): void { } diff --git a/src/Model/Entity/Inbox.php b/src/Model/Entity/Inbox.php index 820e29d..21a9b68 100644 --- a/src/Model/Entity/Inbox.php +++ b/src/Model/Entity/Inbox.php @@ -7,7 +7,7 @@ use Cake\ORM\Entity; class Inbox extends AppModel { - protected $_virtual = ['local_tool_connector_name']; + protected $_virtual = ['local_tool_connector_name', 'severity_variant']; protected function _getLocalToolConnectorName() { @@ -17,4 +17,9 @@ class Inbox extends AppModel } return $localConnectorName; } + + protected function _getSeverityVariant(): string + { + return $this->table()->severityVariant[$this->severity]; + } } diff --git a/src/Model/Entity/Outbox.php b/src/Model/Entity/Outbox.php index 51304e6..98cb40a 100644 --- a/src/Model/Entity/Outbox.php +++ b/src/Model/Entity/Outbox.php @@ -7,5 +7,10 @@ use Cake\ORM\Entity; class Outbox extends AppModel { - + protected $_virtual = ['severity_variant']; + + protected function _getSeverityVariant(): string + { + return $this->table()->severityVariant[$this->severity]; + } } diff --git a/src/Model/Table/InboxTable.php b/src/Model/Table/InboxTable.php index 0617cea..a7626e7 100644 --- a/src/Model/Table/InboxTable.php +++ b/src/Model/Table/InboxTable.php @@ -8,6 +8,7 @@ use Cake\ORM\Table; use Cake\ORM\RulesChecker; use Cake\Validation\Validator; use Cake\Http\Exception\NotFoundException; +use Cake\ORM\ResultSet; use App\Utility\UI\Notification; @@ -15,6 +16,17 @@ Type::map('json', 'Cake\Database\Type\JsonType'); class InboxTable extends AppTable { + public const SEVERITY_PRIMARY = 0, + SEVERITY_INFO = 1, + SEVERITY_WARNING = 2, + SEVERITY_DANGER = 3; + + public $severityVariant = [ + self::SEVERITY_PRIMARY => 'primary', + self::SEVERITY_INFO => 'info', + self::SEVERITY_WARNING => 'warning', + self::SEVERITY_DANGER => 'danger', + ]; public function initialize(array $config): void { @@ -97,8 +109,8 @@ class InboxTable extends AppTable $allNotifications = []; $inboxNotifications = $this->getNotificationsForUser($user); foreach ($inboxNotifications as $notification) { - $title = __('New message'); - $details = $notification->title; + $title = $notification->title; + $details = $notification->message; $router = [ 'controller' => 'inbox', 'action' => 'process', @@ -109,7 +121,7 @@ class InboxTable extends AppTable 'icon' => 'envelope', 'details' => $details, 'datetime' => $notification->created, - 'variant' => 'warning', + 'variant' => $notification->severity_variant, '_useModal' => true, '_sidebarId' => 'inbox', ]))->get(); @@ -117,18 +129,14 @@ class InboxTable extends AppTable return $allNotifications; } - public function getNotificationsForUser(\App\Model\Entity\User $user): array + public function getNotificationsForUser(\App\Model\Entity\User $user): ResultSet { $query = $this->find(); - $conditions = []; - if ($user['role']['perm_admin']) { - // Admin will not see notifications if it doesn't belong to them. They can see process the message from the inbox - $conditions['Inbox.user_id IS'] = null; - } else { - $conditions['Inbox.user_id'] = $user->id; - } + $conditions = [ + 'Inbox.user_id' => $user->id + ]; $query->where($conditions); - $notifications = $query->all()->toArray(); + $notifications = $query->all(); return $notifications; } } diff --git a/src/Model/Table/OutboxTable.php b/src/Model/Table/OutboxTable.php index a78e0c6..eaf7358 100644 --- a/src/Model/Table/OutboxTable.php +++ b/src/Model/Table/OutboxTable.php @@ -8,11 +8,13 @@ use Cake\ORM\Table; use Cake\ORM\RulesChecker; use Cake\Validation\Validator; use Cake\Http\Exception\NotFoundException; +use Cake\ORM\TableRegistry; Type::map('json', 'Cake\Database\Type\JsonType'); class OutboxTable extends AppTable { + public $severityVariant; public function initialize(array $config): void { @@ -22,6 +24,9 @@ class OutboxTable extends AppTable $this->belongsTo('Users'); $this->addBehavior('AuditLog'); $this->setDisplayField('title'); + + $this->Inbox = TableRegistry::getTableLocator()->get('Inbox'); + $this->severityVariant = $this->Inbox->severityVariant; } protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaInterface diff --git a/src/Utility/UI/Notification.php b/src/Utility/UI/Notification.php index a6bf05b..b7e3f17 100644 --- a/src/Utility/UI/Notification.php +++ b/src/Utility/UI/Notification.php @@ -25,7 +25,7 @@ class Notification foreach ($options as $key => $value) { $this->{$key} = $value; } - $this->validate(); + $this->_validate(); } public function get(): array @@ -36,7 +36,7 @@ class Notification return null; } - private function validate() + protected function _validate() { $validator = new Validator(); diff --git a/templates/Inbox/index.php b/templates/Inbox/index.php index 07d8c2e..bee8244 100644 --- a/templates/Inbox/index.php +++ b/templates/Inbox/index.php @@ -53,6 +53,18 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'data_path' => 'created', 'element' => 'datetime' ], + [ + 'name' => 'severity', + 'sort' => 'severity', + 'data_path' => 'severity', + 'element' => 'function', + 'function' => function ($entry, $context) { + return $context->Bootstrap->badge([ + 'text' => $entry->severity_variant, + 'variant' => $entry->severity_variant, + ]); + } + ], [ 'name' => 'scope', 'sort' => 'scope', @@ -80,14 +92,9 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'element' => 'user' ], [ - 'name' => 'description', - 'sort' => 'description', - 'data_path' => 'description', - ], - [ - 'name' => 'comment', - 'sort' => 'comment', - 'data_path' => 'comment', + 'name' => 'message', + 'sort' => 'message', + 'data_path' => 'message', ], ], 'title' => __('Inbox'), diff --git a/templates/Inbox/view.php b/templates/Inbox/view.php index 36e0dfc..6ac08b0 100644 --- a/templates/Inbox/view.php +++ b/templates/Inbox/view.php @@ -33,8 +33,8 @@ echo $this->element( 'path' => 'user_id', ], [ - 'key' => 'description', - 'path' => 'description', + 'key' => 'message', + 'path' => 'message', ], [ 'key' => 'comment', diff --git a/templates/Outbox/index.php b/templates/Outbox/index.php index 3ea0bfc..028e2b1 100644 --- a/templates/Outbox/index.php +++ b/templates/Outbox/index.php @@ -49,6 +49,18 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'data_path' => 'created', 'element' => 'datetime' ], + [ + 'name' => 'severity', + 'sort' => 'severity', + 'data_path' => 'severity', + 'element' => 'function', + 'function' => function ($entry, $context) { + return $context->Bootstrap->badge([ + 'text' => $entry->severity_variant, + 'variant' => $entry->severity_variant, + ]); + } + ], [ 'name' => 'scope', 'sort' => 'scope', @@ -71,14 +83,9 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'element' => 'user' ], [ - 'name' => 'description', - 'sort' => 'description', - 'data_path' => 'description', - ], - [ - 'name' => 'comment', - 'sort' => 'comment', - 'data_path' => 'comment', + 'name' => 'message', + 'sort' => 'message', + 'data_path' => 'message', ], ], 'title' => __('Outbox'), diff --git a/templates/Outbox/view.php b/templates/Outbox/view.php index 5e6b234..d647f77 100644 --- a/templates/Outbox/view.php +++ b/templates/Outbox/view.php @@ -29,8 +29,8 @@ echo $this->element( 'path' => 'user_id', ], [ - 'key' => 'description', - 'path' => 'description', + 'key' => 'message', + 'path' => 'message', ], [ 'key' => 'comment',