]> _ Git - psq.git/commitdiff
custom notifications
authorLouis Jeckel <louis.jeckel@outlook.com>
Mon, 26 Oct 2020 11:05:46 +0000 (12:05 +0100)
committerLouis Jeckel <louis.jeckel@outlook.com>
Mon, 26 Oct 2020 11:05:46 +0000 (12:05 +0100)
app/EmailTemplate.php
app/Nova/Actions/SendCustomNotification.php [new file with mode: 0644]
app/Nova/EmailTemplate.php
app/Nova/User.php
app/TemplateVariables.php
database/migrations/2020_10_26_104249_add_show_on_menu_col_to_email_templates.php [new file with mode: 0644]

index f5fabd7811e46b338f9515471c0dcfc612e2ae62..418cdb8bcf7d57b3f204735b45a18edd2aefecc1 100644 (file)
@@ -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 (file)
index 0000000..d298fb1
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+
+namespace App\Nova\Actions;
+
+use App\EmailTemplate;
+use App\User;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Support\Collection;
+use Laravel\Nova\Actions\Action;
+use Laravel\Nova\Fields\ActionFields;
+use Laravel\Nova\Fields\Text;
+use Laravel\Nova\Fields\Trix;
+
+class SendCustomNotification extends Action
+{
+    use InteractsWithQueue, Queueable;
+
+
+    /**
+     * @var EmailTemplate
+     */
+    private EmailTemplate $template;
+
+    public $confirmButtonText = "Envoyer";
+
+    /**
+     * SendCustomNotification constructor.
+     * @param EmailTemplate $template
+     */
+    public function __construct(EmailTemplate $template)
+    {
+        $this->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]),
+        ];
+    }
+}
index ba51fbcf02f7ecaac4a5c217d92965439d24d655..2a5e2e7148f300e22053ad3901bb648cf16f4159 100644 (file)
@@ -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'),
         ];
     }
 
index 0fdac4b0a956a336058ef705ec77a66b8077ba0e..c8406dd3345293fa5d1eaa28159f7886c5855fca 100644 (file)
@@ -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(),
+        );
     }
 }
index 0a23c3262a01feb573459efac90ae12834076fcd..5f2fe047a4e37c844d1ecd151b42d2b2a4c9627a 100644 (file)
@@ -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 (file)
index 0000000..9b73408
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddShowOnMenuColToEmailTemplates extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('email_templates', function (Blueprint $table) {
+            $table->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');
+        });
+    }
+}