Skip to content

Commit 01a0cae

Browse files
committed
cherry commit from 99741fd
1 parent be6a469 commit 01a0cae

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/Filament/Resources/ContentResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public static function getModelLabel(): string
137137
public static function getEloquentQuery(): Builder
138138
{
139139
return parent::getEloquentQuery()->with([
140+
'latestNonDraftContentVersion', // To check the content is published or not
140141
'publishedVersions', // To get published version, and determine is published
141142
'documentType.templates', // For template use
142143
'parent', // To get parent title

src/Filament/Widgets/PageActivity.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function table(Table $table): Table
3333
->emptyStateIcon(FilamentIcon::resolve('inspirecms::info'))
3434
->emptyStateHeading(__('inspirecms::widgets.page_activity.empty_state.heading'))
3535
->modifyQueryUsing(fn ($query) => $query->with([
36+
'latestNonDraftContentVersion', // To check the content is published or not
3637
'publishedVersions',
3738
]))
3839
->columns([
@@ -51,10 +52,18 @@ public function table(Table $table): Table
5152
TextColumn::make('published_at')
5253
->label(__('inspirecms::resources/content.published_at.label'))
5354
->getStateUsing(function (Model $record) {
54-
return $record->getPublishedVersions()?->sortByDesc('pivot.published_at')?->first()?->pivot->published_at;
55+
if (($latestNonDraftContentVersion = $record->latestNonDraftContentVersion)) {
56+
// If the latest non-draft version exists, check if it is published
57+
if ($latestNonDraftContentVersion->publish_state === 'unpublish') {
58+
return null;
59+
}
60+
}
61+
62+
return $record->getLatestPublishedTime();
5563
})
56-
->formatStateUsing(fn (?Carbon $state) => ($state && $state instanceof DateTimeInterface) ? $state->diffForHumans(now()) : null)
57-
->tooltip(fn ($state) => ($state && $state instanceof DateTimeInterface) ? $state->toDateTimeString() : null)
64+
->placeholder(__('inspirecms::inspirecms.n/a'))
65+
->formatStateUsing(fn (?\Carbon\Carbon $state) => ($state && $state instanceof \DateTimeInterface) ? $state->diffForHumans(now()) : null)
66+
->tooltip(fn ($state) => ($state && $state instanceof \DateTimeInterface) ? $state->toDateTimeString() : null)
5867
->width('5%'),
5968
TextColumn::make('updated_at')
6069
->label(__('inspirecms::resources/content.updated_at.label'))

src/Models/Concerns/HasContentVersions.php

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

64+
public function latestNonDraftContentVersion()
65+
{
66+
return $this->hasOne(InspireCmsConfig::getContentVersionModelClass(), 'content_id')
67+
->ofMany([
68+
'id' => 'MAX',
69+
], function ($query) {
70+
$query->whereNot('publish_state', 'draft');
71+
});
72+
}
73+
6474
public function getPublishedVersions()
6575
{
6676
if (! $this->relationLoaded('publishedVersions')) {
@@ -88,6 +98,15 @@ public function getLatestPublishedContentVersion()
8898
/** {@inheritDoc} */
8999
public function getPublishTime()
90100
{
101+
// Handle case that:
102+
// The content have published, but the latest version is not published (Step 1: 'unpublish', Step 2: 'draft')
103+
if (($latestNonDraftContentVersion = $this->latestNonDraftContentVersion)) {
104+
// If the latest non-draft version exists, check if it is published
105+
if ($latestNonDraftContentVersion->publish_state === 'unpublish') {
106+
return null;
107+
}
108+
}
109+
91110
// If the publish date is in the future, it's not published
92111
return $this->getLatestPublishedContentVersion()?->pivot?->published_at;
93112
}

src/Models/Content.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ public function scopeWhereIsPublished($query, bool $condition = true)
246246
'publishedVersions',
247247
fn ($q) => $q->whereIsPublished()
248248
)
249+
// Handle case that:
250+
// The content have published, but the latest version is not published (Step 1: 'unpublish', Step 2: 'draft')
251+
->whereHas(
252+
'latestNonDraftContentVersion',
253+
fn ($q) => $q->whereIsPublished()
254+
)
249255
->whereNot('status', $unpublishOption->getValue());
250256

251257
} else {
@@ -257,6 +263,10 @@ public function scopeWhereIsPublished($query, bool $condition = true)
257263
'publishedVersions',
258264
fn ($q) => $q->whereIsPublished()
259265
)
266+
->orWhereDoesntHave(
267+
'latestNonDraftContentVersion',
268+
fn ($q) => $q->whereIsPublished()
269+
)
260270
->orWhere('status', $unpublishOption->getValue())
261271
);
262272

0 commit comments

Comments
 (0)