Skip to content

Commit

Permalink
Always load theme relation on emailTempate to save another db call wh…
Browse files Browse the repository at this point in the history
…en previewing
  • Loading branch information
cannycookie committed Sep 7, 2023
1 parent a65cc7e commit 8db8262
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions src/Models/EmailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,44 @@ class EmailTemplate extends Model
'language',
'send_to',
];

/**
* @var string[]
*/
protected $casts = [
'deleted_at' => 'datetime:Y-m-d H:i:s',
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
];
/**
* @var string[]
*/
protected $dates = ['deleted_at'];

/**
* The relationships that should always be loaded.
*
* @var array
*/
protected $with = ['theme'];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setTableFromConfig();
}


public function setTableFromConfig()
{
$this->table = config('email-templates.table_name');
}


public static function findEmailByKey($key, $language = null)
{
$cacheKey = "email_by_key_{$key}_{$language}";

//For multi site domains this key will need to include the site_id
return Cache::remember($cacheKey, now()->addMinutes(60), function () use ($key, $language) {
return self::query()
->language($language ?? config('email-templates.default_locale'))
Expand All @@ -83,16 +99,25 @@ public static function findEmailByKey($key, $language = null)
});
}

/**
* @return \Illuminate\Support\Collection
*/
public static function getSendToSelectOptions()
{
return collect(config('emailTemplate.recipients'));
}

/**
* @return EmailTemplateFactory
*/
protected static function newFactory()
{
return EmailTemplateFactory::new();
}

/**
* @return string
*/
public function __toString()
{
return $this->name ?? class_basename($this);
Expand Down Expand Up @@ -129,31 +154,38 @@ public function getBase64EmailPreviewData()
return base64_encode($content);
}

/**
* @return array
*/
public function getEmailPreviewData()
{

$model = self::createEmailPreviewData();

return [
'user' => $model->user,
'content' => $this->replaceTokens($this->content, $model),
'subject' => $this->replaceTokens($this->subject, $model),
'user' => $model->user,
'content' => $this->replaceTokens($this->content, $model),
'subject' => $this->replaceTokens($this->subject, $model),
'preHeaderText' => $this->replaceTokens($this->preheader, $model),
'title' => $this->replaceTokens($this->title, $model),
'theme' => $this->theme->colours,
'title' => $this->replaceTokens($this->title, $model),
'theme' => $this->theme->colours,
];
}

/**
* @return object
*/
public static function createEmailPreviewData()
{
$model = (object) [];

$userModel = config('email-templates.recipients')[0];
//Setup some data for previewing email template
$model->user = $userModel::first();

$model->tokenUrl = URL::to('/');
$model->verificationUrl = URL::to('/');
$model->expiresAt = now();
/* Not used in preview but need to add something */
$model->plainText = Str::random(32);

return $model;
Expand Down Expand Up @@ -182,7 +214,7 @@ public function scopeLanguage(Builder $query, $language)
public function viewPath(): Attribute
{
return new Attribute(
get: fn () => config('email-templates.template_view_path').'.'.$this->view
get: fn() => config('email-templates.template_view_path').'.'.$this->view
);
}

Expand All @@ -207,7 +239,7 @@ public function getMailableClass()
$directory = str_replace('/', '\\', config('email-templates.mailable_directory', 'Mail/Visualbuilder/EmailTemplates')); // Convert slashes to namespace format
$fullClassName = "\\App\\{$directory}\\{$className}";

if (! class_exists($fullClassName)) {
if (!class_exists($fullClassName)) {
throw new \Exception("Mailable class {$fullClassName} does not exist.");
}

Expand Down

0 comments on commit 8db8262

Please sign in to comment.