48 lines
1.7 KiB
PHP
48 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Admin\Controllers;
|
|
|
|
use App\Models\DocumentAsset;
|
|
use App\Models\DocumentTemplate;
|
|
use App\Models\DocumentTypeCatalog;
|
|
use App\Scopes\CommonController;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class DocumentTemplatesGetController extends CommonController
|
|
{
|
|
public function __invoke(Request $request): JsonResponse
|
|
{
|
|
$documentType = (string) $request->input('type', DocumentTypeCatalog::default());
|
|
|
|
if (!DocumentTypeCatalog::has($documentType)) {
|
|
abort(422, 'Unbekannte Dokumentart.');
|
|
}
|
|
|
|
$blocks = DocumentTemplate::forType($documentType)
|
|
->values()
|
|
->map(fn(DocumentTemplate $block): array => [
|
|
'block' => $block->block,
|
|
'label' => DocumentTypeCatalog::blockLabel($documentType, $block->block),
|
|
'content' => (string) $block->content,
|
|
'editable' => $block->editable,
|
|
'source' => in_array($block->block, DocumentTypeCatalog::SOURCE_BLOCKS, true),
|
|
]);
|
|
|
|
return response()->json([
|
|
'documentType' => $documentType,
|
|
'documentTypes' => DocumentTypeCatalog::options(),
|
|
'blocks' => $blocks,
|
|
// Die Bilder gelten für alle Dokumentarten -- sie hängen nicht am Typ.
|
|
'assets' => DocumentAsset::orderBy('name')->get()->map(fn(DocumentAsset $asset): array => [
|
|
'name' => $asset->name,
|
|
'label' => $asset->label,
|
|
'mime' => $asset->mime,
|
|
'token' => '{asset:' . $asset->name . '}',
|
|
'preview' => $asset->toDataUri(),
|
|
]),
|
|
'tokenGroups' => DocumentTypeCatalog::tokenGroups($documentType),
|
|
]);
|
|
}
|
|
}
|