authorize('customers.manage'); } public function create(): void { $this->authorize('customers.manage'); $data = $this->validate(); MailTemplate::create([ 'name' => trim($data['name']), 'subject' => trim($data['subject']), 'body' => $data['body'], // At the end of the list, where a new one belongs until somebody // decides otherwise. 'sort' => (int) MailTemplate::query()->max('sort') + 10, 'active' => true, ]); $this->reset(['name', 'subject', 'body']); $this->dispatch('notify', message: __('templates.created')); } /** * Retire one, or bring it back. * * Not deleted: an answer that is no longer given is still the answer * somebody was given last year, and the register of sent mail refers to it. */ public function toggleActive(string $uuid): void { $this->authorize('customers.manage'); $template = MailTemplate::query()->where('uuid', $uuid)->first(); if ($template !== null) { $template->update(['active' => ! $template->active]); } } /** Move one up or down the list an operator picks from. */ public function move(string $uuid, int $by): void { $this->authorize('customers.manage'); $template = MailTemplate::query()->where('uuid', $uuid)->first(); if ($template !== null) { $template->update(['sort' => max(0, $template->sort + $by)]); } } public function render() { $this->authorize('customers.manage'); return view('livewire.admin.mail-templates', [ 'templates' => MailTemplate::query()->orderBy('sort')->orderBy('name')->get(), // The list IS the documentation: a token added to the renderer // appears here without anybody remembering to write it down twice. 'placeholders' => MailTemplateRenderer::PLACEHOLDERS, ]); } }