From f7081c66f72bff6a39cc6fdc065d84bb462e3868 Mon Sep 17 00:00:00 2001 From: Louis Jeckel Date: Mon, 26 Oct 2020 12:05:46 +0100 Subject: [PATCH] custom notifications --- app/EmailTemplate.php | 22 ++++++ app/Nova/Actions/SendCustomNotification.php | 68 +++++++++++++++++++ app/Nova/EmailTemplate.php | 2 + app/Nova/User.php | 7 +- app/TemplateVariables.php | 15 +++- ...dd_show_on_menu_col_to_email_templates.php | 32 +++++++++ 6 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 app/Nova/Actions/SendCustomNotification.php create mode 100644 database/migrations/2020_10_26_104249_add_show_on_menu_col_to_email_templates.php diff --git a/app/EmailTemplate.php b/app/EmailTemplate.php index f5fabd7..418cdb8 100644 --- a/app/EmailTemplate.php +++ b/app/EmailTemplate.php @@ -2,6 +2,7 @@ namespace App; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; @@ -11,11 +12,16 @@ use Illuminate\Support\Str; * @property string $subject * @property string $content * @property string $slug + * @property string $name + * @property boolean $show_in_actions */ class EmailTemplate extends Model { public $timestamps = false; + protected $casts = [ + 'show_in_actions' => 'boolean' + ]; /** * @param $slug @@ -56,6 +62,14 @@ class EmailTemplate extends Model return self::fillWithContentFor($user, $this->content); } + /** + * @param User $user + * @return string + */ + public function fillSubjectFor(User $user) + { + return self::fillWithContentFor($user, $this->subject); + } /** * @param $url @@ -72,4 +86,12 @@ class EmailTemplate extends Model 'color' => $color ])->render(); } + + /** + * @param Builder $builder + */ + public function scopeInActionsMenu(Builder $builder) + { + $builder->where('show_in_actions', 1); + } } diff --git a/app/Nova/Actions/SendCustomNotification.php b/app/Nova/Actions/SendCustomNotification.php new file mode 100644 index 0000000..d298fb1 --- /dev/null +++ b/app/Nova/Actions/SendCustomNotification.php @@ -0,0 +1,68 @@ +template = $template; + } + + public function name() + { + return "Notif: " . $this->template->name; + } + + /** + * Perform the action on the given models. + * + * @param \Laravel\Nova\Fields\ActionFields $fields + * @param \Illuminate\Support\Collection $models + * @return mixed + */ + public function handle(ActionFields $fields, Collection $models) + { + $models->map(fn(User $user) => $user->sendTemplateEmail($fields->subject, $fields->content)); + + return Action::message(sprintf('%d messages envoyés', $models->count())); + } + + /** + * Get the fields available on the action. + * + * @return array + */ + public function fields() + { + return [ + Text::make('Sujet', 'subject')->withMeta(['value' => $this->template->subject]), + Trix::make('Contenu', 'content')->withMeta(['value' => $this->template->content]), + ]; + } +} diff --git a/app/Nova/EmailTemplate.php b/app/Nova/EmailTemplate.php index ba51fbc..2a5e2e7 100644 --- a/app/Nova/EmailTemplate.php +++ b/app/Nova/EmailTemplate.php @@ -3,6 +3,7 @@ namespace App\Nova; use Illuminate\Http\Request; +use Laravel\Nova\Fields\Boolean; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; use Laravel\Nova\Fields\Trix; @@ -58,6 +59,7 @@ class EmailTemplate extends Resource Text::make('Nom', 'name'), Text::make('Sujet', 'subject'), Trix::make('Contenu', 'content')->alwaysShow(), + Boolean::make('Dans menu action ?', 'show_in_actions'), ]; } diff --git a/app/Nova/User.php b/app/Nova/User.php index 0fdac4b..c8406dd 100644 --- a/app/Nova/User.php +++ b/app/Nova/User.php @@ -4,6 +4,7 @@ namespace App\Nova; use App\MailgunEvent; use App\Nova\Actions\ImportUsers; +use App\Nova\Actions\SendCustomNotification; use App\Nova\Actions\SendNotification; use App\Nova\Actions\StartTrial; use App\Nova\Actions\ValidateAddress; @@ -190,12 +191,14 @@ class User extends Resource */ public function actions(Request $request) { - return [ + return array_merge([ new ImportUsers, new SendNotification, new StartTrial, // new VerifyEmail, new ValidateAddress, - ]; + ], + \App\EmailTemplate::inActionsMenu()->get()->mapInto(SendCustomNotification::class)->toArray(), + ); } } diff --git a/app/TemplateVariables.php b/app/TemplateVariables.php index 0a23c32..5f2fe04 100644 --- a/app/TemplateVariables.php +++ b/app/TemplateVariables.php @@ -27,7 +27,20 @@ trait TemplateVariables \Mail::to($this)->send(new TemplateMail( $template->fillFor($this), - $template->subject + $template->fillSubjectFor($this) + )); + } + + /** + * @param $subject + * @param $content + * Fills subject and content, then sends mail + */ + public function sendTemplateEmail($subject, $content) + { + \Mail::to($this)->send(new TemplateMail( + EmailTemplate::fillWithContentFor($this, $content), + EmailTemplate::fillWithContentFor($this, $subject) )); } diff --git a/database/migrations/2020_10_26_104249_add_show_on_menu_col_to_email_templates.php b/database/migrations/2020_10_26_104249_add_show_on_menu_col_to_email_templates.php new file mode 100644 index 0000000..9b73408 --- /dev/null +++ b/database/migrations/2020_10_26_104249_add_show_on_menu_col_to_email_templates.php @@ -0,0 +1,32 @@ +boolean('show_in_actions')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('email_templates', function (Blueprint $table) { + $table->dropColumn('show_in_actions'); + }); + } +} -- 2.39.5