5
5
namespace Atk4 \Data \Model ;
6
6
7
7
use Atk4 \Core \Factory ;
8
+ use Atk4 \Data \Exception ;
8
9
9
10
trait UserActionsTrait
10
11
{
@@ -28,6 +29,10 @@ trait UserActionsTrait
28
29
*/
29
30
public function addUserAction (string $ name , $ defaults = []): UserAction
30
31
{
32
+ if ($ this ->isEntity () && $ this ->getModel ()->hasUserAction ($ name )) {
33
+ $ this ->assertIsModel ();
34
+ }
35
+
31
36
if ($ defaults instanceof \Closure) {
32
37
$ defaults = ['callback ' => $ defaults ];
33
38
}
@@ -49,9 +54,27 @@ public function addUserAction(string $name, $defaults = []): UserAction
49
54
*/
50
55
public function hasUserAction (string $ name ): bool
51
56
{
57
+ if ($ this ->isEntity () && $ this ->getModel ()->hasUserAction ($ name )) {
58
+ return true ;
59
+ }
60
+
52
61
return $ this ->_hasInCollection ($ name , 'userActions ' );
53
62
}
54
63
64
+ private function addUserActionFromModel (string $ name , UserAction $ action ): void
65
+ {
66
+ $ this ->assertIsEntity ();
67
+ $ action ->getOwner ()->assertIsModel ();
68
+ if (\Closure::bind (fn () => $ action ->entity , null , UserAction::class)() !== null ) {
69
+ throw new Exception ('Model action entity is expected to be null ' );
70
+ }
71
+
72
+ // clone action and store it in entity
73
+ $ action = clone $ action ;
74
+ $ action ->unsetOwner ();
75
+ $ this ->_addIntoCollection ($ name , $ action , 'userActions ' );
76
+ }
77
+
55
78
/**
56
79
* Returns list of actions for this model. Can filter actions by records they apply to.
57
80
* It will also skip system user actions (where system === true).
@@ -62,6 +85,12 @@ public function hasUserAction(string $name): bool
62
85
*/
63
86
public function getUserActions (string $ appliesTo = null ): array
64
87
{
88
+ if ($ this ->isEntity ()) {
89
+ foreach (array_diff_key ($ this ->getModel ()->getUserActions ($ appliesTo ), $ this ->userActions ) as $ name => $ action ) {
90
+ $ this ->addUserActionFromModel ($ name , $ action );
91
+ }
92
+ }
93
+
65
94
return array_filter ($ this ->userActions , function ($ action ) use ($ appliesTo ) {
66
95
return !$ action ->system && ($ appliesTo === null || $ action ->appliesTo === $ appliesTo );
67
96
});
@@ -72,6 +101,10 @@ public function getUserActions(string $appliesTo = null): array
72
101
*/
73
102
public function getUserAction (string $ name ): UserAction
74
103
{
104
+ if ($ this ->isEntity () && !$ this ->_hasInCollection ($ name , 'userActions ' ) && $ this ->getModel ()->hasUserAction ($ name )) {
105
+ $ this ->addUserActionFromModel ($ name , $ this ->getModel ()->getUserAction ($ name ));
106
+ }
107
+
75
108
return $ this ->_getFromCollection ($ name , 'userActions ' );
76
109
}
77
110
@@ -82,10 +115,12 @@ public function getUserAction(string $name): UserAction
82
115
*/
83
116
public function removeUserAction (string $ name )
84
117
{
85
- foreach (( array ) $ name as $ action ) {
86
- $ this ->_removeFromCollection ( $ action , ' userActions ' );
118
+ if ( $ this -> isEntity () && $ this -> getModel ()-> hasUserAction ( $ name ) ) {
119
+ $ this ->assertIsModel ( );
87
120
}
88
121
122
+ $ this ->_removeFromCollection ($ name , 'userActions ' );
123
+
89
124
return $ this ;
90
125
}
91
126
0 commit comments