Skip to content

Commit 99741fd

Browse files
committed
1 parent a62697c commit 99741fd

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/Filament/Resources/ContentResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ public static function getModelLabel(): string
441441
public static function getEloquentQuery(): Builder
442442
{
443443
return parent::getEloquentQuery()->with([
444+
'latestNonDraftContentVersion', // To check the content is published or not
444445
'publishedVersions', // To get published version, and determine is published
445446
'documentType.templates', // For template use
446447
'parent', // To get parent title

src/Filament/Widgets/PageActivity.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function table(Table $table): Table
2929
->emptyStateIcon(FilamentIcon::resolve('inspirecms::info'))
3030
->emptyStateHeading(__('inspirecms::widgets.page_activity.empty_state.heading'))
3131
->modifyQueryUsing(fn ($query) => $query->with([
32+
'latestNonDraftContentVersion', // To check the content is published or not
3233
'publishedVersions',
3334
]))
3435
->columns([
@@ -47,8 +48,16 @@ public function table(Table $table): Table
4748
Tables\Columns\TextColumn::make('published_at')
4849
->label(__('inspirecms::resources/content.published_at.label'))
4950
->getStateUsing(function (Model $record) {
50-
return $record->getPublishedVersions()?->sortByDesc('pivot.published_at')?->first()?->pivot->published_at;
51+
if (($latestNonDraftContentVersion = $record->latestNonDraftContentVersion)) {
52+
// If the latest non-draft version exists, check if it is published
53+
if ($latestNonDraftContentVersion->publish_state === 'unpublish') {
54+
return null;
55+
}
56+
}
57+
58+
return $record->getLatestPublishedTime();
5159
})
60+
->placeholder(__('inspirecms::inspirecms.n/a'))
5261
->formatStateUsing(fn (?\Carbon\Carbon $state) => ($state && $state instanceof \DateTimeInterface) ? $state->diffForHumans(now()) : null)
5362
->tooltip(fn ($state) => ($state && $state instanceof \DateTimeInterface) ? $state->toDateTimeString() : null)
5463
->width('5%'),

src/Models/Concerns/HasContentVersions.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ public function latestContentVersion()
5959
return $this->hasOne(InspireCmsConfig::getContentVersionModelClass(), 'content_id')->latestOfMany();
6060
}
6161

62+
public function latestNonDraftContentVersion()
63+
{
64+
return $this->hasOne(InspireCmsConfig::getContentVersionModelClass(), 'content_id')
65+
->ofMany([
66+
'id' => 'MAX',
67+
], function ($query) {
68+
$query->whereNot('publish_state', 'draft');
69+
});
70+
}
71+
6272
public function getPublishedVersions()
6373
{
6474
if (! $this->relationLoaded('publishedVersions')) {
@@ -86,6 +96,15 @@ public function getLatestPublishedContentVersion()
8696
/** {@inheritDoc} */
8797
public function getPublishTime()
8898
{
99+
// Handle case that:
100+
// The content have published, but the latest version is not published (Step 1: 'unpublish', Step 2: 'draft')
101+
if (($latestNonDraftContentVersion = $this->latestNonDraftContentVersion)) {
102+
// If the latest non-draft version exists, check if it is published
103+
if ($latestNonDraftContentVersion->publish_state === 'unpublish') {
104+
return null;
105+
}
106+
}
107+
89108
// If the publish date is in the future, it's not published
90109
return $this->getLatestPublishedContentVersion()?->pivot?->published_at;
91110
}

src/Models/Content.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ public function scopeWhereIsPublished($query, bool $condition = true)
238238
'publishedVersions',
239239
fn ($q) => $q->whereIsPublished()
240240
)
241+
// Handle case that:
242+
// The content have published, but the latest version is not published (Step 1: 'unpublish', Step 2: 'draft')
243+
->whereHas(
244+
'latestNonDraftContentVersion',
245+
fn ($q) => $q->whereIsPublished()
246+
)
241247
->whereNot('status', $unpublishOption->getValue());
242248

243249
} else {
@@ -249,6 +255,10 @@ public function scopeWhereIsPublished($query, bool $condition = true)
249255
'publishedVersions',
250256
fn ($q) => $q->whereIsPublished()
251257
)
258+
->orWhereDoesntHave(
259+
'latestNonDraftContentVersion',
260+
fn ($q) => $q->whereIsPublished()
261+
)
252262
->orWhere('status', $unpublishOption->getValue())
253263
);
254264

0 commit comments

Comments
 (0)