Example #1
0
 /**
  * @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;
 }
Example #2
0
 /**
  * @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);
 }
Example #3
0
 public function getByIdea(Idea $idea, $userId, $userHash)
 {
     $query = $this->getAll()->join('c.idea', 'i');
     $query = UserRepository::get($query, 'c', 'uc', 'commentUser');
     $query = UserRepository::get($query, 'i', 'ui', 'ideaUser');
     $query = $this->getVote($query, $userId, $userHash);
     return $query->andWhere('c.idea_id = :idea')->setParameter('idea', $idea->getId())->addOrderBy('c.dateUpdate', 'DESC')->getQuery()->getArrayResult();
 }
Example #4
0
 public function edit($data, Idea $idea)
 {
     if (!$idea->getUser()) {
         $idea->setUser($this->token->getToken()->getUser());
         $idea->setUserId($this->token->getToken()->getUser()->getId());
     }
     return $this->editWithUserValidation(IdeaType::class, $data, $idea);
 }
Example #5
0
 /**
  * @param Request $request
  * @param FormInterface $form
  */
 public function process(Request $request, FormInterface $form)
 {
     $form->handleRequest($request);
     if ($form->isValid()) {
         $data = $form->getData();
         $idea = new IdeaEntity();
         $idea->setTitle($data['title'])->setDescription($data['description'])->setUser($this->getUserFromTokenStorage());
         $this->ideaService->saveIdea($idea);
         if ($data['youtube-flag'] === true) {
             $youtubeLink = new Link();
             $youtubeLink->setIdea($idea)->setType(Link::TYPE_YOUTUBE)->setLink($data['youtube-value']);
             $this->linkService->saveLink($youtubeLink);
         }
     }
 }
Example #6
0
 /**
  * @ParamConverter("idea", class="AppBundle:Idea", options={"mapping":{"idea_id" = "id"}})
  */
 public function deleteAction(Request $request, Idea $idea)
 {
     $user = $idea->getUser();
     $responseData = $this->get('app.model.idea')->delete($idea);
     if ($responseData['valid']) {
         //$this->get('session')->getFlashBag()->add('success', "Idea were deleted.");
         return $this->redirectToRoute('app_user_profile', ['user_username' => $user->getUsername()]);
     } else {
         throw $this->createNotFoundException();
     }
 }
Example #7
0
 /**
  * @param Idea $idea
  */
 public function saveIdea(Idea $idea)
 {
     $idea->setCreatedAt(new \DateTime());
     $this->entityManager->persist($idea);
     $this->entityManager->flush($idea);
 }