Files
landing-api/app/Models/ReplyTemplate.php
Josep Piguillem 76d022053b Answer the inbox from the panel, with templates
A reply starts from a template filled in the language the person wrote in,
is sent by the application when it has a mailer that delivers or handed to
the replier's mail program otherwise, and is recorded on the submission.
The same is available through the API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 10:45:47 +02:00

65 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\ApplicationKind;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
/**
* An answer the inbox gives often enough to write once: "thanks, we'll be in
* touch", "this call is filled", "here is how we work".
*
* Written in every language, because the answer goes out in the one the
* person wrote in. `{name}`, `{subject}` and `{sender}` are filled in.
*/
#[Fillable(['name', 'kind', 'subject', 'body', 'position'])]
class ReplyTemplate extends Model
{
use HasFactory, HasTranslations;
public const PLACEHOLDERS = ['{name}', '{subject}', '{sender}'];
/**
* @var array<int, string>
*/
public array $translatable = ['subject', 'body'];
protected function casts(): array
{
return ['position' => 'integer'];
}
/**
* @param Builder<$this> $query
* @return Builder<$this>
*/
public function scopeFor(Builder $query, ApplicationKind $kind): Builder
{
return $query
->where(fn (Builder $q) => $q->whereNull('kind')->orWhere('kind', $kind->value))
->orderBy('position')
->orderBy('name');
}
/**
* Subject and body for one submission, in its language.
*
* @return array{subject: string, body: string}
*/
public function renderFor(Application $application, ?User $sender = null): array
{
$values = [$application->name, $application->subject(), $sender?->name ?? (string) config('app.name')];
return [
'subject' => str_replace(self::PLACEHOLDERS, $values, (string) $this->getTranslation('subject', $application->locale)),
'body' => str_replace(self::PLACEHOLDERS, $values, (string) $this->getTranslation('body', $application->locale)),
];
}
}