1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link http://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Traits\Model; |
15
|
|
|
|
16
|
|
|
use Exception; |
17
|
|
|
use Maslosoft\Mangan\EntityManager; |
18
|
|
|
use Maslosoft\Mangan\Events\Event; |
19
|
|
|
use Maslosoft\Mangan\Events\ModelEvent; |
20
|
|
|
use Maslosoft\Mangan\Events\RestoreEvent; |
21
|
|
|
use Maslosoft\Mangan\Events\TrashEvent; |
22
|
|
|
use Maslosoft\Mangan\Finder; |
23
|
|
|
use Maslosoft\Mangan\Helpers\PkManager; |
24
|
|
|
use Maslosoft\Mangan\Interfaces\TrashInterface; |
25
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
26
|
|
|
use Maslosoft\Mangan\Model\Trash; |
27
|
|
|
use Maslosoft\Mangan\ScenarioManager; |
28
|
|
|
use Maslosoft\Mangan\Validator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Uswe this trait to make model trashable |
32
|
|
|
* |
33
|
|
|
* @author Piotr |
34
|
|
|
*/ |
35
|
|
|
trait TrashableTrait |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Move to trash. Validation will be performed |
40
|
|
|
* before trashing with `trash` (TrashInterface::ScenarioTrash) scenario. |
41
|
|
|
* |
42
|
|
|
* |
43
|
|
|
* @see TrashInterface::ScenarioTrash |
44
|
|
|
* @param boolean $runValidation whether to perform validation before saving the record. |
45
|
|
|
* If the validation fails, the record will not be saved to database. |
46
|
|
|
* |
47
|
|
|
* @return boolean |
48
|
|
|
* @Ignored |
49
|
|
|
*/ |
50
|
|
|
public function trash($runValidation = true) |
51
|
|
|
{ |
52
|
|
|
ScenarioManager::setScenario($this, TrashInterface::ScenarioTrash); |
53
|
|
|
$validator = new Validator($this); |
54
|
|
|
if (!$runValidation || $validator->validate()) |
55
|
|
|
{ |
56
|
|
|
$eventBefore = new TrashEvent($this); |
57
|
|
|
if (Event::hasHandler($this, TrashInterface::EventBeforeTrash)) |
58
|
|
|
{ |
59
|
|
|
if (!Event::valid($this, TrashInterface::EventBeforeTrash, $eventBefore)) |
60
|
|
|
{ |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
$meta = ManganMeta::create($this); |
65
|
|
|
|
66
|
|
|
$trash = $eventBefore->getTrash(); |
67
|
|
|
if (empty($trash)) |
68
|
|
|
{ |
69
|
|
|
$trash = new Trash(); |
70
|
|
|
} |
71
|
|
|
$trash->name = (string) $this; |
|
|
|
|
72
|
|
|
$trash->data = $this; |
|
|
|
|
73
|
|
|
$trash->type = isset($meta->type()->label) ? $meta->type()->label : get_class($this); |
|
|
|
|
74
|
|
|
$trash->save(); |
75
|
|
|
|
76
|
|
|
$eventAfter = new TrashEvent($this); |
77
|
|
|
|
78
|
|
|
Event::trigger($this, TrashInterface::EventAfterTrash, $eventAfter); |
79
|
|
|
|
80
|
|
|
// Use deleteOne, to avoid beforeDelete event, |
81
|
|
|
// which should be raised only when really removing document: |
82
|
|
|
// when emtying trash |
83
|
|
|
|
84
|
|
|
$em = new EntityManager($this); |
85
|
|
|
$em->deleteOne(PkManager::prepareFromModel($this)); |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Restore trashed item |
93
|
|
|
* @return boolean |
94
|
|
|
* @throws Exception |
95
|
|
|
* @Ignored |
96
|
|
|
*/ |
97
|
|
|
public function restore() |
98
|
|
|
{ |
99
|
|
|
if (!$this instanceof TrashInterface) |
100
|
|
|
{ |
101
|
|
|
// When trying to restore normal document instead of trash item |
102
|
|
|
throw new Exception('Restore can be performed only on `%s` instance', TrashInterface::class); |
103
|
|
|
} |
104
|
|
|
$em = new EntityManager($this->data); |
|
|
|
|
105
|
|
|
|
106
|
|
|
Event::trigger($this->data, TrashInterface::EventBeforeRestore); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$saved = $em->save(); |
109
|
|
|
if (!$saved) |
110
|
|
|
{ |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
$finder = new Finder($this->data); |
|
|
|
|
114
|
|
|
$model = $finder->find(PkManager::prepareFromModel($this->data)); |
|
|
|
|
115
|
|
|
if (!$model) |
116
|
|
|
{ |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
$eventAfter = new RestoreEvent(); |
120
|
|
|
$eventAfter->setTrashed($this); |
|
|
|
|
121
|
|
|
Event::trigger($model, TrashInterface::EventAfterRestore, $eventAfter); |
122
|
|
|
|
123
|
|
|
$trashEm = new EntityManager($this); |
|
|
|
|
124
|
|
|
|
125
|
|
|
// Use deleteOne, to avoid beforeDelete event, |
126
|
|
|
// which should be raised only when really removing document: |
127
|
|
|
// when emtying trash |
128
|
|
|
$this->data = null; |
|
|
|
|
129
|
|
|
|
130
|
|
|
$trashEm->deleteOne(PkManager::prepareFromModel($this)); |
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: