/** * @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; }