示例#1
0
 /**
  * @Route("/{slug}-{id}", requirements={"slug"=".+"}, name="covoiturage_view")
  * @Template()
  */
 public function viewAction(Request $request, $id, $slug)
 {
     if (!$this->getUserLayer()->isUser()) {
         return $this->createAccessDeniedResponse();
     }
     /** @var EntityManager $em */
     $em = $this->getDoctrine()->getManager();
     /** @var Covoit $covoit */
     $covoit = $em->createQueryBuilder()->select('c, cs, ca')->from('EtuModuleCovoitBundle:Covoit', 'c')->leftJoin('c.author', 'ca')->leftJoin('c.subscriptions', 'cs')->where('c.id = :id')->setParameter('id', $id)->getQuery()->getOneOrNullResult();
     if (!$covoit) {
         throw $this->createNotFoundException('Covoit not found');
     }
     // One URL to rule them all
     if ($slug != $covoit->getStartCity()->getSlug() . '-' . $covoit->getEndCity()->getSlug()) {
         return $this->redirect($this->generateUrl('covoiturage_view', ['id' => $covoit->getId(), 'slug' => $covoit->getStartCity()->getSlug() . '-' . $covoit->getEndCity()->getSlug()]), 301);
     }
     $message = new CovoitMessage();
     $message->setAuthor($this->getUser());
     $message->setCovoit($covoit);
     $messageForm = $this->createForm($this->get('etu.covoit.form.message'), $message);
     if ($request->getMethod() == 'POST' && $messageForm->submit($request)->isValid()) {
         $em->persist($message);
         $em->flush();
         // Send notifications to subscribers
         $notif = new Notification();
         $notif->setModule($this->getCurrentBundle()->getIdentifier())->setHelper('covoit_new_message')->setAuthorId($this->getUser()->getId())->setEntityType('covoit')->setEntityId($covoit->getId())->addEntity($message);
         $this->getNotificationsSender()->send($notif);
         // Add current user as subscriber
         $this->getSubscriptionsManager()->subscribe($this->getUser(), 'covoit', $covoit->getId());
         $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'covoit.messages.message_sent'));
         return $this->redirect($this->generateUrl('covoiturage_view', ['id' => $covoit->getId(), 'slug' => $covoit->getStartCity()->getSlug() . '-' . $covoit->getEndCity()->getSlug()]));
     }
     return ['covoit' => $covoit, 'messageForm' => $messageForm->createView()];
 }
示例#2
0
 /**
  * @Route("/edit/message/{id}", defaults={"id" = 1}, requirements={"id" = "\d+"}, name="covoiturage_my_edit_message")
  * @Template()
  */
 public function editMessageAction(Request $request, CovoitMessage $message)
 {
     if (!$this->getUserLayer()->isUser()) {
         return $this->createAccessDeniedResponse();
     }
     if ($message->getAuthor()->getId() != $this->getUser()->getId()) {
         throw new AccessDeniedHttpException();
     }
     $form = $this->createForm($this->get('etu.covoit.form.message'), $message);
     if ($request->getMethod() == 'POST' && $form->submit($request)->isValid()) {
         /** @var EntityManager $em */
         $em = $this->getDoctrine()->getManager();
         $em->persist($message);
         $em->flush();
         $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'covoit.messages.message_edited'));
         return $this->redirect($this->generateUrl('covoiturage_view', ['id' => $message->getCovoit()->getId(), 'slug' => $message->getCovoit()->getStartCity()->getSlug() . '-' . $message->getCovoit()->getEndCity()->getSlug()]));
     }
     return ['form' => $form->createView(), 'covoitMessage' => $message];
 }