diff --git a/composer.json b/composer.json index f840237..cd01df7 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,9 @@ } ], "require": { - "php": "^8.0", - "filament/filament": "^2.0" + "php": "^8.1", + "filament/filament": "^2.0", + "oddvalue/laravel-drafts": "^1.3" }, "require-dev": { "orchestra/testbench": "^7.0" diff --git a/composer.lock b/composer.lock index b6fb4d5..714da2c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8b81206aa481dadc3fe36f249d5bd95d", + "content-hash": "5ebc323e04859c15863ce1afefd983d0", "packages": [ { "name": "akaunting/laravel-money", @@ -460,6 +460,345 @@ }, "time": "2022-10-27T11:44:00+00:00" }, + { + "name": "doctrine/cache", + "version": "2.2.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/cache.git", + "reference": "1ca8f21980e770095a31456042471a57bc4c68fb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/cache/zipball/1ca8f21980e770095a31456042471a57bc4c68fb", + "reference": "1ca8f21980e770095a31456042471a57bc4c68fb", + "shasum": "" + }, + "require": { + "php": "~7.1 || ^8.0" + }, + "conflict": { + "doctrine/common": ">2.2,<2.4" + }, + "require-dev": { + "cache/integration-tests": "dev-master", + "doctrine/coding-standard": "^9", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "psr/cache": "^1.0 || ^2.0 || ^3.0", + "symfony/cache": "^4.4 || ^5.4 || ^6", + "symfony/var-exporter": "^4.4 || ^5.4 || ^6" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Common\\Cache\\": "lib/Doctrine/Common/Cache" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.", + "homepage": "https://www.doctrine-project.org/projects/cache.html", + "keywords": [ + "abstraction", + "apcu", + "cache", + "caching", + "couchdb", + "memcached", + "php", + "redis", + "xcache" + ], + "support": { + "issues": "https://github.com/doctrine/cache/issues", + "source": "https://github.com/doctrine/cache/tree/2.2.0" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fcache", + "type": "tidelift" + } + ], + "time": "2022-05-20T20:07:39+00:00" + }, + { + "name": "doctrine/dbal", + "version": "3.6.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/dbal.git", + "reference": "57815c7bbcda3cd18871d253c1dd8cbe56f8526e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/dbal/zipball/57815c7bbcda3cd18871d253c1dd8cbe56f8526e", + "reference": "57815c7bbcda3cd18871d253c1dd8cbe56f8526e", + "shasum": "" + }, + "require": { + "composer-runtime-api": "^2", + "doctrine/cache": "^1.11|^2.0", + "doctrine/deprecations": "^0.5.3|^1", + "doctrine/event-manager": "^1|^2", + "php": "^7.4 || ^8.0", + "psr/cache": "^1|^2|^3", + "psr/log": "^1|^2|^3" + }, + "require-dev": { + "doctrine/coding-standard": "11.1.0", + "fig/log-test": "^1", + "jetbrains/phpstorm-stubs": "2022.3", + "phpstan/phpstan": "1.10.3", + "phpstan/phpstan-strict-rules": "^1.5", + "phpunit/phpunit": "9.6.4", + "psalm/plugin-phpunit": "0.18.4", + "squizlabs/php_codesniffer": "3.7.2", + "symfony/cache": "^5.4|^6.0", + "symfony/console": "^4.4|^5.4|^6.0", + "vimeo/psalm": "4.30.0" + }, + "suggest": { + "symfony/console": "For helpful console commands such as SQL execution and import of files." + }, + "bin": [ + "bin/doctrine-dbal" + ], + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\DBAL\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + } + ], + "description": "Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.", + "homepage": "https://www.doctrine-project.org/projects/dbal.html", + "keywords": [ + "abstraction", + "database", + "db2", + "dbal", + "mariadb", + "mssql", + "mysql", + "oci8", + "oracle", + "pdo", + "pgsql", + "postgresql", + "queryobject", + "sasql", + "sql", + "sqlite", + "sqlserver", + "sqlsrv" + ], + "support": { + "issues": "https://github.com/doctrine/dbal/issues", + "source": "https://github.com/doctrine/dbal/tree/3.6.1" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdbal", + "type": "tidelift" + } + ], + "time": "2023-03-02T19:26:24+00:00" + }, + { + "name": "doctrine/deprecations", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/deprecations.git", + "reference": "0e2a4f1f8cdfc7a92ec3b01c9334898c806b30de" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/deprecations/zipball/0e2a4f1f8cdfc7a92ec3b01c9334898c806b30de", + "reference": "0e2a4f1f8cdfc7a92ec3b01c9334898c806b30de", + "shasum": "" + }, + "require": { + "php": "^7.1|^8.0" + }, + "require-dev": { + "doctrine/coding-standard": "^9", + "phpunit/phpunit": "^7.5|^8.5|^9.5", + "psr/log": "^1|^2|^3" + }, + "suggest": { + "psr/log": "Allows logging deprecations via PSR-3 logger implementation" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Deprecations\\": "lib/Doctrine/Deprecations" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.", + "homepage": "https://www.doctrine-project.org/", + "support": { + "issues": "https://github.com/doctrine/deprecations/issues", + "source": "https://github.com/doctrine/deprecations/tree/v1.0.0" + }, + "time": "2022-05-02T15:47:09+00:00" + }, + { + "name": "doctrine/event-manager", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/event-manager.git", + "reference": "750671534e0241a7c50ea5b43f67e23eb5c96f32" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/event-manager/zipball/750671534e0241a7c50ea5b43f67e23eb5c96f32", + "reference": "750671534e0241a7c50ea5b43f67e23eb5c96f32", + "shasum": "" + }, + "require": { + "php": "^8.1" + }, + "conflict": { + "doctrine/common": "<2.9" + }, + "require-dev": { + "doctrine/coding-standard": "^10", + "phpstan/phpstan": "^1.8.8", + "phpunit/phpunit": "^9.5", + "vimeo/psalm": "^4.28" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Common\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + }, + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com" + } + ], + "description": "The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.", + "homepage": "https://www.doctrine-project.org/projects/event-manager.html", + "keywords": [ + "event", + "event dispatcher", + "event manager", + "event system", + "events" + ], + "support": { + "issues": "https://github.com/doctrine/event-manager/issues", + "source": "https://github.com/doctrine/event-manager/tree/2.0.0" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fevent-manager", + "type": "tidelift" + } + ], + "time": "2022-10-12T20:59:15+00:00" + }, { "name": "doctrine/inflector", "version": "2.0.6", @@ -2497,6 +2836,88 @@ ], "time": "2023-02-08T01:06:31+00:00" }, + { + "name": "oddvalue/laravel-drafts", + "version": "v1.3.0", + "source": { + "type": "git", + "url": "https://github.com/oddvalue/laravel-drafts.git", + "reference": "0dbd0f799932adc1e6eb4f65e5c30e0476fda6ad" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/oddvalue/laravel-drafts/zipball/0dbd0f799932adc1e6eb4f65e5c30e0476fda6ad", + "reference": "0dbd0f799932adc1e6eb4f65e5c30e0476fda6ad", + "shasum": "" + }, + "require": { + "doctrine/dbal": "^3.3", + "illuminate/contracts": "^9.0|^10.0", + "php": "^8.0", + "spatie/laravel-package-tools": "^1.9.2" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.8", + "nunomaduro/collision": "^6.0", + "nunomaduro/larastan": "^2.0.1", + "orchestra/testbench": "^7.0|^8.0", + "pestphp/pest": "^1.21", + "pestphp/pest-plugin-laravel": "^1.1|^2.0", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-phpunit": "^1.0", + "phpunit/phpunit": "^9.5", + "roave/security-advisories": "dev-latest", + "spatie/pest-plugin-test-time": "^1.1" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Oddvalue\\LaravelDrafts\\LaravelDraftsServiceProvider" + ], + "aliases": { + "LaravelDrafts": "Oddvalue\\LaravelDrafts\\Facades\\LaravelDrafts" + } + } + }, + "autoload": { + "psr-4": { + "Oddvalue\\LaravelDrafts\\": "src", + "Oddvalue\\LaravelDrafts\\Database\\Factories\\": "database/factories" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "jim", + "email": "jim@oddvalue.co.uk", + "role": "Developer" + } + ], + "description": "A simple, drop-in drafts/revisions system for Laravel models", + "homepage": "https://github.com/oddvalue/laravel-drafts", + "keywords": [ + "Revisions", + "drafts", + "laravel", + "oddvalue" + ], + "support": { + "issues": "https://github.com/oddvalue/laravel-drafts/issues", + "source": "https://github.com/oddvalue/laravel-drafts/tree/v1.3.0" + }, + "funding": [ + { + "url": "https://github.com/oddvalue", + "type": "github" + } + ], + "time": "2023-02-28T06:36:45+00:00" + }, { "name": "phpoption/phpoption", "version": "1.9.1", @@ -2572,6 +2993,55 @@ ], "time": "2023-02-25T19:38:58+00:00" }, + { + "name": "psr/cache", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/cache.git", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/cache/zipball/aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Cache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for caching libraries", + "keywords": [ + "cache", + "psr", + "psr-6" + ], + "support": { + "source": "https://github.com/php-fig/cache/tree/3.0.0" + }, + "time": "2021-02-03T23:26:27+00:00" + }, { "name": "psr/container", "version": "2.0.2", diff --git a/dist/plugin.css b/dist/plugin.css new file mode 100644 index 0000000..fc46ff2 --- /dev/null +++ b/dist/plugin.css @@ -0,0 +1 @@ +.bottom-4{bottom:1rem}.\!mx-8{margin-left:2rem!important;margin-right:2rem!important}.w-max{width:-moz-max-content;width:max-content}.\!rounded-md{border-radius:.375rem!important}.\!bg-warning-400{--tw-bg-opacity:1!important;background-color:rgb(251 191 36/var(--tw-bg-opacity))!important}.\!p-0{padding:0!important}.\!p-0\.5{padding:.125rem!important}.pl-\[var\(--sidebar-width\)\]{padding-left:var(--sidebar-width)}.pt-5{padding-top:1.25rem}.ring-1,.ring-2{box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow,0 0 #0000)}.ring-gray-300{--tw-ring-opacity:1;--tw-ring-color:rgb(209 213 219/var(--tw-ring-opacity))}.ring-success-500{--tw-ring-opacity:1;--tw-ring-color:rgb(34 197 94/var(--tw-ring-opacity))}.first-letter\:font-extrabold:first-letter{font-weight:800}:is([dir=rtl] .rtl\:divide-x-reverse)>:not([hidden])~:not([hidden]){--tw-divide-x-reverse:1}@media not all and (min-width:1024px){.max-lg\:overflow-x-scroll{overflow-x:scroll}.max-lg\:pb-3{padding-bottom:.75rem}.max-lg\:pt-1{padding-top:.25rem}}@media (min-width:640px){:is([dir=rtl] .sm\:rtl\:space-x-reverse)>:not([hidden])~:not([hidden]){--tw-space-x-reverse:1}}@media (min-width:1024px){.lg\:items-end{align-items:flex-end}}.\[\&\>div\>div\]\:\!p-1>div>div{padding:.25rem!important}.\[\&\>div\>div\]\:\!px-1>div>div{padding-left:.25rem!important;padding-right:.25rem!important} \ No newline at end of file diff --git a/resources/lang/en/actions.php b/resources/lang/en/actions.php new file mode 100644 index 0000000..2b36c9b --- /dev/null +++ b/resources/lang/en/actions.php @@ -0,0 +1,7 @@ + 'Publish', + 'publish_and_create_another' => 'Publish and create another', + 'unpublish' => 'Unpublish', + 'save-draft' => 'Save draft', +]; diff --git a/resources/lang/en/paginator.php b/resources/lang/en/paginator.php new file mode 100644 index 0000000..1061c6d --- /dev/null +++ b/resources/lang/en/paginator.php @@ -0,0 +1,15 @@ + 'Revisions', + + 'counts' => [ + 'overview' => ':published, :drafts, :revisions', + 'published' => ':count published|:count published', + 'drafts' => ':count draft|:count drafts', + 'revisions' => ':count/:max revisions', + ], + + 'published' => 'Published', + 'draft' => 'Draft', + 'current' => 'Current', +]; diff --git a/resources/lang/en/tables.php b/resources/lang/en/tables.php new file mode 100644 index 0000000..1cd3102 --- /dev/null +++ b/resources/lang/en/tables.php @@ -0,0 +1,6 @@ + [ + 'title' => 'Drafts', + ], +]; diff --git a/resources/views/filament/list-draftable-records.blade.php b/resources/views/filament/list-draftable-records.blade.php new file mode 100644 index 0000000..df1ce69 --- /dev/null +++ b/resources/views/filament/list-draftable-records.blade.php @@ -0,0 +1,11 @@ + + {{ $this->table }} + + diff --git a/resources/views/filament/revisions-paginator.blade.php b/resources/views/filament/revisions-paginator.blade.php new file mode 100644 index 0000000..725dfa9 --- /dev/null +++ b/resources/views/filament/revisions-paginator.blade.php @@ -0,0 +1,7 @@ +
+
+ +
diff --git a/resources/views/livewire/draftable-table.blade.php b/resources/views/livewire/draftable-table.blade.php new file mode 100644 index 0000000..530f50e --- /dev/null +++ b/resources/views/livewire/draftable-table.blade.php @@ -0,0 +1,12 @@ +
+
+
+ + {{__('filament-drafts::tables.draftable-table.title')}} + +
+
+ {{ $this->table }} +
diff --git a/resources/views/livewire/revisions-paginator.blade.php b/resources/views/livewire/revisions-paginator.blade.php new file mode 100644 index 0000000..611c035 --- /dev/null +++ b/resources/views/livewire/revisions-paginator.blade.php @@ -0,0 +1,93 @@ +
+
+ {{ __('filament-drafts::paginator.title') }} +
{{ __('filament-drafts::paginator.counts.overview', [ + 'published' => trans_choice('filament-drafts::paginator.counts.published', $counts['published']), + 'drafts' => trans_choice('filament-drafts::paginator.counts.drafts', $counts['drafts']), + 'revisions' => trans_choice('filament-drafts::paginator.counts.revisions', $counts['revisions'], [ + 'max' => config('drafts.revisions.keep'), +]), + ]) }} +
+ +
diff --git a/src/Admin/Actions/PublishAction.php b/src/Admin/Actions/PublishAction.php new file mode 100644 index 0000000..db8b895 --- /dev/null +++ b/src/Admin/Actions/PublishAction.php @@ -0,0 +1,26 @@ +color('success') + ->action('publish') + ->label(__('filament-drafts::actions.publish')) + ; + } + +} diff --git a/src/Admin/Actions/SaveDraftAction.php b/src/Admin/Actions/SaveDraftAction.php new file mode 100644 index 0000000..cf4d84a --- /dev/null +++ b/src/Admin/Actions/SaveDraftAction.php @@ -0,0 +1,42 @@ +action($this->draft(...)) + ->label(__('filament-drafts::actions.save-draft')) + ; + } + + protected function draft(): void + { + $livewire = $this->getLivewire(); + $livewire->shouldSaveAsDraft = true; + + if ($livewire instanceof CreateRecord) { + $livewire->create(); + } + + if ($livewire instanceof EditRecord) { + $livewire->save(); + } + } + +} diff --git a/src/Admin/Actions/UnpublishAction.php b/src/Admin/Actions/UnpublishAction.php new file mode 100644 index 0000000..931b738 --- /dev/null +++ b/src/Admin/Actions/UnpublishAction.php @@ -0,0 +1,36 @@ +color('secondary') + ->visible(fn() => $this->getLivewire()->record->isPublished()) + ->requiresConfirmation() + ->action($this->unpublish(...)) + ->label(__('filament-drafts::actions.unpublish')) + ; + } + + public function unpublish(): void + { + $record = $this->getLivewire()->record; + $record::withoutTimestamps(fn() => $record->update(['is_published' => false])); + + $this->getLivewire()->emit('updateRevisions', $record->id); + } + +} diff --git a/src/Admin/Resources/Concerns/Draftable.php b/src/Admin/Resources/Concerns/Draftable.php new file mode 100644 index 0000000..4370478 --- /dev/null +++ b/src/Admin/Resources/Concerns/Draftable.php @@ -0,0 +1,31 @@ +withDrafts(); + } +} diff --git a/src/Admin/Resources/Pages/Create/Draftable.php b/src/Admin/Resources/Pages/Create/Draftable.php new file mode 100644 index 0000000..1b9bec8 --- /dev/null +++ b/src/Admin/Resources/Pages/Create/Draftable.php @@ -0,0 +1,48 @@ +getModel()::make([ + ...$data, + 'is_published' => !$this->shouldSaveAsDraft, + ]); + + $model->withoutRevision()->save(); + return $model; + } + + protected function getCreateFormAction(): Action + { + return parent::getCreateFormAction() + ->color('success') + ->label(__('filament-drafts::actions.publish')); + } + + protected function getCreateAnotherFormAction(): Action { + return parent::getCreateAnotherFormAction() + ->label(__('filament-drafts::actions.publish_and_create_another')); + } + + protected function getFormActions(): array + { + return [ + ...array_slice(parent::getFormActions(), 0, 1), + SaveDraftAction::make(), + ...array_slice(parent::getFormActions(), 1), + ]; + } +} diff --git a/src/Admin/Resources/Pages/Edit/Draftable.php b/src/Admin/Resources/Pages/Edit/Draftable.php new file mode 100644 index 0000000..7e70903 --- /dev/null +++ b/src/Admin/Resources/Pages/Edit/Draftable.php @@ -0,0 +1,109 @@ + $this->getResource(), + 'record' => $this->getRecord(), + ]); + } + ); + } + + protected function handleRecordUpdate(Model $record, array $data): Model + { + if ($record->isPublished() && $this->shouldSaveAsDraft) { + $record->updateAsDraft($data); + } else if ($record->isPublished() && !$this->shouldSaveAsDraft) { + $record->update($data); + } else if (!$record->is_current && $this->shouldSaveAsDraft) { + $record->updateAsDraft($data); + } else { + // Unpublish all other revisions + if (!$this->shouldSaveAsDraft) { + /** @var HasMany $revisions */ + $record::withoutTimestamps(fn() => $record->revisions() + ->where('is_published', true) + ->update(['is_published' => false])); + } + + $record->update([ + ...$data, + 'is_published' => !$this->shouldSaveAsDraft, + ]); + } + + $this->emit('updateRevisions', $record->id); + + return $record; + } + + protected function getSaveFormAction(): Action + { + return parent::getSaveFormAction() + ->color('success') + ->label(fn(EditRecord $livewire) => $livewire->getRecord()->isPublished() + ? __('filament::resources/pages/edit-record.form.actions.save.label') + : __('filament-drafts::actions.publish') + ); + } + + protected function getActions(): array + { + return [ + UnpublishAction::make(), + ...parent::getActions(), + ]; + } + + protected function getFormActions(): array + { + return [ + ...array_slice(parent::getFormActions(), 0, 1), + SaveDraftAction::make(), + ...array_slice(parent::getFormActions(), 1), + ]; + } + + protected function getSavedNotificationTitle(): ?string + { + return $this->shouldSaveAsDraft + ? 'Draft saved' + : 'Published'; + } + + protected function getSavedNotification(): ?Notification + { + $notification = parent::getSavedNotification(); + + $this->shouldSaveAsDraft = false; + + return $notification; + } + +} diff --git a/src/Admin/Resources/Pages/List/Draftable.php b/src/Admin/Resources/Pages/List/Draftable.php new file mode 100644 index 0000000..b46f375 --- /dev/null +++ b/src/Admin/Resources/Pages/List/Draftable.php @@ -0,0 +1,44 @@ +withoutDrafts(); + } + + /** + * Overrides the view and renders it. + */ + public function render(): View + { + return view(static::getView(), $this->getViewData()) + ->layout(static::$layout, $this->getLayoutData()); + } +} diff --git a/src/FilamentDraftsServiceProvider.php b/src/FilamentDraftsServiceProvider.php index 7c1ae59..3166cd9 100644 --- a/src/FilamentDraftsServiceProvider.php +++ b/src/FilamentDraftsServiceProvider.php @@ -1,6 +1,26 @@ __DIR__ . '/../dist/plugin.css', + ]; + + public function packageBooted(): void + { + parent::packageBooted(); + + Livewire::component('filament-drafts::draftable-table', DraftableTable::class); + Livewire::component('filament-drafts::revisions-paginator', RevisionsPaginator::class); + } + } diff --git a/src/Tables/Http/Livewire/DraftableTable.php b/src/Tables/Http/Livewire/DraftableTable.php new file mode 100644 index 0000000..aa5624c --- /dev/null +++ b/src/Tables/Http/Livewire/DraftableTable.php @@ -0,0 +1,61 @@ +resource = $resource; + } + + protected function getTableQuery(): Builder + { + return $this->resource::getModel()::query()->onlyDrafts()->where('is_current', true); + } + + protected function getTableColumns(): array + { + $columns = $this->resource::table(Table::make()) + ->getColumns(); + +// $columns[0]->label('Ä…sdasd'); + + return [ +// TextColumn::make('_published') +// ->label('Model') +// ->formatStateUsing(fn() => new HtmlString( +// Blade::render(<< +//test +//
+//blade +// ))), + ...$columns, + ]; + } + + protected function getTableRecordUrlUsing(): ?Closure + { + return fn($record) => $this->resource::getUrl('edit', ['record' => $record]); + } + + public function render(): View + { + return view('filament-drafts::livewire.draftable-table'); + } + +} diff --git a/src/Tables/Http/Livewire/RevisionsPaginator.php b/src/Tables/Http/Livewire/RevisionsPaginator.php new file mode 100644 index 0000000..9bbdf56 --- /dev/null +++ b/src/Tables/Http/Livewire/RevisionsPaginator.php @@ -0,0 +1,63 @@ + 'updateRevisions', + ]; + + /** + * Updates the revisions list. + * + * @param int $id + * @return void + */ + public function updateRevisions(int $id): void + { + $record = $this->record::withDrafts()->find($id); + + $this->revisions = $record->revisions() +// ->orderByDesc('is_current') + ->orderByDesc('updated_at') + ->get(); + $this->counts = [ + 'published' => $this->revisions->where('is_published', true)->count(), + 'drafts' => $this->revisions->where('is_published', false)->where('is_current', true)->count(), + 'revisions' => $this->revisions->where('is_published', false)->where('is_current', false)->count(), + ]; + + $index = $this->revisions->search($record); + $this->previousRevision = $this->revisions->get($index - 1); + $this->nextRevision = $this->revisions->get($index + 1); + } + + public function mount(string $resource, Model $record): void + { + $this->resource = $resource; + $this->record = $record; + + $this->updateRevisions($this->record->id); + } + + public function render(): View + { + return view('filament-drafts::livewire.revisions-paginator'); + } +}