Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEV-1859] Feature - Upgrade to Laravel 8 #2

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added storing Force Delete
  • Loading branch information
Sergey Zhidkov authored and duellsy committed Sep 8, 2020
commit b3c462e340fa45b05540a2004a0ba853dfa1e8cc
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ To better format the output for `deleted_at` entries, you can use the `isEmpty`

<a name="control"></a>

### Storing Force Delete
By default the Force Delete of a model is not stored as a revision.

If you want to store the Force Delete as a revision you can override this behavior by setting `revisionForceDeleteEnabled ` to `true` by adding the following to your model:
```php
protected $revisionForceDeleteEnabled = true;
```

### Storing Creations
By default the creation of a new model is not stored as a revision.
Only subsequent changes to a model is stored.
Expand Down
32 changes: 32 additions & 0 deletions src/Venturecraft/Revisionable/Revisionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static function boot()
static::deleted(function ($model) {
$model->preSave();
$model->postDelete();
$model->postForceDelete();
});
}
/**
Expand Down Expand Up @@ -224,6 +225,37 @@ public function postDelete()
}
}

/**
* If forcedeletes are enabled, set the value created_at of model to null
*
* @return void|bool
*/
public function postForceDelete()
{
if (empty($this->revisionForceDeleteEnabled)) {
return false;
}

if ((!isset($this->revisionEnabled) || $this->revisionEnabled)
&& (($this->isSoftDelete() && $this->isForceDeleting()) || !$this->isSoftDelete())) {

$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'key' => self::CREATED_AT,
'old_value' => $this->{self::CREATED_AT},
'new_value' => null,
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);

$revision = Revisionable::newModel();
\DB::table($revision->getTable())->insert($revisions);
\Event::dispatch('revisionable.deleted', array('model' => $this, 'revisions' => $revisions));
}
}

/**
* Attempt to find the user id of the currently logged in user
* Supports Cartalyst Sentry/Sentinel based authentication, as well as stock Auth
Expand Down
32 changes: 32 additions & 0 deletions src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public static function bootRevisionableTrait()
static::deleted(function ($model) {
$model->preSave();
$model->postDelete();
$model->postForceDelete();
});
}

Expand Down Expand Up @@ -281,6 +282,37 @@ public function postDelete()
}
}

/**
* If forcedeletes are enabled, set the value created_at of model to null
*
* @return void|bool
*/
public function postForceDelete()
{
if (empty($this->revisionForceDeleteEnabled)) {
return false;
}

if ((!isset($this->revisionEnabled) || $this->revisionEnabled)
&& (($this->isSoftDelete() && $this->isForceDeleting()) || !$this->isSoftDelete())) {

$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'key' => self::CREATED_AT,
'old_value' => $this->{self::CREATED_AT},
'new_value' => null,
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);

$revision = Revisionable::newModel();
\DB::table($revision->getTable())->insert($revisions);
\Event::dispatch('revisionable.deleted', array('model' => $this, 'revisions' => $revisions));
}
}

/**
* Attempt to find the user id of the currently logged in user
* Supports Cartalyst Sentry/Sentinel based authentication, as well as stock Auth
Expand Down