diff --git a/src/Pages/Page.php b/src/Pages/Page.php index 1276493a..25501461 100644 --- a/src/Pages/Page.php +++ b/src/Pages/Page.php @@ -77,9 +77,9 @@ public static function getSlug(): string return static::$slug ?? Str::kebab(static::$title ?? class_basename(static::class)); } - public static function getUrl(): string + public static function getUrl(array $parameters = [], bool $absolute = true): string { - return route(static::getRouteName()); + return route(static::getRouteName(), $parameters, $absolute); } protected function notify(string $status, string $message): void diff --git a/src/Resources/Pages/Concerns/HasRecordBreadcrumb.php b/src/Resources/Pages/Concerns/HasRecordBreadcrumb.php index 8691ae85..853bb177 100644 --- a/src/Resources/Pages/Concerns/HasRecordBreadcrumb.php +++ b/src/Resources/Pages/Concerns/HasRecordBreadcrumb.php @@ -13,13 +13,13 @@ protected function getBreadcrumbs(): array ]; if ($resource::hasRecordTitle()) { - if ($resource::canView($this->record)) { + if ($resource::canEdit($this->record)) { $breadcrumbs[ - $resource::getUrl('view', ['record' => $this->record]) + $resource::getUrl('edit', ['record' => $this->record]) ] = $this->getRecordTitle(); - } elseif ($resource::canEdit($this->record)) { + } elseif ($resource::canView($this->record)) { $breadcrumbs[ - $resource::getUrl('edit', ['record' => $this->record]) + $resource::getUrl('view', ['record' => $this->record]) ] = $this->getRecordTitle(); } else { $breadcrumbs[] = $this->getRecordTitle(); diff --git a/src/Resources/Pages/CreateRecord.php b/src/Resources/Pages/CreateRecord.php index 03a4e295..a72ed221 100644 --- a/src/Resources/Pages/CreateRecord.php +++ b/src/Resources/Pages/CreateRecord.php @@ -99,14 +99,14 @@ protected function getRedirectUrl(): ?string { $resource = static::getResource(); - if ($resource::canView($this->record)) { - return $resource::getUrl('view', ['record' => $this->record]); - } - if ($resource::canEdit($this->record)) { return $resource::getUrl('edit', ['record' => $this->record]); } + if ($resource::canView($this->record)) { + return $resource::getUrl('view', ['record' => $this->record]); + } + return null; } } diff --git a/src/Resources/Pages/EditRecord.php b/src/Resources/Pages/EditRecord.php index 0894604f..8c3becc6 100644 --- a/src/Resources/Pages/EditRecord.php +++ b/src/Resources/Pages/EditRecord.php @@ -75,8 +75,12 @@ public function delete(): void { abort_unless(static::getResource()::canDelete($this->record), 403); + $this->callHook('beforeDelete'); + $this->record->delete(); + $this->callHook('afterDelete'); + $this->redirect(static::getResource()::getUrl('index')); } @@ -146,12 +150,6 @@ protected function getForms(): array protected function getRedirectUrl(): ?string { - $resource = static::getResource(); - - if (! $resource::canView($this->record)) { - return null; - } - - return $resource::getUrl('view', ['record' => $this->record]); + return null; } } diff --git a/src/Resources/Pages/ListRecords.php b/src/Resources/Pages/ListRecords.php index 4c985134..9397e748 100644 --- a/src/Resources/Pages/ListRecords.php +++ b/src/Resources/Pages/ListRecords.php @@ -35,11 +35,10 @@ protected function getResourceTable(): Table $resource = static::getResource(); - if ($resource::hasPage('view')) { - $table->actions([$this->getViewLinkTableAction()]); - } elseif ($resource::hasPage('edit')) { - $table->actions([$this->getEditLinkTableAction()]); - } + $table->actions(array_merge( + ($resource::hasPage('view') ? [$this->getViewLinkTableAction()] : []), + ($resource::hasPage('edit') ? [$this->getEditLinkTableAction()] : []), + )); if ($resource::canDeleteAny()) { $table->bulkActions([$this->getDeleteTableBulkAction()]); diff --git a/src/Resources/RelationManagers/RelationManager.php b/src/Resources/RelationManagers/RelationManager.php index ed3404ca..68f21652 100644 --- a/src/Resources/RelationManagers/RelationManager.php +++ b/src/Resources/RelationManagers/RelationManager.php @@ -64,13 +64,13 @@ protected function callHook(string $hook): void protected function can(string $action, ?Model $record = null): bool { - $policy = Gate::getPolicyFor($this->getRelatedModel()); + $policy = Gate::getPolicyFor($model = $this->getRelatedModel()); - if ($policy === null || ! method_exists($policy, $action)) { + if ($policy === null || (! method_exists($policy, $action))) { return true; } - return Gate::check($action, $record); + return Gate::check($action, $record ?? $model); } protected function canAccess(): bool diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index b4fe1d91..42cc650c 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -75,13 +75,13 @@ public static function resolveRecordRouteBinding($key): ?Model public static function can(string $action, ?Model $record = null): bool { - $policy = Gate::getPolicyFor(static::getModel()); + $policy = Gate::getPolicyFor($model = static::getModel()); - if ($policy === null || ! method_exists($policy, $action)) { + if ($policy === null || (! method_exists($policy, $action))) { return true; } - return Gate::check($action, $record); + return Gate::check($action, $record ?? $model); } public static function canAccess(): bool @@ -152,14 +152,14 @@ public static function getGlobalSearchResultTitle(Model $record): string public static function getGlobalSearchResultUrl(Model $record): ?string { - if (static::canView($record)) { - return static::getUrl('view', ['record' => $record]); - } - if (static::canEdit($record)) { return static::getUrl('edit', ['record' => $record]); } + if (static::canView($record)) { + return static::getUrl('view', ['record' => $record]); + } + return null; }