Skip to content

Commit 9173ed2

Browse files
committed
Merge branch 'release/1.0'
2 parents e52cef4 + f7a53d3 commit 9173ed2

11 files changed

+1045
-134
lines changed

CHANGES.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## 1.0 Initial release
2+
3+
- implement controller and model
4+
- log updates including diffs and all fields
5+
- log create/delete operations
6+
- track multi-model operations
7+
- ability to customize
8+
- add documentation
9+
- undo() operation for update/create/delete
10+
- support for custom actions

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
This extension for Agile Data implements advanced logging capabilities as well as some elements of
44
Event Sourcing.
55

6+
## Documentation
7+
8+
https://github.com/atk4/audit/blob/develop/docs/index.md
9+
10+
## Real Usage Example
11+
12+
https://github.com/atk4/audit/blob/develop/docs/full-example.md
613

714
## Installation
815

docs/custom.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
# Custom Events
3+
4+
There are three ways to add custom events inside audit log.
5+
6+
## Custom action based on change
7+
8+
In this approach, we will register a `beforeSave` hook that will examine the nature of our change and if certain condition is met, will customise the log message.
9+
10+
``` php
11+
$this->addField('gender', ['enum' => ['M','F']]);
12+
$this->addHook('beforeSave', function($m) {
13+
if ($m->isDirty('gender')) {
14+
$m->audit_log['action'] = 'genderbending';
15+
}
16+
});
17+
```
18+
19+
Property `audit_log` is only available while model is being saved and will point to a model object. After operation completes, the property is removed.
20+
21+
During the save, however, we can use this to change values. Another important point is that when `audit_log` is initially being set-up it was already saved, so that model will have a real `id` set. If no additional changes are done to the `$m` model or it's `audit_log` model, then there won't be any need to perform secondary save.
22+
23+
(note: value of `descr` is computed later, but if you set your own value there, then the model will keep it instead)
24+
25+
## Setting action before action starts
26+
27+
The method described above will only work during the modifications hook of the model. What about situations when you want to perform custom action from outside the model? In this case you should set a property for controller:
28+
29+
``` php
30+
$m->load(2); // surname=Shatwell
31+
$m->audit_log_controller->custom_action = 'married';
32+
$m['surname'] = 'Shira';
33+
$m->save();
34+
```
35+
36+
In this example a person is being married, so the surname have to be changed. But instead of using default action, we can set it to `married` through `custom_action` property.
37+
38+
After the next audit_log operation is completed, the custom_action will be emptied and the next operation will have default action set.
39+
40+
Additionally you can also set other fields through use of `controller->custom_fields` property:
41+
42+
```php
43+
$m->load(2);
44+
$m->audit_log_controller->custom_fields['descr'] = 'User got older';
45+
$m['age']++;
46+
$m->save();
47+
```
48+
49+
## Pushing custom actions
50+
51+
In this final scenario you would want to record action when something happened with the model without actually modifying the model itself. For that AuditLog controller have added a handy method `log()` for you right inside your method:
52+
53+
``` php
54+
$m->load(2);
55+
$m->log('inspection', ['value' => $m['test_value']]);
56+
$m->unload();
57+
```
58+
59+
In this case the nothing has happened with the model, but we are still recording information about it with a custom action `inspection`. Additionally we are populating `requested_diff` field with second argument array passed into log() method.
60+
61+
## Custom AuditLog Model
62+
63+
Sometimes you would want to have your own model. Although you could create `AuditLog` model from scratch, you would be using some of the useful features, so I recommend you to extend the existing class:
64+
65+
``` php
66+
class CustomLog extends \atk4\audit\model\AuditLog {
67+
function init(){
68+
parent::init();
69+
$this->addField('custom_data', ['type' => 'struct']);
70+
}
71+
}
72+
```
73+
74+
This will extend AuditLog by adding additional field definition which you can use for storing your own information in. Type `struct` allow you to store arrays too.
75+
76+
## Using custom descriptions
77+
78+
Field `descr` inside a model stores a human-readable value. If you have a different criteria for "human readable", then you can re-define description in your audit model. To do so, define method `getDescr` inside your AuditLog model:
79+
80+
``` php
81+
class CustomLog extends \atk4\audit\model\AuditLog {
82+
function getDescr(){
83+
return count($this['request_diff']).' fields magically change';
84+
}
85+
}
86+
```
87+
88+
This will populate `descr` with text like *"2 fields magically change".*

0 commit comments

Comments
 (0)