Skip to content

Commit

Permalink
add filament-plugins integration
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Sep 16, 2024
1 parent 0b7a82b commit 8a21596
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 130 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 29 additions & 0 deletions module.json
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 0 additions & 3 deletions resources/lang/ar.json

This file was deleted.

2 changes: 1 addition & 1 deletion resources/lang/ar/messages.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [
"group" => "التنبيهات",
"group" => "الإعدادات",
"back" => "رجوع",
"notifications" => [
"title" => "التنبيهات",
Expand Down
3 changes: 0 additions & 3 deletions resources/lang/en.json

This file was deleted.

2 changes: 1 addition & 1 deletion resources/lang/en/messages.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [
"group" => "Notifications",
"group" => "Settings",
"back" => "Back",
"notifications" => [
"title" => "Notifications",
Expand Down
261 changes: 143 additions & 118 deletions src/FilamentAlertsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +25,8 @@

class FilamentAlertsPlugin implements Plugin
{
private bool $isActive = false;

public function getId(): string
{
return 'filament-alerts';
Expand Down Expand Up @@ -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
] : []);
}

}

Expand Down Expand Up @@ -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"
],
]);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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')),
];
}
}
Loading

0 comments on commit 8a21596

Please sign in to comment.