From d3233740b3c542c0a11de49be8970c137b8122dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Sun, 8 Aug 2021 23:44:59 +0300 Subject: [PATCH] make sure notification always returns array --- app/Http/Controllers/Common/Notifications.php | 43 +++++++++++-------- .../Livewire/Common/Notifications/NewApps.php | 33 ++++++++------ app/Http/ViewComposers/Header.php | 16 +++---- app/Http/ViewComposers/Notifications.php | 6 +-- app/Traits/Modules.php | 6 +-- 5 files changed, 57 insertions(+), 47 deletions(-) diff --git a/app/Http/Controllers/Common/Notifications.php b/app/Http/Controllers/Common/Notifications.php index b283dba9e..0e72e8ab6 100644 --- a/app/Http/Controllers/Common/Notifications.php +++ b/app/Http/Controllers/Common/Notifications.php @@ -2,15 +2,15 @@ namespace App\Http\Controllers\Common; -use Date; use App\Abstracts\Http\Controller; -use App\Traits\Modules as RemoteModules; use App\Http\Requests\Common\Notification as Request; +use App\Traits\Modules; +use App\Utilities\Date; use Illuminate\Support\Str; class Notifications extends Controller { - use RemoteModules; + use Modules; /** * Display a listing of the resource. @@ -36,13 +36,17 @@ class Notifications extends Controller } // Hide New Apps Notifications - $module_notifications = $this->getNotifications('new-apps' ); + $module_notifications = $this->getNotifications('new-apps'); foreach ($module_notifications as $module_notification) { - setting()->set('notifications.'. user()->id . '.' . $module_notification->alias . '.name', $module_notification->name); - setting()->set('notifications.'. user()->id . '.' . $module_notification->alias . '.message', $module_notification->alias); - setting()->set('notifications.'. user()->id . '.' . $module_notification->alias . '.date', Date::now()); - setting()->set('notifications.'. user()->id . '.' . $module_notification->alias . '.status', '0'); + $prefix = 'notifications.' . user()->id . '.' . $module_notification->alias; + + setting()->set([ + $prefix . '.name' => $module_notification->name, + $prefix . '.message' => $module_notification->alias, + $prefix . '.date' => Date::now(), + $prefix . '.status' => '0', + ]); } setting()->save(); @@ -68,17 +72,20 @@ class Notifications extends Controller $notifications = $this->getNotifications($path); - if ($notifications) { - foreach ($notifications as $notification) { - if ($notification->id == $id) { - setting()->set('notifications.'. $path . '.' . $id . '.name', $notification->name); - setting()->set('notifications.'. $path . '.' . $id . '.message', $notification->message); - setting()->set('notifications.'. $path . '.' . $id . '.date', Date::now()); - setting()->set('notifications.'. $path . '.' . $id . '.status', '0'); + foreach ($notifications as $notification) { + if ($notification->id == $id) { + $prefix = 'notifications.' . $path . '.' . $id; - setting()->save(); - break; - } + setting()->set([ + $prefix . '.name' => $notification->name, + $prefix . '.message' => $notification->message, + $prefix . '.date' => Date::now(), + $prefix . '.status' => '0', + ]); + + setting()->save(); + + break; } } diff --git a/app/Http/Livewire/Common/Notifications/NewApps.php b/app/Http/Livewire/Common/Notifications/NewApps.php index 21e4a03e6..5e76a6769 100644 --- a/app/Http/Livewire/Common/Notifications/NewApps.php +++ b/app/Http/Livewire/Common/Notifications/NewApps.php @@ -2,8 +2,8 @@ namespace App\Http\Livewire\Common\Notifications; -use Date; use App\Traits\Modules; +use App\Utilities\Date; use Livewire\Component; class NewApps extends Component @@ -12,39 +12,46 @@ class NewApps extends Component public function markRead($alias) { - $notifications = $this->getNotifications('new-apps' ); + $notifications = $this->getNotifications('new-apps'); foreach ($notifications as $notification) { if ($notification->alias != $alias) { continue; } - $readed = $notification; + $read = $notification; } - setting()->set('notifications.'. user()->id . '.' . $alias . '.name', $readed->name); - setting()->set('notifications.'. user()->id . '.' . $alias . '.message', $readed->alias); - setting()->set('notifications.'. user()->id . '.' . $alias . '.date', Date::now()); - setting()->set('notifications.'. user()->id . '.' . $alias . '.status', '0'); + $prefix = 'notifications.' . user()->id . '.' . $alias; + + setting()->set([ + $prefix . '.name' => $read->name, + $prefix . '.message' => $read->alias, + $prefix . '.date' => Date::now(), + $prefix . '.status' => '0', + ]); setting()->save(); $this->dispatchBrowserEvent('mark-read', [ 'type' => 'new-apps', - 'message' => trans('notifications.messages.mark_read', ['type' => $notification->name]), + 'message' => trans('notifications.messages.mark_read', ['type' => $read->name]), ]); } public function markReadAll() { - $notifications = $this->getNotifications('new-apps' ); + $notifications = $this->getNotifications('new-apps'); foreach ($notifications as $notification) { - setting()->set('notifications.'. user()->id . '.' . $notification->alias . '.name', $notification->name); - setting()->set('notifications.'. user()->id . '.' . $notification->alias . '.message', $notification->alias); - setting()->set('notifications.'. user()->id . '.' . $notification->alias . '.date', Date::now()); - setting()->set('notifications.'. user()->id . '.' . $notification->alias . '.status', '0'); + $prefix = 'notifications.' . user()->id . '.' . $notification->alias; + setting()->set([ + $prefix . '.name' => $notification->name, + $prefix . '.message' => $notification->alias, + $prefix . '.date' => Date::now(), + $prefix . '.status' => '0', + ]); } setting()->save(); diff --git a/app/Http/ViewComposers/Header.php b/app/Http/ViewComposers/Header.php index b379a9233..31a9e38ba 100644 --- a/app/Http/ViewComposers/Header.php +++ b/app/Http/ViewComposers/Header.php @@ -2,8 +2,8 @@ namespace App\Http\ViewComposers; -use App\Utilities\Versions; use App\Traits\Modules; +use App\Utilities\Versions; use Illuminate\View\View; class Header @@ -71,16 +71,14 @@ class Header $new_apps = $this->getNotifications('new-apps'); - if ($new_apps) { - foreach ($new_apps as $key => $new_app) { - if (setting('notifications.' . user()->id . '.' . $new_app->alias)) { - unset($new_apps[$key]); + foreach ($new_apps as $key => $new_app) { + if (setting('notifications.' . user()->id . '.' . $new_app->alias)) { + unset($new_apps[$key]); - continue; - } - - $notifications++; + continue; } + + $notifications++; } } diff --git a/app/Http/ViewComposers/Notifications.php b/app/Http/ViewComposers/Notifications.php index 8d16b1284..6bf9e6c97 100644 --- a/app/Http/ViewComposers/Notifications.php +++ b/app/Http/ViewComposers/Notifications.php @@ -3,7 +3,7 @@ namespace App\Http\ViewComposers; use App\Traits\Modules; -use Route; +use Illuminate\Support\Facades\Route; use Illuminate\View\View; class Notifications @@ -29,9 +29,7 @@ class Notifications $path = str_replace('{company_id}/', '', $path); - if (!$notifications = $this->getNotifications($path)) { - return; - } + $notifications = $this->getNotifications($path); // Push to a stack foreach ($notifications as $notification) { diff --git a/app/Traits/Modules.php b/app/Traits/Modules.php index 2617aca42..d5683d881 100644 --- a/app/Traits/Modules.php +++ b/app/Traits/Modules.php @@ -396,7 +396,7 @@ trait Modules return false; } - public function getNotifications($path) + public function getNotifications($path): array { $key = 'apps.notifications'; @@ -407,10 +407,10 @@ trait Modules } if (!empty($data) && array_key_exists($path, $data)) { - return $data[$path]; + return (array) $data[$path]; } - return false; + return []; } public function getPageNumberOfModules($data = [])