mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
109 lines
3.0 KiB
PHP
109 lines
3.0 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;
|
|
use Symfony\Component\Security\Core\Role\Role;
|
|
|
|
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())
|
|
->setType('task_default')
|
|
;
|
|
|
|
$this->denyAccessUnlessGranted(TaskVoter::CREATE, $task, 'You are not '
|
|
. 'allowed to create this task');
|
|
|
|
$form = $this->createCreateForm($task);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted()) {
|
|
if ($form->isValid()) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->persist($task);
|
|
|
|
$this->addFlash('success', "The task is created");
|
|
|
|
$em->flush();
|
|
} else {
|
|
$this->addFlash('error', "This form contains errors");
|
|
}
|
|
}
|
|
|
|
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(),
|
|
'role' => new Role(TaskVoter::CREATE)
|
|
]);
|
|
|
|
$form->add('submit', SubmitType::class);
|
|
|
|
return $form;
|
|
}
|
|
|
|
}
|