@php $letter = $letter ?? null; $letterStatus = old('status', $letter->status ?? 1); // A template is not tied to a client or project: the active keywords of both staff types (Letter Management > // Keywords) are listed once each. On Generate, a keyword is filled with the details of the staff the letter // is for; one kept for a single staff type is filled only for staff of that type $letterSourceFields = collect(\App\Models\LetterKeyword::STAFF_TYPES)->mapWithKeys(fn ($type) => [ $type => \App\Services\LetterService::sourceFields($type), ]); $letterKeywords = collect(\App\Models\LetterKeyword::STAFF_TYPES) ->flatMap(fn ($type) => collect(\App\Services\LetterService::activeKeywords($type))->map(fn ($keyword) => $keyword + [ 'staff_type' => $type, 'source_label' => $letterSourceFields[$type][$keyword['source_field']] ?? $keyword['source_field'], ])) ->groupBy('keyword_key') ->map(fn ($types) => $types->first() + [ 'staff_types' => $types->pluck('staff_type')->all(), 'source_labels' => $types->pluck('source_label')->unique()->implode(' / '), 'display_names' => $types->groupBy('display_name')->map(fn ($displayTypes, $displayName) => [ 'display_name' => $displayName, 'staff_types' => $displayTypes->pluck('staff_type')->all(), 'source_labels' => $displayTypes->pluck('source_label')->unique()->implode(' / '), ])->values(), ]) ->sortBy('display_name', SORT_NATURAL | SORT_FLAG_CASE) ->values(); $letterStaffTypeCount = count(\App\Models\LetterKeyword::STAFF_TYPES); // The keywords grouped into searchable categories by what their source field is filled from $letterKeywordCategory = function (array $keyword) { if (in_array($keyword['source_field'], ['authorised_person', 'signatory_designation', 'founder_name'])) { return 'Authorised Signatory'; } foreach (\App\Services\LetterService::SOURCE_FIELDS as $group => $fields) { if (array_key_exists($keyword['source_field'], $fields)) { return [ 'Letter' => 'Letter Details', 'Staff' => 'Staff Details', 'Work' => 'Employment Details', 'Common' => 'Company Details', 'Company' => 'Client / Project Details', 'Client' => 'Client Details', 'Project' => 'Project Details', 'Salary' => 'Salary Details', ][$group] ?? $group; } } return 'Other'; }; $letterKeywordOrder = ['Staff Details', 'Client / Project Details', 'Client Details', 'Project Details', 'Employment Details', 'Salary Details', 'Letter Details', 'Company Details', 'Authorised Signatory', 'Other']; $letterKeywordGroups = $letterKeywords ->map(fn ($keyword) => $keyword + ['category' => $letterKeywordCategory($keyword)]) ->groupBy('category') ->sortBy(fn ($keywords, $category) => array_search($category, $letterKeywordOrder)); @endphp

Letter Preview (A4)

Highlighted fields are filled in when the letter is generated
{{-- The Title field only names the template; the preview shows the letter content alone, exactly as it will be printed. ck-content: the preview is styled by the same CKEditor content stylesheet as the editor, so tables, images and colour bars keep their shape --}}

{{ $letter ? 'Edit Letter' : 'Add Letter' }}

@error('title')
{{ $message }}
@enderror
@forelse ($letterKeywordGroups as $category => $keywords)
{{ $category }}
@foreach ($keywords as $keyword) @foreach ($keyword['display_names'] as $keywordDisplay) @php $displayLabel = count($keyword['display_names']) > 1 ? implode(' / ', $keywordDisplay['staff_types']) . ': ' . $keywordDisplay['display_name'] : $keywordDisplay['display_name']; @endphp {{ $displayLabel }} @endforeach @endforeach
@empty No active keywords. @endforelse
No keywords match the search. @error('content')
{{ $message }}
@enderror
Cancel @if (!$letter || $letter->isDraft()) @endif