|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
use Spatie\Permission\Exceptions\UnauthorizedException; |
|
8
|
|
|
use Yaro\Jarboe\Exceptions\PermissionDenied; |
|
9
|
|
|
use Yaro\Jarboe\Table\CRUD; |
|
10
|
|
|
|
|
11
|
|
|
trait RevertHandlerTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Revert model to specific version. |
|
15
|
|
|
* |
|
16
|
|
|
* @param Request $request |
|
17
|
|
|
* @param $id |
|
18
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
19
|
|
|
* @throws PermissionDenied |
|
20
|
|
|
* @throws UnauthorizedException |
|
21
|
|
|
*/ |
|
22
|
4 |
|
public function handleRevert(Request $request, $id) |
|
23
|
|
|
{ |
|
24
|
4 |
|
$this->beforeInit(); |
|
25
|
4 |
|
$this->init(); |
|
26
|
4 |
|
$this->bound(); |
|
27
|
|
|
|
|
28
|
4 |
|
if (!$this->can('revert')) { |
|
29
|
1 |
|
throw UnauthorizedException::forPermissions(['revert']); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
$model = $this->crud()->repo()->find($id); |
|
33
|
3 |
|
if (!$this->crud()->actions()->isAllowed('history', $model)) { |
|
34
|
1 |
|
throw new PermissionDenied(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
$version = $model->versions()->find($request->get('version')); |
|
38
|
2 |
|
if (is_null($version)) { |
|
39
|
1 |
|
throw new RuntimeException('Version does not exist'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
$version->revert(); |
|
43
|
|
|
|
|
44
|
1 |
|
return response()->json([ |
|
45
|
1 |
|
'ok' => true, |
|
46
|
1 |
|
'timeline' => view('jarboe::crud.inc.history.timeline', [ |
|
47
|
1 |
|
'crud' => $this->crud(), |
|
48
|
1 |
|
'item' => $model, |
|
49
|
1 |
|
'versions' => $model->versions()->latest()->paginate(8), |
|
50
|
1 |
|
])->render(), |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
abstract protected function beforeInit(); |
|
55
|
|
|
abstract protected function init(); |
|
56
|
|
|
abstract protected function bound(); |
|
57
|
|
|
abstract protected function crud(): CRUD; |
|
58
|
|
|
abstract protected function can($action): bool; |
|
59
|
|
|
} |
|
60
|
|
|
|