/** * @Template() * @ParamConverter("user", class="CoreUserBundle:User", options={"mapping":{"user_username" = "username"}}) * @ParamConverter("idea", class="AppBundle:Idea", options={"mapping":{"idea_slug" = "slug"}}) */ public function viewAction(Request $request, User $user, Idea $idea, $comment_id) { $doctrine = $this->getDoctrine(); $security = $this->get('security.authorization_checker'); $formService = $this->get('core.base.form'); $repository = $doctrine->getRepository('AppBundle:Comment'); $loggedUser = $security->isGranted('IS_AUTHENTICATED_FULLY') ? $this->getUser()->getId() : null; $hashUser = $formService->getUserHash($request); $comment = $repository->getById($comment_id, $loggedUser, $hashUser); $comment['comment_type'] = Comment::types[$comment['comment_type']]; if ($idea->getUserId() !== $user->getId() || $comment['idea_id'] !== $idea->getId()) { throw new $this->createNotFoundException(); } $viewData = []; $viewData['ideaUser'] = $user; $viewData['idea'] = $idea; $viewData['comment'] = $comment; $viewData['userHash'] = $hashUser; if ($comment['user_id'] && $this->getUser() && $comment['user_id'] == $this->getUser()->getId() || $security->isGranted('ROLE_MANAGER')) { $comment = $repository->find($comment['comment_id']); $formData = $request->request->get(CommentType::name); $responseData = $this->get('app.model.comment')->edit($formData, $comment); $viewData['commentForm'] = $responseData['form']->createView(); if ($responseData['valid']) { return $this->redirectToRoute('app_comment_view', ['user_username' => $user->getUsername(), 'idea_slug' => $idea->getSlug(), 'comment_id' => $responseData['embedded']['comment']['id']]); } } return $viewData; }
/** * @ParamConverter("user", class="CoreUserBundle:User", options={"mapping":{"user_username" = "username"}}) * @ParamConverter("idea", class="AppBundle:Idea", options={"mapping":{"idea_slug" = "slug"}}) * @ParamConverter("comment", class="AppBundle:Comment", options={"mapping":{"comment_id" = "id"}}) */ public function addAction(Request $request, User $user, Idea $idea, Comment $comment) { if ($idea->getUserId() !== $user->getId() || $comment->getIdeaId() !== $idea->getId()) { throw new $this->createNotFoundException(); } $doctrine = $this->getDoctrine(); $manager = $doctrine->getManager(); $voteUser = $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY') ? $this->getUser() : null; $hash = $voteUser ? null : $this->get('core.base.form')->getUserHash($request); if ($voteUser && $voteUser->getId() === $comment->getUserId()) { $this->get('session')->getFlashBag()->add('error', "You can't vote for your own idea."); } else { $exist = $doctrine->getRepository('AppBundle:Vote')->findOneBy(['user' => $voteUser, 'hash' => $hash, 'comment' => $comment]); if ($exist) { $this->get('session')->getFlashBag()->add('error', 'You have already voted for this comment.'); } else { $vote = new Vote(); $vote->setComment($comment); if ($voteUser) { $vote->setUser($voteUser); } else { $vote->setHash($hash); } $manager->persist($vote); $manager->flush(); } } return $this->end($request, $user, $idea, $comment); }
/** * @Template() * @ParamConverter("user", class="CoreUserBundle:User", options={"mapping":{"user_username" = "username"}}) * @ParamConverter("idea", class="AppBundle:Idea", options={"mapping":{"idea_slug" = "slug"}}) */ public function viewAction(Request $request, User $user, Idea $idea) { if ($idea->getUserId() !== $user->getId()) { throw $this->createNotFoundException(); } $security = $this->get('security.authorization_checker'); $formService = $this->get('core.base.form'); $doctrine = $this->getDoctrine(); $isOwner = false; $loggedUserId = null; $hashUser = $formService->getUserHash($request); if ($security->isGranted('IS_AUTHENTICATED_FULLY')) { $loggedUserId = $this->getUser()->getId(); $isOwner = $idea->getUserId() === $loggedUserId; } $viewData = []; $viewData['user'] = $user; $viewData['userHash'] = $hashUser; $viewData['idea'] = $idea->toArray($loggedUserId, ['user' => true]); $viewData['isOwner'] = $isOwner; /* IDEA */ ////////// if ($isOwner || $security->isGranted('ROLE_MANAGER')) { $formData = $request->request->get(IdeaType::name); $responseDataIdea = $this->get('app.model.idea')->edit($formData, $idea); $viewData['ideaForm'] = $responseDataIdea['form']->createView(); if ($responseDataIdea['valid']) { //$request->getSession()->getFlashBag()->add('success', "Idea were updated."); return $this->redirectToRoute('app_idea_view', ['user_username' => $user->getUsername(), 'idea_slug' => $responseDataIdea['embedded']['idea']['slug']]); } } /* COMMENT */ ///////////// $formData = $request->request->get(CommentGroupType::name); $responseDataComments = $this->get('app.model.comment')->createMultiple($idea, $formData, $hashUser); $viewData['commentGroupForm'] = $responseDataComments['form']->createView(); if ($responseDataComments['valid']) { $lastCommentId = end($responseDataComments['embedded']['comments'])['id']; $url = $this->generateUrl('app_idea_view', ['user_username' => $user->getUsername(), 'idea_slug' => $idea->getSlug()]); return $this->redirect($url . '#comment' . $lastCommentId); } $commentsByDate = $doctrine->getRepository('AppBundle:Comment')->getByIdea($idea, $loggedUserId, $hashUser); // Mise en forme des commentaires $viewData['commentsByUser'] = []; $viewData['commentsByType'] = []; $viewData['commentsTypes'] = []; foreach (Comment::types as $type) { $viewData['commentsByType'][$type] = []; $viewData['commentsTypes'][] = $type; } $commentTypes = []; foreach (array_values(Comment::types) as $type) { $commentTypes[$type] = []; } foreach ($commentsByDate as &$comment) { $comment['comment_type'] = Comment::types[$comment['comment_type']]; if (!isset($viewData['commentsByUser'][$comment['commentUser_username']])) { $viewData['commentsByUser'][$comment['commentUser_username']] = $commentTypes; } $viewData['commentsByUser'][$comment['commentUser_username']][$comment['comment_type']][] = $comment; $viewData['commentsByType'][$comment['comment_type']][] = $comment; } return $viewData; }