namespace App;
+use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
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
return self::fillWithContentFor($user, $this->content);
}
+ /**
+ * @param User $user
+ * @return string
+ */
+ public function fillSubjectFor(User $user)
+ {
+ return self::fillWithContentFor($user, $this->subject);
+ }
/**
* @param $url
'color' => $color
])->render();
}
+
+ /**
+ * @param Builder $builder
+ */
+ public function scopeInActionsMenu(Builder $builder)
+ {
+ $builder->where('show_in_actions', 1);
+ }
}
--- /dev/null
+<?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]),
+ ];
+ }
+}
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;
Text::make('Nom', 'name'),
Text::make('Sujet', 'subject'),
Trix::make('Contenu', 'content')->alwaysShow(),
+ Boolean::make('Dans menu action ?', 'show_in_actions'),
];
}
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;
*/
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(),
+ );
}
}
\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)
));
}
--- /dev/null
+<?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');
+ });
+ }
+}