Advertisement
AeroCross

Symfony2 Issue with Forms

May 8th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.76 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Vendor\AppBundle\Controller;
  4.  
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  7. use Vendor\AppBundle\Chat\ChatRequest;
  8. use Vendor\AppBundle\Document\CannedMessage;
  9. use Vendor\AppBundle\Document\Operator;
  10. use Vendor\AppBundle\Document\Session;
  11. use Vendor\AppBundle\Document\User;
  12. use Vendor\AppBundle\Document\Visit;
  13. use Vendor\AppBundle\Document\Visitor;
  14. use Vendor\AppBundle\Form\ChatRequestType;
  15. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Session\Session as SessionStorage;
  20. use Swift_Message;
  21.  
  22.     /**
  23.      * @Route("/", name="app_homepage")
  24.      * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  25.      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response|\Symfony\Component\HttpFoundation\RedirectResponse
  26.      */
  27.     public function indexAction()
  28.     {
  29.         $visitor = $this->getVisitorByKey();
  30.         $session = new SessionStorage();
  31.         if ($this->getOperator()) {
  32.             throw new NotFoundHttpException('No app found.');
  33.         }
  34.  
  35.         $appRequest = new appRequest();
  36.  
  37.         /* @var $form \Symfony\Component\Form\Form */
  38.         $form = $this->createForm(new appRequestType(), $appRequest);
  39.         $response = new Response();
  40.  
  41.         if ('POST' == $this->getRequest()->getMethod()) {
  42.             $form->bindRequest($this->getRequest());
  43.             $appRequest = $form->getData();
  44.  
  45.             if ($form->isValid()) {
  46.                 $dm = $this->getDocumentManager();
  47.  
  48.                 $visitor->setEmail($appRequest->getEmail());
  49.                 $visitor->setName($appRequest->getName());
  50.                 $dm->persist($visitor);
  51.  
  52.                 $visit = $this->getVisitByKey($visitor);
  53.  
  54.                 $appSession = Session::create($visit, $appRequest->getQuestion(), Session::STATUS_WAITING);
  55.                 $dm->persist($appSession);
  56.                 $dm->flush();
  57.  
  58.                 $session->getBag('appsession')->set('appsession', $appSession->getId());
  59.                 $this->cacheUserForSession($visitor, $appSession);
  60.  
  61.                 return $this->redirect($this->generateUrl('sglc_app_load', array('id' => $appSession->getId())));
  62.             } else {
  63.                 $response->setStatusCode(403);
  64.             }
  65.         }
  66.  
  67.         return $this->render('VendorAppBundle:app:index.html.twig', array(
  68.             'visitor' => $visitor,
  69.             'errorMsg' => $session->getFlashBag()->get('errorMsg', array()),
  70.             'form' => $form->createView()
  71.         ), $response);
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement