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
|
|
|
$formVars = [ |
30
|
|
|
'description' => __( |
31
|
|
|
"x_delete_confirm", |
32
|
|
|
"Are you sure you want to delete this :name?", |
33
|
|
|
['name' => __($this->label)] |
34
|
|
|
), |
35
|
|
|
'buttonClass' => 'btn-danger', |
36
|
|
|
'buttonID' => 'mainDeleteBtn', |
37
|
|
|
'buttonText' => __("delete", "Delete"), |
38
|
|
|
'data' => $data |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
$formHtml = $this->formService->formHtml( |
42
|
|
|
"/{$this->adminDirName}/delete/{$id}", |
43
|
|
|
$fields, |
44
|
|
|
$this->csrfManager->getToken(), |
45
|
|
|
$formVars |
46
|
|
|
); |
47
|
|
|
$formHtml = $this->formService->addMessages( |
48
|
|
|
$formHtml, |
49
|
|
|
$this->flashManager->getData('errors', []) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
return $this->renderResponse( |
|
|
|
|
53
|
|
|
$response, |
54
|
|
|
__( |
55
|
|
|
"x_delete", |
56
|
|
|
"Delete :name", |
57
|
|
|
['name' => __($this->label)] |
58
|
|
|
), |
59
|
|
|
$formHtml |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function handleDelete( |
64
|
|
|
Request $request, |
65
|
|
|
Response $response, |
66
|
|
|
array $args |
67
|
|
|
): Response { |
68
|
|
|
$id = $args['id']; |
69
|
|
|
$data = $request->getParsedBody() ?? []; |
70
|
|
|
|
71
|
|
|
$results = $this->model->validate( |
72
|
|
|
$data, |
73
|
|
|
['id' => $id, 'context' => 'delete'] |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
if (!$results['valid']) { |
77
|
|
|
$this->flashManager->addErrors($results['errors']); |
78
|
|
|
return $this->redirectResponse( |
79
|
|
|
$request, |
80
|
|
|
$response, |
81
|
|
|
"/{$this->adminDirName}/index" |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// validate csrf token |
86
|
|
|
$redirectTo = "/{$this->adminDirName}/delete/{$id}"; |
87
|
|
|
$redirectResponse = $this->validateCsrfToken($data, $request, $response, $redirectTo); |
|
|
|
|
88
|
|
|
if ($redirectResponse) { |
89
|
|
|
return $redirectResponse; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// delete data |
93
|
|
|
try { |
94
|
|
|
if ($this->model->delete($id)) { |
95
|
|
|
$this->flashManager->addMessage( |
96
|
|
|
'success', |
97
|
|
|
__( |
98
|
|
|
"x_delete_success", |
99
|
|
|
":name deleted successfully.", |
100
|
|
|
['name' => __($this->label)] |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
return $this->redirectResponse( |
104
|
|
|
$request, |
105
|
|
|
$response, |
106
|
|
|
"/{$this->adminDirName}/index" |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} catch (\Exception $e) { |
110
|
|
|
$this->flashManager->addErrors([ |
111
|
|
|
__( |
112
|
|
|
"x_delete_failed", |
113
|
|
|
"Failed to delete :name", |
114
|
|
|
['name' => __($this->label)] |
115
|
|
|
) |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$redirectTo = "/{$this->adminDirName}/edit/{$id}"; |
120
|
|
|
return $this->redirectResponse($request, $response, $redirectTo); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|