Created
January 28, 2014 18:05
-
-
Save smilesrg/8672923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\BrainGames\CoreBundle\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\HttpFoundation\Request; | |
use PrestoHeads\BrainGames\CoreBundle\Form\ExistingContractType; | |
use PrestoHeads\BrainGames\CoreBundle\Form\ContractRequestType; | |
use PrestoHeads\BrainGames\CoreBundle\Document\ContractRequest; | |
/** | |
* @Route("/contract") | |
*/ | |
class ContractController extends Controller | |
{ | |
/** | |
* @Route("/save", name="contract_save") | |
* @Method({"POST"}) | |
*/ | |
public function saveAction(Request $request) | |
{ | |
$user = $this->getUserEntity(); | |
$form = $this->createForm(new ExistingContractType(), $user); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$this->updateUserEntity($user); | |
} | |
return $this->redirect($request->headers->get('referer')); | |
} | |
/** | |
* @Route("/addrequest", name="contract_add_request") | |
* @Method("POST") | |
*/ | |
public function addrequestAction(Request $request) | |
{ | |
$user = $this->getUserEntity(); | |
$contractRequest = new ContractRequest(); | |
$form = $this->createForm(new ContractRequestType(), $contractRequest); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$user->setContractRequest($contractRequest); | |
$this->updateUserEntity($user); | |
} | |
return $this->redirect($request->headers->get('referer')); | |
} | |
/** | |
* Возвращает сущность текущего юзера. | |
* | |
* @return User | |
*/ | |
private function getUserEntity() | |
{ | |
$userId = $this->getUser()->getId(); | |
$user = $this->container->get('doctrine_mongodb')->getRepository('PrestoHeadsBrainGamesCoreBundle:User') | |
->findOneBy(['id' => $userId]) | |
; | |
return $user; | |
} | |
/** | |
* Обновляет сущность текущего юзера. | |
* | |
*/ | |
private function updateUserEntity(User $user) | |
{ | |
$em = $this->container->get('doctrine_mongodb')->getManager(); | |
$em->persist($user); | |
$em->flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment