v0.9.11
This commit is contained in:
commit
c63e98e91e
297 files changed
+20546
No files matched your search
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Resources;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use IriksIT\Manager\Models\BlockTemplate;
|
||||
use IriksIT\Manager\Support\AuditTrail;
|
||||
use IriksIT\Manager\Support\Blocks;
|
||||
use IriksIT\Manager\Support\Resources;
|
||||
|
||||
/**
|
||||
* The page layout of a list (`manager.resources.<key>.template`): the blocks of the page of one record, edited with the same block editor
|
||||
* as a page. Whoever may manage the list (`<key>.manage`) may change its layout; nobody else sees it.
|
||||
*/
|
||||
class TemplateController
|
||||
{
|
||||
public function show(string $resource): JsonResponse
|
||||
{
|
||||
$definition = $this->resource($resource);
|
||||
|
||||
return response()->json($this->payload($definition));
|
||||
}
|
||||
|
||||
public function update(Request $request, string $resource): JsonResponse
|
||||
{
|
||||
$definition = $this->resource($resource);
|
||||
$allowed = Blocks::templateTypes($resource);
|
||||
|
||||
$data = $request->validate(Blocks::rules((array) $request->input('blocks', []), $allowed, bindings: true));
|
||||
|
||||
$template = BlockTemplate::query()->firstOrNew(['resource' => $resource]);
|
||||
$template->blocks = Blocks::clean((array) ($data['blocks'] ?? []), $allowed);
|
||||
$template->save();
|
||||
app(AuditTrail::class)->about($template);
|
||||
|
||||
return response()->json($this->payload($definition));
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function resource(string $key): array
|
||||
{
|
||||
$definition = Resources::find($key);
|
||||
abort_if($definition === null || $definition['template'] === false, 404);
|
||||
Gate::authorize($key.'.manage');
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $definition
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function payload(array $definition): array
|
||||
{
|
||||
$template = BlockTemplate::query()->where('resource', $definition['key'])->first();
|
||||
$allowed = Blocks::templateTypes($definition['key']);
|
||||
|
||||
return [
|
||||
'resource' => ['key' => $definition['key'], 'label' => $definition['label'], 'singular' => $definition['singular'], 'simple_label' => $definition['simple_label'], 'simple_singular' => $definition['simple_singular']],
|
||||
// What `{record.<field>}` can name: the fields of the list, and the number of the record.
|
||||
'record_fields' => [['key' => 'id', 'label' => 'Number'], ...array_values(array_map(fn (array $field) => ['key' => $field['key'], 'label' => $field['label']], $definition['fields']))],
|
||||
'blocks' => Blocks::forEditor(is_array($template?->blocks) ? $template->blocks : []),
|
||||
'block_types' => Blocks::catalogue($allowed),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user