src/Controller/User/GuestbookController.php line 48

Open in your IDE?
  1. <?php
  2. /*
  3.  * Author: Dominik Piekarski <code@dompie.de>
  4.  * Created at: 2021/08/26 11:46
  5.  */
  6. declare(strict_types=1);
  7. namespace App\Controller\User;
  8. use App\Form\GuestbookEntryForm;
  9. use App\Security\ApiUser;
  10. use App\Service\Client\GuestbookService;
  11. use App\Service\Client\Media\MemberMediaService;
  12. use App\Service\Client\User\LikeService;
  13. use App\Service\Client\User\MemberPropertyService;
  14. use App\Service\Client\User\MemberService;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. /**
  21.  * @Route("/user/profile/gaestebuch")
  22.  */
  23. class GuestbookController extends AbstractController
  24. {
  25.     protected MemberService $memberService;
  26.     protected MemberPropertyService $mpService;
  27.     protected GuestbookService $guestbookService;
  28.     protected MemberMediaService $memberMediaService;
  29.     protected LikeService $likeService;
  30.     public function __construct(MemberService    $memberServiceMemberPropertyService $mpService,
  31.                                 GuestbookService $guestbookServiceMemberMediaService $mmService,
  32.                                 LikeService      $likeService)
  33.     {
  34.         $this->memberService $memberService;
  35.         $this->mpService $mpService;
  36.         $this->guestbookService $guestbookService;
  37.         $this->memberMediaService $mmService;
  38.         $this->likeService $likeService;
  39.     }
  40.     public function index(Request $requeststring $usernameint $page): Response
  41.     {
  42.         $member $this->memberService->getMemberDetailByUsername($username);
  43.         if ($this->mpService->isGuestbookDisabled($member['id'])) {
  44.             $this->addFlash('info'$username ' hat das Gästebuch deaktiviert.');
  45.             return $this->redirectToRoute('app_user_profile_detail', ['username' => $username]);
  46.         }
  47.         if (!is_array($member) || $member['is_active'] === false) {
  48.             $this->addFlash('info''Den Nutzer gibt es nicht mehr.');
  49.             return $this->redirectToRoute('app_user_profile_detail', ['username' => $username]);
  50.         }
  51.         $guestbookEntryForm $this->createForm(GuestbookEntryForm::class, null, ['username' => $username]);
  52.         $guestbookEntryForm->handleRequest($request);
  53.         if ($guestbookEntryForm->isSubmitted() && $guestbookEntryForm->isValid()) {
  54.             /** @var ApiUser $user */
  55.             $user $this->getUser();
  56.             $data $guestbookEntryForm->getData();
  57.             $this->guestbookService->createEntry($user->getMemberId(), $member['id'], $data['entry']);
  58.             $this->guestbookService->dispatchCreatedEvent($username);
  59.             $this->addFlash('success'"Vielen Dank für Deinen Eintrag. Er wird nach einer manuellen Prüfung durch $username freigeschaltet.");
  60.             return $this->redirectToRoute('app_user_profile_guestbook', ['username' => $username'page' => $page]);
  61.         }
  62.         return $this->render('user/profile/guestbook.html.twig', [
  63.             'member' => $member,
  64.             'profilePhotos' => $this->memberMediaService->getProfilePhotos($member['id'], 3),
  65.             'page' => $page,
  66.             'guestbook' => $this->guestbookService->getGuestbookForMember($member['id'], $page10),
  67.             'guestbookForm' => $guestbookEntryForm->createView(),
  68.             'existingLike' => $this->getUser() ? $this->likeService->getExistingMemberLikeForIssuer($member['id']) : null
  69.         ]);
  70.     }
  71.     /**
  72.      * @param Request $request
  73.      * @return Response
  74.      * @Route("/comment", methods={"POST"})
  75.      */
  76.     public function comment(Request $request)
  77.     {
  78.         /** @var ApiUser $uesr */
  79.         $user $this->getUser();
  80.         if (!$user instanceof ApiUser || $user->getMemberId() <= 0) {
  81.             $msg 'Bitte melden Sie sich an.';
  82.             $this->addFlash('info'$msg);
  83.             return new JsonResponse($msgResponse::HTTP_UNAUTHORIZED);
  84.         }
  85.         if (!$this->isCsrfTokenValid('guestbook-manage'$request->request->get('token'null))) {
  86.             $msg 'Die Änderung wurde nicht verarbeitet.';
  87.             $this->addFlash('info'$msg);
  88.             return new JsonResponse($msgResponse::HTTP_BAD_REQUEST);
  89.         }
  90.         $comment $request->request->get('comment'null);
  91.         if (!$comment || mb_strlen($comment) > 1000) {
  92.             $msg 'Der Kommentar fehlt oder ist zu lang.';
  93.             $this->addFlash('info'$msg);
  94.             return new JsonResponse($msgResponse::HTTP_BAD_REQUEST);
  95.         }
  96.         if ($this->guestbookService->commentOnEntry($user->getMemberId(), $request->request->getInt('entryId'0), $comment)) {
  97.             $this->guestbookService->dispatchCommentedEvent($user->getUsername());
  98.             $msg 'Dein Kommentar wurde hinzugefügt.';
  99.             $this->addFlash('info'$msg);
  100.             return new JsonResponse($msgResponse::HTTP_OK);
  101.         }
  102.         return new JsonResponse(''Response::HTTP_OK);
  103.     }
  104.     /**
  105.      * @param Request $request
  106.      * @return Response
  107.      * @Route("/approve", methods={"POST"})
  108.      */
  109.     public function approve(Request $request): Response
  110.     {
  111.         /** @var ApiUser $uesr */
  112.         $user $this->getUser();
  113.         if (!$user instanceof ApiUser || $user->getMemberId() <= 0) {
  114.             $msg 'Bitte melden Sie sich an.';
  115.             $this->addFlash('info'$msg);
  116.             return new JsonResponse($msgResponse::HTTP_UNAUTHORIZED);
  117.         }
  118.         if (!$this->isCsrfTokenValid('guestbook-manage'$request->request->get('token'null))) {
  119.             $msg 'Die Änderung wurde nicht verarbeitet.';
  120.             $this->addFlash('info'$msg);
  121.             return new JsonResponse($msgResponse::HTTP_BAD_REQUEST);
  122.         }
  123.         $entryId $request->request->getInt('entryId'0);
  124.         $entryOwner $this->memberService->getMemberDetailByUsername($request->request->get('entryOwner'$user->getUsername()));
  125.         if ($this->guestbookService->approveEntry($entryOwner['id'], $entryId)) {
  126.             $this->guestbookService->dispatchApprovedEvent($user->getUsername());
  127.             $msg 'Der Eintrag wurde veröffentlicht.';
  128.             $this->addFlash('success'$msg);
  129.             return new JsonResponse($msg);
  130.         }
  131.         $msg 'Bitte nochmal probieren oder Support kontaktieren.';
  132.         $this->addFlash('info'$msg);
  133.         return new JsonResponse($msgResponse::HTTP_INTERNAL_SERVER_ERROR);
  134.     }
  135.     /**
  136.      * @param Request $request
  137.      * @return Response
  138.      * @Route("/delete", methods={"POST"})
  139.      */
  140.     public function delete(Request $request): Response
  141.     {
  142.         /** @var ApiUser $uesr */
  143.         $user $this->getUser();
  144.         if (!$user instanceof ApiUser || $user->getMemberId() <= 0) {
  145.             $msg 'Bitte melden Sie sich an.';
  146.             $this->addFlash('info'$msg);
  147.             return new JsonResponse($msgResponse::HTTP_UNAUTHORIZED);
  148.         }
  149.         if (!$this->isCsrfTokenValid('guestbook-manage'$request->request->get('token'null))) {
  150.             $msg 'Die Änderung wurde nicht verarbeitet.';
  151.             $this->addFlash('info'$msg);
  152.             return new JsonResponse($msgResponse::HTTP_BAD_REQUEST);
  153.         }
  154.         $entryId $request->request->getInt('entryId'0);
  155.         $recipientUsername $request->request->get('entryOwner');
  156.         $recipientMember $this->memberService->getMemberDetailByUsername($recipientUsername);
  157.         if ($this->guestbookService->deleteEntry($user->getMemberId(), $entryId$recipientMember['id'])) {
  158.             $this->guestbookService->dispatchApprovedEvent($recipientMember['username'] ?? $user->getUsername());
  159.             $msg 'Der Eintrag wurde im Gästebuch gelöscht.';
  160.             $this->addFlash('success'$msg);
  161.             return new JsonResponse($msg);
  162.         }
  163.         $msg 'Bitte nochmal probieren oder Support kontaktieren.';
  164.         $this->addFlash('info'$msg);
  165.         return new JsonResponse($msgResponse::HTTP_INTERNAL_SERVER_ERROR);
  166.     }
  167.     /**
  168.      * @param Request $request
  169.      * @return Response
  170.      * @Route("/spam", methods={"POST"})
  171.      */
  172.     public function markAsSpam(Request $request): Response
  173.     {
  174.         /** @var ApiUser $uesr */
  175.         $user $this->getUser();
  176.         if (!$user instanceof ApiUser || $user->getMemberId() <= 0) {
  177.             $msg 'Bitte melden Sie sich an.';
  178.             $this->addFlash('info'$msg);
  179.             return new JsonResponse($msgResponse::HTTP_UNAUTHORIZED);
  180.         }
  181.         if (!$this->isCsrfTokenValid('guestbook-manage'$request->request->get('token'null))) {
  182.             $msg 'Die Änderung wurde nicht verarbeitet.';
  183.             $this->addFlash('info'$msg);
  184.             return new JsonResponse($msgResponse::HTTP_BAD_REQUEST);
  185.         }
  186.         $entryId $request->request->getInt('entryId'0);
  187.         if ($this->guestbookService->markAsSpam($user->getMemberId(), $entryId)) {
  188.             $this->guestbookService->dispatchApprovedEvent($user->getUsername());
  189.             $msg 'Eintrag als spam markiert.';
  190.             $this->addFlash('success'$msg);
  191.             return new JsonResponse($msg);
  192.         }
  193.         $msg 'Bitte nochmal probieren oder Support kontaktieren.';
  194.         $this->addFlash('info'$msg);
  195.         return new JsonResponse($msgResponse::HTTP_INTERNAL_SERVER_ERROR);
  196.     }
  197. }