]> _ Git - psq.git/commitdiff
add email template filter
authorLouis Jeckel <louis.jeckel@outlook.com>
Mon, 14 Dec 2020 16:42:20 +0000 (17:42 +0100)
committerLouis Jeckel <louis.jeckel@outlook.com>
Mon, 14 Dec 2020 16:42:20 +0000 (17:42 +0100)
app/EmailTemplate.php
app/TemplateVariables.php
app/User.php
database/migrations/2020_12_14_161125_add_scope_to_email_templates.php [new file with mode: 0644]

index b01071714bbdd05041092ddabd8a7a46983faf6e..5f7e21fc4a94c54d0322e5a7a44677d072e9424d 100644 (file)
@@ -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
index 9addcfd486304c83192ea8a4e60d78984f69586d..c74b8f80d78f4c0da200726997ec5d3046ab1af5 100644 (file)
@@ -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)
index 2e8286c100ca6ea9f4ecc46d6a62bd0f9a0f2140..cb34304970cb5220427d7d5de76cacedee039951 100644 (file)
@@ -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 (file)
index 0000000..9b7cb01
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddScopeToEmailTemplates extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('email_templates', function (Blueprint $table) {
+            $table->string('filter_attribute')->nullable();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('email_templates', function (Blueprint $table) {
+            $table->dropColumn('filter_attribute');
+        });
+    }
+}