1 | <?php |
||
2 | |||
3 | namespace LeKoala\CommonExtensions; |
||
4 | |||
5 | use SilverStripe\Forms\FieldList; |
||
6 | use SilverStripe\Security\Member; |
||
7 | use SilverStripe\ORM\DataExtension; |
||
8 | use SilverStripe\Security\Security; |
||
9 | |||
10 | /** |
||
11 | * Track who edited what |
||
12 | * |
||
13 | * @property \LeKoala\CommonExtensions\EditorTrackingExtension|\SilverStripe\ORM\DataObject $owner |
||
14 | * @property int $CreatedByID |
||
15 | * @property int $LastEditedByID |
||
16 | * @method \SilverStripe\Security\Member CreatedBy() |
||
17 | * @method \SilverStripe\Security\Member LastEditedBy() |
||
18 | */ |
||
19 | class EditorTrackingExtension extends DataExtension |
||
20 | { |
||
21 | private static $has_one = [ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
22 | "CreatedBy" => Member::class, |
||
23 | "LastEditedBy" => Member::class, |
||
24 | ]; |
||
25 | |||
26 | public function onBeforeWrite() |
||
27 | { |
||
28 | $user = Security::getCurrentUser(); |
||
29 | if (!$this->owner->ID) { |
||
0 ignored issues
–
show
|
|||
30 | $this->owner->CreatedBy = $user->ID ?? 0; |
||
0 ignored issues
–
show
|
|||
31 | } |
||
32 | $this->owner->LastEditedBy = $user->ID ?? 0; |
||
0 ignored issues
–
show
|
|||
33 | } |
||
34 | |||
35 | public function updateCMSFields(FieldList $fields) |
||
36 | { |
||
37 | $CreatedByID = $fields->dataFieldByName('CreatedByID'); |
||
38 | if ($CreatedByID) { |
||
39 | $fields->replaceField('CreatedByID', $CreatedByID->performReadonlyTransformation()); |
||
40 | } |
||
41 | $LastEditedByID = $fields->dataFieldByName('LastEditedByID'); |
||
42 | if ($LastEditedByID) { |
||
43 | $fields->replaceField('LastEditedByID', $LastEditedByID->performReadonlyTransformation()); |
||
44 | } |
||
45 | } |
||
46 | } |
||
47 |