Skip to content

Commit

Permalink
Remove exception when model are not publishable
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgilles committed Jul 21, 2023
1 parent 23119b4 commit 64534bc
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 66 deletions.
20 changes: 13 additions & 7 deletions src/Nova/Fields/ExpiredAt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -42,5 +47,6 @@ function (DateTime $field, NovaRequest $request, FormData $formData) use ($model
}
}
);
}
}
}
38 changes: 20 additions & 18 deletions src/Nova/Fields/PublicationBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() : '';
});
}
}
49 changes: 26 additions & 23 deletions src/Nova/Fields/PublicationStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
});
}
}
18 changes: 11 additions & 7 deletions src/Nova/Fields/PublishedAt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -42,5 +45,6 @@ function (DateTime $field, NovaRequest $request, FormData $formData) use ($model
}
}
);
}
}
}
34 changes: 23 additions & 11 deletions src/Nova/Fields/PublishedFirstAt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)) {
Expand All @@ -48,5 +59,6 @@ function (DateTime $field, NovaRequest $request, FormData $formData) use ($model
}
}
);
}
}
}

0 comments on commit 64534bc

Please sign in to comment.