From 64534bcbbf70cdfc422cba8f32c9ff8bb3ff1847 Mon Sep 17 00:00:00 2001 From: Gilles Felix Date: Fri, 21 Jul 2023 17:22:12 +0200 Subject: [PATCH] Remove exception when model are not publishable --- src/Nova/Fields/ExpiredAt.php | 20 +++++++---- src/Nova/Fields/PublicationBadge.php | 38 +++++++++++---------- src/Nova/Fields/PublicationStatus.php | 49 ++++++++++++++------------- src/Nova/Fields/PublishedAt.php | 18 ++++++---- src/Nova/Fields/PublishedFirstAt.php | 34 +++++++++++++------ 5 files changed, 93 insertions(+), 66 deletions(-) diff --git a/src/Nova/Fields/ExpiredAt.php b/src/Nova/Fields/ExpiredAt.php index 9289b4d..f1662ec 100644 --- a/src/Nova/Fields/ExpiredAt.php +++ b/src/Nova/Fields/ExpiredAt.php @@ -8,7 +8,6 @@ use Laravel\Nova\Http\Requests\NovaRequest; use Novius\LaravelPublishable\Enums\PublicationStatus; use Novius\LaravelPublishable\Traits\Publishable; -use RuntimeException; /** * @method static static make(mixed $name = null, string|\Closure|callable|object|null $attribute = null, callable|null $resolveCallback = null) @@ -17,21 +16,27 @@ class ExpiredAt extends DateTime { public function __construct($name = null, $attribute = null, callable $resolveCallback = null) { + $name = $name ?? trans('laravel-nova-publishable::messages.fields.expired_at'); + $request = app()->get(NovaRequest::class); $resource = $request->newResource(); /** @var Publishable&Model $model */ $model = $resource->model(); - if (! in_array(Publishable::class, class_uses_recursive($model))) { - throw new RuntimeException('Resource must use trait Novius\LaravePublishable\Traits\Publishable'); + + $rules = ['nullable', 'date']; + $is_publishable = in_array(Publishable::class, class_uses_recursive($model)); + if ($is_publishable) { + $attribute = $attribute ?? $model->getExpiredAtColumn(); + $rules[] = 'after:'.$model->getPublishedAtColumn(); } - $name = $name ?? trans('laravel-nova-publishable::messages.fields.expired_at'); - $attribute = $attribute ?? $model->getExpiredAtColumn(); parent::__construct($name, $attribute, $resolveCallback); $this->nullable() - ->rules('nullable', 'date', 'after:'.$model->getPublishedAtColumn()) - ->dependsOn( + ->rules($rules); + + if ($is_publishable) { + $this->dependsOn( [$model->getPublicationStatusColumn()], function (DateTime $field, NovaRequest $request, FormData $formData) use ($model) { if ($formData->{$model->getPublicationStatusColumn()} === PublicationStatus::scheduled->value) { @@ -42,5 +47,6 @@ function (DateTime $field, NovaRequest $request, FormData $formData) use ($model } } ); + } } } diff --git a/src/Nova/Fields/PublicationBadge.php b/src/Nova/Fields/PublicationBadge.php index 4fd0153..4917a65 100644 --- a/src/Nova/Fields/PublicationBadge.php +++ b/src/Nova/Fields/PublicationBadge.php @@ -6,7 +6,6 @@ use Laravel\Nova\Fields\Badge; use Laravel\Nova\Http\Requests\NovaRequest; use Novius\LaravelPublishable\Traits\Publishable; -use RuntimeException; /** * @method static static make(mixed $name = null, string|\Closure|callable|object|null $attribute = null, callable|null $resolveCallback = null) @@ -15,33 +14,36 @@ class PublicationBadge extends Badge { public function __construct($name = null, $attribute = null, callable $resolveCallback = null) { + $name = $name ?? trans('laravel-nova-publishable::messages.fields.publication_status'); + $request = app()->get(NovaRequest::class); $resource = $request->newResource(); /** @var Publishable&Model $model */ $model = $resource->model(); - if (! in_array(Publishable::class, class_uses_recursive($model))) { - throw new RuntimeException('Resource must use trait Novius\LaravePublishable\Traits\Publishable'); - } - $name = $name ?? trans('laravel-nova-publishable::messages.fields.publication_status'); - parent::__construct($name, function () { - if ($this->resource->isPublished()) { - return 'success'; - } - if ($this->resource->willBePublished()) { - return 'warning'; + $is_publishable = in_array(Publishable::class, class_uses_recursive($model)); + + parent::__construct($name, function () use ($is_publishable) { + if ($is_publishable) { + if ($this->resource->isPublished()) { + return 'success'; + } + if ($this->resource->willBePublished()) { + return 'warning'; + } } return 'danger'; }, $resolveCallback); - $this->icons([ - 'danger' => 'ban', - 'warning' => 'clock', - 'success' => 'check', - ]) - ->label(function () { - return $this->resource->publicationLabel(); + $this + ->icons([ + 'danger' => 'ban', + 'warning' => 'clock', + 'success' => 'check', + ]) + ->label(function () use ($is_publishable) { + return $is_publishable ? $this->resource->publicationLabel() : ''; }); } } diff --git a/src/Nova/Fields/PublicationStatus.php b/src/Nova/Fields/PublicationStatus.php index 580ea68..2dcc2a1 100644 --- a/src/Nova/Fields/PublicationStatus.php +++ b/src/Nova/Fields/PublicationStatus.php @@ -7,7 +7,6 @@ use Laravel\Nova\Http\Requests\NovaRequest; use Novius\LaravelPublishable\Enums\PublicationStatus as PublicationStatusEnum; use Novius\LaravelPublishable\Traits\Publishable; -use RuntimeException; /** * @method static static make(mixed $name = null, string|\Closure|callable|object|null $attribute = null, callable|null $resolveCallback = null) @@ -16,35 +15,39 @@ class PublicationStatus extends Select { public function __construct($name = null, $attribute = null, callable $resolveCallback = null) { + $name = $name ?? trans('laravel-nova-publishable::messages.fields.publication_status'); + $request = app()->get(NovaRequest::class); $resource = $request->newResource(); /** @var Publishable&Model $model */ $model = $resource->model(); - if (! in_array(Publishable::class, class_uses_recursive($model))) { - throw new RuntimeException('Resource must use trait Novius\LaravePublishable\Traits\Publishable'); + + $is_publishable = in_array(Publishable::class, class_uses_recursive($model)); + if ($is_publishable) { + $attribute = $attribute ?? $model->getPublicationStatusColumn(); } - $name = $name ?? trans('laravel-nova-publishable::messages.fields.publication_status'); - $attribute = $attribute ?? $model->getPublicationStatusColumn(); parent::__construct($name, $attribute, $resolveCallback); - $this->options(function () use ($model) { - $statuses = [ - PublicationStatusEnum::draft->value => PublicationStatusEnum::draft->getLabel(), - PublicationStatusEnum::published->value => PublicationStatusEnum::published->getLabel(), - PublicationStatusEnum::unpublished->value => PublicationStatusEnum::unpublished->getLabel(), - PublicationStatusEnum::scheduled->value => PublicationStatusEnum::scheduled->getLabel(), - ]; - - if ($this->resource->{$model->getPublishedFirstAtColumn()} !== null) { - unset($statuses[PublicationStatusEnum::draft->value]); - } else { - unset($statuses[PublicationStatusEnum::unpublished->value]); - } - - return $statuses; - }) - ->rules('required') - ->displayUsingLabels(); + $this->rules('required') + ->displayUsingLabels() + ->options(function () use ($model, $is_publishable) { + $statuses = [ + PublicationStatusEnum::draft->value => PublicationStatusEnum::draft->getLabel(), + PublicationStatusEnum::published->value => PublicationStatusEnum::published->getLabel(), + PublicationStatusEnum::unpublished->value => PublicationStatusEnum::unpublished->getLabel(), + PublicationStatusEnum::scheduled->value => PublicationStatusEnum::scheduled->getLabel(), + ]; + + if ($is_publishable) { + if ($this->resource->{$model->getPublishedFirstAtColumn()} !== null) { + unset($statuses[PublicationStatusEnum::draft->value]); + } else { + unset($statuses[PublicationStatusEnum::unpublished->value]); + } + } + + return $statuses; + }); } } diff --git a/src/Nova/Fields/PublishedAt.php b/src/Nova/Fields/PublishedAt.php index 53270b9..7537202 100644 --- a/src/Nova/Fields/PublishedAt.php +++ b/src/Nova/Fields/PublishedAt.php @@ -8,7 +8,6 @@ use Laravel\Nova\Http\Requests\NovaRequest; use Novius\LaravelPublishable\Enums\PublicationStatus; use Novius\LaravelPublishable\Traits\Publishable; -use RuntimeException; /** * @method static static make(mixed $name = null, string|\Closure|callable|object|null $attribute = null, callable|null $resolveCallback = null) @@ -17,21 +16,25 @@ class PublishedAt extends DateTime { public function __construct($name = null, $attribute = null, callable $resolveCallback = null) { + $name = $name ?? trans('laravel-nova-publishable::messages.fields.published_at'); + $request = app()->get(NovaRequest::class); $resource = $request->newResource(); /** @var Publishable&Model $model */ $model = $resource->model(); - if (! in_array(Publishable::class, class_uses_recursive($model))) { - throw new RuntimeException('Resource must use trait Novius\LaravePublishable\Traits\Publishable'); + + $is_publishable = in_array(Publishable::class, class_uses_recursive($model)); + if ($is_publishable) { + $attribute = $attribute ?? $model->getPublishedAtColumn(); } - $name = $name ?? trans('laravel-nova-publishable::messages.fields.published_at'); - $attribute = $attribute ?? $model->getPublishedAtColumn(); parent::__construct($name, $attribute, $resolveCallback); $this->nullable() - ->rules('nullable', 'date') - ->dependsOn( + ->rules('nullable', 'date'); + + if ($is_publishable) { + $this->dependsOn( [$model->getPublicationStatusColumn()], function (DateTime $field, NovaRequest $request, FormData $formData) use ($model) { if ($formData->{$model->getPublicationStatusColumn()} === PublicationStatus::scheduled->value) { @@ -42,5 +45,6 @@ function (DateTime $field, NovaRequest $request, FormData $formData) use ($model } } ); + } } } diff --git a/src/Nova/Fields/PublishedFirstAt.php b/src/Nova/Fields/PublishedFirstAt.php index d4f39f8..c974ae6 100644 --- a/src/Nova/Fields/PublishedFirstAt.php +++ b/src/Nova/Fields/PublishedFirstAt.php @@ -8,7 +8,6 @@ use Laravel\Nova\Http\Requests\NovaRequest; use Novius\LaravelPublishable\Enums\PublicationStatus; use Novius\LaravelPublishable\Traits\Publishable; -use RuntimeException; /** * @method static static make(mixed $name = null, string|\Closure|callable|object|null $attribute = null, callable|null $resolveCallback = null) @@ -17,28 +16,40 @@ class PublishedFirstAt extends DateTime { public function __construct($name = null, $attribute = null, callable $resolveCallback = null) { + $name = $name ?? trans('laravel-nova-publishable::messages.fields.published_first_at'); + $request = app()->get(NovaRequest::class); $resource = $request->newResource(); /** @var Publishable&Model $model */ $model = $resource->model(); - if (! in_array(Publishable::class, class_uses_recursive($model))) { - throw new RuntimeException('Resource must use trait Novius\LaravePublishable\Traits\Publishable'); + + $is_publishable = in_array(Publishable::class, class_uses_recursive($model)); + if ($is_publishable) { + $attribute = $attribute ?? $model->getPublishedFirstAtColumn(); } - $name = $name ?? trans('laravel-nova-publishable::messages.fields.published_first_at'); - $attribute = $attribute ?? $model->getPublishedFirstAtColumn(); parent::__construct($name, $attribute, $resolveCallback); $this->nullable() ->rules('nullable', 'date') ->hideWhenCreating() - ->hideWhenUpdating(function (NovaRequest $request, Model $model) { - return ! $model->{$model->getPublishedFirstAtColumn()}; - }) - ->hideFromDetail(function (NovaRequest $request, Model $model) { - return ! $model->isPublished(); + ->hideWhenUpdating(function (NovaRequest $request, Model $model) use ($is_publishable) { + if ($is_publishable) { + return ! $model->{$model->getPublishedFirstAtColumn()}; + } + + return false; }) - ->dependsOn( + ->hideFromDetail(function (NovaRequest $request, Model $model) use ($is_publishable) { + if ($is_publishable) { + return ! $model->isPublished(); + } + + return false; + }); + + if ($is_publishable) { + $this->dependsOn( [$model->getPublicationStatusColumn()], function (DateTime $field, NovaRequest $request, FormData $formData) use ($model) { if (in_array($formData->{$model->getPublicationStatusColumn()}, [PublicationStatus::draft->value, PublicationStatus::unpublished->value], true)) { @@ -48,5 +59,6 @@ function (DateTime $field, NovaRequest $request, FormData $formData) use ($model } } ); + } } }