chill-bundles/Controller/SingleTaskController.php

102 lines
2.7 KiB
PHP

<?php
namespace Chill\TaskBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Doctrine\ORM\EntityManager;
use Chill\PersonBundle\Entity\Person;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Chill\TaskBundle\Entity\SingleTask;
use Chill\TaskBundle\Form\SingleTaskType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
class SingleTaskController extends Controller
{
/**
*
* @var EntityManager
*/
protected $em;
/**
*
* @var FormFactoryInterface
*/
protected $formFactory;
/*public function __construct(
EntityManager $em,
FormFactoryInterface $formFactory)
{
$this->em = $em;
$this->formFactory = $formFactory;
}*/
/**
* @Route("/{_locale}/task/single-task/new")
*/
public function newAction(Request $request)
{
$personId = $request->query->getInt('person_id', null);
if ($personId === null) {
return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST);
}
$person = $this->getDoctrine()->getManager()
->getRepository(Person::class)
->find($personId);
if ($person === null) {
throw $this->createNotFoundException("Invalid person id");
}
$task = (new SingleTask())
->setPerson($person)
->setAssignee($this->getUser())
;
$this->denyAccessUnlessGranted(TaskVoter::CREATE, $task, 'You are not '
. 'allowed to create this task');
$form = $this->createCreateForm($task);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($task);
$this->addFlash('success', "The task is created");
$em->flush();
}
return $this->render('ChillTaskBundle:SingleTask:new.html.twig', array(
'form' => $form->createView(),
'task' => $task
));
}
/**
*
* @param SingleTask $task
* @return \Symfony\Component\Form\FormInterface
*/
protected function createCreateForm(SingleTask $task)
{
$form = $this->createForm(SingleTaskType::class, $task, [
'center' => $task->getCenter()
]);
$form->add('submit', SubmitType::class);
return $form;
}
}