Skip to content

Commit

Permalink
filters_on_id setting
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Dec 9, 2022
1 parent e5acdb4 commit 79351d1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
80 changes: 50 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
SilverStripe Soft Delete Module
==================
# SilverStripe Soft Delete Module

Add a soft delete behaviour to your dataobjects. Objects are simply marked as deleted and kept in the database.

Expand All @@ -9,57 +8,78 @@ ModelAdmin and SecurityAdmin are extended to add a new GridField action that rep

This module depends on [lekoala/silverstripe-cms-actions](https://github.com/lekoala/silverstripe-cms-actions) for displaying delete buttons

How to use
==================
# How to use

Simply replace your calls from delete to softDelete.

New extensions hooks are provided to avoid side effects (onBeforeSoftDelete, onAfterSoftDelete).
These are ideals if you have child records that need to be soft deleted with their parent.

Config options
==================

SilverStripe\Admin\ModelAdmin:
softdelete_from_list: true
softdelete_from_list_exclude: []
extensions:
- SoftDeleteModelAdmin
SilverStripe\Admin\SecurityAdmin:
softdelete_from_list: true
softdelete_from_list_exclude: []
extensions:
- SoftDeleteSecurityAdmin
# Config options

```yml
SilverStripe\Admin\ModelAdmin:
softdelete_from_list: true
softdelete_from_list_exclude: []
extensions:
- SoftDeleteModelAdmin
SilverStripe\Admin\SecurityAdmin:
softdelete_from_list: true
softdelete_from_list_exclude: []
extensions:
- SoftDeleteSecurityAdmin
```
You can configure:
- softdelete_from_list: show delete button on a line. Enabled by default.
- softdelete_from_list_exclude: hide the delete button for these classes even if enabled globally
Prevent accidental deletion
==================
- softdelete_from_list: show delete button on a line. Enabled by default.
- softdelete_from_list_exclude: hide the delete button for these classes even if enabled globally
# Prevent accidental deletion
By default, the module will prevent any delete from happening. To allow deletion, you must set
SoftDeletable:$prevent_delete = false
```php
SoftDeletable:$prevent_delete = false
```

The only way from the CMS UI to delete a record is to go to a soft deleted record
and click "Really delete" which will call "forceDelete" on the record.

Disable filtering
==================
# Disable filtering

You can disable filtering globally, using

SoftDeletable::$disable = true
```php
SoftDeletable::$disable = true
```

Or at query level

$dataQuery->setQueryParam('SoftDeletable.filter',false)
```php
$dataQuery->setQueryParam('SoftDeletable.filter',false)
```

# Filtering on ids

By default, this module will let you return soft deleted records if you
ask them specifically by ID. This is by design to prevent things breaking accidentally.
If you want to make sure you don't display soft deleted records, make
sure to implement a proper canView() method that fits your usage.

An alternative option, is to disable that feature:

```yml
SoftDeletable:
filters_on_id: true
```
Keep in mind that `DataObject::get_by_id();` can get cached and it can lead to tricky scenarios.

# Compatibility

Compatibility
==================
Tested with 4.4+

Maintainer
==================
# Maintainer

LeKoala - [email protected]
2 changes: 2 additions & 0 deletions _config/softdelete.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
Name: softdelete
---
SoftDeletable:
filters_on_id: true
SilverStripe\Admin\ModelAdmin:
softdelete_from_list: true
softdelete_from_list_exclude: []
Expand Down
5 changes: 4 additions & 1 deletion code/SoftDeletable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\ORM\DataExtension;
use LeKoala\CmsActions\CustomAction;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\ORM\Queries\SQLSelect;

Expand All @@ -22,6 +23,8 @@
*/
class SoftDeletable extends DataExtension
{
use Configurable;

/**
* Disable the filtering
*
Expand Down Expand Up @@ -99,7 +102,7 @@ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
return;
}
// Don't run if querying by ID
if ($query->filtersOnID()) {
if ($query->filtersOnID() && self::config()->filters_on_id) {
return;
}

Expand Down

0 comments on commit 79351d1

Please sign in to comment.