|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace miBadger\ActiveRecord\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use miBadger\Query\Query; |
|
6
|
|
|
use miBadger\ActiveRecord\ColumnProperty; |
|
7
|
|
|
|
|
8
|
|
|
const TRAIT_SOFT_DELETE_FIELD_KEY = "soft_delete"; |
|
9
|
|
|
|
|
10
|
|
|
trait SoftDelete |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var boolean the soft delete status for the entity this trait is embedded into. */ |
|
13
|
|
|
protected $softDelete; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* this method is required to be called in the constructor for each class that uses this trait. |
|
17
|
|
|
* It adds the required fields to the table definition and registers hooks |
|
18
|
|
|
*/ |
|
19
|
21 |
|
protected function initSoftDelete() |
|
20
|
|
|
{ |
|
21
|
21 |
|
$this->softDelete = false; |
|
22
|
|
|
|
|
23
|
21 |
|
$this->extendTableDefinition(TRAIT_SOFT_DELETE_FIELD_KEY, [ |
|
24
|
21 |
|
'value' => &$this->softDelete, |
|
25
|
|
|
'validate' => null, |
|
26
|
21 |
|
'default' => 0, |
|
27
|
21 |
|
'type' => 'INT', |
|
28
|
21 |
|
'length' => 1, |
|
29
|
21 |
|
'properties' => ColumnProperty::NOT_NULL |
|
30
|
|
|
]); |
|
31
|
|
|
|
|
32
|
21 |
|
$this->registerSearchHook(TRAIT_SOFT_DELETE_FIELD_KEY, 'softDeleteSearchHook'); |
|
33
|
21 |
|
$this->registerReadHook(TRAIT_SOFT_DELETE_FIELD_KEY, 'softDeleteReadHook'); |
|
34
|
21 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The hook that gets called whenever a query is made |
|
38
|
|
|
*/ |
|
39
|
2 |
|
protected function softDeleteSearchHook() |
|
40
|
|
|
{ |
|
41
|
2 |
|
return Query::Equal(TRAIT_SOFT_DELETE_FIELD_KEY, 0); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
8 |
|
protected function softDeleteReadHook() |
|
45
|
|
|
{ |
|
46
|
8 |
|
return Query::Equal(TRAIT_SOFT_DELETE_FIELD_KEY, 0); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* returns the name for the soft delete field in the database |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function getSoftDeleteFieldName() |
|
54
|
|
|
{ |
|
55
|
1 |
|
return TRAIT_SOFT_DELETE_FIELD_KEY; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Mark the current record as soft deleted |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function softDelete() |
|
63
|
|
|
{ |
|
64
|
3 |
|
$this->softDelete = true; |
|
65
|
3 |
|
$this->update(); |
|
|
|
|
|
|
66
|
3 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Undo the current soft deletion status (mark it as non-soft deleted) |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function softRestore() |
|
74
|
|
|
{ |
|
75
|
1 |
|
$this->softDelete = false; |
|
76
|
1 |
|
$this->update(); |
|
77
|
1 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* returns the current soft deletion status |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function getDeletionStatus() |
|
85
|
|
|
{ |
|
86
|
1 |
|
return $this->softDelete; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
|
abstract protected function extendTableDefinition(string $columnName, $definition); |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
abstract protected function registerSearchHook(string $columnName, $fn); |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
abstract protected function registerDeleteHook(string $columnName, $fn); |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
abstract protected function registerUpdateHook(string $columnName, $fn); |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
abstract protected function registerReadHook(string $columnName, $fn); |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
|
|
abstract protected function registerCreateHook(string $columnName, $fn); |
|
118
|
|
|
|
|
119
|
|
|
} |