1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/lav45/yii2-activity-logger |
4
|
|
|
* @copyright Copyright (c) 2017 LAV45 |
5
|
|
|
* @author Aleksey Loban <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace lav45\activityLogger; |
10
|
|
|
|
11
|
|
|
use lav45\activityLogger\storage\DeleteCommand; |
12
|
|
|
use lav45\activityLogger\storage\MessageData; |
13
|
|
|
use Yii; |
14
|
|
|
use yii\base\Behavior; |
15
|
|
|
use yii\base\InvalidConfigException; |
16
|
|
|
use yii\base\InvalidValueException; |
17
|
|
|
use yii\db\ActiveQueryInterface; |
18
|
|
|
use yii\db\ActiveRecord; |
19
|
|
|
use yii\helpers\ArrayHelper; |
20
|
|
|
use yii\helpers\Inflector; |
21
|
|
|
use yii\helpers\StringHelper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ======================= Example usage ====================== |
25
|
|
|
* |
26
|
|
|
* // Recommended |
27
|
|
|
* public function transactions() |
28
|
|
|
* { |
29
|
|
|
* return [ |
30
|
|
|
* ActiveRecord::SCENARIO_DEFAULT => ActiveRecord::OP_ALL, |
31
|
|
|
* ]; |
32
|
|
|
* } |
33
|
|
|
* |
34
|
|
|
* public function behaviors() |
35
|
|
|
* { |
36
|
|
|
* return [ |
37
|
|
|
* [ |
38
|
|
|
* '__class' => 'lav45\activityLogger\ActiveLogBehavior', |
39
|
|
|
* 'attributes' => [ |
40
|
|
|
* // simple attribute |
41
|
|
|
* 'title', |
42
|
|
|
* |
43
|
|
|
* // the value of the attribute is a item in the list |
44
|
|
|
* 'status' => [ |
45
|
|
|
* // => $this->getStatusList() |
46
|
|
|
* 'list' => 'statusList' |
47
|
|
|
* ], |
48
|
|
|
* |
49
|
|
|
* // the attribute value is the [id] of the relation model |
50
|
|
|
* 'owner_id' => [ |
51
|
|
|
* 'relation' => 'owner', |
52
|
|
|
* 'attribute' => 'username', |
53
|
|
|
* ], |
54
|
|
|
* ] |
55
|
|
|
* ] |
56
|
|
|
* ]; |
57
|
|
|
* } |
58
|
|
|
* ============================================================ |
59
|
|
|
* |
60
|
|
|
* @property string $entityName |
61
|
|
|
* @property string $entityId |
62
|
|
|
* @property ActiveRecord $owner |
63
|
|
|
*/ |
64
|
|
|
class ActiveLogBehavior extends Behavior |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* @event MessageEvent an event that is triggered before inserting a record. |
68
|
|
|
* You may add in to the [[MessageEvent::append]] your custom log message. |
69
|
|
|
* @since 1.5.3 |
70
|
|
|
*/ |
71
|
|
|
public const EVENT_BEFORE_SAVE_MESSAGE = 'beforeSaveMessage'; |
72
|
|
|
/** |
73
|
|
|
* @event Event an event that is triggered after inserting a record. |
74
|
|
|
* @since 1.5.3 |
75
|
|
|
*/ |
76
|
|
|
public const EVENT_AFTER_SAVE_MESSAGE = 'afterSaveMessage'; |
77
|
|
|
|
78
|
|
|
public bool $softDelete = false; |
79
|
|
|
/** @since 1.6.0 */ |
80
|
|
|
public ?\Closure $beforeSaveMessage = null; |
81
|
|
|
/** |
82
|
|
|
* [ |
83
|
|
|
* // simple attribute |
84
|
|
|
* 'title', |
85
|
|
|
* |
86
|
|
|
* // simple boolean attribute |
87
|
|
|
* 'is_publish', |
88
|
|
|
* |
89
|
|
|
* // the value of the attribute is a item in the list |
90
|
|
|
* // => $this->getStatusList() |
91
|
|
|
* 'status' => [ |
92
|
|
|
* 'list' => 'statusList' |
93
|
|
|
* ], |
94
|
|
|
* |
95
|
|
|
* // the attribute value is the [id] of the relation model |
96
|
|
|
* 'owner_id' => [ |
97
|
|
|
* 'relation' => 'user', |
98
|
|
|
* 'attribute' => 'username' |
99
|
|
|
* ] |
100
|
|
|
* ] |
101
|
|
|
*/ |
102
|
|
|
public array $attributes = []; |
103
|
|
|
|
104
|
|
|
public bool $identicalAttributes = false; |
105
|
|
|
/** |
106
|
|
|
* A PHP callable that replaces the default implementation of [[isEmpty()]]. |
107
|
|
|
* @since 1.5.2 |
108
|
|
|
*/ |
109
|
|
|
public ?\Closure $isEmpty = null; |
110
|
|
|
/** |
111
|
|
|
* @var \Closure|array|string custom method to getEntityName |
112
|
|
|
* the callback function must return a string |
113
|
|
|
*/ |
114
|
|
|
public $getEntityName; |
115
|
|
|
/** |
116
|
|
|
* @var \Closure|array|string custom method to getEntityId |
117
|
|
|
* the callback function can return a string or array |
118
|
|
|
*/ |
119
|
|
|
public $getEntityId; |
120
|
|
|
/** |
121
|
|
|
* [ |
122
|
|
|
* 'title' => [ |
123
|
|
|
* 'new' => ['value' => 'New title'], |
124
|
|
|
* ], |
125
|
|
|
* 'is_publish' => [ |
126
|
|
|
* 'old' => ['value' => false], |
127
|
|
|
* 'new' => ['value' => true], |
128
|
|
|
* ], |
129
|
|
|
* 'status' => [ |
130
|
|
|
* 'old' => ['id' => 0, 'value' => 'Disabled'], |
131
|
|
|
* 'new' => ['id' => 1, 'value' => 'Active'], |
132
|
|
|
* ], |
133
|
|
|
* 'owner_id' => [ |
134
|
|
|
* 'old' => ['id' => 1, 'value' => 'admin'], |
135
|
|
|
* 'new' => ['id' => 2, 'value' => 'lucy'], |
136
|
|
|
* ] |
137
|
|
|
* ] |
138
|
|
|
*/ |
139
|
|
|
private array $changedAttributes = []; |
140
|
|
|
|
141
|
|
|
private string $action; |
142
|
|
|
|
143
|
|
|
private ManagerInterface $logger; |
144
|
|
|
|
145
|
28 |
|
public function __construct( |
146
|
|
|
ManagerInterface $logger, |
147
|
|
|
array $config = [] |
148
|
|
|
) |
149
|
|
|
{ |
150
|
28 |
|
$this->logger = $logger; |
151
|
28 |
|
parent::__construct($config); |
152
|
|
|
} |
153
|
|
|
|
154
|
28 |
|
public function init(): void |
155
|
|
|
{ |
156
|
28 |
|
$this->initAttributes(); |
157
|
|
|
} |
158
|
|
|
|
159
|
28 |
|
private function initAttributes(): void |
160
|
|
|
{ |
161
|
28 |
|
foreach ($this->attributes as $key => $value) { |
162
|
27 |
|
if (is_int($key)) { |
163
|
27 |
|
unset($this->attributes[$key]); |
164
|
27 |
|
$this->attributes[$value] = []; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
28 |
|
public function events(): array |
170
|
|
|
{ |
171
|
28 |
|
if (false === $this->logger->isEnabled()) { |
172
|
1 |
|
return []; |
173
|
|
|
} |
174
|
27 |
|
return [ |
175
|
27 |
|
ActiveRecord::EVENT_BEFORE_INSERT => [$this, 'beforeSave'], |
176
|
27 |
|
ActiveRecord::EVENT_BEFORE_UPDATE => [$this, 'beforeSave'], |
177
|
27 |
|
ActiveRecord::EVENT_BEFORE_DELETE => [$this, 'beforeDelete'], |
178
|
27 |
|
ActiveRecord::EVENT_AFTER_INSERT => [$this, 'afterSave'], |
179
|
27 |
|
ActiveRecord::EVENT_AFTER_UPDATE => [$this, 'afterSave'], |
180
|
27 |
|
]; |
181
|
|
|
} |
182
|
|
|
|
183
|
26 |
|
public function beforeSave(): void |
184
|
|
|
{ |
185
|
26 |
|
$this->changedAttributes = $this->prepareChangedAttributes(); |
186
|
24 |
|
$this->action = $this->owner->getIsNewRecord() ? 'created' : 'updated'; |
187
|
|
|
} |
188
|
|
|
|
189
|
24 |
|
public function afterSave(): void |
190
|
|
|
{ |
191
|
24 |
|
if (empty($this->changedAttributes)) { |
192
|
4 |
|
return; |
193
|
|
|
} |
194
|
24 |
|
$this->saveMessage($this->action, $this->changedAttributes); |
195
|
|
|
} |
196
|
|
|
|
197
|
4 |
|
public function beforeDelete(): void |
198
|
|
|
{ |
199
|
4 |
|
if (false === $this->softDelete) { |
200
|
3 |
|
$this->logger->delete(new DeleteCommand([ |
201
|
3 |
|
'entityName' => $this->getEntityName(), |
202
|
3 |
|
'entityId' => $this->getEntityId(), |
203
|
3 |
|
])); |
204
|
|
|
} |
205
|
4 |
|
$this->saveMessage('deleted', $this->prepareChangedAttributes(true)); |
206
|
|
|
} |
207
|
|
|
|
208
|
26 |
|
private function prepareChangedAttributes(bool $unset = false): array |
209
|
|
|
{ |
210
|
26 |
|
$result = []; |
211
|
26 |
|
foreach ($this->attributes as $attribute => $options) { |
212
|
26 |
|
$old = $this->owner->getOldAttribute($attribute); |
213
|
26 |
|
$new = false === $unset ? $this->owner->getAttribute($attribute) : null; |
214
|
|
|
|
215
|
26 |
|
if ($this->isEmpty($old) && $this->isEmpty($new)) { |
216
|
26 |
|
continue; |
217
|
|
|
} |
218
|
26 |
|
if (false === $unset && false === $this->owner->isAttributeChanged($attribute, $this->identicalAttributes)) { |
219
|
12 |
|
continue; |
220
|
|
|
} |
221
|
26 |
|
$result[$attribute] = $this->resolveStoreValues($old, $new, $options); |
222
|
|
|
} |
223
|
24 |
|
return $result; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string|int|null $old_id |
228
|
|
|
* @param string|int|null $new_id |
229
|
|
|
*/ |
230
|
26 |
|
protected function resolveStoreValues($old_id, $new_id, array $options): array |
231
|
|
|
{ |
232
|
26 |
|
if (isset($options['list'])) { |
233
|
24 |
|
$value = $this->resolveListValues($old_id, $new_id, $options['list']); |
234
|
24 |
|
} elseif (isset($options['relation'], $options['attribute'])) { |
235
|
19 |
|
$value = $this->resolveRelationValues($old_id, $new_id, $options['relation'], $options['attribute']); |
236
|
|
|
} else { |
237
|
22 |
|
$value = $this->resolveSimpleValues($old_id, $new_id); |
238
|
|
|
} |
239
|
24 |
|
return $value; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string|int|null $old_id |
244
|
|
|
* @param string|int|null $new_id |
245
|
|
|
*/ |
246
|
22 |
|
private function resolveSimpleValues($old_id, $new_id): array |
247
|
|
|
{ |
248
|
22 |
|
return [ |
249
|
22 |
|
'old' => ['value' => $old_id], |
250
|
22 |
|
'new' => ['value' => $new_id], |
251
|
22 |
|
]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string|int|array|null $old_id |
256
|
|
|
* @param string|int|array|null $new_id |
257
|
|
|
*/ |
258
|
24 |
|
private function resolveListValues($old_id, $new_id, string $listName): array |
259
|
|
|
{ |
260
|
24 |
|
$old = $new = []; |
261
|
24 |
|
$old['id'] = $old_id; |
262
|
24 |
|
$new['id'] = $new_id; |
263
|
24 |
|
$list = []; |
264
|
|
|
|
265
|
24 |
|
if (is_array($old_id) || is_array($new_id)) { |
266
|
1 |
|
$list = ArrayHelper::getValue($this->owner, $listName); |
267
|
|
|
} |
268
|
24 |
|
if (is_array($old_id)) { |
269
|
1 |
|
$old['value'] = array_intersect_key($list, array_flip($old_id)); |
270
|
24 |
|
} elseif ($old_id) { |
271
|
4 |
|
$old['value'] = ArrayHelper::getValue($this->owner, [$listName, $old_id]); |
272
|
|
|
} else { |
273
|
24 |
|
$old['value'] = null; |
274
|
|
|
} |
275
|
24 |
|
if (is_array($new_id)) { |
276
|
1 |
|
$new['value'] = array_intersect_key($list, array_flip($new_id)); |
277
|
24 |
|
} elseif ($new_id) { |
278
|
23 |
|
$new['value'] = ArrayHelper::getValue($this->owner, [$listName, $new_id]); |
279
|
|
|
} else { |
280
|
4 |
|
$new['value'] = null; |
281
|
|
|
} |
282
|
24 |
|
return [ |
283
|
24 |
|
'old' => $old, |
284
|
24 |
|
'new' => $new |
285
|
24 |
|
]; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param string|int|null $old_id |
290
|
|
|
* @param string|int|null $new_id |
291
|
|
|
*/ |
292
|
19 |
|
private function resolveRelationValues($old_id, $new_id, string $relation, string $attribute): array |
293
|
|
|
{ |
294
|
19 |
|
$old = $new = []; |
295
|
19 |
|
$old['id'] = $old_id; |
296
|
19 |
|
$new['id'] = $new_id; |
297
|
|
|
|
298
|
|
|
/** @var ActiveQueryInterface $relationQuery */ |
299
|
19 |
|
$relationQuery = clone $this->owner->getRelation($relation); |
300
|
18 |
|
if (count($relationQuery->link) > 1) { |
|
|
|
|
301
|
1 |
|
throw new InvalidConfigException('Relation model can only be linked through one primary key.'); |
302
|
|
|
} |
303
|
17 |
|
$relationQuery->primaryModel = null; |
|
|
|
|
304
|
17 |
|
$idAttribute = array_keys($relationQuery->link)[0]; |
305
|
17 |
|
$targetId = array_filter([$old_id, $new_id]); |
306
|
|
|
|
307
|
17 |
|
$relationModels = $relationQuery |
308
|
17 |
|
->where([$idAttribute => $targetId]) |
309
|
17 |
|
->indexBy($idAttribute) |
310
|
17 |
|
->limit(count($targetId)) |
311
|
17 |
|
->all(); |
312
|
|
|
|
313
|
17 |
|
if ($old_id) { |
314
|
4 |
|
$old['value'] = ArrayHelper::getValue($relationModels, [$old_id, $attribute]); |
315
|
|
|
} else { |
316
|
17 |
|
$old['value'] = null; |
317
|
|
|
} |
318
|
17 |
|
if ($new_id) { |
319
|
17 |
|
$new['value'] = ArrayHelper::getValue($relationModels, [$new_id, $attribute]); |
320
|
|
|
} else { |
321
|
2 |
|
$new['value'] = null; |
322
|
|
|
} |
323
|
|
|
|
324
|
17 |
|
return [ |
325
|
17 |
|
'old' => $old, |
326
|
17 |
|
'new' => $new |
327
|
17 |
|
]; |
328
|
|
|
} |
329
|
|
|
|
330
|
24 |
|
protected function saveMessage(string $action, array $data): void |
331
|
|
|
{ |
332
|
24 |
|
$data = $this->beforeSaveMessage($data); |
333
|
24 |
|
$this->addLog($data, $action); |
334
|
24 |
|
$this->afterSaveMessage(); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @param string|array $data |
339
|
|
|
* @since 1.7.0 |
340
|
|
|
*/ |
341
|
24 |
|
public function addLog($data, string $action = null): bool |
342
|
|
|
{ |
343
|
|
|
/** @var MessageData $message */ |
344
|
24 |
|
$message = Yii::createObject([ |
345
|
24 |
|
'__class' => MessageData::class, |
346
|
24 |
|
'entityName' => $this->getEntityName(), |
347
|
24 |
|
'entityId' => $this->getEntityId(), |
348
|
24 |
|
'createdAt' => time(), |
349
|
24 |
|
'action' => $action, |
350
|
24 |
|
'data' => $data, |
351
|
24 |
|
]); |
352
|
24 |
|
return $this->logger->log($message); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @since 1.5.3 |
357
|
|
|
*/ |
358
|
24 |
|
public function beforeSaveMessage(array $data): array |
359
|
|
|
{ |
360
|
24 |
|
if (null !== $this->beforeSaveMessage) { |
361
|
1 |
|
return call_user_func($this->beforeSaveMessage, $data); |
362
|
|
|
} |
363
|
23 |
|
$name = self::EVENT_BEFORE_SAVE_MESSAGE; |
364
|
23 |
|
if (method_exists($this->owner, $name)) { |
365
|
1 |
|
return $this->owner->$name($data); |
366
|
|
|
} |
367
|
22 |
|
$event = new MessageEvent(); |
368
|
22 |
|
$event->logData = $data; |
369
|
22 |
|
$this->owner->trigger($name, $event); |
370
|
22 |
|
return $event->logData; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @since 1.5.3 |
375
|
|
|
*/ |
376
|
24 |
|
public function afterSaveMessage(): void |
377
|
|
|
{ |
378
|
24 |
|
$name = self::EVENT_AFTER_SAVE_MESSAGE; |
379
|
24 |
|
if (method_exists($this->owner, $name)) { |
380
|
1 |
|
$this->owner->$name(); |
381
|
|
|
} else { |
382
|
23 |
|
$this->owner->trigger($name); |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|
386
|
25 |
|
public function getEntityName(): string |
387
|
|
|
{ |
388
|
25 |
|
if (is_string($this->getEntityName)) { |
389
|
25 |
|
return $this->getEntityName; |
390
|
|
|
} |
391
|
1 |
|
if (is_callable($this->getEntityName)) { |
392
|
1 |
|
return call_user_func($this->getEntityName); |
393
|
|
|
} |
394
|
1 |
|
$class = get_class($this->owner); |
395
|
1 |
|
$class = StringHelper::basename($class); |
396
|
1 |
|
$this->getEntityName = Inflector::camel2id($class, '_'); |
397
|
1 |
|
return $this->getEntityName; |
398
|
|
|
} |
399
|
|
|
|
400
|
26 |
|
public function getEntityId(): string |
401
|
|
|
{ |
402
|
26 |
|
if (null === $this->getEntityId) { |
403
|
26 |
|
$result = $this->owner->getPrimaryKey(); |
404
|
4 |
|
} elseif (is_callable($this->getEntityId)) { |
405
|
3 |
|
$result = call_user_func($this->getEntityId); |
406
|
|
|
} else { |
407
|
1 |
|
$result = $this->getEntityId; |
408
|
|
|
} |
409
|
26 |
|
if ($this->isEmpty($result)) { |
410
|
1 |
|
throw new InvalidValueException('the property "entityId" can not be empty'); |
411
|
|
|
} |
412
|
25 |
|
if (is_array($result)) { |
413
|
1 |
|
ksort($result); |
414
|
1 |
|
$result = json_encode($result, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
415
|
|
|
} |
416
|
25 |
|
return $result; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Checks if the given value is empty. |
421
|
|
|
* A value is considered empty if it is null, an empty array, or an empty string. |
422
|
|
|
* Note that this method is different from PHP empty(). It will return false when the value is 0. |
423
|
|
|
* @param mixed $value the value to be checked |
424
|
|
|
* @return bool whether the value is empty |
425
|
|
|
* @since 1.5.2 |
426
|
|
|
*/ |
427
|
28 |
|
public function isEmpty($value): bool |
428
|
|
|
{ |
429
|
28 |
|
if (null !== $this->isEmpty) { |
430
|
1 |
|
return call_user_func($this->isEmpty, $value); |
431
|
|
|
} |
432
|
27 |
|
return null === $value || '' === $value || [] === $value; |
433
|
|
|
} |
434
|
|
|
} |