Skip to content

Commit

Permalink
Merge pull request #9 from abdosaeedelhassan/dev
Browse files Browse the repository at this point in the history
Allow changing navigation group from plugin object
  • Loading branch information
cannycookie authored Aug 18, 2024
2 parents a2ffad0 + 37cf7a1 commit 37cc384
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 130 deletions.
142 changes: 76 additions & 66 deletions config/filament-email-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
/**
* If you wish to customise the table name change this before migration
*/
'table_name' => 'vb_email_templates',
'theme_table_name' => 'vb_email_templates_themes',
'table_name' => 'vb_email_templates',
'theme_table_name' => 'vb_email_templates_themes',


/**
* Mail Classes will be generated into this directory
*/
"mailable_directory" => 'Mail/Visualbuilder/EmailTemplates',
"mailable_directory" => 'Mail/Visualbuilder/EmailTemplates',

/**
* If you want to use your own token helper replace this class
Expand All @@ -31,110 +31,120 @@
* }
*/

'tokenHelperClass' => \Visualbuilder\EmailTemplates\DefaultTokenHelper::class,
'tokenHelperClass' => \Visualbuilder\EmailTemplates\DefaultTokenHelper::class,


/**
* Some tokens don't belong to a model. These $models->token will be checked
*/
'known_tokens' => [
'tokenUrl',
'verificationUrl',
'message'
],
'known_tokens' => [
'tokenUrl',
'verificationUrl',
'message'
],

/**
* Admin panel navigation options
*/
'navigation' => [
'templates' => [
'sort' => 10,
'label' => 'Email Templates',
'icon' => 'heroicon-o-envelope',
'group' => 'Content',
'cluster' => false,
'position' => \Filament\Pages\SubNavigationPosition::Top
],
'themes' => [
'sort' => 20,
'label' => 'Email Template Themes',
'icon' => 'heroicon-o-paint-brush',
'group' => 'Content',
'cluster' => false,
'position' => \Filament\Pages\SubNavigationPosition::Top
],
'navigation' => [
'templates' => [
'sort' => 10,
'label' => 'Email Templates',
'icon' => 'heroicon-o-envelope',
'group' => 'Content',
'cluster' => false,
'position' => \Filament\Pages\SubNavigationPosition::Top
],
'themes' => [
'sort' => 20,
'label' => 'Email Template Themes',
'icon' => 'heroicon-o-paint-brush',
'group' => 'Content',
'cluster' => false,
'position' => \Filament\Pages\SubNavigationPosition::Top
],
],

//Email templates will be copied to resources/views/vendor/vb-email-templates/email
//default.blade.php is base view that can be customised below
'default_view' => 'default',
'default_view' => 'default',

'template_view_path' => 'vb-email-templates::email',
'template_view_path' => 'vb-email-templates::email',

'template_keys' => [
'user-welcome'=>'User Welcome Email',
'user-request-reset'=>'User Request Password Reset',
'user-password-reset-success'=>'User Password Reset',
'user-locked-out'=>'User Account Locked Out',
'user-verify-email'=>'User Verify Email',
'user-verified'=>'User Verified',
'user-login'=>'User Logged In',
],

//Default Logo
'logo' => 'media/email-templates/logo.png',
'logo' => 'media/email-templates/logo.png',

//Browsed Logo
'browsed_logo' => 'media/email-templates/logos',
'browsed_logo' => 'media/email-templates/logos',

//Logo size in pixels -> 200 pixels high is plenty big enough.
'logo_width' => '500',
'logo_height' => '126',
'logo_width' => '500',
'logo_height' => '126',

//Content Width in Pixels
'content_width' => '600',
'content_width' => '600',

//Contact details included in default email templates
'customer-services' => ['email' => '[email protected]',
'phone' => '+441273 455702'],
'customer-services' => ['email' => '[email protected]',
'phone' => '+441273 455702'],

//Footer Links
'links' => [
['name' => 'Website', 'url' => 'https://yourwebsite.com', 'title' => 'Goto website'],
['name' => 'Privacy Policy', 'url' => 'https://yourwebsite.com/privacy-policy', 'title' => 'View Privacy Policy'],
],
'links' => [
['name' => 'Website', 'url' => 'https://yourwebsite.com', 'title' => 'Goto website'],
['name' => 'Privacy Policy', 'url' => 'https://yourwebsite.com/privacy-policy', 'title' => 'View Privacy Policy'],
],

//Options for alternative languages
//Note that Laravel default locale is just 'en' you can use this but
//we are being more specific to cater for English vs USA languages
'default_locale' => 'en_GB',
'default_locale' => 'en_GB',

//These will be included in the language picker when editing an email template
'languages' => [
'en_GB' => ['display' => 'British', 'flag-icon' => 'gb'],
'en_US' => ['display' => 'USA', 'flag-icon' => 'us'],
'es' => ['display' => 'Español', 'flag-icon' => 'es'],
'fr' => ['display' => 'Français', 'flag-icon' => 'fr'],
'pt' => ['display' => 'Brasileiro', 'flag-icon' => 'br'],
'in' => ['display' => 'Hindi', 'flag-icon' => 'in'],
],
'languages' => [
'en_GB' => ['display' => 'British', 'flag-icon' => 'gb'],
'en_US' => ['display' => 'USA', 'flag-icon' => 'us'],
'es' => ['display' => 'Español', 'flag-icon' => 'es'],
'fr' => ['display' => 'Français', 'flag-icon' => 'fr'],
'pt' => ['display' => 'Brasileiro', 'flag-icon' => 'br'],
'in' => ['display' => 'Hindi', 'flag-icon' => 'in'],
],

//Notifiable Models who can receive emails
'recipients' => [
App\Models\User::class,
],
'recipients' => [
App\Models\User::class,
],

/**
* Allowed config keys which can be inserted into email templates
* eg use ##config.app.name## in the email template for automatic replacement.
*/
'config_keys' => [
'app.name',
'app.url',
'email-templates.customer-services'
// Add other safe config keys here.
// We don't want to allow all config keys they may contain secret keys or credentials
],
'config_keys' => [
'app.name',
'app.url',
'email-templates.customer-services'
// Add other safe config keys here.
// We don't want to allow all config keys they may contain secret keys or credentials
],

//Most built-in emails can be automatically sent with minimal setup,
//except "request password reset" requires a function in the User's model. See readme.md for details
'send_emails' => [
'new_user_registered' => true,
'verification' => true,
'user_verified' => true,
'login' => true,
'password_reset_success' => true,
'locked_out' => true,
],
'send_emails' => [
'new_user_registered' => true,
'verification' => true,
'user_verified' => true,
'login' => true,
'password_reset_success' => true,
'locked_out' => true,
],

];
1 change: 1 addition & 0 deletions database/migrations/create_email_templates_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ return new class extends Migration
$table->string('title', 50)->nullable()->comment('First line of email h1 string');
$table->text('content')->nullable();
$table->string('logo', 191)->nullable();
$table->integer('ordering')->nullable();
$table->timestamps();
$table->softDeletes();
$table->unique(['key', 'language']);
Expand Down
13 changes: 13 additions & 0 deletions src/EmailTemplatesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class EmailTemplatesPlugin implements Plugin
{
public string $navigationGroup;

public static function make(): static
{
return app(static::class);
Expand All @@ -24,6 +26,17 @@ public function getId(): string
return 'filament-email-templates';
}

public function navigationGroup($navigationGroup): static
{
$this->navigationGroup = $navigationGroup;
return $this;
}

public function getNavigationGroup(): string
{
return $this->navigationGroup ?? config('filament-email-templates.navigation.templates.group');
}

public function register(Panel $panel): void
{
$panel->resources([
Expand Down
65 changes: 33 additions & 32 deletions src/Models/EmailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,28 @@ class EmailTemplate extends Model
* @var array
*/
protected $fillable = [
'from',
'key',
'name',
'view',
'subject',
'title',
'preheader',
'content',
'language',
'logo',
'from',
'key',
'name',
'view',
'subject',
'title',
'preheader',
'content',
'language',
'logo',
'ordering',

];

/**
* @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',
'from' => 'array',
'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',
'from' => 'array',
];
/**
* @var string[]
Expand Down Expand Up @@ -93,9 +94,9 @@ public static function findEmailByKey($key, $language = null)
//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('filament-email-templates.default_locale'))
->where("key", $key)
->firstOrFail();
->language($language ?? config('filament-email-templates.default_locale'))
->where("key", $key)
->firstOrFail();
});
}

Expand Down Expand Up @@ -162,13 +163,13 @@ public function getEmailPreviewData()
$models = self::createEmailPreviewData();

return [
'user' => $models->user,
'content' => TokenHelper::replace($this->content, $models),
'subject' => TokenHelper::replace($this->subject, $models),
'preHeaderText' => TokenHelper::replace($this->preheader, $models),
'title' => TokenHelper::replace($this->title, $models),
'theme' => $this->theme->colours,
'logo' => $this->logo,
'user' => $models->user,
'content' => TokenHelper::replace($this->content ?? '', $models),
'subject' => TokenHelper::replace($this->subject ?? '', $models),
'preHeaderText' => TokenHelper::replace($this->preheader ?? '', $models),
'title' => TokenHelper::replace($this->title ?? '', $models),
'theme' => $this->theme->colours,
'logo' => $this->logo,
];
}

Expand All @@ -177,7 +178,7 @@ public function getEmailPreviewData()
*/
public static function createEmailPreviewData()
{
$models = (object) [];
$models = (object)[];

$userModel = config('filament-email-templates.recipients')[0];
//Setup some data for previewing email template
Expand All @@ -195,7 +196,7 @@ public static function createEmailPreviewData()
/**
* Efficient method to return requested template locale or default language template in one query
*
* @param Builder $query
* @param Builder $query
* @param $language
*
* @return Builder
Expand All @@ -205,10 +206,10 @@ public function scopeLanguage(Builder $query, $language)
$languages = [$language, config('filament-email-templates.default_locale')];

return $query->whereIn('language', $languages)
->orderByRaw(
"(CASE WHEN language = ? THEN 1 ELSE 2 END)",
[$language]
);
->orderByRaw(
"(CASE WHEN language = ? THEN 1 ELSE 2 END)",
[$language]
);
}

/**
Expand All @@ -217,7 +218,7 @@ public function scopeLanguage(Builder $query, $language)
public function viewPath(): Attribute
{
return new Attribute(
get: fn () => config('filament-email-templates.template_view_path').'.'.$this->view
get: fn() => config('filament-email-templates.template_view_path') . '.' . $this->view
);
}

Expand All @@ -227,7 +228,7 @@ public function viewPath(): Attribute
public function getMailableExistsAttribute(): bool
{
$className = Str::studly($this->key);
$filePath = app_path(config('filament-email-templates.mailable_directory')."/{$className}.php");
$filePath = app_path(config('filament-email-templates.mailable_directory') . "/{$className}.php");

return File::exists($filePath);
}
Expand Down
Loading

0 comments on commit 37cc384

Please sign in to comment.