5
5
use Illuminate \Database \Eloquent \Model ;
6
6
use Illuminate \Database \Eloquent \Relations \MorphMany ;
7
7
use Illuminate \Database \Eloquent \SoftDeletes ;
8
- use Scrn \Journal \Events \ActivityPrepared ;
9
8
use Scrn \Journal \Models \Activity ;
10
9
11
10
trait LogsActivity
12
11
{
12
+ use DetectsChanges;
13
13
use LogsRelatedActivity;
14
14
15
15
/**
@@ -36,31 +36,23 @@ protected static function bootLogsActivity()
36
36
37
37
list ($ old_data , $ new_data ) = method_exists ($ model , $ attributeGetter ) ? $ model ->$ attributeGetter (...func_get_args ()) : null ;
38
38
39
- $ activity = $ model -> transformActivity ( journal ()->action ($ event )->on ($ model )->data ($ old_data , $ new_data )->toActivity () );
39
+ $ activity = journal ()->action ($ event )->on ($ model )->data ($ old_data , $ new_data )->toActivity ();
40
40
41
- event ( new ActivityPrepared ( $ activity ));
41
+ $ model -> transformActivity ( $ activity )-> save ( );
42
42
});
43
43
}
44
44
}
45
45
46
46
/**
47
- * Get the attributes for a created event.
47
+ * Get the attributes for the created event.
48
48
*
49
49
* @return array
50
50
*/
51
51
public function getCreatedEventAttributes (): array
52
52
{
53
- $ attributes = [];
54
-
55
- foreach ($ this ->getAttributes () as $ attribute => $ value ) {
56
- if ($ this ->shouldAttributeBeLogged ($ attribute )) {
57
- $ attributes [$ attribute ] = $ value ;
58
- }
59
- }
60
-
61
53
return [
62
54
null ,
63
- $ attributes ,
55
+ $ this -> getLoggedAttributeValues () ,
64
56
];
65
57
}
66
58
@@ -71,15 +63,13 @@ public function getCreatedEventAttributes(): array
71
63
*/
72
64
public function getUpdatedEventAttributes (): array
73
65
{
74
- $ old = [] ;
75
- $ new = [] ;
66
+ $ old = $ this -> getOldAttributes () ;
67
+ $ new = $ this -> getLoggedAttributeValues () ;
76
68
77
- foreach ($ this ->getDirty () as $ attribute => $ value ) {
78
- if ($ this ->shouldAttributeBeLogged ($ attribute )) {
79
- $ old [$ attribute ] = $ this ->getOriginal ($ attribute );
80
- $ new [$ attribute ] = $ this ->getAttribute ($ attribute );
81
- }
82
- }
69
+ $ new = array_diff_uassoc ($ new , $ old , function ($ new , $ old ) {
70
+ return $ new <=> $ old ;
71
+ });
72
+ $ old = array_intersect_key ($ old , $ new );
83
73
84
74
return [
85
75
$ old ,
@@ -94,22 +84,14 @@ public function getUpdatedEventAttributes(): array
94
84
*/
95
85
public function getDeletedEventAttributes (): array
96
86
{
97
- $ attributes = [];
98
-
99
- foreach ($ this ->getAttributes () as $ attribute => $ value ) {
100
- if ($ this ->shouldAttributeBeLogged ($ attribute )) {
101
- $ attributes [$ attribute ] = $ value ;
102
- }
103
- }
104
-
105
87
return [
106
- $ attributes ,
88
+ $ this -> getLoggedAttributeValues () ,
107
89
null ,
108
90
];
109
91
}
110
92
111
93
/**
112
- * Get the attributes for the deleted event.
94
+ * Get the attributes for the restored event.
113
95
*
114
96
* @return array
115
97
*/
@@ -140,53 +122,6 @@ public function getLoggedEvents(): array
140
122
return $ events ;
141
123
}
142
124
143
- /**
144
- * Determine if the attribute should be logged.
145
- *
146
- * @param string $attribute
147
- * @return bool
148
- */
149
- public function shouldAttributeBeLogged (string $ attribute ): bool
150
- {
151
- return in_array ($ attribute , $ this ->getLoggedAttributes ()) && ! in_array ($ attribute , $ this ->getIgnoredAttributes ());
152
- }
153
-
154
- /**
155
- * Get the attributes that should be logged.
156
- *
157
- * @return array
158
- */
159
- public function getLoggedAttributes (): array
160
- {
161
- $ attributes = $ this ->loggedAttributes ?? array_keys ($ this ->attributes );
162
-
163
- if (! $ this ->shouldLogTimestamps ()) {
164
- $ attributes = array_diff ($ attributes , [static ::CREATED_AT , static ::UPDATED_AT ]);
165
- }
166
-
167
- return $ attributes ;
168
- }
169
-
170
- /**
171
- * Get the attributes that should never be logged.
172
- *
173
- * @return array
174
- */
175
- public function getIgnoredAttributes (): array
176
- {
177
- return $ this ->ignoredAttributes ?? [];
178
- }
179
-
180
- /**
181
- * Determine if timestamps should be logged.
182
- *
183
- * @return bool
184
- */
185
- public function shouldLogTimestamps (): bool
186
- {
187
- return $ this ->logTimestamps ?? config ()->get ('journal.timestamps ' , false );
188
- }
189
-
190
125
/**
191
126
* Determine if the event should be logged.
192
127
*
@@ -204,31 +139,6 @@ public function shouldLogEvent(string $event): bool
204
139
return true ;
205
140
}
206
141
207
- /**
208
- * Get the models original relation values.
209
- *
210
- * @param string $key
211
- * @param null $default
212
- * @return mixed|array
213
- */
214
- public function getOriginalRelation (string $ key , $ default = null )
215
- {
216
- return array_get ($ this ->originalRelations , $ key , $ default );
217
- }
218
-
219
- /**
220
- * Sync a single original relation with its current value.
221
- *
222
- * @param string $relation
223
- * @return $this
224
- */
225
- public function syncOriginalRelation (string $ relation )
226
- {
227
- $ this ->originalRelations [$ relation ] = $ this ->getRelationValue ($ relation );
228
-
229
- return $ this ;
230
- }
231
-
232
142
/**
233
143
* Resolve the attribute getter for the given event.
234
144
*
0 commit comments