Skip to content

Commit

Permalink
Merge pull request #3 from okeonline/dev-page-actions
Browse files Browse the repository at this point in the history
Add Archive and UnArchive page actions
  • Loading branch information
rvzug authored Aug 9, 2024
2 parents fa08445 + f8d87e8 commit c62f89f
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 7 deletions.
68 changes: 64 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Filament plugin for archiving and unarchiving table records (eloquent models) based on the [Laravel Archivable package by Joe Butcher](https://github.com/joelbutcher/laravel-archivable).

This filament plugin adds an [ArchiveAction](#archiveunarchive-actions), an [UnArchiveAction](#archiveunarchive-actions) and a [ArchivedFilter](#filtering) to your resource tables. It's also possible to [add custom row-classes for archived records](#add-custom-classes-to-archived-rows).
This filament plugin adds an [ArchiveAction](#archiveunarchive-actions), an [UnArchiveAction](#archiveunarchive-actions) and a [ArchivedFilter](#filtering) to your resource tables. It's also possible to [add custom row-classes for archived records](#add-custom-classes-to-archived-rows). In addition to table-actions, the package provides also page-actions for view/edit pages to archive and unarchive your records.

## Requirements

Expand Down Expand Up @@ -48,7 +48,7 @@ Follow his installation instructions, which -in short- instructs:

## Usage

### Archive and Unarchive actions
### Archive and UnArchive table actions
As soon as the ```Archivable```-trait from the [Laravel Archivable package](#installation) is added to the model, it is possible to add the following actions to the corresponding resource table:

```php
Expand Down Expand Up @@ -78,7 +78,7 @@ It will show the ```ArchiveAction``` on records that aren't archived, and will s

> You should add **both** actions to the same table. The action itself wil determine if it should be shown on the record.
The actions are normal table actions, similar to the [Delete](https://filamentphp.com/docs/3.x/actions/prebuilt-actions/delete) and [Restore](https://filamentphp.com/docs/3.x/actions/prebuilt-actions/restore) actions of FilamentPHP. You can add all features that are described in the [FilamentPHP Table Actions Documentation](https://filamentphp.com/docs/3.x/tables/actions), like:
The actions are normal **table** actions, similar to the [Delete](https://filamentphp.com/docs/3.x/actions/prebuilt-actions/delete) and [Restore](https://filamentphp.com/docs/3.x/actions/prebuilt-actions/restore) table actions of FilamentPHP. You can add all features that are described in the [FilamentPHP Table Actions Documentation](https://filamentphp.com/docs/3.x/tables/actions), like:

- ```hiddenLabel()```
- ```tooltip()```
Expand All @@ -92,7 +92,47 @@ ArchiveAction::make()
->tooltip('Archive'),
```

The actions call the ```$model->archive()``` and ```$model->unArchive()``` methods that are provided by the [Laravel Achivable package](https://github.com/joelbutcher/laravel-archivable?tab=readme-ov-file#extensions).
The table actions call the ```$model->archive()``` and ```$model->unArchive()``` methods that are provided by the [Laravel Achivable package](https://github.com/joelbutcher/laravel-archivable?tab=readme-ov-file#extensions).

### Archive and UnArchive page actions
As soon as the ```Archivable```-trait from the [Laravel Archivable package](#installation) is added to the model, it is possible to add the following actions to the resource **pages**:

```php
// in e.g. Filament/PostResource/EditPage.php
use Okeonline\FilamentArchivable\Actions\ArchiveAction;
use Okeonline\FilamentArchivable\Actions\UnArchiveAction;

// ...
protected function getHeaderActions(): array
{
return [
ArchiveAction::make(),
UnArchiveAction::make(),
];
}
```

It will show the ```ArchiveAction``` on records that aren't archived, and will show the ```UnArchiveAction``` on those which are currently archived:

> Be aware that there is a difference between **table** actions and **normal (page)** actions. You can not use the page actions as a table action, vice versa. Nevertheless, the business-logic is the same. *Read [this page](https://filamentphp.com/docs/3.x/actions/overview) for more information about the difference.*
> You should add **both** actions to the same page. The action itself wil determine if it should be shown on the record. Check [this](#add-custom-classes-to-archived-rows) if you want to edit (and unarchive) records that are being archived.
The actions are normal actions, similar to the [Delete](https://filamentphp.com/docs/3.x/actions/prebuilt-actions/delete) and [Restore](https://filamentphp.com/docs/3.x/actions/prebuilt-actions/restore) actions of FilamentPHP. You can add all features that are described in the [FilamentPHP Actions Documentation](https://filamentphp.com/docs/3.x/actions/overview), like:

- ```color()```
- ```size()```
- ```disabled()```
- ```icon()```
- ... etc.

```php
use Filament\Support\Enums\ActionSize;

ArchiveAction::make()
->color('success')
->size(ActionSize::Large),
```

### Filtering

Expand Down Expand Up @@ -122,6 +162,26 @@ public static function table(Table $table): Table

The ```ArchivedFilter``` will respect all options that Tenary Filters have, so check the [Tenary Filter Documentation of Filament](https://filamentphp.com/docs/3.x/tables/filters/ternary) to customize the filter.

#### Ability to view/edit/delete archived records
If you want to be able to view/edit/delete archived records, you should disable the global ```ArchivedScope::class``` on your resource ``` getEloquentQuery()``` method:

```php
// in your resource:
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use LaravelArchivable\Scopes\ArchivableScope;

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class, // only if soft deleting is also active, otherwise it can be ommitted
ArchivableScope::class,
]);
}
```
See [Disabeling global scopes on Filament](https://filamentphp.com/docs/3.x/panels/resources/getting-started#disabling-global-scopes) for more information about the default resource query.

### Add custom classes to archived rows

This plugin comes with a ```Table::macro``` which allows you to add custom (CSS/Tailwind) classes to ***table-rows** that are archived*:
Expand Down
65 changes: 65 additions & 0 deletions resources/lang/en/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

return [

'archive' => [

'single' => [

'label' => 'Archive',

'modal' => [

'heading' => 'Archive :label',

'actions' => [

'archive' => [

'label' => 'Archive',
],

],

],

'notifications' => [

'archived' => [

'title' => 'Record archived',
],
],
],
],

'unarchive' => [

'single' => [

'label' => 'Unarchive',

'modal' => [

'heading' => 'Unarchive :label',

'actions' => [

'unarchive' => [

'label' => 'Unarchive',
],

],
],

'notifications' => [

'unarchived' => [

'title' => 'Record unarchived',
],
],
],
],
];
65 changes: 65 additions & 0 deletions resources/lang/nl/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

return [

'archive' => [

'single' => [

'label' => 'Archiveer',

'modal' => [

'heading' => 'Archiveer :label',

'actions' => [

'archive' => [

'label' => 'Archiveren',
],

],

],

'notifications' => [

'archived' => [

'title' => 'Record gearchiveerd',
],
],
],
],

'unarchive' => [

'single' => [

'label' => 'Herstellen',

'modal' => [

'heading' => 'Herstel :label',

'actions' => [

'unarchive' => [

'label' => 'Herstellen',
],

],
],

'notifications' => [

'unarchived' => [

'title' => 'Record hersteld',
],
],
],
],
];
62 changes: 62 additions & 0 deletions src/Actions/ArchiveAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Okeonline\FilamentArchivable\Actions;

use Filament\Actions\Action;
use Filament\Actions\Concerns\CanCustomizeProcess;
use Illuminate\Database\Eloquent\Model;

class ArchiveAction extends Action
{
use CanCustomizeProcess;

public static function getDefaultName(): ?string
{
return 'archive';
}

protected function setUp(): void
{
parent::setUp();

$this->label(__('filament-archivable::actions.archive.single.label'));

$this->modalHeading(fn (): string => __('filament-archivable::actions.archive.single.modal.heading', ['label' => $this->getRecordTitle()]));

$this->modalSubmitActionLabel(__('filament-archivable::actions.archive.single.modal.actions.archived.label'));

$this->successNotificationTitle(__('filament-archivable::actions.archive.single.notifications.archived.title'));

$this->color('warning');

$this->icon('heroicon-m-archive-box-arrow-down');

$this->requiresConfirmation();

$this->modalIcon('heroicon-m-archive-box-arrow-down');

$this->hidden(static function (Model $record): bool {
if (! method_exists($record, 'isArchived')) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

return $record->isArchived();
});

$this->action(function (): void {
$result = $this->process(static fn (Model $record) => $record->archive());

if (! $result) {
// @codeCoverageIgnoreStart
$this->failure();

return;
// @codeCoverageIgnoreEnd
}

$this->success();
});
}
}
70 changes: 70 additions & 0 deletions src/Actions/UnArchiveAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Okeonline\FilamentArchivable\Actions;

use Filament\Actions\Action;
use Filament\Actions\Concerns\CanCustomizeProcess;
use Illuminate\Database\Eloquent\Model;

class UnArchiveAction extends Action
{
use CanCustomizeProcess;

public static function getDefaultName(): ?string
{
return 'unarchive';
}

protected function setUp(): void
{
parent::setUp();

$this->label(__('filament-archivable::actions.unarchive.single.label'));

$this->modalHeading(fn (): string => __('filament-archivable::actions.unarchive.single.modal.heading', ['label' => $this->getRecordTitle()]));

$this->modalSubmitActionLabel(__('filament-archivable::actions.unarchive.single.modal.actions.unarchive.label'));

$this->successNotificationTitle(__('filament-archivable::actions.unarchive.single.notifications.unarchived.title'));

$this->color('gray');

$this->icon('heroicon-m-arrow-uturn-left');

$this->requiresConfirmation();

$this->modalIcon('heroicon-o-arrow-uturn-left');

$this->action(function (Model $record): void {
if (! method_exists($record, 'unArchive')) {
// @codeCoverageIgnoreStart
$this->failure();

return;
// @codeCoverageIgnoreEnd
}

$result = $this->process(static fn () => $record->unArchive());

if (! $result) {
// @codeCoverageIgnoreStart
$this->failure();

return;
// @codeCoverageIgnoreEnd
}

$this->success();
});

$this->visible(static function (Model $record): bool {
if (! method_exists($record, 'isArchived')) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

return $record->isArchived();
});
}
}
Loading

0 comments on commit c62f89f

Please sign in to comment.