Skip to content

Commit f6402bb

Browse files
committed
Add undo button to logs.
1 parent 2a26b11 commit f6402bb

File tree

6 files changed

+103
-7
lines changed

6 files changed

+103
-7
lines changed

README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ public static function changeLogBookDisplayValue($column, $value)
8787
}
8888
```
8989

90+
If you want to hide the undo button for this model specifically, you can add $logBookUndoable to your model.
91+
You can also disable the undo button in the log book form widget.
92+
93+
```php
94+
/**
95+
* Hides or shows undo button for current field
96+
*
97+
* @var bool $logBookLogUndoable
98+
*/
99+
public $logBookLogUndoable = false
100+
```
101+
90102
# Form Widget
91103

92104
You can use the formwidget as follows: <br/>
@@ -100,16 +112,18 @@ Options: <br/>
100112
| limitPerPage | 20 | int |
101113
| startPage | 1 | int |
102114
| showLogRelations | null | array or string |
115+
| showUndoChangesButton| true | bool |
116+
| refreshFormAfterUndo | true | bool |
103117

104118
Example:
105119
```yaml
106-
#tab logbook
107120
_logbook@update:
108121
type: jacob_logbook_log
109122
limitPerPage: 10 #optional
110123
startPage: 1 #optional
111124
showLogRelations: #optional (contains the name(s) of the relations)
112125
- customer
113126
- anotherRelationName
114-
tab: logbook
127+
showUndoChangesButton: true
128+
refreshFormAfterUndo: false
115129
```

formwidgets/LogBook.php

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php namespace Jacob\LogBook\FormWidgets;
22

33
use Backend\Classes\FormWidgetBase;
4+
use Illuminate\Routing\Redirector;
5+
use Jacob\LogBook\Classes\Entities\Changes;
6+
use Jacob\Logbook\Models\Log;
47

58
/**
69
* LogBook Form Widget
@@ -23,6 +26,12 @@ class LogBook extends FormWidgetBase
2326
/** @var array|string|null */
2427
public $showLogRelations = null;
2528

29+
/** @var bool $showUndoChangeButton */
30+
public $showUndoChangesButton = true;
31+
32+
/** @var bool $refreshFromAfterUndo */
33+
public $refreshFormAfterUndo = true;
34+
2635
/**
2736
* @inheritDoc
2837
*/
@@ -32,6 +41,8 @@ public function init()
3241
'limitPerPage',
3342
'startPage',
3443
'showLogRelations',
44+
'showUndoChangesButton',
45+
'refreshFormAfterUndo',
3546
]);
3647
}
3748

@@ -60,6 +71,7 @@ public function prepareVars()
6071

6172
$this->vars['name'] = $this->formField->getName();
6273
$this->vars['model'] = $this->model;
74+
$this->vars['showUndoChangesButton'] = $this->showUndoChangesButton;
6375
}
6476

6577
/**
@@ -81,7 +93,7 @@ public function prepareLogs()
8193
*/
8294
public function onLogBookChangePage()
8395
{
84-
$page = (int)post('page');
96+
$page = (int)post('page', 1);
8597
$this->prepareVars();
8698
$this->vars['logs'] = $this->model->getLogsFromLogBook(
8799
$this->limitPerPage,
@@ -94,6 +106,45 @@ public function onLogBookChangePage()
94106
];
95107
}
96108

109+
/**
110+
* Undo change from logbook
111+
*
112+
* @return mixed
113+
* @throws \Exception
114+
*/
115+
public function onLogBookUndoChange()
116+
{
117+
$id = post('id', null);
118+
119+
/** @var Log $log */
120+
$log = Log::find($id);
121+
122+
if (!$log || ($log->getAttribute('changes')['type'] ?? null) !== Changes::TYPE_UPDATED) {
123+
return $this->onLogBookChangePage();
124+
}
125+
126+
/** @var \Model $modelInstance */
127+
$modelInstance = app($log->getAttribute('model'));
128+
129+
/** @var \Model $changedModel */
130+
$changedModel = $modelInstance->find($log->getAttribute('model_key'));
131+
132+
/** @var array $changedAttribute */
133+
foreach ($log->getAttribute('changes')['changedAttributes'] ?? [] as $changedAttribute) {
134+
$changedModel->setAttribute($changedAttribute['column'], $changedAttribute['old']);
135+
}
136+
137+
$changedModel->save();
138+
139+
if ($this->refreshFormAfterUndo) {
140+
/** @var Redirector $redirect */
141+
$redirect = resolve(Redirector::class);
142+
return $redirect->refresh();
143+
}
144+
145+
return $this->onLogBookChangePage();
146+
}
147+
97148
/**
98149
* @param mixed $value
99150
* @return null|string

formwidgets/logbook/partials/_logbook.htm

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<tr>
66
<th><span><?= e(trans('jacob.logbook::lang.log')) ?></span></th>
77
<th><span><?= e(trans('jacob.logbook::lang.date')) ?></span></th>
8+
<?php if ($showUndoChangesButton): ?>
9+
<th><span></span></th>
10+
<?php endif; ?>
811
</tr>
912
</thead>
1013
<tbody>
@@ -21,7 +24,7 @@
2124
'model' => $modelName
2225
])) ?>
2326
<?php else: ?>
24-
<?= e(trans('jacob.logbook::lang.changes.unknown', [
27+
<?= e(trans('jacob.logbook::lang.changes.unknown', [
2528
'type' => trans('jacob.logbook::lang.changes.type.' . $changes['type']),
2629
'model' => $modelName
2730
])) ?>
@@ -42,6 +45,22 @@
4245
<td>
4346
<?= $log->updated_at->format('l j F Y H:i:s') ?>
4447
</td>
48+
<?php if ($showUndoChangesButton): ?>
49+
<td>
50+
<?php if ($currentModel->logBookLogUndoable ?? true && $changes['type'] === Jacob\LogBook\Classes\Entities\Changes::TYPE_UPDATED): ?>
51+
<button class="btn btn-primary"
52+
data-request="onLogBookUndoChange"
53+
data-request-data="id: <?= $log->getkey() ?>"
54+
data-request-flash>
55+
<?= e(trans('jacob.logbook::lang.undo')) ?>
56+
</button>
57+
<?php else: ?>
58+
<button disabled class="btn btn-primary">
59+
<?= e(trans('jacob.logbook::lang.undo')) ?>
60+
</button>
61+
<?php endif; ?>
62+
</td>
63+
<?php endif; ?>
4564
</tr>
4665
<?php endforeach; ?>
4766
</tbody>

lang/en/lang.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
return [
44
'log' => 'Log',
55
'date' => 'Date',
6+
'undo' => 'Undo',
67
'changes' => [
78
'user' => ':user has :type :model',
89
'unknown' => ':model is :type',

lang/nl/lang.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
return [
44
'log' => 'Log',
55
'date' => 'Datum',
6+
'undo' => 'Ongedaan maken',
67
'changes' => [
78
'user' => ':user heeft :model :type ',
89
'unknown' => ':model is :type',
910
'type' => [
10-
'updated' => 'aangepast',
11+
'updated' => 'gewijzigd',
1112
'created' => 'aangemaakt',
1213
'deleted' => 'verwijderd',
1314
],
14-
'column' => ':column is veranderd van :from naar :to'
15+
'column' => ':column is gewijzigd van :from naar :to'
1516
],
1617
];

traits/LogChanges.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ trait LogChanges
3131
* If true -> log items are deleted when the model is deleted
3232
* If false -> a new log item will be created with status deleted.
3333
*
34-
* @var bool
34+
* @var bool $deleteLogbookAfterDelete
3535
*
3636
* protected $deleteLogbookAfterDelete = false;
3737
*/
@@ -40,9 +40,19 @@ trait LogChanges
4040
* Here you can override the model name that is displayed in the log files.
4141
* The name is going to be translated when possible.
4242
*
43+
* string $logBookModelName
44+
*
4345
* public $logBookModelName = 'MyModelName'
4446
*/
4547

48+
/**
49+
* Hides or shows undo button for current field
50+
*
51+
* @var bool $logBookLogUndoable
52+
*
53+
* public $logBookLogUndoable = false
54+
*/
55+
4656
/**
4757
* If you override this function you can change the value that is displayed in the log book
4858
* This can be useful for example with a dropdown

0 commit comments

Comments
 (0)