From: Louis Jeckel Date: Mon, 26 Oct 2020 12:12:38 +0000 (+0100) Subject: Notifications X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=60ac72e8379078a6151626cbe521acfe0a63b02e;p=psq.git Notifications --- diff --git a/app/Nova/Actions/SendCustomNotification.php b/app/Nova/Actions/SendCustomNotification.php index d298fb1..97f5b67 100644 --- a/app/Nova/Actions/SendCustomNotification.php +++ b/app/Nova/Actions/SendCustomNotification.php @@ -23,6 +23,8 @@ class SendCustomNotification extends Action */ private EmailTemplate $template; + public $showOnTableRow = true; + public $confirmButtonText = "Envoyer"; /** @@ -48,7 +50,7 @@ class SendCustomNotification extends Action */ public function handle(ActionFields $fields, Collection $models) { - $models->map(fn(User $user) => $user->sendTemplateEmail($fields->subject, $fields->content)); + $models->map(fn($sendsEmail) => $sendsEmail->sendTemplateEmail($fields->subject, $fields->content)); return Action::message(sprintf('%d messages envoyés', $models->count())); } @@ -65,4 +67,14 @@ class SendCustomNotification extends Action Trix::make('Contenu', 'content')->withMeta(['value' => $this->template->content]), ]; } + + /** + * @return array + */ + public static function getActions() + { + return \App\EmailTemplate::inActionsMenu()->get()->mapInto(self::class)->toArray(); + } } + + diff --git a/app/Nova/Actions/SendNotification.php b/app/Nova/Actions/SendNotification.php index b24e3e5..9a0e25f 100644 --- a/app/Nova/Actions/SendNotification.php +++ b/app/Nova/Actions/SendNotification.php @@ -3,6 +3,7 @@ namespace App\Nova\Actions; use App\EmailTemplate; +use App\SendsEmails; use App\User; use FontLib\Table\Type\name; use Illuminate\Bus\Queueable; @@ -36,7 +37,7 @@ class SendNotification extends Action public function handle(ActionFields $fields, Collection $models) { - $models->map(fn(User $user) => $user->sendEmailFromTemplate($fields->notification)); + $models->map(fn($sendsEmail) => $sendsEmail->sendEmailFromTemplate($fields->notification)); return Action::message(sprintf('%d messages envoyés', $models->count())); } diff --git a/app/Nova/EmailTemplate.php b/app/Nova/EmailTemplate.php index 2a5e2e7..220e7b8 100644 --- a/app/Nova/EmailTemplate.php +++ b/app/Nova/EmailTemplate.php @@ -56,9 +56,9 @@ class EmailTemplate extends Resource { return [ Text::make('Nom code', 'slug')->hideFromIndex(), - Text::make('Nom', 'name'), + Text::make('Nom', 'name')->required(), Text::make('Sujet', 'subject'), - Trix::make('Contenu', 'content')->alwaysShow(), + Trix::make('Contenu', 'content')->alwaysShow()->required(), Boolean::make('Dans menu action ?', 'show_in_actions'), ]; } diff --git a/app/Nova/Organization.php b/app/Nova/Organization.php index 7c8f051..3ef8c5c 100644 --- a/app/Nova/Organization.php +++ b/app/Nova/Organization.php @@ -2,6 +2,8 @@ namespace App\Nova; +use App\Nova\Actions\SendCustomNotification; +use App\Nova\Actions\SendNotification; use App\Nova\Actions\SetSubscriptionStatus; use App\Nova\Filters\SubscriptionStatusFilter; use App\Nova\Filters\TypeFilter; @@ -109,9 +111,10 @@ class Organization extends Resource */ public function actions(Request $request) { - return [ + return array_merge([ new SetSubscriptionStatus(true), new SetSubscriptionStatus(false), - ]; + new SendNotification, + ], SendCustomNotification::getActions()); } } diff --git a/app/Nova/User.php b/app/Nova/User.php index c8406dd..829c2be 100644 --- a/app/Nova/User.php +++ b/app/Nova/User.php @@ -198,7 +198,7 @@ class User extends Resource // new VerifyEmail, new ValidateAddress, ], - \App\EmailTemplate::inActionsMenu()->get()->mapInto(SendCustomNotification::class)->toArray(), + SendCustomNotification::getActions(), ); } } diff --git a/app/Organization.php b/app/Organization.php index 566ea48..3b56136 100644 --- a/app/Organization.php +++ b/app/Organization.php @@ -14,7 +14,7 @@ use Laravel\Scout\Searchable; * @property string name * @property boolean $subscription_active */ -class Organization extends Model +class Organization extends Model implements SendsEmails { use Searchable; @@ -74,4 +74,21 @@ class Organization extends Model return $this->name; } + + /** + * @param $template + */ + public function sendEmailFromTemplate($template) + { + $this->members->map->sendEmailFromTemplate($template); + } + + /** + * @param $subject + * @param $content + */ + public function sendTemplateEmail($subject, $content) + { + $this->members->map->sendTemplateEmail($subject, $content); + } } diff --git a/app/SendsEmails.php b/app/SendsEmails.php new file mode 100644 index 0000000..7fad518 --- /dev/null +++ b/app/SendsEmails.php @@ -0,0 +1,13 @@ +id(); - $table->string('slug'); + $table->string('slug')->nullable(); $table->string('name'); $table->text('subject')->nullable(); $table->text('content');