1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jidaikobo\Kontiki\Controllers\Traits; |
4
|
|
|
|
5
|
|
|
use Jidaikobo\Kontiki\Services\FormService; |
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
8
|
|
|
|
9
|
|
|
trait DeleteTrait |
10
|
|
|
{ |
11
|
|
|
public function delete( |
12
|
|
|
Request $request, |
13
|
|
|
Response $response, |
14
|
|
|
array $args |
15
|
|
|
): Response { |
16
|
|
|
$id = $args['id']; |
17
|
|
|
$data = $this->model->getById($id); |
18
|
|
|
|
19
|
|
|
if (!$data) { |
20
|
|
|
return $this->redirectResponse( |
|
|
|
|
21
|
|
|
$request, |
22
|
|
|
$response, |
23
|
|
|
"{$this->label}_index" |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$fields = $this->model->getFields('delete', $data); |
28
|
|
|
|
29
|
|
|
$formHtml = $this->formService->formHtml( |
30
|
|
|
"/{$this->adminDirName}/delete/{$id}", |
31
|
|
|
$fields, |
32
|
|
|
$this->csrfManager->getToken(), |
33
|
|
|
__( |
34
|
|
|
"x_delete_confirm", |
35
|
|
|
"Are you sure you want to delete this :name?", |
36
|
|
|
['name' => __($this->label)] |
37
|
|
|
), |
38
|
|
|
__("delete", "Delete"), |
39
|
|
|
); |
40
|
|
|
$formHtml = $this->formService->addMessages( |
41
|
|
|
$formHtml, |
42
|
|
|
$this->flashManager->getData('errors', []) |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
return $this->renderResponse( |
|
|
|
|
46
|
|
|
$response, |
47
|
|
|
__( |
48
|
|
|
"x_delete", |
49
|
|
|
"Delete :name", |
50
|
|
|
['name' => __($this->label)] |
51
|
|
|
), |
52
|
|
|
$formHtml |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function handleDelete( |
57
|
|
|
Request $request, |
58
|
|
|
Response $response, |
59
|
|
|
array $args |
60
|
|
|
): Response { |
61
|
|
|
$id = $args['id']; |
62
|
|
|
$data = $request->getParsedBody() ?? []; |
63
|
|
|
|
64
|
|
|
$results = $this->model->validate( |
65
|
|
|
$data, |
66
|
|
|
['id' => $id, 'context' => 'delete'] |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
if (!$results['valid']) { |
70
|
|
|
$this->flashManager->addErrors($results['errors']); |
71
|
|
|
return $this->redirectResponse( |
72
|
|
|
$request, |
73
|
|
|
$response, |
74
|
|
|
"/{$this->adminDirName}/index" |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// validate csrf token |
79
|
|
|
$redirectTo = "/{$this->adminDirName}/delete/{$id}"; |
80
|
|
|
$redirectResponse = $this->validateCsrfToken($data, $request, $response, $redirectTo); |
|
|
|
|
81
|
|
|
if ($redirectResponse) { |
82
|
|
|
return $redirectResponse; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// delete data |
86
|
|
|
try { |
87
|
|
|
if ($this->model->delete($id)) { |
88
|
|
|
$this->flashManager->addMessage( |
89
|
|
|
'success', |
90
|
|
|
__( |
91
|
|
|
"x_delete_success", |
92
|
|
|
":name deleted successfully.", |
93
|
|
|
['name' => __($this->label)] |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
return $this->redirectResponse( |
97
|
|
|
$request, |
98
|
|
|
$response, |
99
|
|
|
"/{$this->adminDirName}/index" |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
$this->flashManager->addErrors([ |
104
|
|
|
__( |
105
|
|
|
"x_delete_failed", |
106
|
|
|
"Failed to delete :name", |
107
|
|
|
['name' => __($this->label)] |
108
|
|
|
) |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$redirectTo = "/{$this->adminDirName}/edit/{$id}"; |
113
|
|
|
return $this->redirectResponse($request, $response, $redirectTo); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|