From 1deb7cbf22bdcd63e090ee753be4094c3257b566 Mon Sep 17 00:00:00 2001 From: Benjamin Takats Date: Thu, 15 Dec 2022 16:08:25 +0100 Subject: [PATCH] publishing library items --- app/Http/Livewire/Tables/LibraryItemTable.php | 3 +- app/Models/LibraryItem.php | 2 + app/Nova/Actions/SetStatusAction.php | 52 ++++++++ app/Nova/LibraryItem.php | 25 +++- composer.json | 2 + composer.lock | 120 +++++++++++++++++- config/model-status.php | 18 +++ ...022_12_15_145559_create_statuses_table.php | 24 ++++ 8 files changed, 243 insertions(+), 3 deletions(-) create mode 100644 app/Nova/Actions/SetStatusAction.php create mode 100644 config/model-status.php create mode 100644 database/migrations/2022_12_15_145559_create_statuses_table.php diff --git a/app/Http/Livewire/Tables/LibraryItemTable.php b/app/Http/Livewire/Tables/LibraryItemTable.php index a4a50e53..b1954fd9 100644 --- a/app/Http/Livewire/Tables/LibraryItemTable.php +++ b/app/Http/Livewire/Tables/LibraryItemTable.php @@ -153,6 +153,7 @@ class LibraryItemTable extends DataTableComponent fn($query) => $query->where('libraries.name', $this->currentTab))) ->withCount([ 'lecturer', - ]); + ]) + ->currentStatus('published'); } } diff --git a/app/Models/LibraryItem.php b/app/Models/LibraryItem.php index fa19df37..adb38591 100644 --- a/app/Models/LibraryItem.php +++ b/app/Models/LibraryItem.php @@ -10,6 +10,7 @@ use Spatie\Image\Manipulations; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; use Spatie\MediaLibrary\MediaCollections\Models\Media; +use Spatie\ModelStatus\HasStatuses; use Spatie\Tags\HasTags; class LibraryItem extends Model implements HasMedia, Sortable @@ -18,6 +19,7 @@ class LibraryItem extends Model implements HasMedia, Sortable use InteractsWithMedia; use HasTags; use SortableTrait; + use HasStatuses; /** * The attributes that aren't mass assignable. diff --git a/app/Nova/Actions/SetStatusAction.php b/app/Nova/Actions/SetStatusAction.php new file mode 100644 index 00000000..b318f67e --- /dev/null +++ b/app/Nova/Actions/SetStatusAction.php @@ -0,0 +1,52 @@ +setStatus($fields->status); + } + } + + /** + * Get the fields available on the action. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * + * @return array + */ + public function fields(NovaRequest $request) + { + return [ + Select::make('Status') + ->options([ + 'draft' => 'DRAFT', + 'published' => 'PUBLISHED', + ]) + ->displayUsingLabels() + ]; + } +} diff --git a/app/Nova/LibraryItem.php b/app/Nova/LibraryItem.php index f13eb795..6955d4f4 100644 --- a/app/Nova/LibraryItem.php +++ b/app/Nova/LibraryItem.php @@ -4,6 +4,7 @@ namespace App\Nova; use App\Enums\LibraryItemType; use App\Notifications\ModelCreatedNotification; +use App\Nova\Actions\SetStatusAction; use Ebess\AdvancedNovaMediaLibrary\Fields\Files; use Ebess\AdvancedNovaMediaLibrary\Fields\Images; use Illuminate\Database\Eloquent\Model; @@ -17,6 +18,7 @@ use Laravel\Nova\Fields\Text; use Laravel\Nova\Http\Requests\NovaRequest; use Spatie\LaravelOptions\Options; use Spatie\TagsField\Tags; +use WesselPerik\StatusField\StatusField; class LibraryItem extends Resource { @@ -67,6 +69,25 @@ class LibraryItem extends Resource ID::make() ->sortable(), + StatusField::make('Status',) + ->icons([ + 'clock' => $this->status === 'draft', + 'check-circle' => $this->status === 'published', + ]) + ->tooltip([ + 'clock' => 'Pending publication', + 'check-circle' => 'Published' + ]) + ->info([ + 'clock' => 'Pending publication.', + 'check-circle' => 'Published.' + ]) + ->color([ + 'clock' => 'blue-500', + 'check-circle' => 'green-500' + ]) + ->exceptOnForms(), + Images::make(__('Main picture'), 'main') ->conversionOnIndexView('thumb'), @@ -160,6 +181,8 @@ class LibraryItem extends Resource */ public function actions(Request $request) { - return []; + return [ + new SetStatusAction(), + ]; } } diff --git a/composer.json b/composer.json index b3d10be9..39ccc159 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "spatie/laravel-google-fonts": "^1.2", "spatie/laravel-markdown": "^2.2", "spatie/laravel-medialibrary": "^10.0.0", + "spatie/laravel-model-status": "^1.11", "spatie/laravel-options": "^1.0", "spatie/laravel-ray": "^1.31", "spatie/laravel-sluggable": "^3.4", @@ -44,6 +45,7 @@ "stijnvanouplines/blade-country-flags": "^1.0", "symfony/http-client": "^6.2", "symfony/mailgun-mailer": "^6.2", + "wesselperik/nova-status-field": "^2.1", "wireui/wireui": "^1.17", "ziffmedia/nova-select-plus": "^2.0" }, diff --git a/composer.lock b/composer.lock index aaa520c6..4047fc90 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": "f361449843e880ec16cbc06c10871928", + "content-hash": "910dfec50f7d5534308b74a252331b64", "packages": [ { "name": "akuechler/laravel-geoly", @@ -7539,6 +7539,77 @@ ], "time": "2022-12-06T07:29:40+00:00" }, + { + "name": "spatie/laravel-model-status", + "version": "1.11.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-model-status.git", + "reference": "6380fa82ac98ab755b08a034d2c1608212e26478" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-model-status/zipball/6380fa82ac98ab755b08a034d2c1608212e26478", + "reference": "6380fa82ac98ab755b08a034d2c1608212e26478", + "shasum": "" + }, + "require": { + "illuminate/support": "^8.71|^9.0", + "php": "^8.0" + }, + "require-dev": { + "orchestra/testbench": "^6.23|^7.0", + "phpunit/phpunit": "^9.4" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\ModelStatus\\ModelStatusServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Spatie\\ModelStatus\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Thomas Verhelst", + "email": "tvke91@gmail.com", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "A package to enable assigning statuses to Eloquent Models", + "homepage": "https://github.com/spatie/laravel-model-status", + "keywords": [ + "laravel-status", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-model-status/issues", + "source": "https://github.com/spatie/laravel-model-status/tree/1.11.0" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + } + ], + "time": "2022-02-03T07:36:04+00:00" + }, { "name": "spatie/laravel-options", "version": "1.0.3", @@ -11803,6 +11874,53 @@ }, "time": "2022-06-03T18:03:27+00:00" }, + { + "name": "wesselperik/nova-status-field", + "version": "2.1.2", + "source": { + "type": "git", + "url": "https://github.com/wesselperik/nova-status-field.git", + "reference": "1b59ca03b658a2ed73b086df7d41394326fe80ef" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wesselperik/nova-status-field/zipball/1b59ca03b658a2ed73b086df7d41394326fe80ef", + "reference": "1b59ca03b658a2ed73b086df7d41394326fe80ef", + "shasum": "" + }, + "require": { + "laravel/nova": "^4.0", + "php": "^8.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "WesselPerik\\StatusField\\FieldServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "WesselPerik\\StatusField\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A Laravel Nova field for displaying statuses.", + "keywords": [ + "laravel", + "nova", + "status" + ], + "support": { + "issues": "https://github.com/wesselperik/nova-status-field/issues", + "source": "https://github.com/wesselperik/nova-status-field/tree/2.1.2" + }, + "time": "2022-09-14T19:06:22+00:00" + }, { "name": "wireui/wireui", "version": "v1.17.7", diff --git a/config/model-status.php b/config/model-status.php new file mode 100644 index 00000000..91806935 --- /dev/null +++ b/config/model-status.php @@ -0,0 +1,18 @@ + Spatie\ModelStatus\Status::class, + + /* + * The name of the column which holds the ID of the model related to the statuses. + * + * You can change this value if you have set a different name in the migration for the statuses table. + */ + 'model_primary_key_attribute' => 'model_id', +]; diff --git a/database/migrations/2022_12_15_145559_create_statuses_table.php b/database/migrations/2022_12_15_145559_create_statuses_table.php new file mode 100644 index 00000000..19a5e2fb --- /dev/null +++ b/database/migrations/2022_12_15_145559_create_statuses_table.php @@ -0,0 +1,24 @@ +increments('id'); + $table->string('name'); + $table->text('reason')->nullable(); + $table->morphs('model'); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('statuses'); + } +}