|
2 | 2 |
|
3 | 3 | namespace phpMyFAQ\Controller\Frontend; |
4 | 4 |
|
| 5 | +use Exception; |
| 6 | +use phpMyFAQ\Configuration; |
5 | 7 | use phpMyFAQ\Controller\AbstractController; |
| 8 | +use phpMyFAQ\Filter; |
| 9 | +use phpMyFAQ\Rating; |
| 10 | +use phpMyFAQ\Session; |
| 11 | +use phpMyFAQ\Translation; |
| 12 | +use phpMyFAQ\User\CurrentUser; |
6 | 13 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
7 | 16 |
|
8 | 17 | class VotingController extends AbstractController |
9 | 18 | { |
10 | | - public function create(): JsonResponse |
| 19 | + /** |
| 20 | + * @throws Exception |
| 21 | + */ |
| 22 | + public function create(Request $request): JsonResponse |
11 | 23 | { |
12 | | - return $this->json(['status' => 'ok']); |
| 24 | + $configuration = Configuration::getConfigurationInstance(); |
| 25 | + $user = CurrentUser::getCurrentUser($configuration); |
| 26 | + |
| 27 | + $rating = new Rating($configuration); |
| 28 | + $session = new Session($configuration); |
| 29 | + $session->setCurrentUser($user); |
| 30 | + |
| 31 | + $data = json_decode($request->getContent()); |
| 32 | + |
| 33 | + $faqId = Filter::filterVar($data->id ?? null, FILTER_VALIDATE_INT, 0); |
| 34 | + $vote = Filter::filterVar($data->value, FILTER_VALIDATE_INT); |
| 35 | + $userIp = Filter::filterVar($request->server->get('REMOTE_ADDR'), FILTER_VALIDATE_IP); |
| 36 | + |
| 37 | + if (isset($vote) && $rating->check($faqId, $userIp) && $vote > 0 && $vote < 6) { |
| 38 | + try { |
| 39 | + $session->userTracking('save_voting', $faqId); |
| 40 | + } catch (Exception $exception) { |
| 41 | + $configuration->getLogger()->error('Error saving voting', ['exception' => $exception]); |
| 42 | + } |
| 43 | + |
| 44 | + $votingData = [ |
| 45 | + 'record_id' => $faqId, |
| 46 | + 'vote' => $vote, |
| 47 | + 'user_ip' => $userIp, |
| 48 | + ]; |
| 49 | + |
| 50 | + if ($rating->getNumberOfVotings($faqId) === 0) { |
| 51 | + $rating->addVoting($votingData); |
| 52 | + } else { |
| 53 | + $rating->update($votingData); |
| 54 | + } |
| 55 | + |
| 56 | + return $this->json( |
| 57 | + [ |
| 58 | + 'success' => Translation::get('msgVoteThanks'), |
| 59 | + 'rating' => $rating->getVotingResult($faqId), |
| 60 | + ], |
| 61 | + Response::HTTP_OK |
| 62 | + ); |
| 63 | + } elseif (!$rating->check($faqId, $userIp)) { |
| 64 | + try { |
| 65 | + $session->userTracking('error_save_voting', $faqId); |
| 66 | + } catch (Exception $exception) { |
| 67 | + return $this->json(['error' => $exception->getMessage()], Response::HTTP_BAD_REQUEST); |
| 68 | + } |
| 69 | + |
| 70 | + return $this->json(['error' => Translation::get('err_VoteTooMuch')], Response::HTTP_BAD_REQUEST); |
| 71 | + } else { |
| 72 | + try { |
| 73 | + $session->userTracking('error_save_voting', $faqId); |
| 74 | + } catch (Exception $exception) { |
| 75 | + return $this->json(['error' => $exception->getMessage()], Response::HTTP_BAD_REQUEST); |
| 76 | + } |
| 77 | + |
| 78 | + return $this->json(['error' => Translation::get('err_noVote')], Response::HTTP_BAD_REQUEST); |
| 79 | + } |
13 | 80 | } |
14 | 81 | } |
0 commit comments