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; |
13
|
|
|
|
14
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior; |
15
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractEventsManager; |
16
|
|
|
use Zemit\Mvc\Model\Behavior; |
17
|
|
|
|
18
|
|
|
trait SoftDelete |
19
|
|
|
{ |
20
|
|
|
use AbstractBehavior; |
21
|
|
|
use AbstractEventsManager; |
22
|
|
|
use Attribute; |
23
|
|
|
|
24
|
|
|
public Behavior\SoftDelete $softDeleteBehavior; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initializing SoftDelete |
28
|
|
|
*/ |
29
|
|
|
public function initializeSoftDelete(array $options = []): void |
30
|
|
|
{ |
31
|
|
|
$options ??= $this->getOptionsManager()->get('softDelete'); |
|
|
|
|
32
|
|
|
$this->setSoftDeleteBehavior(new Behavior\SoftDelete($options)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return the soft delete behavior instance |
37
|
|
|
*/ |
38
|
|
|
public function getSoftDeleteBehavior(): ?Behavior\SoftDelete |
39
|
|
|
{ |
40
|
|
|
return $this->softDeleteBehavior; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set the SoftDeleteBehavior variable |
45
|
|
|
* Attach the SoftDelete behavior class |
46
|
|
|
*/ |
47
|
|
|
public function setSoftDeleteBehavior(Behavior\SoftDelete $softDeleteBehavior): void |
48
|
|
|
{ |
49
|
|
|
$this->softDeleteBehavior = $softDeleteBehavior; |
50
|
|
|
$this->addBehavior($softDeleteBehavior); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Disable the soft delete for the current instance |
55
|
|
|
* Note: Zemit SoftDelete behavior must be attached |
56
|
|
|
*/ |
57
|
|
|
public function disableSoftDelete(): void |
58
|
|
|
{ |
59
|
|
|
$this->getSoftDeleteBehavior()->disable(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Enable the soft delete for the current instance |
64
|
|
|
* Note: Zemit SoftDelete behavior must be attached |
65
|
|
|
*/ |
66
|
|
|
public function enableSoftDelete(): void |
67
|
|
|
{ |
68
|
|
|
$this->getSoftDeleteBehavior()->enable(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Helper method to check if the row is soft deleted |
73
|
|
|
*/ |
74
|
|
|
public function isDeleted(?string $field = null, ?int $deletedValue = null): bool |
75
|
|
|
{ |
76
|
|
|
$field ??= self::DELETED_FIELD; |
|
|
|
|
77
|
|
|
$deletedValue ??= self::YES; |
|
|
|
|
78
|
|
|
return $this->getAttribute($field) === $deletedValue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Restore a previously Soft-deleted entry |
83
|
|
|
* Events: |
84
|
|
|
* - beforeRestore |
85
|
|
|
* - notRestored |
86
|
|
|
* - afterRestore |
87
|
|
|
* |
88
|
|
|
* @throws \Exception |
89
|
|
|
* @todo add a check from orm.events setup state |
90
|
|
|
*/ |
91
|
|
|
public function restore(?string $field = null, ?int $notDeletedValue = null): bool |
92
|
|
|
{ |
93
|
|
|
$ormEvents = (bool)ini_get('orm.events'); |
94
|
|
|
|
95
|
|
|
if ($ormEvents) { |
96
|
|
|
$this->skipped = false; |
|
|
|
|
97
|
|
|
|
98
|
|
|
// fire event, allowing to stop options or skip the current operation |
99
|
|
|
if ($this->fireEventCancel('beforeRestore') === false) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($this->skipped) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// get settings |
109
|
|
|
$field ??= self::DELETED_FIELD; |
|
|
|
|
110
|
|
|
$notDeletedValue ??= self::NO; |
|
|
|
|
111
|
|
|
|
112
|
|
|
// restore |
113
|
|
|
$this->assign([$field => $notDeletedValue], [$field]); |
|
|
|
|
114
|
|
|
$save = $this->save(); |
|
|
|
|
115
|
|
|
|
116
|
|
|
// check if the entity was really restored |
117
|
|
|
$value = $this->getAttribute($field); |
118
|
|
|
$restored = $save && $value === $notDeletedValue; |
119
|
|
|
|
120
|
|
|
// fire events |
121
|
|
|
if ($ormEvents) { |
122
|
|
|
if (!$restored) { |
123
|
|
|
$this->fireEvent('notRestored'); |
124
|
|
|
} |
125
|
|
|
else { |
126
|
|
|
$this->fireEvent('afterRestore'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $restored; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|