-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndpoint.php
More file actions
370 lines (301 loc) · 13.8 KB
/
Endpoint.php
File metadata and controls
370 lines (301 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
// Import additionnal class into the global namespace
use \LaswitchTech\Core\Base\BaseEndpoint;
class DocumentsEndpoint extends BaseEndpoint {
/**
* Constructor
*/
public function __construct()
{
// Call the parent constructor
parent::__construct();
// Initialize the Endpoint
$this->init('documents');
// Set Properties
$this->required = ['type','targetTable','targetId'];
$this->optional = ['docvals','locale'];
// Set the Level
switch($this->Request->getNamespace()){
case "/documents/approve":
case "/documents/disapprove":
$this->Level = 3;
break;
}
}
/**
* Create a record
*/
public function createAction(): array
{
// Import Global Classes
global $UUID;
// Call the parent constructor
$message = parent::createAction();
// Check if the record is accessible
if($message['status'] == 200){
// Retrieve the parameters
$parameters = $message['data']['parameters'];
// Initialize the fields array
$fields = [];
// Retrieve the Document Type
$doctype = $this->Model->Doctypes->fetch(intval($parameters['type']));
if(!empty($doctype)){
// Setup our document
$fields['filename'] = $doctype['filename'];
$fields['doctype'] = $doctype['id'];
$fields['title'] = $doctype['title'];
$fields['subject'] = $doctype['subject'];
$fields['author'] = $this->Auth->user()->vcard('name');
$fields['creator'] = $this->Auth->user()->vcard('name');
$fields['keywords'] = $doctype['title'];
$fields['docvals'] = $parameters['docvals'];
$fields['watermark'] = "DRAFT";
$fields['uuid'] = $UUID->toString($message['data']['record']['id'].$message['data']['record']['created']);
// Parse the Document Filename
$fields['filename'] = $this->Helper->Documents->replace($fields['filename'], $this->Model->Documents->variables($fields['docvals']));
// Check if the Event Plugin is accessible
if($this->Helper->Core->isInstalled('event')){
// Initialize the Events
$message['data']['event'] = [];
// Setup a new event
$event = [
'category' => 'Document',
'message' => 'New Document Created by <vcard>'.$this->Auth->user()->vcard['id'].':'.$this->Auth->user()->username.'</vcard>',
'icon' => 'circle',
'color' => 'secondary',
'link' => '/plugin/'.$message['data']['record']['targetTable'].'/details?id='.$message['data']['record']['targetId'],
'targetTable' => $message['data']['record']['targetTable'],
'targetId' => $message['data']['record']['targetId'],
];
// Create the event
$message['data']['event'][] = $this->Model->Event->create($event);
}
// Check if $fields is empty
if(!empty($fields)){
$affectedRows = $this->Model->{$this->name}->update($message['data']['record']['id'], $fields);
// Check if we send out the notification
if($affectedRows){
// Retrieve the updated record
$message['data']['record'] = $this->Model->{$this->name}->fetch($message['data']['record']['id']);
}
}
} else {
$message = ["status" => 400, "message" => "Bad Request", "data" => "Invalid Document Type"];
}
}
// Return the message
return $message;
}
/**
* Update a record
*/
public function updateAction(): array
{
// Call the parent constructor
$message = parent::updateAction();
// Check if the record is accessible
if($message['status'] == 200){
// Check if the Event Plugin is accessible
if($this->Helper->Core->isInstalled('event')){
// Initialize the Events
$message['data']['event'] = [];
// Setup a new event
$event = [
'category' => 'Document',
'message' => 'Document Updated by <vcard>'.$this->Auth->user()->vcard['id'].':'.$this->Auth->user()->username.'</vcard>',
'icon' => 'circle',
'color' => 'secondary',
'link' => '/plugin/'.$message['data']['record']['targetTable'].'/details?id='.$message['data']['record']['targetId'],
'targetTable' => $message['data']['record']['targetTable'],
'targetId' => $message['data']['record']['targetId'],
];
// Create the event
$message['data']['event'][] = $this->Model->Event->create($event);
}
}
// Return the message
return $message;
}
/**
* Delete a record
*/
public function deleteAction(): array
{
// Call the parent constructor
$message = parent::deleteAction();
// Check if the record is accessible
if($message['status'] == 200){
// Check if the Event Plugin is accessible
if($this->Helper->Core->isInstalled('event')){
// Initialize the Events
$message['data']['event'] = [];
// Setup a new event
$event = [
'category' => 'Document',
'message' => 'Document Deleted by <vcard>'.$this->Auth->user()->vcard['id'].':'.$this->Auth->user()->username.'</vcard>',
'icon' => 'circle',
'color' => 'secondary',
'link' => '/plugin/'.$message['data']['record']['targetTable'].'/details?id='.$message['data']['record']['targetId'],
'targetTable' => $message['data']['record']['targetTable'],
'targetId' => $message['data']['record']['targetId'],
];
// Create the event
$message['data']['event'][] = $this->Model->Event->create($event);
}
}
// Return the message
return $message;
}
/**
* Archive a record
*/
public function archiveAction(): array
{
// Call the parent constructor
$message = parent::archiveAction();
// Check if the record is accessible
if($message['status'] == 200){
// Check if the Event Plugin is accessible
if($this->Helper->Core->isInstalled('event')){
// Initialize the Events
$message['data']['event'] = [];
// Setup a new event
$event = [
'category' => 'Document',
'message' => 'Document Archived by <vcard>'.$this->Auth->user()->vcard['id'].':'.$this->Auth->user()->username.'</vcard>',
'icon' => 'circle',
'color' => 'secondary',
'link' => '/plugin/'.$message['data']['record']['targetTable'].'/details?id='.$message['data']['record']['targetId'],
'targetTable' => $message['data']['record']['targetTable'],
'targetId' => $message['data']['record']['targetId'],
];
// Create the event
$message['data']['event'][] = $this->Model->Event->create($event);
}
}
// Return the message
return $message;
}
/**
* Recover a record
*/
public function recoverAction(): array
{
// Call the parent constructor
$message = parent::recoverAction();
// Check if the record is accessible
if($message['status'] == 200){
// Check if the Event Plugin is accessible
if($this->Helper->Core->isInstalled('event')){
// Initialize the Events
$message['data']['event'] = [];
// Setup a new event
$event = [
'category' => 'Document',
'message' => 'Document Recovered by <vcard>'.$this->Auth->user()->vcard['id'].':'.$this->Auth->user()->username.'</vcard>',
'icon' => 'circle',
'color' => 'secondary',
'link' => '/plugin/'.$message['data']['record']['targetTable'].'/details?id='.$message['data']['record']['targetId'],
'targetTable' => $message['data']['record']['targetTable'],
'targetId' => $message['data']['record']['targetId'],
];
// Create the event
$message['data']['event'][] = $this->Model->Event->create($event);
}
}
// Return the message
return $message;
}
/**
* Approve a document
*/
public function approveAction(): array
{
// Set the default message
$message = ["status" => 200, "message" => "OK", "data" => []];
// Retrieve the record
$record = $this->Model->{$this->name}->fetch(intval($this->Request->getParams('GET','id')));
// Check if the record is accessible
if(empty($record)){
$message = ["status" => 404, "message" => "Not Found", "data" => "Could not find the requested ".$this->name."."];
} else {
// Check if the organization owns the record
if(array_key_exists('organization',$record) && $record['organization']['id'] != $this->Auth->user()->organization()->id){
$message = ["status" => 403, "message" => "Forbidden", "data" => "You are not allowed to access this ".$this->name."."];
}
// Check if the user is authorized to access the record
if(array_key_exists('assignedTo',$record) && $record['assignedTo']['id'] != $this->Auth->user()->id && !$this->Auth->isAuthorized("AccountManager", 1)){
$message = ["status" => 403, "message" => "Forbidden", "data" => "You are not allowed to access this ".$this->name."."];
}
}
// Check if the record is accessible
if($message['status'] == 200){
// Check the request method
if($this->Request->getMethod() == "GET"){
// Update the record
$affectedRows = $this->Model->{$this->name}->update($record['id'], [
"isApproved" => 1,
"approvedOn" => date("Y-m-d H:i:s"),
"watermark" => null
]);
// Check if the record was updated
if($affectedRows){
// Retrieve the record
$message['data']['record'] = $this->Model->{$this->name}->fetch($record['id']);
} else {
$message = ["status" => 500, "message" => "Internal Server Error", "data" => "An error occurred while updating the ".$this->name."."];
}
} else {
$message = ["status" => 405, "message" => "Method Not Allowed", "data" => "The method is not allowed for the requested URL."];
}
}
// Return the message
return $message;
}
/**
* Disapprove a document
*/
public function disapproveAction(): array
{
// Set the default message
$message = ["status" => 200, "message" => "OK", "data" => []];
// Retrieve the record
$record = $this->Model->{$this->name}->fetch(intval($this->Request->getParams('GET','id')));
// Check if the record is accessible
if(empty($record)){
$message = ["status" => 404, "message" => "Not Found", "data" => "Could not find the requested ".$this->name."."];
} else {
// Check if the organization owns the record
if(array_key_exists('organization',$record) && $record['organization']['id'] != $this->Auth->user()->organization()->id){
$message = ["status" => 403, "message" => "Forbidden", "data" => "You are not allowed to access this ".$this->name."."];
}
// Check if the user is authorized to access the record
if(array_key_exists('assignedTo',$record) && $record['assignedTo']['id'] != $this->Auth->user()->id && !$this->Auth->isAuthorized("AccountManager", 1)){
$message = ["status" => 403, "message" => "Forbidden", "data" => "You are not allowed to access this ".$this->name."."];
}
}
// Check if the record is accessible
if($message['status'] == 200){
// Check the request method
if($this->Request->getMethod() == "GET"){
// Update the record
$affectedRows = $this->Model->{$this->name}->update($record['id'], [
"isApproved" => 0,
"approvedOn" => null,
"watermark" => "DRAFT"
]);
// Check if the record was updated
if($affectedRows){
// Retrieve the record
$message['data']['record'] = $this->Model->{$this->name}->fetch($record['id']);
} else {
$message = ["status" => 500, "message" => "Internal Server Error", "data" => "An error occurred while updating the ".$this->name."."];
}
} else {
$message = ["status" => 405, "message" => "Method Not Allowed", "data" => "The method is not allowed for the requested URL."];
}
}
// Return the message
return $message;
}
}