1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Table\Actions; |
4
|
|
|
|
5
|
|
|
use Yaro\Jarboe\Table\CRUD; |
6
|
|
|
|
7
|
|
|
class ActionsContainer |
8
|
|
|
{ |
9
|
|
|
private $actions = []; |
10
|
|
|
|
11
|
43 |
|
public function find($ident) |
12
|
|
|
{ |
13
|
|
|
/** @var AbstractAction $action */ |
14
|
43 |
|
foreach ($this->actions as $action) { |
15
|
43 |
|
if ($action->identifier() == $ident) { |
16
|
38 |
|
return $action; |
17
|
|
|
} |
18
|
|
|
} |
19
|
11 |
|
return null; |
20
|
|
|
} |
21
|
|
|
|
22
|
2 |
|
public function getRowActions(): array |
23
|
|
|
{ |
24
|
2 |
|
$actions = []; |
25
|
|
|
/** @var AbstractAction $action */ |
26
|
2 |
|
foreach ($this->actions as $action) { |
27
|
2 |
|
if ($action->identifier() != 'create') { |
28
|
2 |
|
$actions[] = $action; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
return $actions; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function remove($ident) |
36
|
|
|
{ |
37
|
1 |
|
$actions = []; |
38
|
|
|
/** @var AbstractAction $action */ |
39
|
1 |
|
foreach ($this->actions as $action) { |
40
|
1 |
|
if ($action->identifier() != $ident) { |
41
|
1 |
|
$actions[] = $action; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$this->actions = $actions; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
92 |
|
public function add($actions) |
49
|
|
|
{ |
50
|
92 |
|
$actions = is_array($actions) ? $actions : [$actions]; |
51
|
92 |
|
foreach ($actions as $action) { |
52
|
92 |
|
$this->addAction($action); |
53
|
|
|
} |
54
|
92 |
|
} |
55
|
|
|
|
56
|
92 |
|
private function addAction(AbstractAction $action) |
57
|
|
|
{ |
58
|
92 |
|
$this->actions[] = $action; |
59
|
92 |
|
} |
60
|
|
|
|
61
|
92 |
|
public function set(array $actions = []) |
62
|
|
|
{ |
63
|
92 |
|
$this->actions = []; |
64
|
92 |
|
$this->add($actions); |
65
|
92 |
|
} |
66
|
|
|
|
67
|
32 |
|
public function isAllowed($ident, $model = null): bool |
68
|
|
|
{ |
69
|
32 |
|
$action = $this->find($ident); |
70
|
32 |
|
if (!$action) { |
71
|
2 |
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
31 |
|
return $action->isAllowed($model); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function shouldRender($ident, $model = null): bool |
78
|
|
|
{ |
79
|
1 |
|
$action = $this->find($ident); |
80
|
1 |
|
if (!$action) { |
81
|
1 |
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $action->shouldRender($model); |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
public function moveAfter($baseActionIdent, $movableActionIdent) |
88
|
|
|
{ |
89
|
4 |
|
if (!$this->find($baseActionIdent) || !$this->find($movableActionIdent)) { |
90
|
3 |
|
throw new \RuntimeException( |
91
|
3 |
|
sprintf('No action defined for identifier [%s] or [%s]', $baseActionIdent, $movableActionIdent) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$actions = []; |
96
|
|
|
/** @var AbstractAction $action */ |
97
|
1 |
|
foreach ($this->actions as $action) { |
98
|
1 |
|
if ($action->identifier() == $movableActionIdent) { |
99
|
1 |
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$actions[] = $action; |
103
|
1 |
|
if ($action->identifier() == $baseActionIdent) { |
104
|
1 |
|
$actions[] = $this->find($movableActionIdent); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$this->actions = $actions; |
109
|
1 |
|
} |
110
|
|
|
|
111
|
4 |
|
public function moveBefore($baseActionIdent, $movableActionIdent) |
112
|
|
|
{ |
113
|
4 |
|
if (!$this->find($baseActionIdent) || !$this->find($movableActionIdent)) { |
114
|
3 |
|
throw new \RuntimeException( |
115
|
3 |
|
sprintf('No action defined for identifier [%s] or [%s]', $baseActionIdent, $movableActionIdent) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$actions = []; |
120
|
|
|
/** @var AbstractAction $action */ |
121
|
1 |
|
foreach ($this->actions as $action) { |
122
|
1 |
|
if ($action->identifier() == $movableActionIdent) { |
123
|
1 |
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
if ($action->identifier() == $baseActionIdent) { |
127
|
1 |
|
$actions[] = $this->find($movableActionIdent); |
128
|
|
|
} |
129
|
1 |
|
$actions[] = $action; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
$this->actions = $actions; |
133
|
1 |
|
} |
134
|
|
|
|
135
|
60 |
|
public function setCrud(CRUD $crud) |
136
|
|
|
{ |
137
|
|
|
/** @var AbstractAction $action */ |
138
|
60 |
|
foreach ($this->actions as $action) { |
139
|
60 |
|
$action->setCrud($crud); |
140
|
|
|
} |
141
|
60 |
|
} |
142
|
|
|
} |
143
|
|
|
|