From e896a4458a5a583601c7394cddf5c861f0d5c7dc Mon Sep 17 00:00:00 2001 From: Louis Jeckel Date: Mon, 14 Dec 2020 17:42:20 +0100 Subject: [PATCH] add email template filter --- app/EmailTemplate.php | 1 + app/TemplateVariables.php | 7 ++++ app/User.php | 13 ++++++-- ...14_161125_add_scope_to_email_templates.php | 32 +++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 database/migrations/2020_12_14_161125_add_scope_to_email_templates.php diff --git a/app/EmailTemplate.php b/app/EmailTemplate.php index b010717..5f7e21f 100644 --- a/app/EmailTemplate.php +++ b/app/EmailTemplate.php @@ -16,6 +16,7 @@ use Illuminate\Support\Str; * @property string $content * @property string $slug * @property string $name + * @property string $filter_attribute // if set, $user->{$filter_attribute} has to be TRUE for email to be sent * @property boolean $show_in_actions */ class EmailTemplate extends Model diff --git a/app/TemplateVariables.php b/app/TemplateVariables.php index 9addcfd..c74b8f8 100644 --- a/app/TemplateVariables.php +++ b/app/TemplateVariables.php @@ -18,6 +18,7 @@ trait TemplateVariables /** * @param string|EmailTemplate $template + * @return bool */ public function sendEmailFromTemplate($template) { @@ -25,14 +26,20 @@ trait TemplateVariables $template = EmailTemplate::findSlug($template); } + if($this->__get($template->filter_attribute) === false) { + return false; + } + \Mail::to($this)->send($template->getMailableFor($this)); + return true; } /** * @param $subject * @param $content * @todo Broken + * @deprecated * Fills subject and content, then sends mail */ public function sendTemplateEmail($subject, $content) diff --git a/app/User.php b/app/User.php index 2e8286c..cb34304 100644 --- a/app/User.php +++ b/app/User.php @@ -505,7 +505,7 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails */ public function scopeIncompleteProfiles(Builder $builder): void { - $builder->whereNull($this->profileFilledAttributes, 'or'); + $builder->where(fn(Builder $builder) => $builder->whereNull($this->profileFilledAttributes, 'or')); } /** @@ -634,7 +634,16 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails */ public function getProfileFilledAttribute(): bool { - return !in_array(null, $this->only($this->profileFilledAttributes)); + return ! $this->getProfileIncompleteAttribute(); + } + + /** + * @return bool + * Returns true if one $profileFilledAttributes is not filled + */ + public function getProfileIncompleteAttribute(): bool + { + return in_array(null, $this->only($this->profileFilledAttributes)); } /** diff --git a/database/migrations/2020_12_14_161125_add_scope_to_email_templates.php b/database/migrations/2020_12_14_161125_add_scope_to_email_templates.php new file mode 100644 index 0000000..9b7cb01 --- /dev/null +++ b/database/migrations/2020_12_14_161125_add_scope_to_email_templates.php @@ -0,0 +1,32 @@ +string('filter_attribute')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('email_templates', function (Blueprint $table) { + $table->dropColumn('filter_attribute'); + }); + } +} -- 2.39.5