mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-28 10:33:49 +00:00
add show, edit, delete actions for tasks + template twig
This commit is contained in:
@@ -22,140 +22,348 @@ use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
class SingleTaskController extends Controller
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @Route(
|
||||
* "/{_locale}/task/task/list/person/{personId}",
|
||||
* name="chill_task_task_list"
|
||||
* )
|
||||
*/
|
||||
public function listAction(
|
||||
Request $request,
|
||||
Person $personId
|
||||
) {
|
||||
$person = $personId;
|
||||
/* @var $paginatorFactory \Chill\MainBundle\Pagination\PaginatorFactory */
|
||||
$paginatorFactory = $this->get('chill_main.paginator_factory');
|
||||
/* @var $taskRepository SingleTaskRepository */
|
||||
$taskRepository = $this->get('chill_task.single_task_repository');
|
||||
/* @var $viewParams array The parameters for the view */
|
||||
$viewParams['person'] = $person;
|
||||
|
||||
// collect parameters for filter
|
||||
$params['person'] = $person;
|
||||
|
||||
if ($request->query->has('date_status')) {
|
||||
$statuses = $request->query->get('date_status');
|
||||
$singleStatus = count($statuses) === 1;
|
||||
// check for invalid parameters
|
||||
$diff = \array_diff(
|
||||
$statuses,
|
||||
SingleTaskRepository::DATE_STATUSES)
|
||||
;
|
||||
|
||||
if (count($diff) > 0) {
|
||||
return new Response(
|
||||
'date_status not allowed: '. \implode(', ', $diff),
|
||||
Response::HTTP_BAD_REQUEST
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
foreach(SingleTaskRepository::DATE_STATUSES as $type) {
|
||||
if($request->query->has('date_status')
|
||||
&& FALSE === \in_array($type, $statuses ?? [])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$params['date_status'] = $type;
|
||||
$count = $taskRepository
|
||||
->countByParameters($params, $this->getUser())
|
||||
;
|
||||
$paginator = $paginatorFactory->create($count);
|
||||
// $paginator->setItemsPerPage(2);
|
||||
|
||||
$viewParams['single_task_'.$type.'_count'] = $count;
|
||||
$viewParams['single_task_'.$type.'_paginator'] = $paginator;
|
||||
$viewParams['single_task_'.$type.'_tasks'] = $taskRepository
|
||||
->findByParameters($params, $this->getUser(),
|
||||
$singleStatus ? $paginator->getCurrentPage()->getFirstItemNumber() : 0,
|
||||
$singleStatus ? $paginator->getItemsPerPage() : 10)
|
||||
;
|
||||
}
|
||||
|
||||
return $this->render('ChillTaskBundle:Task:index.html.twig', $viewParams);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Route("/{_locale}/task/single-task/new")
|
||||
* @Route(
|
||||
* "/{_locale}/task/single-task/new",
|
||||
* name="chill_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);
|
||||
|
||||
|
||||
// $this->denyAccessUnlessGranted(TaskVoter::CREATE, $task, 'You are not '
|
||||
// . 'allowed to create this task');
|
||||
|
||||
$form = $this->setCreateForm($task, new Role(TaskVoter::CREATE));
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
if ($form->isValid()) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->persist($task);
|
||||
|
||||
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', "The task is created");
|
||||
|
||||
|
||||
return $this->redirectToRoute('chill_task_task_list', [
|
||||
'personId' => $task->getPerson()->getId()
|
||||
]);
|
||||
|
||||
|
||||
} else {
|
||||
$this->addFlash('error', "This form contains errors");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $this->render('ChillTaskBundle:SingleTask:new.html.twig', array(
|
||||
'form' => $form->createView(),
|
||||
'task' => $task
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @Route(
|
||||
* "/{_locale}/task/single-task/{id}/show",
|
||||
* name="chill_task_single_task_show"
|
||||
* )
|
||||
*/
|
||||
public function showAction(Request $request, $id)
|
||||
{
|
||||
/* @var $taskRepository SingleTaskRepository */
|
||||
$taskRepository = $this->get('chill_task.single_task_repository');
|
||||
|
||||
$task = $taskRepository->find($id);
|
||||
|
||||
if (!is_null($task->getPerson() === !null)) {
|
||||
$personId = $task->getPerson()->getId();
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
// $this->denyAccessUnlessGranted(TaskVoter::SEE, $task, 'You are not '
|
||||
// . 'allowed to view this task');
|
||||
|
||||
if (!$task) {
|
||||
throw $this->createNotFoundException('Unable to find Task entity.');
|
||||
}
|
||||
|
||||
return $this->render('ChillTaskBundle:SingleTask:show.html.twig', array(
|
||||
'task' => $task,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Route(
|
||||
* "/{_locale}/task/single-task/{id}/edit",
|
||||
* name="chill_task_single_task_edit"
|
||||
* )
|
||||
*/
|
||||
public function editAction(Request $request, $id)
|
||||
{
|
||||
/* @var $taskRepository SingleTaskRepository */
|
||||
$taskRepository = $this->get('chill_task.single_task_repository');
|
||||
|
||||
$task = $taskRepository->find($id);
|
||||
|
||||
if (!is_null($task->getPerson() === !null)) {
|
||||
$personId = $task->getPerson()->getId();
|
||||
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");
|
||||
}
|
||||
}
|
||||
// $this->denyAccessUnlessGranted(TaskVoter::UPDATE, $task, 'You are not '
|
||||
// . 'allowed to edit this task');
|
||||
|
||||
if (!$task) {
|
||||
throw $this->createNotFoundException('Unable to find Task entity.');
|
||||
}
|
||||
|
||||
$form = $this->setCreateForm($task, new Role(TaskVoter::UPDATE));
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
if ($form->isValid()) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->persist($task);
|
||||
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', "Success : task updated!");
|
||||
|
||||
return $this->redirectToRoute('chill_task_task_list', [
|
||||
'personId' => $task->getPerson()->getId()
|
||||
]);
|
||||
|
||||
} else {
|
||||
$this->addFlash('error', "This form contains errors");
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillTaskBundle:SingleTask:edit.html.twig', array(
|
||||
'task' => $task,
|
||||
'form' => $form->createView()
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Route(
|
||||
* "/{_locale}/task/single-task/{id}/delete",
|
||||
* name="chill_task_single_task_delete"
|
||||
* )
|
||||
*/
|
||||
public function deleteAction(Request $request, $id)
|
||||
{
|
||||
/* @var $taskRepository SingleTaskRepository */
|
||||
$taskRepository = $this->get('chill_task.single_task_repository');
|
||||
|
||||
$task = $taskRepository->find($id);
|
||||
|
||||
if (!is_null($task->getPerson() === !null)) {
|
||||
|
||||
$personId = $task->getPerson()->getId();
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// $this->denyAccessUnlessGranted(TaskVoter::DELETE, $task, 'You are not '
|
||||
// . 'allowed to delete this task');
|
||||
|
||||
if (!$task) {
|
||||
throw $this->createNotFoundException('Unable to find Task entity.');
|
||||
}
|
||||
|
||||
$form = $this->createDeleteForm($id);
|
||||
|
||||
if ($request->getMethod() === Request::METHOD_DELETE) {
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isValid()) {
|
||||
$logger = $this->get('chill.main.logger');
|
||||
|
||||
$logger->notice("A task has been removed", array(
|
||||
'by_user' => $this->getUser()->getUsername(),
|
||||
'task_id' => $task->getId(),
|
||||
'description' => $task->getDescription(),
|
||||
'assignee' => $task->getAssignee(),
|
||||
'scope_id' => $task->getScope()->getId(),
|
||||
//'start_date' => $task->getStartDate()->format('Y-m-d'),
|
||||
//'end_date' => $task->getEndDate()->format('Y-m-d'),
|
||||
//'warning_interval' => $task->getWarningInterval()->format('Y-m-d')
|
||||
));
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->remove($task);
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')
|
||||
->trans("The task has been successfully removed."));
|
||||
|
||||
return $this->redirect($this->generateUrl(
|
||||
'chill_task_task_list', array(
|
||||
'personId' => $personId
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $this->render('ChillTaskBundle:SingleTask:confirm_delete.html.twig', array(
|
||||
'task' => $task,
|
||||
'delete_form' => $form->createView()
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param SingleTask $task
|
||||
* @param Role $role
|
||||
* @return \Symfony\Component\Form\FormInterface
|
||||
*/
|
||||
protected function createCreateForm(SingleTask $task)
|
||||
protected function setCreateForm(SingleTask $task, Role $role)
|
||||
{
|
||||
$form = $this->createForm(SingleTaskType::class, $task, [
|
||||
'center' => $task->getCenter(),
|
||||
'role' => new Role(TaskVoter::CREATE)
|
||||
'role' => $role
|
||||
]);
|
||||
|
||||
|
||||
$form->add('submit', SubmitType::class);
|
||||
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Route(
|
||||
* "/{_locale}/task/task/list/person/{personId}",
|
||||
* name="chill_task_task_list"
|
||||
* )
|
||||
* Creates a form to delete a Task entity by id.
|
||||
*
|
||||
* @param mixed $id The entity id
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
public function listAction(
|
||||
Request $request,
|
||||
Person $personId,
|
||||
PaginatorFactory $paginatorFactory
|
||||
) {
|
||||
$person = $personId;
|
||||
/* @var $taskRepository SingleTaskRepository */
|
||||
$taskRepository = $this->get('chill_task.single_task_repository');
|
||||
/* @var $viewParams array The parameters for the view */
|
||||
$viewParams['person'] = $person;
|
||||
|
||||
// collect parameters for filter
|
||||
$params['person'] = $person;
|
||||
|
||||
if ($request->query->has('date_status')) {
|
||||
$statuses = $request->query->get('date_status');
|
||||
$singleStatus = count($statuses) === 1;
|
||||
// check for invalid parameters
|
||||
$diff = \array_diff(
|
||||
$statuses,
|
||||
SingleTaskRepository::DATE_STATUSES)
|
||||
;
|
||||
|
||||
if (count($diff) > 0) {
|
||||
return new Response(
|
||||
'date_status not allowed: '. \implode(', ', $diff),
|
||||
Response::HTTP_BAD_REQUEST
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
foreach(SingleTaskRepository::DATE_STATUSES as $type) {
|
||||
if($request->query->has('date_status')
|
||||
&& FALSE === \in_array($type, $statuses ?? [])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$params['date_status'] = $type;
|
||||
$count = $taskRepository
|
||||
->countByParameters($params, $this->getUser())
|
||||
;
|
||||
$paginator = $paginatorFactory->create($count);
|
||||
$viewParams['single_task_'.$type.'_count'] = $count;
|
||||
$viewParams['single_task_'.$type.'_paginator'] = $paginator;
|
||||
$viewParams['single_task_'.$type.'_tasks'] = $taskRepository
|
||||
->findByParameters($params, $this->getUser(),
|
||||
$singleStatus ? $paginator->getCurrentPage()->getFirstItemNumber() : 0,
|
||||
$singleStatus ? $paginator->getItemsPerPage() : 10)
|
||||
;
|
||||
}
|
||||
|
||||
return $this->render('ChillTaskBundle:Task:index.html.twig', $viewParams);
|
||||
private function createDeleteForm($id)
|
||||
{
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl(
|
||||
'chill_task_single_task_delete',
|
||||
array('id' => $id)))
|
||||
->setMethod('DELETE')
|
||||
->add('submit', SubmitType::class, array('label' => 'Delete'))
|
||||
->getForm()
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
protected function getPersonParam(Request $request, EntityManagerInterface $em)
|
||||
{
|
||||
$person = $em->getRepository(Person::class)
|
||||
@@ -171,7 +379,7 @@ class SingleTaskController extends Controller
|
||||
|
||||
return $person;
|
||||
}
|
||||
|
||||
|
||||
protected function getUserParam(Request $request, EntityManagerInterface $em)
|
||||
{
|
||||
$user = $em->getRepository(User::class)
|
||||
|
Reference in New Issue
Block a user