Skip to content

Commit d66a707

Browse files
committed
hotfix: Cannot publish content if previous states is "draft" and propertyData is no changes
1 parent bb1c28f commit d66a707

File tree

5 files changed

+67
-30
lines changed

5 files changed

+67
-30
lines changed

src/Base/Filament/Concerns/ContentFormTrait.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use SolutionForest\InspireCms\Base\Filament\Resources\Pages\BaseContentCreatePage;
1717
use SolutionForest\InspireCms\Base\Filament\Resources\Pages\CreateContentRecord\Concerns\Translatable as CmsCreateContentRecordsTranslatable;
1818
use SolutionForest\InspireCms\Filament\Resources\ContentResource;
19+
use SolutionForest\InspireCms\Helpers\DiffHelper;
1920
use SolutionForest\InspireCms\InspireCmsConfig;
2021
use SolutionForest\InspireCms\Models\Contracts\Content;
2122
use Throwable;
@@ -234,7 +235,10 @@ public function handlePublishableRecordCreateOrUpdate(array $data, array $publis
234235

235236
$record->setPublishableState($publishableAction);
236237

237-
$this->propertyDataIsDirtyPreCheck($record->getLatestVersionPropertyData(), $data['propertyData'] ?? []);
238+
$this->propertyDataIsDirtyPreCheck(
239+
array_merge($record->getLatestVersionPropertyData(), $record->latestContentVersion?->getVersioningCheckDiffData() ?? []),
240+
array_merge($data['propertyData'] ?? [], $record->contentVersions()->make([... $publishableData, 'publish_state' => $publishableAction])?->getVersioningCheckDiffData() ?? []),
241+
);
238242

239243
$this->record = $this->handleRecordUpdate($record, $data);
240244
// endregion Handle Record Updating
@@ -389,14 +393,7 @@ protected function propertyDataIsDirtyPreCheck($from, $to)
389393
return;
390394
}
391395

392-
$modelTmpContentVersion = app(InspireCmsConfig::getContentVersionModelClass(), [
393-
'attributes' => [
394-
'from_data' => $from,
395-
'to_data' => $to,
396-
],
397-
]);
398-
399-
$diff = $modelTmpContentVersion->getDifferences();
396+
$diff = DiffHelper::compareArrays($from, $to);
400397

401398
// Display notification if no differences found
402399
if (empty($diff)) {

src/Helpers/DiffHelper.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace SolutionForest\InspireCms\Helpers;
4+
5+
class DiffHelper
6+
{
7+
public static function compareArrays(array $from, array $to): array
8+
{
9+
$diff = [];
10+
foreach ($from as $key => $value) {
11+
if (!array_key_exists($key, $to) || $to[$key] !== $value) {
12+
$diff[$key] = ['from' => $value, 'to' => $to[$key] ?? null];
13+
}
14+
}
15+
16+
foreach ($to as $key => $value) {
17+
if (!array_key_exists($key, $from)) {
18+
$diff[$key] = ['from' => null, 'to' => $value];
19+
}
20+
}
21+
22+
return $diff;
23+
}
24+
}

src/Listeners/Content/ProcessContentVersion.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SolutionForest\InspireCms\Events\Content\CreatingPublishContentVersion;
1111
use SolutionForest\InspireCms\Events\Content\DispatchContentVersion;
1212
use SolutionForest\InspireCms\Events\Content\GenerateSitemap;
13+
use SolutionForest\InspireCms\Helpers\DiffHelper;
1314
use SolutionForest\InspireCms\Models\Contracts\Base\HasContentVersions;
1415
use SolutionForest\InspireCms\Models\Contracts\ContentVersion;
1516

@@ -41,8 +42,15 @@ protected function processContentVersion(HasContentVersions | Model $model)
4142

4243
$contentVersion = $model->contentVersions()->make($contentVersionData);
4344

44-
if ($contentVersion instanceof ContentVersion && count($contentVersion->getDifferences()) === 0) {
45-
return;
45+
$latestVersion = $model->latestContentVersion;
46+
if ($contentVersion instanceof ContentVersion) {
47+
$from = array_merge($contentVersion->from_data ?? [], $latestVersion?->getVersioningCheckDiffData() ?? []);
48+
$to = array_merge($contentVersion->to_data ?? [], $contentVersion?->getVersioningCheckDiffData() ?? []);
49+
$diff = DiffHelper::compareArrays($from, $to);
50+
// If there are no differences, skip creating the version.
51+
if (count($diff) === 0) {
52+
return;
53+
}
4654
}
4755

4856
// Unload the relations to prevent large amounts of unnecessary data from being serialized.

src/Models/ContentVersion.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SolutionForest\InspireCms\Models;
44

55
use Illuminate\Database\Eloquent\Prunable;
6+
use SolutionForest\InspireCms\Helpers\DiffHelper;
67
use SolutionForest\InspireCms\InspireCmsConfig;
78
use SolutionForest\InspireCms\Models\Contracts\ContentVersion as ContentVersionContract;
89
use SolutionForest\InspireCms\Observers\ContentVersionObserver;
@@ -37,26 +38,26 @@ public function publishLog()
3738

3839
public function getDifferences()
3940
{
40-
$from = $this->from_data;
41-
$to = $this->to_data;
42-
43-
$diff = [];
44-
45-
foreach ($to as $key => $value) {
46-
if (! array_key_exists($key, $from)) {
47-
$diff[$key] = [
48-
'from' => null,
49-
'to' => $value,
50-
];
51-
} elseif ($from[$key] !== $value) {
52-
$diff[$key] = [
53-
'from' => $from[$key],
54-
'to' => $value,
55-
];
56-
}
57-
}
41+
return DiffHelper::compareArrays(
42+
$this->from_data ?? [],
43+
$this->to_data ?? []
44+
);
45+
}
46+
47+
protected function getVersioningCheckDiffAttribtues()
48+
{
49+
return [
50+
'publish_state',
51+
'avoid_to_clean',
52+
];
53+
}
5854

59-
return $diff;
55+
public function getVersioningCheckDiffData()
56+
{
57+
return [
58+
'publish_state' => $this->publish_state ?? 'draft',
59+
'avoid_to_clean' => $this->avoid_to_clean ?? inspirecms_content_statuses()->getOption($this->publish_state ?? 'draft')?->isPublishable() ?? null,
60+
];
6061
}
6162

6263
// region Scopes

src/Models/Contracts/ContentVersion.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public function publishLog();
3838
*/
3939
public function getDifferences();
4040

41+
/**
42+
* Get the attributes to check for versioning differences.
43+
*
44+
* @return array
45+
*/
46+
public function getVersioningCheckDiffData();
47+
4148
/**
4249
* Scope a query to only include published content versions.
4350
*

0 commit comments

Comments
 (0)