1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the he8us/feedback package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace He8us\FeedbackBundle\Controller; |
10
|
|
|
|
11
|
|
|
use He8us\FeedbackBundle\Entity\Feedback; |
12
|
|
|
use He8us\FeedbackBundle\Service\FeedbackService; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
18
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
19
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class FeedbackAdminController |
23
|
|
|
* |
24
|
|
|
* @package He8us\FeedbackBundle\Controller |
25
|
|
|
* @author Cedric Michaux <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class FeedbackAdminController extends Controller |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param string $status |
31
|
|
|
* |
32
|
|
|
* @return Response |
33
|
|
|
*/ |
34
|
|
|
public function indexAction(string $status): Response |
35
|
|
|
{ |
36
|
|
|
if ($status !== Feedback::STATUS_DONE && $status !== Feedback::STATUS_READ) { |
37
|
|
|
$status = Feedback::STATUS_NONE; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->render("He8usFeedbackBundle:FeedbackAdmin:index.html.twig", [ |
41
|
|
|
'status' => $status, |
42
|
|
|
'feedbacks' => $this->getFeedbackService()->findByStatus($status), |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return FeedbackService |
48
|
|
|
*/ |
49
|
|
|
private function getFeedbackService():FeedbackService |
50
|
|
|
{ |
51
|
|
|
return $this->get('he8us_feedback.feedback_service'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* @param $id |
57
|
|
|
* |
58
|
|
|
* @return JsonResponse |
59
|
|
|
*/ |
60
|
|
|
public function deleteAction(Request $request, $id): JsonResponse |
61
|
|
|
{ |
62
|
|
|
$feedback = $this->getFeedback($request, $id); |
63
|
|
|
|
64
|
|
|
$this->getFeedbackService()->delete($feedback); |
65
|
|
|
|
66
|
|
|
return JsonResponse::create(['status' => true]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Request $request |
71
|
|
|
* @param $id |
72
|
|
|
* |
73
|
|
|
* @return Feedback |
74
|
|
|
*/ |
75
|
|
|
private function getFeedback(Request $request, $id): Feedback |
76
|
|
|
{ |
77
|
|
|
$this->validateRequest($request); |
78
|
|
|
|
79
|
|
|
$feedback = $this->getFeedbackService()->findById($id); |
80
|
|
|
|
81
|
|
|
$this->validateFeedback($feedback); |
82
|
|
|
|
83
|
|
|
return $feedback; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Request $request |
88
|
|
|
* |
89
|
|
|
* @throws NotFoundHttpException |
90
|
|
|
*/ |
91
|
|
|
private function validateRequest(Request $request) |
92
|
|
|
{ |
93
|
|
|
if (!$request->isXmlHttpRequest()) { |
94
|
|
|
throw new NotFoundHttpException($this->getTranslator()->trans('page.not_found')); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return TranslatorInterface |
100
|
|
|
*/ |
101
|
|
|
private function getTranslator():TranslatorInterface |
102
|
|
|
{ |
103
|
|
|
return $this->get('translator'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $feedback |
108
|
|
|
* |
109
|
|
|
* @throws NotFoundHttpException |
110
|
|
|
*/ |
111
|
|
|
private function validateFeedback(Feedback $feedback = null) |
112
|
|
|
{ |
113
|
|
|
if (!$feedback) { |
114
|
|
|
throw new NotFoundHttpException($this->getTranslator()->trans('feedback.not_found')); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Request $request |
120
|
|
|
* @param $id |
121
|
|
|
* @param string $type |
122
|
|
|
* |
123
|
|
|
* @return JsonResponse |
124
|
|
|
*/ |
125
|
|
|
public function markAsAction(Request $request, $id, string $type = "read"): JsonResponse |
126
|
|
|
{ |
127
|
|
|
$feedback = $this->getFeedback($request, $id); |
128
|
|
|
|
129
|
|
|
$feedback->setStatus($this->getStatus($type)); |
130
|
|
|
|
131
|
|
|
$this->getFeedbackService()->save($feedback); |
132
|
|
|
|
133
|
|
|
return JsonResponse::create(['status' => true]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $type |
138
|
|
|
* |
139
|
|
|
* @return int |
140
|
|
|
*/ |
141
|
|
|
private function getStatus(string $type) |
142
|
|
|
{ |
143
|
|
|
if ($type == 'read') { |
144
|
|
|
return Feedback::STATUS_READ; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($type == 'done') { |
148
|
|
|
return Feedback::STATUS_DONE; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return Feedback::STATUS_NONE; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param Request $request |
156
|
|
|
* @param Feedback $id |
157
|
|
|
* |
158
|
|
|
* @return Response |
159
|
|
|
*/ |
160
|
|
|
public function replyAction(Request $request, Feedback $id): Response |
161
|
|
|
{ |
162
|
|
|
$data = []; |
163
|
|
|
$data['message'] = $id; |
164
|
|
|
$data['toemail'] = $this->container->getParameter('system_email'); |
165
|
|
|
|
166
|
|
|
if ($request->isMethod('POST')) { |
167
|
|
|
$this->sendMessage($request, $id); |
168
|
|
|
} |
169
|
|
|
return $this->render('He8usFeedbackBundle:FeedbackAdmin:reply.html.twig', $data); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param Request $request |
174
|
|
|
* @param Feedback $feedback |
175
|
|
|
*/ |
176
|
|
|
public function sendMessage(Request $request, Feedback $feedback) |
177
|
|
|
{ |
178
|
|
|
$form = $request->get('message'); |
179
|
|
|
$mailer = $this->container->get('mailer'); |
180
|
|
|
$message = $mailer->createMessage() |
181
|
|
|
->setSubject($form['subject']) |
182
|
|
|
->setFrom($this->container->getParameter('system_email')) |
183
|
|
|
->setTo($feedback->getEmail()) |
184
|
|
|
->setBody( |
185
|
|
|
$this->container->get('twig')->render($this->container->getParameter('feedback_reply_mail_layout'), |
186
|
|
|
['form' => $form, 'feedback' => $feedback]) |
187
|
|
|
) |
188
|
|
|
->setContentType('text/html'); |
189
|
|
|
$mailer->send($message); |
190
|
|
|
|
191
|
|
|
/** @var Session $session */ |
192
|
|
|
$session = $this->container->get('session'); |
193
|
|
|
$session->getFlashBag()->add('success', $this->getTranslator()->trans("feedback.message.sent_success")); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|