Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 119 additions & 90 deletions src/Http/Controllers/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,126 +24,77 @@ public function __construct(

public function create(string $globalFilter, string $parentUri, EntityKey $entityKey)
{
$entity = $this->entityManager->entityFor($entityKey);

$form = $entity->getFormOrFail($entityKey->multiformKey());
[$entity, $form] = $this->resolveEntityAndForm($entityKey);

if ($form instanceof SharpSingleForm) {
// There is no creation in SingleForms
return $this->edit($globalFilter, $parentUri, $entityKey);
}

$this->authorizationManager->check('create', $entityKey);
$form->buildFormConfig();
$formData = $form->newInstance();

if (app()->environment('testing')) {
Inertia::share('_rawData', $formData);
}

return Inertia::render('Form/Form', [
'form' => FormData::from([
...$this->buildFormData($form, $formData, $entityKey),
'title' => $form->getCreateTitle() ?: trans('sharp::breadcrumb.form.create_entity', [
'entity' => $entity->getLabelOrFail($entityKey->multiformKey()),
]),
]),
'breadcrumb' => BreadcrumbData::from([
'items' => sharp()->context()->breadcrumb()->allSegments(),
$formData = $this->buildFormInstanceData($form, null);

return $this->renderFormView(
form: $form,
formData: $formData,
entityKey: $entityKey,
instanceId: null,
title: $form->getCreateTitle() ?: trans('sharp::breadcrumb.form.create_entity', [
'entity' => $entity->getLabelOrFail($entityKey->multiformKey()),
]),
'cancelUrl' => sharp()->context()->breadcrumb()->getPreviousSegmentUrl(),
'endpointUrl' => route('code16.sharp.form.store', [
cancelUrl: sharp()->context()->breadcrumb()->getPreviousSegmentUrl(),
endpointUrl: route('code16.sharp.form.store', [
'parentUri' => $parentUri,
'entityKey' => $entityKey,
'previous_page_url' => $this->getPreviousPageUrlFromReferer(),
]),
]);
);
}

public function edit(string $globalFilter, string $parentUri, EntityKey $entityKey, ?string $instanceId = null)
{
$entity = $this->entityManager->entityFor($entityKey);
[$entity, $form] = $this->resolveEntityAndForm($entityKey);

$this->authorizationManager->check(
$entity->hasShow() ? 'update' : 'view',
$entityKey,
$instanceId
);

$form = $entity->getFormOrFail($entityKey->multiformKey());
$this->ensureInstanceIdMatchesForm($form, $instanceId);

abort_if(
(! $instanceId && ! $form instanceof SharpSingleForm)
|| ($instanceId && $form instanceof SharpSingleForm),
404,
);

$form->buildFormConfig();
$formData = $this->buildFormInstanceData($form, $instanceId);
$titleEntityLabel = $this->resolveTitleEntityLabel($form, $formData, $entity, $entityKey);

$formData = $form instanceof SharpSingleForm || $instanceId !== null
? $form->instance($instanceId)
: $form->newInstance();

if ($breadcrumbLabel = $form->getBreadcrumbCustomLabel($formData)) {
sharp()->context()->breadcrumb()->setCurrentInstanceLabel($breadcrumbLabel);
$titleEntityLabel = $breadcrumbLabel;
}

$titleEntityLabel ??= sharp()
->context()
->breadcrumb()
->getParentShowCachedBreadcrumbLabel() ?: $entity->getLabelOrFail($entityKey->multiformKey());

if (app()->environment('testing')) {
Inertia::share('_rawData', $formData);
}

return Inertia::render('Form/Form', [
'form' => FormData::from([
...$this->buildFormData($form, $formData, $entityKey, $instanceId),
'title' => $form->getEditTitle() ?: trans('sharp::breadcrumb.form.edit_entity', [
'entity' => $titleEntityLabel,
]),
]),
'breadcrumb' => BreadcrumbData::from([
'items' => sharp()->context()->breadcrumb()->allSegments(),
return $this->renderFormView(
form: $form,
formData: $formData,
entityKey: $entityKey,
instanceId: $instanceId,
title: $form->getEditTitle() ?: trans('sharp::breadcrumb.form.edit_entity', [
'entity' => $titleEntityLabel,
]),
'cancelUrl' => $this->previousUrlWithHighlightedQuery(
cancelUrl: $this->previousUrlWithHighlightedQuery(
sharp()->context()->breadcrumb()->getPreviousSegmentUrl(),
$entityKey,
$instanceId
),
'endpointUrl' => route('code16.sharp.form.update', [
endpointUrl: route('code16.sharp.form.update', [
'parentUri' => $parentUri,
'entityKey' => $entityKey,
'instanceId' => $instanceId,
'previous_page_url' => $this->getPreviousPageUrlFromReferer(),
]),
]);
);
}

public function update(string $globalFilter, string $parentUri, EntityKey $entityKey, ?string $instanceId = null)
{
$this->authorizationManager->check('update', $entityKey, $instanceId);

$form = $this->entityManager
->entityFor($entityKey)
->getFormOrFail($entityKey->multiformKey());

abort_if(
(! $instanceId && ! $form instanceof SharpSingleForm)
|| ($instanceId && $form instanceof SharpSingleForm),
404,
);

$formattedData = $form->formatAndValidateRequestData(request()->all(), $instanceId);
$instanceId = $form->update($instanceId, $formattedData);

if ($instanceId === null && ! $form instanceof SharpSingleForm) {
report(new SharpFormUpdateException('The update() method in '.get_class($form).' must return the newly created instance id'));
}

$this->uploadManager->dispatchJobs($instanceId);
[, $form] = $this->resolveEntityAndForm($entityKey);
$this->ensureInstanceIdMatchesForm($form, $instanceId);
$instanceId = $this->persistForm($form, $instanceId);

$previousUrl = request()->query('previous_page_url') ?: sharp()->context()->breadcrumb()->getPreviousSegmentUrl();

Expand All @@ -152,9 +103,7 @@ public function update(string $globalFilter, string $parentUri, EntityKey $entit

public function store(string $globalFilter, string $parentUri, EntityKey $entityKey)
{
$form = $this->entityManager
->entityFor($entityKey)
->getFormOrFail($entityKey->multiformKey());
[, $form] = $this->resolveEntityAndForm($entityKey);

if ($form instanceof SharpSingleForm) {
// There is no creation in SingleForms
Expand All @@ -163,14 +112,7 @@ public function store(string $globalFilter, string $parentUri, EntityKey $entity

$this->authorizationManager->check('create', $entityKey);
$form->buildFormConfig();
$formattedData = $form->formatAndValidateRequestData(request()->all());
$instanceId = $form->update(null, $formattedData);

if ($instanceId === null) {
report(new SharpFormUpdateException('The update() method in '.get_class($form).' must return the newly created instance id'));
}

$this->uploadManager->dispatchJobs($instanceId);
$instanceId = $this->persistForm($form, null);

$previousUrl = sharp()->context()->breadcrumb()->getPreviousSegmentUrl();

Expand Down Expand Up @@ -198,6 +140,93 @@ private function previousUrlWithHighlightedQuery(string $url, EntityKey $entityK
]);
}

/**
* @return array{0: mixed, 1: SharpForm}
*/
private function resolveEntityAndForm(EntityKey $entityKey): array
{
$entity = $this->entityManager->entityFor($entityKey);

return [$entity, $entity->getFormOrFail($entityKey->multiformKey())];
}

private function ensureInstanceIdMatchesForm(SharpForm $form, ?string $instanceId): void
{
abort_if(
(! $instanceId && ! $form instanceof SharpSingleForm)
|| ($instanceId && $form instanceof SharpSingleForm),
404,
);
}

private function buildFormInstanceData(SharpForm $form, ?string $instanceId): array
{
$form->buildFormConfig();

return $form instanceof SharpSingleForm || $instanceId !== null
? $form->instance($instanceId)
: $form->newInstance();
}

private function resolveTitleEntityLabel(
SharpForm $form,
array $formData,
mixed $entity,
EntityKey $entityKey,
): string {
if ($breadcrumbLabel = $form->getBreadcrumbCustomLabel($formData)) {
sharp()->context()->breadcrumb()->setCurrentInstanceLabel($breadcrumbLabel);

return $breadcrumbLabel;
}

return sharp()
->context()
->breadcrumb()
->getParentShowCachedBreadcrumbLabel()
?: $entity->getLabelOrFail($entityKey->multiformKey());
}

private function renderFormView(
SharpForm $form,
array $formData,
EntityKey $entityKey,
?string $instanceId,
string $title,
string $cancelUrl,
string $endpointUrl,
) {
if (app()->environment('testing')) {
Inertia::share('_rawData', $formData);
}

return Inertia::render('Form/Form', [
'form' => FormData::from([
...$this->buildFormData($form, $formData, $entityKey, $instanceId),
'title' => $title,
]),
'breadcrumb' => BreadcrumbData::from([
'items' => sharp()->context()->breadcrumb()->allSegments(),
]),
'cancelUrl' => $cancelUrl,
'endpointUrl' => $endpointUrl,
]);
}

private function persistForm(SharpForm $form, ?string $instanceId): ?string
{
$formattedData = $form->formatAndValidateRequestData(request()->all(), $instanceId);
$instanceId = $form->update($instanceId, $formattedData);

if ($instanceId === null && ! $form instanceof SharpSingleForm) {
report(new SharpFormUpdateException('The update() method in '.get_class($form).' must return the newly created instance id'));
}

$this->uploadManager->dispatchJobs($instanceId);

return $instanceId;
}

private function getPreviousPageUrlFromReferer(): ?string
{
if (! request()->header('referer')) {
Expand Down
Loading