Skip to content

Commit

Permalink
Render template with global variables (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 authored Mar 29, 2023
1 parent 2951b84 commit 4c09794
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Core/EmailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, '<mjml>')) {
$contentHtml = self::renderMjml($content);
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/EscolaLmsTemplatesEmailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,6 +147,8 @@ function () {
));
}
);

Setting::observe(SettingObserver::class);
}

public function bootForConsole()
Expand Down
32 changes: 32 additions & 0 deletions src/Jobs/CompleteGlobalVariableJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace EscolaLms\TemplatesEmail\Jobs;

use EscolaLms\Templates\Models\TemplateSection;
use EscolaLms\TemplatesEmail\Core\EmailChannel;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CompleteGlobalVariableJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function handle(): void
{
$templateSections = TemplateSection::where('content', 'LIKE', '%' . $this->name . '%')->get();

foreach ($templateSections as $section) {
EmailChannel::processTemplateAfterSaving($section->template);
}
}
}
17 changes: 17 additions & 0 deletions src/Observers/SettingObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace EscolaLms\TemplatesEmail\Observers;

use EscolaLms\Settings\Models\Setting;
use EscolaLms\TemplatesEmail\Jobs\CompleteGlobalVariableJob;
use Illuminate\Support\Str;

class SettingObserver
{
public function saved(Setting $setting): void
{
$key = Str::ucfirst($setting->key) . Str::ucfirst($setting->type);
$name = '@GlobalSettings' . Str::ucfirst(Str::camel(preg_replace('/[^a-z0-9]+/i', ' ', ($key))));
CompleteGlobalVariableJob::dispatch($name);
}
}
57 changes: 57 additions & 0 deletions tests/Feature/SettingObserverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace EscolaLms\TemplatesEmail\Tests\Feature;

use EscolaLms\Settings\Models\Setting;
use EscolaLms\Templates\Core\SettingsVariables;
use EscolaLms\Templates\Models\Template;
use EscolaLms\Templates\Models\TemplateSection;
use EscolaLms\TemplatesEmail\Core\EmailChannel;
use EscolaLms\TemplatesEmail\Core\EmailVariables;
use EscolaLms\TemplatesEmail\Tests\Mocks\TestEvent;
use EscolaLms\TemplatesEmail\Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Config;

class SettingObserverTest extends TestCase
{
use DatabaseTransactions;

public function setUp(): void
{
parent::setUp();
if (!class_exists(\EscolaLms\Settings\EscolaLmsSettingsServiceProvider::class)) {
$this->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);
}
}

0 comments on commit 4c09794

Please sign in to comment.