1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace App\Controller; |
5
|
|
|
|
6
|
|
|
use BEdita\WebTools\Utility\ApiTools; |
7
|
|
|
use Cake\Http\Response; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* History Controller |
11
|
|
|
* |
12
|
|
|
* @property \App\Controller\Component\HistoryComponent $History |
13
|
|
|
* @property \App\Controller\Component\SchemaComponent $Schema |
14
|
|
|
*/ |
15
|
|
|
class HistoryController extends AppController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritDoc} |
19
|
|
|
* {@codeCoverageIgnore} |
20
|
|
|
*/ |
21
|
|
|
public function initialize(): void |
22
|
|
|
{ |
23
|
|
|
parent::initialize(); |
24
|
|
|
|
25
|
|
|
$this->loadComponent('History'); |
26
|
|
|
$this->loadComponent('Schema'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get objects list: id, title, uname only / no relationships, no links. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function objects(): void |
35
|
|
|
{ |
36
|
|
|
$this->viewBuilder()->setClassName('Json'); |
37
|
|
|
$this->getRequest()->allowMethod('get'); |
38
|
|
|
$query = array_merge( |
39
|
|
|
$this->getRequest()->getQueryParams(), |
40
|
|
|
['fields' => 'id,title,uname'] |
41
|
|
|
); |
42
|
|
|
$response = ApiTools::cleanResponse((array)$this->apiClient->get('objects', $query)); |
43
|
|
|
$data = $response['data']; |
44
|
|
|
$meta = $response['meta']; |
45
|
|
|
$this->set(compact('data', 'meta')); |
46
|
|
|
$this->setSerialize(['data', 'meta']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get history data by ID |
51
|
|
|
* |
52
|
|
|
* @param string|int $id Object ID. |
53
|
|
|
* @param int $page Page number. |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function info($id, int $page): void |
57
|
|
|
{ |
58
|
|
|
$this->viewBuilder()->setClassName('Json'); |
59
|
|
|
$this->getRequest()->allowMethod('get'); |
60
|
|
|
$schema = (array)$this->Schema->getSchema($this->getRequest()->getParam('object_type')); |
61
|
|
|
$response = $this->History->fetch($id, $schema, compact('page')); |
62
|
|
|
$data = $response['data']; |
63
|
|
|
$meta = $response['meta']; |
64
|
|
|
$this->set(compact('data', 'meta')); |
65
|
|
|
$this->setSerialize(['data', 'meta']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Clone an object from a specific point of the history. |
70
|
|
|
* |
71
|
|
|
* @param string|int $id Object ID. |
72
|
|
|
* @param string|int $historyId History object ID. |
73
|
|
|
* @return \Cake\Http\Response|null |
74
|
|
|
*/ |
75
|
|
|
public function clone($id, $historyId): ?Response |
76
|
|
|
{ |
77
|
|
|
$this->setHistory($id, $historyId, false); |
78
|
|
|
|
79
|
|
|
return $this->redirect(['_name' => 'modules:clone', 'object_type' => $this->getRequest()->getParam('object_type')] + compact('id')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Restore an object from a specific point of the history. |
84
|
|
|
* |
85
|
|
|
* @param string|int $id Object ID. |
86
|
|
|
* @param string|int $historyId History object ID. |
87
|
|
|
* @return \Cake\Http\Response|null |
88
|
|
|
*/ |
89
|
|
|
public function restore($id, $historyId): ?Response |
90
|
|
|
{ |
91
|
|
|
$this->setHistory($id, $historyId, true); |
92
|
|
|
|
93
|
|
|
return $this->redirect(['_name' => 'modules:view', 'object_type' => $this->getRequest()->getParam('object_type')] + compact('id')); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Load history data and write into session by ID. |
98
|
|
|
* |
99
|
|
|
* @param string|int $id The object type. |
100
|
|
|
* @param string|int $historyId Object ID. |
101
|
|
|
* @param bool $keepUname Keep previous uname. |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
protected function setHistory($id, $historyId, $keepUname): void |
105
|
|
|
{ |
106
|
|
|
$objectType = $this->getRequest()->getParam('object_type'); |
107
|
|
|
$options = compact('objectType', 'id', 'historyId', 'keepUname') + [ |
108
|
|
|
'ApiClient' => $this->apiClient, |
109
|
|
|
'Request' => $this->getRequest(), |
110
|
|
|
'Schema' => $this->Schema, |
111
|
|
|
]; |
112
|
|
|
$this->History->write($options); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|