Skip to content

Laravel Livewire configuration

When you are using email builder with Livewire, you can configure email builder via app/Livewire/EmailBuilder/Concerns/HasEmailBuilderConfig.php file.

Config content

Following is the content of the config file.

php
<?php

namespace App\Livewire\EmailBuilder\Concerns;

use Livewire\Attributes\On;
use Livewire\Attributes\Locked;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use App\EmailBuilder\Enums\EmailBuilderRoute;
use Apsonex\EmailBuilderLaravel\Facades\EmailBuilderLaravel;
use Apsonex\EmailBuilderLaravel\Support\EmailTemplatesManager;

trait HasEmailBuilderConfig
{
    #[Locked]
    public ?Model $emailTemplate = null;

    public function resolveEmailTemplateById($id)
    {
        $this->emailTemplate = EmailBuilderLaravel::emailTemplateQuery()->whereId($id)->firstOrFail();
    }

    public function goBack()
    {
        $this->redirect(
            EmailBuilderRoute::Web_Email_Templates_Index->url()
        );
    }

    protected function baseConfig(): array
    {
        return [
            // Logo for desktop and mobile screen
            // 'logo' => [
            //     'lg' => 'https://example.com/logo.png',
            //     'sm' => 'https://example.com/icon.png',
            // ],
            'fake' => false, // Use fake response for testing. It will not query AI apiUrl.
            'debug' => false, // Enable debugging. It will print console log
            'domain' => config('app.url'),
            'api_url' => config('email-builder-laravel.api_url'),
            // Some of the data can be cached.
            // Enable cache. Use s -> seconds, h -> hours, d -> days, m -> months for ttl
            // Note: cache should be enable in production only
            'cache' => [
                'enable' => !app()->environment('local'),
                'ttl' => '6H',
            ],
            'record' => [
                'index_url' => EmailBuilderRoute::Web_Email_Templates_Index->url(),
                'create_url' => EmailBuilderRoute::Web_Email_Templates_Create->url(),
                'edit_url' => EmailBuilderRoute::Web_Email_Templates_Edit->url(id: '__EMAIL_TEMPLATE_ID__'),
                'email_template' => $this->emailTemplate?->toArray(),
            ],
            // Provide initial config data
            'initial_config' => [
                'config' => \Apsonex\EmailBuilderLaravel\Controllers\InitializeController::initialConfig(),
            ],
        ];
    }

    /**
     * `onBroadcastAction` will trigger, when user is ready to publish the email template
     *  Use this method to redirect user to broadcast configuration page, so that user can select email receivables and configure other options.
     */
    public function onBroadcastAction(string|int $emailTemplateId, string $subject, string $html, string $emailConfig,  ?string $image = null, ?string $imageMime = null): array
    {
        return [
            'status' => 'success', // success or error
            'message' => 'Error', // message to display
            'redirectUrl' => url(''), // optional
        ];
    }

    /**
     * `onPrepareToSaveAction` will trigger, when email template is ready to be used and html is created.
     * This method will save the html with preview image and mark email ready to be used with some sort of automation or programmatically.
     * Suggest: https://automationcode.site/ plays nicely with email builder for automation.
     */
    public function onPrepareToSaveAction(string|int $emailTemplateId, string $subject, string $html, string $emailConfig,  ?string $image = null, ?string $imageMime = null): array
    {
        $payload = [
            'subject' => $subject,
            'emailConfig' => $emailConfig,
            'html' => $html,
            'image' => $image,
            'imageMime' => $imageMime,
        ];
        $validator = Validator::make($payload, [
            'subject' => 'required|string',
            'emailConfig' => 'required|string',
            'html' => 'nullable|string',
            'image' => 'nullable|string',
            'imageMime' => 'nullable|string|in:image/png,image/webp',
        ]);

        if ($validator->fails()) {
            return [
                'status' => 'error', // success or error
                'message' => implode(' ', $validator->errors()->all()), // message to display
            ];
        }

        EmailTemplatesManager::make()->update($emailTemplateId, $validator->validated());

        return [
            'status' => 'success', // success or error
            'message' => 'Email template updated', // message to display
            'redirectUrl' => EmailBuilderRoute::Web_Email_Templates_Index->url(), // optional
        ];
    }

    /**
     * Track tokens usage on subject creation
     */
    #[On('on-subject-created')]
    public function onSubjectCreated($finishReason, $inputTokens,  $outputTokens, $totalTokens)
    {
        //
    }

    /**
     * Track tokens usage on block creation
     */
    #[On('on-block-created')]
    public function onBlockCreated($finishReason, $inputTokens,  $outputTokens, $totalTokens)
    {
        //
    }

    /**
     * Track tokens usage on email creation
     */
    #[On('on-email-created')]
    public function onEmailCreated($finishReason, $inputTokens,  $outputTokens, $totalTokens)
    {
        //
    }
}