From 5c5b890942bf9803d1c520d47e979e77e29da9a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Sat, 19 Jun 2021 22:30:14 +0300 Subject: [PATCH] Import notification file and updated other file.. --- .../Livewire/Common/Notifications/Imports.php | 67 +++++++++++++++++-- .../Livewire/Common/Notifications/NewApps.php | 7 +- .../Common/Notifications/Reminder.php | 3 +- app/Http/ViewComposers/Header.php | 13 +++- app/Notifications/Common/ImportCompleted.php | 25 ++++++- app/Notifications/Common/ImportFailed.php | 19 +++++- app/Utilities/Import.php | 9 ++- resources/lang/en-GB/header.php | 1 + resources/lang/en-GB/notifications.php | 5 +- .../common/notifications/imports.blade.php | 11 ++- .../common/notifications/new-apps.blade.php | 30 +++++++-- .../views/partials/admin/navbar.blade.php | 19 ++++++ 12 files changed, 182 insertions(+), 27 deletions(-) diff --git a/app/Http/Livewire/Common/Notifications/Imports.php b/app/Http/Livewire/Common/Notifications/Imports.php index 23f58a906..6d86bafdc 100644 --- a/app/Http/Livewire/Common/Notifications/Imports.php +++ b/app/Http/Livewire/Common/Notifications/Imports.php @@ -4,23 +4,82 @@ namespace App\Http\Livewire\Common\Notifications; use Livewire\Component; use Livewire\WithPagination; +use Illuminate\Support\Collection; +use Illuminate\Notifications\DatabaseNotification; +use Illuminate\Support\Str; class Imports extends Component { use WithPagination; + protected $listeners = [ + 'refreshParent' => '$notifications', + ]; + + public function markRead($notification_id) + { + $notification = DatabaseNotification::find($notification_id); + $data = $notification->getAttribute('data'); + + $notification->markAsRead(); + + $this->dispatchBrowserEvent('mark-read', [ + 'type' => 'import', + 'message' => trans('notifications.messages.mark_read', ['type' => $data['translation']]), + ]); + } + + public function markReadAll() + { + $notifications = $this->getNotifications(); + + foreach ($notifications as $notification) { + $notification->markAsRead(); + } + + $this->dispatchBrowserEvent('mark-read-all', [ + 'type' => 'import', + 'message' => trans('notifications.messages.mark_read_all', ['type' => trans('import.import')]), + ]); + } + public function render() { $limit = 5; - $notifications = user()->notifications()->unread() - ->where('type', 'App\Notifications\Common\ImportCompleted') - ->orWhere('type', 'App\Notifications\Common\ImportFailed') - ->paginate($limit); + $notifications = $this->getNotifications($limit); return view('livewire.common.notifications.imports', compact('notifications')); } + protected function getNotifications($limit = false) + { + $query = user()->notifications()->unread() + ->where('type', 'App\Notifications\Common\ImportCompleted') + ->orWhere('type', 'App\Notifications\Common\ImportFailed'); + + if ($limit) { + $notifications = $query->paginate($limit); + } else { + $notifications = $query->get(); + } + + if ($notifications->items()) { + $items = []; + + foreach ($notifications->items() as $key => $notification) { + $data = (object) $notification->getAttribute('data'); + $data->notification_id = $notification->getAttribute('id'); + + $items[] = $data; + } + + $notifications->setCollection(Collection::make($items)); + } + + return $notifications; + } + public function paginationView() { return 'vendor.livewire.default'; diff --git a/app/Http/Livewire/Common/Notifications/NewApps.php b/app/Http/Livewire/Common/Notifications/NewApps.php index 421c4e296..f3b4b0015 100644 --- a/app/Http/Livewire/Common/Notifications/NewApps.php +++ b/app/Http/Livewire/Common/Notifications/NewApps.php @@ -2,12 +2,17 @@ namespace App\Http\Livewire\Common\Notifications; +use App\Traits\Modules; use Livewire\Component; class NewApps extends Component { + use Modules; + public function render() { - return view('livewire.common.notifications.new-apps'); + $notifications = $this->getNotifications('new-apps'); + + return view('livewire.common.notifications.new-apps', compact('notifications')); } } diff --git a/app/Http/Livewire/Common/Notifications/Reminder.php b/app/Http/Livewire/Common/Notifications/Reminder.php index 62ce4cdc5..a30bd08a5 100644 --- a/app/Http/Livewire/Common/Notifications/Reminder.php +++ b/app/Http/Livewire/Common/Notifications/Reminder.php @@ -46,12 +46,13 @@ class Reminder extends Component public function render() { - $limit = 10; + $limit = 5; $type = config('type.' . $this->type . '.notification.class'); $documents = user()->notifications()->unread() ->where('type', $type) + ->where('data', 'like', '%template_alias:{$this->type}_remind_admin%') ->paginate($limit); $items = []; diff --git a/app/Http/ViewComposers/Header.php b/app/Http/ViewComposers/Header.php index e52b1c62e..19d72fe3d 100644 --- a/app/Http/ViewComposers/Header.php +++ b/app/Http/ViewComposers/Header.php @@ -20,7 +20,7 @@ class Header { $user = user(); - $invoices = $bills = $exports = $imports = []; + $new_apps = $invoices = $bills = $exports = $imports = []; $updates = $notifications = 0; $company = null; @@ -51,11 +51,11 @@ class Header $notifications++; break; case 'App\Notifications\Common\ImportCompleted': - $import_completed[$data['bill_id']] = $data['amount']; + $imports['completed'][] = $data['translation']; $notifications++; break; case 'App\Notifications\Common\ImportFailed': - $import_failed[$data['bill_id']] = $data['amount']; + $imports['failed'][] = ''; $notifications++; break; case 'App\Notifications\Purchase\Bill': @@ -68,6 +68,12 @@ class Header break; } } + + $new_apps = $this->getNotifications('new-apps'); + + foreach ($new_apps as $new_app) { + $notifications++; + } } if ($user->can('read-install-updates')) { @@ -80,6 +86,7 @@ class Header $view->with([ 'user' => $user, 'notifications' => $notifications, + 'new_apps' => $new_apps, 'exports' => $exports, 'imports' => $imports, 'bills' => $bills, diff --git a/app/Notifications/Common/ImportCompleted.php b/app/Notifications/Common/ImportCompleted.php index 512b583b7..b667a6830 100644 --- a/app/Notifications/Common/ImportCompleted.php +++ b/app/Notifications/Common/ImportCompleted.php @@ -11,11 +11,18 @@ class ImportCompleted extends Notification implements ShouldQueue { use Queueable; + protected $translation; + + protected $total_rows; + /** * Create a notification instance. */ - public function __construct() + public function __construct($translation, $total_rows) { + $this->translation = $translation; + $this->total_rows = $total_rows; + $this->onQueue('notifications'); } @@ -27,7 +34,7 @@ class ImportCompleted extends Notification implements ShouldQueue */ public function via($notifiable) { - return ['mail']; + return ['mail', 'database']; } /** @@ -45,4 +52,18 @@ class ImportCompleted extends Notification implements ShouldQueue ->line(trans('notifications.import.completed.description')) ->action(trans_choice('general.dashboards', 1), $dashboard_url); } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + 'translation' => $this->translation, + 'total_rows' => $this->total_rows, + ]; + } } diff --git a/app/Notifications/Common/ImportFailed.php b/app/Notifications/Common/ImportFailed.php index 7eda94032..b4aec313f 100644 --- a/app/Notifications/Common/ImportFailed.php +++ b/app/Notifications/Common/ImportFailed.php @@ -38,7 +38,7 @@ class ImportFailed extends Notification implements ShouldQueue */ public function via($notifiable) { - return ['mail']; + return ['mail', 'database']; } /** @@ -50,8 +50,8 @@ class ImportFailed extends Notification implements ShouldQueue public function toMail($notifiable) { $message = (new MailMessage) - ->subject(trans('notifications.import.failed.subject')) - ->line(trans('notifications.import.failed.description')); + ->subject(trans('notifications.import.failed.subject')) + ->line(trans('notifications.import.failed.description')); foreach ($this->errors as $error) { $message->line($error); @@ -59,4 +59,17 @@ class ImportFailed extends Notification implements ShouldQueue return $message; } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + 'errors' => $this->errors, + ]; + } } diff --git a/app/Utilities/Import.php b/app/Utilities/Import.php index e177bfd41..f2b3d58f3 100644 --- a/app/Utilities/Import.php +++ b/app/Utilities/Import.php @@ -24,8 +24,15 @@ class Import $file = $request->file('import'); if (should_queue()) { + $rows = $class->toArray($file); + + $total_rows = 0; + if (!empty($rows[0])) { + $total_rows = count($rows[0]); + } + $class->queue($file)->onQueue('imports')->chain([ - new NotifyUser(user(), new ImportCompleted), + new NotifyUser(user(), new ImportCompleted($translation, $total_rows)), ]); $message = trans('messages.success.import_queued', ['type' => $translation]); diff --git a/resources/lang/en-GB/header.php b/resources/lang/en-GB/header.php index e40d06f08..2632643fb 100644 --- a/resources/lang/en-GB/header.php +++ b/resources/lang/en-GB/header.php @@ -7,6 +7,7 @@ return [ 'notifications' => [ 'counter' => '{0} You have no notification|{1} You have :count notification|[2,*] You have :count notifications', + 'new_apps' => '{1} :count published app|[2,*] :count published apps', 'overdue_invoices' => '{1} :count overdue invoice|[2,*] :count overdue invoices', 'upcoming_bills' => '{1} :count upcoming bill|[2,*] :count upcoming bills', 'view_all' => 'View All', diff --git a/resources/lang/en-GB/notifications.php b/resources/lang/en-GB/notifications.php index ad63ff4e7..d7387ec83 100644 --- a/resources/lang/en-GB/notifications.php +++ b/resources/lang/en-GB/notifications.php @@ -10,6 +10,7 @@ return [ 'read_all' => 'Read All', 'mark_read' => 'Mark Read', 'mark_read_all' => 'Mark Read All', + 'new_apps' => 'New App|New Apps', 'upcoming_bills' => 'Upcoming Bills', 'recurring_invoices' => 'Recurring Invoices', 'recurring_bills' => 'Recurring Bills', @@ -61,6 +62,8 @@ return [ 'messages' => [ 'mark_read' => ':type is read this notification!', 'mark_read_all' => ':type is read all notification!', - 'export' => ':type export is ready! The export file is ready to download from the following :file_name' + 'new_app' => ':type app published.', + 'export' => ':type export is ready! The export file is ready to download from the following :file_name', + 'import' => ':type import finished! Added :count :type rows.' ], ]; diff --git a/resources/views/livewire/common/notifications/imports.blade.php b/resources/views/livewire/common/notifications/imports.blade.php index dddd3598d..855d0752c 100644 --- a/resources/views/livewire/common/notifications/imports.blade.php +++ b/resources/views/livewire/common/notifications/imports.blade.php @@ -1,9 +1,9 @@ @if ($notifications->total()) -
+
-
{{ trans('general.export') }}
+
{{ trans('import.import') }}
@@ -20,16 +20,15 @@
- +
@foreach ($notifications as $notification)
@if (empty($notification->message)) - {!! trans('notifications.messages.export', [ + {!! trans('notifications.messages.import', [ 'type' => $notification->translation, - 'file_name' => $notification->file_name, - 'url' => $notification->download_url + 'count' => $notification->total_rows ]) !!} @else {!! $notification->message !!} diff --git a/resources/views/livewire/common/notifications/new-apps.blade.php b/resources/views/livewire/common/notifications/new-apps.blade.php index c51622f2b..567034348 100644 --- a/resources/views/livewire/common/notifications/new-apps.blade.php +++ b/resources/views/livewire/common/notifications/new-apps.blade.php @@ -1,7 +1,27 @@ -
-
- -
+@if ($notifications) +
+
+
+
+

{{ trans_choice('notifications.new_apps', 2) }}

+
+
+
+
+ + + @foreach ($notifications as $notification) + + + + @endforeach + +
+ {!! $notification->message !!} +
+
+
+
-
+@endif diff --git a/resources/views/partials/admin/navbar.blade.php b/resources/views/partials/admin/navbar.blade.php index 680c058d3..fb034796a 100644 --- a/resources/views/partials/admin/navbar.blade.php +++ b/resources/views/partials/admin/navbar.blade.php @@ -127,6 +127,25 @@ @endif
+ @stack('notification_new_apps_start') + + @if (!empty($new_apps) && count($new_apps)) + +
+
+ +
+
+
+

{{ trans_choice('header.notifications.new_apps', count($new_apps), ['count' => count($new_apps)]) }}

+
+
+
+
+ @endif + + @stack('notification_new_apps_end') + @stack('notification_exports_completed_start') @if (!empty($exports['completed']) && count($exports['completed']))