From 8a215969f6b03dd0f8f15d26cf02577562a8b8af Mon Sep 17 00:00:00 2001 From: Fady Mondy Date: Mon, 16 Sep 2024 17:51:10 +0300 Subject: [PATCH] add filament-plugins integration --- README.md | 1 - module.json | 29 ++ resources/lang/ar.json | 3 - resources/lang/ar/messages.php | 2 +- resources/lang/en.json | 3 - resources/lang/en/messages.php | 2 +- src/FilamentAlertsPlugin.php | 261 ++++++++++-------- .../Pages/ManageNotificationsLogs.php | 7 +- .../NotificationsTemplateResource.php | 5 + .../Pages/ListNotificationsTemplates.php | 6 + .../Pages/ManageUserNotifications.php | 18 +- 11 files changed, 207 insertions(+), 130 deletions(-) create mode 100644 module.json delete mode 100644 resources/lang/ar.json delete mode 100644 resources/lang/en.json diff --git a/README.md b/README.md index b25e30e..5c944b3 100644 --- a/README.md +++ b/README.md @@ -301,7 +301,6 @@ you can publish migrations file by use this command php artisan vendor:publish --tag="filament-alerts-migrations" ``` - ## Other Filament Packages Checkout our [Awesome TomatoPHP](https://github.com/tomatophp/awesome) diff --git a/module.json b/module.json new file mode 100644 index 0000000..b0a7582 --- /dev/null +++ b/module.json @@ -0,0 +1,29 @@ +{ + "name": "FilamentAlerts", + "alias": "filament-alerts", + "description": { + "ar": "قم بإرسال التنبيهات المختلفة للمستخدمين عن طريق اكثر من مزود خدمة", + "en": "Send notifications to users using notification templates and multi-notification channels", + "gr": "Send notifications to users using notification templates and multi-notification channels", + "sp": "Send notifications to users using notification templates and multi-notification channels" + }, + "keywords": [], + "priority": 0, + "providers": [ + "TomatoPHP\\FilamentAlerts\\FilamentAlertsServiceProvider" + ], + "files": [], + "title": { + "ar": "التنبيهات", + "en": "Alerts Sender", + "gr": "Alerts Sender", + "sp": "Alerts Sender" + }, + "color": "#cc1448", + "icon": "heroicon-c-bell", + "placeholder": "https://raw.githubusercontent.com/tomatophp/filament-alerts/master/arts/3x1io-tomato-alerts.jpg", + "type": "plugin", + "version": "v2.0.3", + "github" : "https://github.com/tomatophp/filament-alerts", + "docs" : "https://github.com/tomatophp/filament-alerts" +} diff --git a/resources/lang/ar.json b/resources/lang/ar.json deleted file mode 100644 index 0db3279..0000000 --- a/resources/lang/ar.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - -} diff --git a/resources/lang/ar/messages.php b/resources/lang/ar/messages.php index 73f8d37..1883d9f 100644 --- a/resources/lang/ar/messages.php +++ b/resources/lang/ar/messages.php @@ -1,7 +1,7 @@ "التنبيهات", + "group" => "الإعدادات", "back" => "رجوع", "notifications" => [ "title" => "التنبيهات", diff --git a/resources/lang/en.json b/resources/lang/en.json deleted file mode 100644 index 0db3279..0000000 --- a/resources/lang/en.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - -} diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index cc61c92..2a62cf3 100644 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -1,7 +1,7 @@ "Notifications", + "group" => "Settings", "back" => "Back", "notifications" => [ "title" => "Notifications", diff --git a/src/FilamentAlertsPlugin.php b/src/FilamentAlertsPlugin.php index b25b5ce..12e181e 100644 --- a/src/FilamentAlertsPlugin.php +++ b/src/FilamentAlertsPlugin.php @@ -7,11 +7,13 @@ use Filament\Notifications\Actions\ActionGroup; use Filament\Notifications\Notification; use Filament\Panel; +use Filament\SpatieLaravelTranslatablePlugin; use Filament\Support\Facades\FilamentView; use Filament\View\PanelsRenderHook; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Config; use Illuminate\View\View; +use Nwidart\Modules\Module; use TomatoPHP\FilamentAlerts\Resources\NotificationsLogsResource; use TomatoPHP\FilamentAlerts\Resources\NotificationsTemplateResource; use TomatoPHP\FilamentAlerts\Resources\UserNotificationResource; @@ -23,6 +25,8 @@ class FilamentAlertsPlugin implements Plugin { + private bool $isActive = false; + public function getId(): string { return 'filament-alerts'; @@ -112,16 +116,29 @@ public function getId(): string public function register(Panel $panel): void { - $panel - ->resources((!$this->hideNotificationsResource) ? [ - NotificationsLogsResource::class, - UserNotificationResource::class, - NotificationsTemplateResource::class - ]: []) - ->pages($this->useSettingHub ? [ - NotificationsSettingsPage::class, - EmailSettingsPage::class - ]:[]); + if(class_exists(Module::class)){ + if(\Nwidart\Modules\Facades\Module::find('FilamentAlerts')->isEnabled()){ + $this->isActive = true; + } + } + else { + $this->isActive = true; + } + + if($this->isActive) { + + $panel + ->plugin(SpatieLaravelTranslatablePlugin::make()) + ->resources((!$this->hideNotificationsResource) ? [ + NotificationsLogsResource::class, + UserNotificationResource::class, + NotificationsTemplateResource::class + ] : []) + ->pages($this->useSettingHub ? [ + NotificationsSettingsPage::class, + EmailSettingsPage::class + ] : []); + } } @@ -211,125 +228,133 @@ public function apiModel(?string $apiModel = \App\Models\User::class): static public function boot(Panel $panel): void { - if(class_exists(FilamentSettingsHub::class) && $this->useSettingHub) - { - FilamentSettingsHub::register([ - SettingHold::make() - ->label('filament-alerts::messages.settings.firebase.title') - ->icon('heroicon-o-fire') - ->route('filament.'.filament()->getCurrentPanel()->getId().'.pages.notifications-settings-page') - ->description('filament-alerts::messages.settings.firebase.description') - ->group('filament-alerts::messages.settings.group'), - SettingHold::make() - ->label('filament-alerts::messages.settings.email.title') - ->icon('heroicon-o-envelope') - ->route('filament.'.filament()->getCurrentPanel()->getId().'.pages.email-settings-page') - ->description('filament-alerts::messages.settings.email.description') - ->group('filament-alerts::messages.settings.group'), - ]); - - - try { - Config::set('mail.mailers.smtp', [ - 'transport' => setting('mail_mailer'), - 'host' => setting('mail_host'), - 'port' => setting('mail_port'), - 'encryption' => setting('mail_encryption'), - 'username' => setting('mail_username'), - 'password' => setting('mail_password'), - 'timeout' => null, - 'auth_mode' => null, - ]); + if(class_exists(Module::class)){ + if(\Nwidart\Modules\Facades\Module::find('FilamentAlerts')->isEnabled()){ + $this->isActive = true; + } + } + else { + $this->isActive = true; + } - Config::set('mail.from', [ - 'address' => setting('mail_from_address'), - 'name' => setting('mail_from_name'), + if($this->isActive) { + if (class_exists(FilamentSettingsHub::class) && $this->useSettingHub) { + FilamentSettingsHub::register([ + SettingHold::make() + ->label('filament-alerts::messages.settings.firebase.title') + ->icon('heroicon-o-fire') + ->page(NotificationsSettingsPage::class) + ->order(2) + ->description('filament-alerts::messages.settings.firebase.description') + ->group('filament-alerts::messages.settings.group'), + SettingHold::make() + ->label('filament-alerts::messages.settings.email.title') + ->icon('heroicon-o-envelope') + ->page(EmailSettingsPage::class) + ->order(2) + ->description('filament-alerts::messages.settings.email.description') + ->group('filament-alerts::messages.settings.group'), ]); - Config::set('firebase.projects.app', [ - 'credentials' => env('FIREBASE_CREDENTIALS', public_path('storage/' . setting('fcm_cr'))), - 'database' => [ - 'url' => env('FIREBASE_DATABASE_URL', setting('fcm_database_url')), - ] - ]); - } catch (\Exception $e) { - \Log::error($e); + try { + Config::set('mail.mailers.smtp', [ + 'transport' => setting('mail_mailer'), + 'host' => setting('mail_host'), + 'port' => setting('mail_port'), + 'encryption' => setting('mail_encryption'), + 'username' => setting('mail_username'), + 'password' => setting('mail_password'), + 'timeout' => null, + 'auth_mode' => null, + ]); + + Config::set('mail.from', [ + 'address' => setting('mail_from_address'), + 'name' => setting('mail_from_name'), + ]); + + Config::set('firebase.projects.app', [ + 'credentials' => env('FIREBASE_CREDENTIALS', public_path('storage/' . setting('fcm_cr'))), + 'database' => [ + 'url' => env('FIREBASE_DATABASE_URL', setting('fcm_database_url')), + ] + ]); + + } catch (\Exception $e) { + \Log::error($e); + } } - } - Config::set('filament-alerts.provider', count($this->providers)?$this->providers:[]); - Config::set('filament-alerts.models', count($this->models)?$this->models:[]); - Config::set('filament-alerts.lang', count($this->lang)?$this->lang:[]); - Config::set('filament-alerts.types', count($this->types)?$this->types:[]); - - if($this->useEmail){ - Notification::macro('sendToEmail', function (Model $user): static - { - /** @var Notification $this */ - $user->notifyEmail( - message: $this->body, - subject: $this->title, - url: count($this->actions)? $this->actions[0]->getUrl()??null : null - ); - - return $this; - }); - } + Config::set('filament-alerts.provider', count($this->providers) ? $this->providers : []); + Config::set('filament-alerts.models', count($this->models) ? $this->models : []); + Config::set('filament-alerts.lang', count($this->lang) ? $this->lang : []); + Config::set('filament-alerts.types', count($this->types) ? $this->types : []); + + if ($this->useEmail) { + Notification::macro('sendToEmail', function (Model $user): static { + /** @var Notification $this */ + $user->notifyEmail( + message: $this->body, + subject: $this->title, + url: count($this->actions) ? $this->actions[0]->getUrl() ?? null : null + ); + + return $this; + }); + } - if($this->useSlack){ - Notification::macro('sendToSlack', function (Model $user): static - { - /** @var Notification $this */ - $user->notifySlack( - title: $this->title, - message: $this->body, - url: count($this->actions)? $this->actions[0]->getUrl() : null, - webhook: config('filament-alerts.drivers.slack.webhook') - ); - - return $this; - }); - } + if ($this->useSlack) { + Notification::macro('sendToSlack', function (Model $user): static { + /** @var Notification $this */ + $user->notifySlack( + title: $this->title, + message: $this->body, + url: count($this->actions) ? $this->actions[0]->getUrl() : null, + webhook: config('filament-alerts.drivers.slack.webhook') + ); + + return $this; + }); + } - if($this->useDiscord){ - Notification::macro('sendToDiscord', function (Model $user): static - { - /** @var Notification $this */ - $user->notifyDiscord( - title: $this->title, - message: $this->body, - url: count($this->actions)? $this->actions[0]->getUrl() : null, - webhook: config('filament-alerts.drivers.discord.webhook') - ); - - return $this; - }); - } + if ($this->useDiscord) { + Notification::macro('sendToDiscord', function (Model $user): static { + /** @var Notification $this */ + $user->notifyDiscord( + title: $this->title, + message: $this->body, + url: count($this->actions) ? $this->actions[0]->getUrl() : null, + webhook: config('filament-alerts.drivers.discord.webhook') + ); + + return $this; + }); + } - if($this->useSMSMisr){ - Notification::macro('sendToSMSMisr', function (Model $user): static - { - /** @var Notification $this */ - $user->notifySMSMisr( - message: $this->body - ); + if ($this->useSMSMisr) { + Notification::macro('sendToSMSMisr', function (Model $user): static { + /** @var Notification $this */ + $user->notifySMSMisr( + message: $this->body + ); - return $this; - }); - } + return $this; + }); + } - if($this->useFCM){ - $this->providers = array_merge($this->providers, [ - [ - "name" => 'FCM Web', - "id" => "fcm-web" - ], - [ - "name" => 'FCM Mobile', - "id" => "fcm-api" - ], - ]); + if ($this->useFCM) { + $this->providers = array_merge($this->providers, [ + [ + "name" => 'FCM Web', + "id" => "fcm-web" + ], + [ + "name" => 'FCM Mobile', + "id" => "fcm-api" + ], + ]); + } } } diff --git a/src/Resources/NotificationsLogsResource/Pages/ManageNotificationsLogs.php b/src/Resources/NotificationsLogsResource/Pages/ManageNotificationsLogs.php index 28ce9b1..5c9f479 100644 --- a/src/Resources/NotificationsLogsResource/Pages/ManageNotificationsLogs.php +++ b/src/Resources/NotificationsLogsResource/Pages/ManageNotificationsLogs.php @@ -7,6 +7,7 @@ use TomatoPHP\FilamentAlerts\Models\NotificationsTemplate; use TomatoPHP\FilamentAlerts\Resources\NotificationsLogsResource; use Filament\Actions; +use TomatoPHP\FilamentAlerts\Resources\UserNotificationResource; class ManageNotificationsLogs extends ManageRecords { @@ -20,8 +21,10 @@ public function getTitle(): string protected function getHeaderActions(): array { return [ - Action::make('notifications')->action(fn()=> redirect()->route('filament.admin.resources.user-notifications.index'))->color('danger')->label(trans('filament-alerts::messages.back')), - + Action::make('notifications') + ->action(fn()=> redirect()->to(UserNotificationResource::getUrl('index'))) + ->color('danger') + ->label(trans('filament-alerts::messages.back')), ]; } } diff --git a/src/Resources/NotificationsTemplateResource.php b/src/Resources/NotificationsTemplateResource.php index a9645a7..85f144e 100644 --- a/src/Resources/NotificationsTemplateResource.php +++ b/src/Resources/NotificationsTemplateResource.php @@ -31,6 +31,11 @@ class NotificationsTemplateResource extends Resource protected static ?int $navigationSort = 1; + public static function shouldRegisterNavigation(): bool + { + return false; // TODO: Change the autogenerated stub + } + public static function getNavigationGroup(): string { return trans('filament-alerts::messages.group'); diff --git a/src/Resources/NotificationsTemplateResource/Pages/ListNotificationsTemplates.php b/src/Resources/NotificationsTemplateResource/Pages/ListNotificationsTemplates.php index 8d81932..d36c58f 100644 --- a/src/Resources/NotificationsTemplateResource/Pages/ListNotificationsTemplates.php +++ b/src/Resources/NotificationsTemplateResource/Pages/ListNotificationsTemplates.php @@ -2,9 +2,11 @@ namespace TomatoPHP\FilamentAlerts\Resources\NotificationsTemplateResource\Pages; +use Filament\Pages\Actions\Action; use TomatoPHP\FilamentAlerts\Resources\NotificationsTemplateResource; use Filament\Actions; use Filament\Resources\Pages\ListRecords; +use TomatoPHP\FilamentAlerts\Resources\UserNotificationResource; class ListNotificationsTemplates extends ListRecords { @@ -30,6 +32,10 @@ protected function getHeaderActions(): array { return [ Actions\CreateAction::make()->label(trans('filament-alerts::messages.templates.create')), + Actions\Action::make('notifications') + ->action(fn()=> redirect()->to(UserNotificationResource::getUrl('index'))) + ->color('danger') + ->label(trans('filament-alerts::messages.back')), Actions\LocaleSwitcher::make(), ]; } diff --git a/src/Resources/UserNotificationResource/Pages/ManageUserNotifications.php b/src/Resources/UserNotificationResource/Pages/ManageUserNotifications.php index 9931701..8b72441 100644 --- a/src/Resources/UserNotificationResource/Pages/ManageUserNotifications.php +++ b/src/Resources/UserNotificationResource/Pages/ManageUserNotifications.php @@ -4,7 +4,10 @@ use Filament\Pages\Actions\Action; use Filament\Resources\Pages\ManageRecords; +use TomatoPHP\FilamentAlerts\Models\NotificationsLogs; use TomatoPHP\FilamentAlerts\Models\NotificationsTemplate; +use TomatoPHP\FilamentAlerts\Resources\NotificationsLogsResource; +use TomatoPHP\FilamentAlerts\Resources\NotificationsTemplateResource; use TomatoPHP\FilamentAlerts\Resources\UserNotificationResource; use Filament\Actions; use Filament\Resources\Pages\ListRecords; @@ -50,7 +53,20 @@ protected function getHeaderActions(): array ->fire(); }) ->label(trans('filament-alerts::messages.notifications.create')), - Action::make('logs')->action(fn()=> redirect()->route('filament.admin.resources.notifications-logs.index'))->color('info')->label(trans('filament-alerts::messages.notifications.logs')), + Action::make('templates') + ->icon('heroicon-o-document-text') + ->hiddenLabel() + ->action(fn()=> redirect()->to(NotificationsTemplateResource::getUrl('index'))) + ->color('danger') + ->tooltip(trans('filament-alerts::messages.templates.title')) + ->label(trans('filament-alerts::messages.templates.title')), + Action::make('logs') + ->icon('heroicon-o-archive-box-arrow-down') + ->hiddenLabel() + ->action(fn()=> redirect()->to(NotificationsLogsResource::getUrl('index'))) + ->color('info') + ->tooltip(trans('filament-alerts::messages.notifications.logs')) + ->label(trans('filament-alerts::messages.notifications.logs')), ]; }