mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-28 02:23:51 +00:00
first implementation of controller
This commit is contained in:
101
Controller/SingleTaskController.php
Normal file
101
Controller/SingleTaskController.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user