1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
use Venturecraft\Revisionable\Revision; |
6
|
|
|
|
7
|
|
|
trait ViewsAndRestoresRevisions |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Build a list of Revisions, grouped by revision date |
11
|
|
|
* |
12
|
|
|
* @return [Array] of revision groups, keyed by revision date |
|
|
|
|
13
|
|
|
*/ |
14
|
|
|
public function listRevisions($id) |
15
|
|
|
{ |
16
|
|
|
$revisions = []; |
17
|
|
|
// Group revisions by change date |
18
|
|
|
foreach($this->getEntry($id)->revisionHistory as $history) { |
|
|
|
|
19
|
|
|
|
20
|
|
|
// Get just the date from the revision created timestamp |
21
|
|
|
$revisionDate = date('Y-m-d', strtotime((string)$history->created_at)); |
22
|
|
|
|
23
|
|
|
// Be sure to instantiate the initial grouping array |
24
|
|
|
if(!array_key_exists($revisionDate, $revisions)) { |
25
|
|
|
$revisions[$revisionDate] = []; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Push onto the top of the current group - so we get orderBy decending timestamp |
29
|
|
|
array_unshift($revisions[$revisionDate], $history); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Sort the array by timestamp descending (so that the most recent are at the top) |
33
|
|
|
arsort($revisions); |
34
|
|
|
|
35
|
|
|
return $revisions; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Restore a single revision |
40
|
|
|
* |
41
|
|
|
* @param [int] $id The ID of the source CRUD Model instance to update |
|
|
|
|
42
|
|
|
* @param [int] $revisionId The ID of the revision to use for the update |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function restoreRevision($id, $revisionId) { |
45
|
|
|
$entry = $this->getEntry($id); |
|
|
|
|
46
|
|
|
$revision = Revision::findOrFail($revisionId); |
47
|
|
|
|
48
|
|
|
// Update the revisioned field with the old value |
49
|
|
|
$entry->{$revision->fieldName()} = $revision->oldValue(); |
50
|
|
|
$entry->save(); |
51
|
|
|
|
52
|
|
|
// Reload the entry so we have the latest revisions |
53
|
|
|
$entry = $this->getEntry($id); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.