1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model\Blameable; |
13
|
|
|
|
14
|
|
|
use Zemit\Mvc\Model; |
15
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior; |
16
|
|
|
use Zemit\Mvc\Model\Behavior\Transformable; |
17
|
|
|
use Zemit\Mvc\Model\Identity; |
18
|
|
|
use Zemit\Mvc\Model\Options; |
19
|
|
|
use Zemit\Mvc\Model\Snapshots; |
20
|
|
|
use Zemit\Mvc\Model\SoftDelete; |
21
|
|
|
|
22
|
|
|
trait Deleted |
23
|
|
|
{ |
24
|
|
|
use AbstractBehavior; |
25
|
|
|
use Options; |
26
|
|
|
use Identity; |
27
|
|
|
use Snapshots; |
28
|
|
|
use SoftDelete; |
29
|
|
|
|
30
|
|
|
public Transformable $deletedBehavior; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializing Deleted |
34
|
|
|
*/ |
35
|
|
|
public function initializeDeleted(?array $options = null): void |
36
|
|
|
{ |
37
|
|
|
$options ??= $this->getOptionsManager()->get('deleted') ?? []; |
38
|
|
|
|
39
|
|
|
$fieldBy = $options['fieldBy'] ?? 'deletedBy'; |
40
|
|
|
$fieldAs = $options['fieldAs'] ?? 'deletedAs'; |
41
|
|
|
$fieldAt = $options['fieldAt'] ?? 'deletedAt'; |
42
|
|
|
|
43
|
|
|
$this->setDeletedBehavior(new Transformable([ |
44
|
|
|
'beforeDelete' => [ |
45
|
|
|
$fieldBy => $this->getCurrentUserIdCallback(false), |
46
|
|
|
$fieldAs => $this->getCurrentUserIdCallback(true), |
47
|
|
|
$fieldAt => date(Model::DATETIME_FORMAT), |
48
|
|
|
], |
49
|
|
|
'beforeValidationOnUpdate' => [ |
50
|
|
|
$fieldBy => $this->hasChangedCallback(function ($model, $field) { |
|
|
|
|
51
|
|
|
return $model->isDeleted() |
52
|
|
|
? $this->getCurrentUserIdCallback()() |
53
|
|
|
: $model->readAttribute($field); |
54
|
|
|
}), |
55
|
|
|
$fieldAs => $this->hasChangedCallback(function ($model, $field) { |
56
|
|
|
return $model->isDeleted() |
57
|
|
|
? $this->getCurrentUserIdCallback(true)() |
58
|
|
|
: $model->readAttribute($field); |
59
|
|
|
}), |
60
|
|
|
$fieldAt => $this->hasChangedCallback(function ($model, $field) { |
61
|
|
|
return $model->isDeleted() |
62
|
|
|
? date(Model::DATETIME_FORMAT) |
63
|
|
|
: $model->readAttribute($field); |
64
|
|
|
}), |
65
|
|
|
], |
66
|
|
|
])); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set Deleted Behavior |
71
|
|
|
*/ |
72
|
|
|
public function setDeletedBehavior(Transformable $deletedBehavior): void |
73
|
|
|
{ |
74
|
|
|
$this->deletedBehavior = $deletedBehavior; |
75
|
|
|
$this->addBehavior($this->deletedBehavior); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get Deleted Behavior |
80
|
|
|
*/ |
81
|
|
|
public function getDeletedBehavior(): Transformable |
82
|
|
|
{ |
83
|
|
|
return $this->deletedBehavior; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|