From 4c097940a701dda76f7bae99bbf5343b748ac4d4 Mon Sep 17 00:00:00 2001 From: Maciej Rymarz <59456825+mako321@users.noreply.github.com> Date: Wed, 29 Mar 2023 19:22:10 +0200 Subject: [PATCH] Render template with global variables (#71) --- src/Core/EmailChannel.php | 5 ++ ...EscolaLmsTemplatesEmailServiceProvider.php | 4 ++ src/Jobs/CompleteGlobalVariableJob.php | 32 +++++++++++ src/Observers/SettingObserver.php | 17 ++++++ tests/Feature/SettingObserverTest.php | 57 +++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 src/Jobs/CompleteGlobalVariableJob.php create mode 100644 src/Observers/SettingObserver.php create mode 100644 tests/Feature/SettingObserverTest.php diff --git a/src/Core/EmailChannel.php b/src/Core/EmailChannel.php index 26ea90a..18fc969 100644 --- a/src/Core/EmailChannel.php +++ b/src/Core/EmailChannel.php @@ -5,6 +5,7 @@ use EscolaLms\Core\Models\User; use EscolaLms\Templates\Contracts\TemplateChannelContract; use EscolaLms\Templates\Core\AbstractTemplateChannelClass; +use EscolaLms\Templates\Core\SettingsVariables; use EscolaLms\Templates\Core\TemplateSectionSchema; use EscolaLms\Templates\Enums\TemplateSectionTypeEnum; use EscolaLms\Templates\Events\EventWrapper; @@ -82,6 +83,10 @@ public static function processTemplateAfterSaving(Template $template): Template { $content = $template->sections()->where('key', 'content')->first()->content; + foreach (SettingsVariables::settings() as $key => $variable) { + $content = Str::replace($key, $variable['value'], $content); + } + if (Str::contains($content, '')) { $contentHtml = self::renderMjml($content); } else { diff --git a/src/EscolaLmsTemplatesEmailServiceProvider.php b/src/EscolaLmsTemplatesEmailServiceProvider.php index d8e20ec..b15e770 100644 --- a/src/EscolaLmsTemplatesEmailServiceProvider.php +++ b/src/EscolaLmsTemplatesEmailServiceProvider.php @@ -11,10 +11,12 @@ use EscolaLms\Consultations\EscolaLmsConsultationsServiceProvider; use EscolaLms\CourseAccess\EscolaLmsCourseAccessServiceProvider; use EscolaLms\Settings\Facades\AdministrableConfig; +use EscolaLms\Settings\Models\Setting; use EscolaLms\Tasks\EscolaLmsTasksServiceProvider; use EscolaLms\Templates\EscolaLmsTemplatesServiceProvider; use EscolaLms\Templates\Repository\Contracts\TemplateRepositoryContract; use EscolaLms\TemplatesEmail\Core\EmailChannel; +use EscolaLms\TemplatesEmail\Observers\SettingObserver; use EscolaLms\TemplatesEmail\Providers\AssignWithoutAccountTemplatesEventServiceProvider; use EscolaLms\TemplatesEmail\Providers\AuthTemplatesEventServiceProvider; use EscolaLms\TemplatesEmail\Providers\AuthTemplatesServiceProvider; @@ -145,6 +147,8 @@ function () { )); } ); + + Setting::observe(SettingObserver::class); } public function bootForConsole() diff --git a/src/Jobs/CompleteGlobalVariableJob.php b/src/Jobs/CompleteGlobalVariableJob.php new file mode 100644 index 0000000..b5bde49 --- /dev/null +++ b/src/Jobs/CompleteGlobalVariableJob.php @@ -0,0 +1,32 @@ +name = $name; + } + + public function handle(): void + { + $templateSections = TemplateSection::where('content', 'LIKE', '%' . $this->name . '%')->get(); + + foreach ($templateSections as $section) { + EmailChannel::processTemplateAfterSaving($section->template); + } + } +} diff --git a/src/Observers/SettingObserver.php b/src/Observers/SettingObserver.php new file mode 100644 index 0000000..9b83174 --- /dev/null +++ b/src/Observers/SettingObserver.php @@ -0,0 +1,17 @@ +key) . Str::ucfirst($setting->type); + $name = '@GlobalSettings' . Str::ucfirst(Str::camel(preg_replace('/[^a-z0-9]+/i', ' ', ($key)))); + CompleteGlobalVariableJob::dispatch($name); + } +} diff --git a/tests/Feature/SettingObserverTest.php b/tests/Feature/SettingObserverTest.php new file mode 100644 index 0000000..6f12feb --- /dev/null +++ b/tests/Feature/SettingObserverTest.php @@ -0,0 +1,57 @@ +markTestSkipped('Settings package not installed'); + } + } + + public function testShouldUpdateTemplateWhenGlobalVariableIsChanged(): void + { + $template = Template::factory()->create([ + 'channel' => EmailChannel::class, + 'event' => TestEvent::class, + ]); + + TemplateSection::factory(['key' => 'title', 'template_id' => $template->getKey()])->create(); + TemplateSection::factory([ + 'key' => 'content', + 'template_id' => $template->getKey(), + 'content' => EmailVariables::wrapWithMjml('@GlobalSettingsHeaderText'), + ]) + ->create(); + + SettingsVariables::clearSettings(); + Setting::firstOrCreate([ + 'group' => 'mail', + 'key' => 'header', + 'value' => 'Header Test', + 'public' => true, + 'enumerable' => true, + 'type' => 'text', + ]); + + $template->refresh(); + $contentHtmlSection = $template->sections->where('key', 'contentHtml')->first(); + $this->assertStringContainsString('Header Test', $contentHtmlSection->content); + } +}