|
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\DataCollectorTranslator; |
|
20
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class FeedbackAdminController |
|
24
|
|
|
* |
|
25
|
|
|
* @package He8us\FeedbackBundle\Controller |
|
26
|
|
|
* @author Cedric Michaux <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class FeedbackAdminController extends Controller |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $status |
|
32
|
|
|
* |
|
33
|
|
|
* @return Response |
|
34
|
|
|
*/ |
|
35
|
|
|
public function indexAction(string $status): Response |
|
36
|
|
|
{ |
|
37
|
|
|
if ($status !== Feedback::STATUS_DONE && $status !== Feedback::STATUS_READ) { |
|
38
|
|
|
$status = Feedback::STATUS_NONE; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->render("He8usFeedbackBundle:FeedbackAdmin:index.html.twig", [ |
|
42
|
|
|
'status' => $status, |
|
43
|
|
|
'feedbacks' => $this->getFeedbackService()->findByStatus($status), |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return FeedbackService |
|
49
|
|
|
*/ |
|
50
|
|
|
private function getFeedbackService():FeedbackService |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->get('he8us_feedback.feedback_service'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Request $request |
|
57
|
|
|
* @param $id |
|
58
|
|
|
* |
|
59
|
|
|
* @return JsonResponse |
|
60
|
|
|
*/ |
|
61
|
|
|
public function deleteAction(Request $request, $id): JsonResponse |
|
62
|
|
|
{ |
|
63
|
|
|
$feedback = $this->getFeedback($request, $id); |
|
64
|
|
|
|
|
65
|
|
|
$this->getFeedbackService()->delete($feedback); |
|
66
|
|
|
|
|
67
|
|
|
return JsonResponse::create(['status' => true]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param Request $request |
|
72
|
|
|
* @param $id |
|
73
|
|
|
* |
|
74
|
|
|
* @return Feedback |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getFeedback(Request $request, $id): Feedback |
|
77
|
|
|
{ |
|
78
|
|
|
$this->validateRequest($request); |
|
79
|
|
|
|
|
80
|
|
|
$feedback = $this->getFeedbackService()->findById($id); |
|
81
|
|
|
|
|
82
|
|
|
$this->validateFeedback($feedback); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
return $feedback; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param Request $request |
|
89
|
|
|
* |
|
90
|
|
|
* @throws NotFoundHttpException |
|
91
|
|
|
*/ |
|
92
|
|
|
private function validateRequest(Request $request) |
|
93
|
|
|
{ |
|
94
|
|
|
if (!$request->isXmlHttpRequest()) { |
|
95
|
|
|
throw new NotFoundHttpException($this->getTranslator()->trans('page.not_found')); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $feedback |
|
101
|
|
|
* |
|
102
|
|
|
* @throws NotFoundHttpException |
|
103
|
|
|
*/ |
|
104
|
|
|
private function validateFeedback(Feedback $feedback = null) |
|
105
|
|
|
{ |
|
106
|
|
|
if (!$feedback) { |
|
107
|
|
|
throw new NotFoundHttpException($this->getTranslator()->trans('feedback.not_found')); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param Request $request |
|
113
|
|
|
* @param $id |
|
114
|
|
|
* @param string $type |
|
115
|
|
|
* |
|
116
|
|
|
* @return JsonResponse |
|
117
|
|
|
*/ |
|
118
|
|
|
public function markAsAction(Request $request, $id, string $type = "read"): JsonResponse |
|
119
|
|
|
{ |
|
120
|
|
|
$feedback = $this->getFeedback($request, $id); |
|
121
|
|
|
|
|
122
|
|
|
$feedback->setStatus($this->getStatus($type)); |
|
123
|
|
|
|
|
124
|
|
|
$this->getFeedbackService()->save($feedback); |
|
125
|
|
|
|
|
126
|
|
|
return JsonResponse::create(['status' => true]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param $type |
|
131
|
|
|
* |
|
132
|
|
|
* @return int |
|
133
|
|
|
*/ |
|
134
|
|
|
private function getStatus(string $type) |
|
135
|
|
|
{ |
|
136
|
|
|
if ($type == 'read') { |
|
137
|
|
|
return Feedback::STATUS_READ; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if ($type == 'done') { |
|
141
|
|
|
return Feedback::STATUS_DONE; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return Feedback::STATUS_NONE; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param Request $request |
|
149
|
|
|
* @param Feedback $id |
|
150
|
|
|
* |
|
151
|
|
|
* @return Response |
|
152
|
|
|
*/ |
|
153
|
|
|
public function replyAction(Request $request, Feedback $id): Response |
|
154
|
|
|
{ |
|
155
|
|
|
$data = []; |
|
156
|
|
|
$data['message'] = $id; |
|
157
|
|
|
$data['toemail'] = $this->container->getParameter('system_email'); |
|
158
|
|
|
|
|
159
|
|
|
if ($request->isMethod('POST')) { |
|
160
|
|
|
$this->sendMessage($request, $id); |
|
161
|
|
|
} |
|
162
|
|
|
return $this->render('He8usFeedbackBundle:FeedbackAdmin:reply.html.twig', $data); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param Request $request |
|
167
|
|
|
* @param Feedback $feedback |
|
168
|
|
|
*/ |
|
169
|
|
|
public function sendMessage(Request $request, Feedback $feedback): void |
|
170
|
|
|
{ |
|
171
|
|
|
$form = $request->get('message'); |
|
172
|
|
|
$mailer = $this->container->get('mailer'); |
|
173
|
|
|
$message = $mailer->createMessage() |
|
174
|
|
|
->setSubject($form['subject']) |
|
175
|
|
|
->setFrom($this->container->getParameter('system_email')) |
|
176
|
|
|
->setTo($feedback->getEmail()) |
|
177
|
|
|
->setBody( |
|
178
|
|
|
$this->container->get('twig')->render($this->container->getParameter('feedback_reply_mail_layout'), |
|
179
|
|
|
['form' => $form, 'feedback' => $feedback]) |
|
180
|
|
|
) |
|
181
|
|
|
->setContentType('text/html'); |
|
182
|
|
|
$mailer->send($message); |
|
183
|
|
|
|
|
184
|
|
|
/** @var Session $session */ |
|
185
|
|
|
$session = $this->container->get('session'); |
|
186
|
|
|
$session->getFlashBag()->add('success', $this->getTranslator()->trans("feedback.message.sent_success")); |
|
187
|
|
|
|
|
188
|
|
|
return true; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @return TranslatorInterface |
|
193
|
|
|
*/ |
|
194
|
|
|
private function getTranslator():TranslatorInterface |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->get('translator'); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.