|
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 history using query parameters. |
|
31
|
|
|
* This is a proxy for /api/history endpoint. |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function get(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->viewBuilder()->setClassName('Json'); |
|
38
|
|
|
$this->getRequest()->allowMethod('get'); |
|
39
|
|
|
$query = $this->getRequest()->getQueryParams(); |
|
40
|
|
|
$response = ApiTools::cleanResponse((array)$this->apiClient->get('/history', $query)); |
|
41
|
|
|
$data = $response['data']; |
|
42
|
|
|
$meta = $response['meta']; |
|
43
|
|
|
$this->set(compact('data', 'meta')); |
|
44
|
|
|
$this->setSerialize(['data', 'meta']); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get objects list: id, title, uname only / no relationships, no links. |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function objects(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->viewBuilder()->setClassName('Json'); |
|
55
|
|
|
$this->getRequest()->allowMethod('get'); |
|
56
|
|
|
$query = array_merge( |
|
57
|
|
|
$this->getRequest()->getQueryParams(), |
|
58
|
|
|
['fields' => 'id,title,uname'] |
|
59
|
|
|
); |
|
60
|
|
|
$response = ApiTools::cleanResponse((array)$this->apiClient->get('objects', $query)); |
|
61
|
|
|
$data = $response['data']; |
|
62
|
|
|
$meta = $response['meta']; |
|
63
|
|
|
$this->set(compact('data', 'meta')); |
|
64
|
|
|
$this->setSerialize(['data', 'meta']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get history data by ID |
|
69
|
|
|
* |
|
70
|
|
|
* @param string|int $id Object ID. |
|
71
|
|
|
* @param int $page Page number. |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
public function info($id, int $page): void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->viewBuilder()->setClassName('Json'); |
|
77
|
|
|
$this->getRequest()->allowMethod('get'); |
|
78
|
|
|
$schema = (array)$this->Schema->getSchema($this->getRequest()->getParam('object_type')); |
|
79
|
|
|
$response = $this->History->fetch($id, $schema, compact('page')); |
|
80
|
|
|
$data = $response['data']; |
|
81
|
|
|
$meta = $response['meta']; |
|
82
|
|
|
$this->set(compact('data', 'meta')); |
|
83
|
|
|
$this->setSerialize(['data', 'meta']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Clone an object from a specific point of the history. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string|int $id Object ID. |
|
90
|
|
|
* @param string|int $historyId History object ID. |
|
91
|
|
|
* @return \Cake\Http\Response|null |
|
92
|
|
|
*/ |
|
93
|
|
|
public function clone($id, $historyId): ?Response |
|
94
|
|
|
{ |
|
95
|
|
|
$this->setHistory($id, $historyId, false); |
|
96
|
|
|
|
|
97
|
|
|
return $this->redirect(['_name' => 'modules:clone', 'object_type' => $this->getRequest()->getParam('object_type')] + compact('id')); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Restore an object from a specific point of the history. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string|int $id Object ID. |
|
104
|
|
|
* @param string|int $historyId History object ID. |
|
105
|
|
|
* @return \Cake\Http\Response|null |
|
106
|
|
|
*/ |
|
107
|
|
|
public function restore($id, $historyId): ?Response |
|
108
|
|
|
{ |
|
109
|
|
|
$this->setHistory($id, $historyId, true); |
|
110
|
|
|
|
|
111
|
|
|
return $this->redirect(['_name' => 'modules:view', 'object_type' => $this->getRequest()->getParam('object_type')] + compact('id')); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Load history data and write into session by ID. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string|int $id The object type. |
|
118
|
|
|
* @param string|int $historyId Object ID. |
|
119
|
|
|
* @param bool $keepUname Keep previous uname. |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function setHistory($id, $historyId, $keepUname): void |
|
123
|
|
|
{ |
|
124
|
|
|
$objectType = $this->getRequest()->getParam('object_type'); |
|
125
|
|
|
$options = compact('objectType', 'id', 'historyId', 'keepUname') + [ |
|
126
|
|
|
'ApiClient' => $this->apiClient, |
|
127
|
|
|
'Request' => $this->getRequest(), |
|
128
|
|
|
'Schema' => $this->Schema, |
|
129
|
|
|
]; |
|
130
|
|
|
$this->History->write($options); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|