diff --git a/src/Filament/Resources/AbstractContentResource.php b/src/Filament/Resources/AbstractContentResource.php index 7061f205..854ad342 100644 --- a/src/Filament/Resources/AbstractContentResource.php +++ b/src/Filament/Resources/AbstractContentResource.php @@ -24,7 +24,6 @@ use Illuminate\Database\Eloquent\SoftDeletingScope; use Portable\FilaCms\Filament\Forms\Components\StatusBadge; use Portable\FilaCms\Filament\Resources\AbstractContentResource\Pages; -use Portable\FilaCms\Filament\Resources\AbstractContentResource\RelationManagers; use Portable\FilaCms\Filament\Traits\IsProtectedResource; use Portable\FilaCms\Models\Author; use Portable\FilaCms\Models\Page; @@ -200,7 +199,6 @@ public static function table(Table $table): Table public static function getRelations(): array { return [ - RelationManagers\RevisionsRelationManager::class, ]; } diff --git a/src/Filament/Resources/AbstractContentResource/RelationManagers/RevisionsRelationManager.php b/src/Filament/Resources/AbstractContentResource/RelationManagers/RevisionsRelationManager.php deleted file mode 100644 index 3989d6f2..00000000 --- a/src/Filament/Resources/AbstractContentResource/RelationManagers/RevisionsRelationManager.php +++ /dev/null @@ -1,66 +0,0 @@ -schema([]); - } - - public function table(Table $table): Table - { - return $table - ->recordTitleAttribute('History') - ->columns([ - ViewColumn::make('user_id') - ->view('fila-cms::tables.columns.user-responsible') - ->label('Editor') - ->sortable() - ->searchable(), - ViewColumn::make('old_value') - ->view('fila-cms::tables.columns.diff-old') - ->label('Old Value'), - ViewColumn::make('new_value') - ->view('fila-cms::tables.columns.diff-new') - ->label('New Value'), - Tables\Columns\TextColumn::make('key')->label('Property')->sortable()->searchable(), - Tables\Columns\TextColumn::make('created_at')->label('Date Changed')->sortable(), - ]) - ->filters([ - // - ]) - ->headerActions([ - - ]) - ->actions([ - Tables\Actions\DeleteAction::make(), - Action::make('revert') - ->label('Revert') - ->button() - ->action(function ($record) { - $model = $record->revisionable()->withoutGlobalScope(PublishedScope::class)->first(); - - $model->{$record->key} = $record->old_value; - $model->save(); - }), - ]) - ->bulkActions([ - Tables\Actions\BulkActionGroup::make([ - Tables\Actions\DeleteBulkAction::make(), - ]), - ]); - } -} diff --git a/src/Filament/Traits/HasExcerpt.php b/src/Filament/Traits/HasExcerpt.php index d8001951..a3143d0a 100644 --- a/src/Filament/Traits/HasExcerpt.php +++ b/src/Filament/Traits/HasExcerpt.php @@ -12,7 +12,7 @@ trait HasExcerpt public function excerpt(): Attribute { $excerpt = $this->{$this->excerptField}; - $content = $excerpt['content']; + $content = is_array($excerpt) ? $excerpt['content'] : []; // get first content with "paragraph" $paragraph = ''; diff --git a/src/Models/AbstractContentModel.php b/src/Models/AbstractContentModel.php index 66551ea8..1bb7e300 100644 --- a/src/Models/AbstractContentModel.php +++ b/src/Models/AbstractContentModel.php @@ -16,6 +16,7 @@ use Portable\FilaCms\Filament\Traits\HasExcerpt; use Portable\FilaCms\Filament\Traits\HasTaxonomies; use Portable\FilaCms\Models\Scopes\PublishedScope; +use Portable\FilaCms\Versionable\FilaCmsVersion; use RalphJSmit\Laravel\SEO\Support\HasSEO; use RalphJSmit\Laravel\SEO\Support\SEOData; use Str; @@ -39,6 +40,8 @@ abstract class AbstractContentModel extends Model protected $versionStrategy = VersionStrategy::SNAPSHOT; + public string $versionModel = FilaCmsVersion::class; + protected $versionable = [ 'title', 'slug', diff --git a/src/Providers/FilaCmsServiceProvider.php b/src/Providers/FilaCmsServiceProvider.php index da3b2034..3f7644b9 100644 --- a/src/Providers/FilaCmsServiceProvider.php +++ b/src/Providers/FilaCmsServiceProvider.php @@ -2,13 +2,13 @@ namespace Portable\FilaCms\Providers; +use FilamentTiptapEditor\TiptapEditor; use Illuminate\Foundation\AliasLoader; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; use Livewire\Livewire; use Portable\FilaCms\Facades\FilaCms as FacadesFilaCms; use Portable\FilaCms\FilaCms; -use FilamentTiptapEditor\TiptapEditor; use Portable\FilaCms\Filament\Blocks\RelatedResourceBlock; class FilaCmsServiceProvider extends ServiceProvider @@ -42,6 +42,7 @@ public function boot() Livewire::component('portable.fila-cms.livewire.content-resource-list', \Portable\FilaCms\Livewire\ContentResourceList::class); Livewire::component('portable.fila-cms.livewire.content-resource-show', \Portable\FilaCms\Livewire\ContentResourceShow::class); Blade::componentNamespace('Portable\\FilaCms\\Views\\Components', 'fila-cms'); + config(['versionable.user_model' => config('auth.providers.users.model')]); } public function register() diff --git a/src/Versionable/FilaCmsDiff.php b/src/Versionable/FilaCmsDiff.php new file mode 100644 index 00000000..08ef2560 --- /dev/null +++ b/src/Versionable/FilaCmsDiff.php @@ -0,0 +1,86 @@ +differOptions; + } + + $oldContents = $this->fromVersion->contents; + $newContents = $this->toVersion->contents; + + $diffStats = new Collection(); + + foreach ($oldContents as $key => $value) { + if ($newContents[$key] !== $oldContents[$key]) { + $newData = $newContents[$key]; + $oldData = $oldContents[$key]; + + if(is_array($newData) || is_array($oldData)) { + $newData = tiptap_converter()->asText($newData); + $oldData = tiptap_converter()->asText($oldData); + } + + $diffStats->push( + (new Differ( + explode("\n", $newData), + explode("\n", $oldData), + ))->getStatistics() + ); + } + } + + return [ + 'inserted' => $diffStats->sum('inserted'), + 'deleted' => $diffStats->sum('deleted'), + 'unmodified' => $diffStats->sum('unmodified'), + 'changedRatio' => $diffStats->sum('changedRatio'), + ]; + } + + + public function render(?string $renderer = null, array $differOptions = [], array $renderOptions = []): array + { + if (empty($differOptions)) { + $differOptions = $this->differOptions; + } + + if (empty($renderOptions)) { + $renderOptions = $this->renderOptions; + } + + $oldContents = $this->fromVersion->contents; + $newContents = $this->toVersion->contents; + + $diff = []; + $createDiff = function ($key, $old, $new) use (&$diff, $renderer, $differOptions, $renderOptions) { + if ($renderer) { + $old = is_string($old) ? $old : (is_array($old) ? tiptap_converter()->asText($old) : json_encode($old)); + $new = is_string($new) ? $new : (is_array($new) ? tiptap_converter()->asText($new) : json_encode($new)); + $diff[$key] = str_replace('\n No newline at end of file', '', DiffHelper::calculate($old, $new, $renderer, $differOptions, $renderOptions)); + } else { + $diff[$key] = compact('old', 'new'); + } + }; + + foreach ($oldContents as $key => $value) { + $createDiff($key, Arr::get($newContents, $key), Arr::get($oldContents, $key)); + } + + foreach (array_diff_key($oldContents, $newContents) as $key => $value) { + $createDiff($key, null, $value); + } + + return $diff; + } +} diff --git a/src/Versionable/FilaCmsVersion.php b/src/Versionable/FilaCmsVersion.php new file mode 100644 index 00000000..fc77c35d --- /dev/null +++ b/src/Versionable/FilaCmsVersion.php @@ -0,0 +1,32 @@ +previousVersion() ?? new static(); + } + + return new FilaCmsDiff($this, $toVersion, $differOptions, $renderOptions); + } + + public function revertWithoutSaving(): ?Model + { + $oldContents = $newContents = $this->contents; + $newContents['contents'] = json_encode($this->contents['contents']); + $this->contents = $newContents; + $result = parent::revertWithoutSaving(); + $this->contents = $oldContents; + + return $result; + } +}