Skip to content

Commit 184115c

Browse files
committed
Add support for soft deleted models, clean up code and drop support for php 7.0
1 parent 5c0a746 commit 184115c

15 files changed

+350
-318
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 2.0.0
2+
- Refactor codebase
3+
- Drop support for php 7.0
4+
- Add support for soft deleted models

Diff for: Plugin.php

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,35 @@
1-
<?php namespace Jacob\Logbook;
1+
<?php
2+
3+
namespace Jacob\Logbook;
24

35
use Jacob\LogBook\FormWidgets\LogBook;
46
use Jacob\Logbook\ReportWidgets\LogBookModelChanges;
57
use System\Classes\PluginBase;
68

7-
/**
8-
* Logbook Plugin Information File
9-
*/
109
class Plugin extends PluginBase
1110
{
12-
/**
13-
* Returns information about this plugin.
14-
*
15-
* @return array
16-
*/
17-
public function pluginDetails()
11+
public function pluginDetails(): array
1812
{
1913
return [
20-
'name' => 'Logbook',
14+
'name' => 'Logbook',
2115
'description' => 'Creates a logbook based on changes in a model',
22-
'author' => 'Jacob',
23-
'icon' => 'icon-leaf'
16+
'author' => 'Jacob',
17+
'icon' => 'icon-leaf'
2418
];
2519
}
2620

27-
/**
28-
* Register form widgets
29-
*
30-
* @return array
31-
*/
32-
public function registerFormWidgets()
21+
public function registerFormWidgets(): array
3322
{
3423
return [
3524
LogBook::class => 'jacob_logbook_log',
3625
];
3726
}
3827

39-
/**
40-
* Register report widgets
41-
* @return array
42-
*/
43-
public function registerReportWidgets()
28+
public function registerReportWidgets(): array
4429
{
4530
return [
4631
LogBookModelChanges::class => [
47-
'label' => 'Logbook of changes in a model'
32+
'label' => 'Logbook of changes in a model'
4833
],
4934
];
5035
}

Diff for: README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Use the LogChanges trait in your model.
1414
This trait will automatically save changes from the model in the database.
1515

1616
```php
17-
use \Jacob\Logbook\Traits\LogChanges;
17+
use Jacob\Logbook\Traits\LogChanges;
1818
```
1919

2020
If you want to ignore fields that don't need the be logged, add this to your model.
@@ -112,6 +112,7 @@ Options: <br/>
112112
| limitPerPage | 20 | int |
113113
| startPage | 1 | int |
114114
| showLogRelations | null | array or string |
115+
| showSoftDeletedRelations | null | array or string |
115116
| showUndoChangesButton| true | bool |
116117
| refreshFormAfterUndo | true | bool |
117118

@@ -124,6 +125,8 @@ _logbook@update:
124125
showLogRelations: #optional (contains the name(s) of the relations)
125126
- customer
126127
- anotherRelationName
127-
showUndoChangesButton: true
128-
refreshFormAfterUndo: false
128+
showSoftDeleteRelations: #optional (contains the name(s) of the relations with soft delete support)
129+
- relationNameWithSoftDeletes
130+
showUndoChangesButton: true #optional
131+
refreshFormAfterUndo: false #optional
129132
```

Diff for: classes/LoadRelation.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Jacob\LogBook\Classes;
4+
5+
class LoadRelation
6+
{
7+
private string $name;
8+
private bool $withTrashed;
9+
10+
public function __construct(string $name, bool $withTrashed)
11+
{
12+
$this->name = $name;
13+
$this->withTrashed = $withTrashed;
14+
}
15+
16+
public function getName(): string
17+
{
18+
return $this->name;
19+
}
20+
21+
public function isWithTrashed(): bool
22+
{
23+
return $this->withTrashed;
24+
}
25+
}

Diff for: classes/entities/Attribute.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,29 @@
44

55
class Attribute extends BaseEntity
66
{
7-
protected $attributes = [
8-
'column',
9-
'old',
10-
'new',
11-
];
12-
}
7+
protected $column;
8+
protected $old;
9+
protected $new;
10+
11+
public function __construct(string $column, $old, $new)
12+
{
13+
$this->column = $column;
14+
$this->old = $old;
15+
$this->new = $new;
16+
}
17+
18+
public function getColumn(): string
19+
{
20+
return $this->column;
21+
}
22+
23+
public function getOld()
24+
{
25+
return $this->old;
26+
}
27+
28+
public function getNew()
29+
{
30+
return $this->new;
31+
}
32+
}

Diff for: classes/entities/BaseEntity.php

+19-69
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,32 @@
22

33
namespace Jacob\LogBook\Classes\Entities;
44

5-
class BaseEntity
5+
abstract class BaseEntity
66
{
7-
/**
8-
* @var array List of all the attributes of this model
9-
*/
10-
protected $attributes = [];
11-
12-
/**
13-
* @var array Storage of all attribute data
14-
*/
15-
protected $attributesData = [];
16-
17-
public function __construct(array $attributes = [])
7+
public function getData(): array
188
{
19-
$this->fillFromArray($attributes);
20-
}
9+
$data = [];
2110

22-
/**
23-
* Fill data from array
24-
* @param array $attributes
25-
*/
26-
protected function fillFromArray(array $attributes)
27-
{
28-
foreach ($attributes as $key => $value) {
29-
$this->__set($key, $value);
30-
}
31-
}
11+
$properties = get_object_vars($this);
3212

33-
/**
34-
* Get data from attribute, child entity or nested entity
35-
*
36-
* @param string $key
37-
* @return null|string|array
38-
*/
39-
public function __get($key)
40-
{
41-
if (isset( $this->attributesData[$key] )) {
42-
return $this->attributesData[$key];
43-
}
13+
foreach ($properties as $name => $value) {
14+
if (is_array($value)) {
15+
$items = [];
4416

45-
return null;
46-
}
47-
/**
48-
* Set data for attribute, child entity or nested entity
49-
*
50-
* @param $key
51-
* @param $value
52-
*/
53-
public function __set($key, $value)
54-
{
55-
if ($this->attributeExists($key)) {
56-
$this->attributesData[$key] = $value;
57-
}
58-
}
17+
foreach ($value as $val) {
18+
if ($val instanceof BaseEntity) {
19+
$items[] = $val->getData();
20+
}
21+
}
5922

60-
/**
61-
* Check if an attribute key exists
62-
*
63-
* @param $key
64-
* @return bool
65-
*/
66-
public function attributeExists($key)
67-
{
68-
return in_array($key, $this->attributes);
69-
}
23+
$value = $items;
24+
} else if ($value instanceof BaseEntity) {
25+
$value = $value->getData();
26+
}
7027

71-
/**
72-
* Get all current data set in this model
73-
* @return array
74-
*/
75-
public function getData()
76-
{
77-
$result = [];
78-
foreach ($this->attributes as $attribute) {
79-
$result[$attribute] = $this->$attribute;
28+
$data[$name] = $value;
8029
}
81-
return $result;
30+
31+
return $data;
8232
}
8333
}

Diff for: classes/entities/Changes.php

+44-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,47 @@
44

55
class Changes extends BaseEntity
66
{
7-
const TYPE_CREATED = 'created';
8-
const TYPE_UPDATED = 'updated';
9-
const TYPE_DELETED = 'deleted';
10-
11-
protected $attributes = [
12-
'type',
13-
'changedAttributes',
14-
];
15-
}
7+
public const TYPE_CREATED = 'created';
8+
public const TYPE_UPDATED = 'updated';
9+
public const TYPE_DELETED = 'deleted';
10+
11+
protected $type;
12+
protected $changedAttributes;
13+
14+
/**
15+
* @param Attribute[] $changedAttributes
16+
*/
17+
public function __construct(string $type, ?array $changedAttributes = null)
18+
{
19+
$this->type = $type;
20+
$this->changedAttributes = $changedAttributes;
21+
}
22+
23+
public function getType(): string
24+
{
25+
return $this->type;
26+
}
27+
28+
public function isTypeCreated(): string
29+
{
30+
return $this->type === self::TYPE_CREATED;
31+
}
32+
33+
public function isTypeUpdated(): string
34+
{
35+
return $this->type === self::TYPE_UPDATED;
36+
}
37+
38+
public function isTypeDeleted(): string
39+
{
40+
return $this->type === self::TYPE_DELETED;
41+
}
42+
43+
/**
44+
* @return Attribute[]|null
45+
*/
46+
public function getChangedAttributes(): ?array
47+
{
48+
return $this->changedAttributes;
49+
}
50+
}

0 commit comments

Comments
 (0)