improve autowiring and add workflow to tasks

This commit is contained in:
2018-04-25 12:03:16 +02:00
parent 196fc2c38f
commit adc830142b
10 changed files with 4669 additions and 117 deletions

View File

@@ -3,103 +3,62 @@
namespace Chill\TaskBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Chill\PersonBundle\Entity\Person;
use Chill\TaskBundle\Entity\SingleTask;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\TaskBundle\Repository\SingleTaskRepository;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
class TaskController extends Controller
{
/**
* @Route(
* "/{_locale}/task/task/list/{personId}",
* name="chill_task_task_list"
* )
*/
public function listAction(Request $request, Person $personId)
{
$person = $personId;
/* @var $taskRepository SingleTaskRepository */
$taskRepository = $this->get('chill_task.single_task_repository');
/* @var $paginatorFactory \Chill\MainBundle\Pagination\PaginatorFactory */
$paginatorFactory = $this->get('chill_main.paginator_factory');
/* @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
public function applyTransitionAction(
$type,
$taskId,
$transition,
SingleTaskRepository $singleTaskRepository,
Registry $registry,
EntityManagerInterface $em,
Request $request
) {
switch ($type) {
case 'single-task':
$task = $singleTaskRepository
->find($taskId)
;
$defaultReturnPath = $this->generateUrl(
'chill_task_task_list_by_person',
[ 'personId' => $task->getPerson() ]
);
}
}
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)
;
break;
default:
return new Response("The type '$type' is not implemented",
Response::HTTP_BAD_REQUEST);
}
return $this->render('ChillTaskBundle:Task:index.html.twig', $viewParams);
}
protected function getPersonParam(Request $request, EntityManagerInterface $em)
{
$person = $em->getRepository(Person::class)
->find($request->query->getInt('person_id'))
;
if (NULL === $person) {
throw $this->createNotFoundException('person not found');
if (NULL === $task) {
$this->createHttpNotFoundException("task with id '$taskId' and type "
. "'$type' does not exists");
}
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person, "You are "
. "not allowed to see this person");
return $person;
}
protected function getUserParam(Request $request, EntityManagerInterface $em)
{
$user = $em->getRepository(User::class)
->find($request->query->getInt('user_id'))
;
if (NULL === $user) {
throw $this->createNotFoundException('user not found');
}
return $user;
}
// we simply check that the user can see the task. Other ACL checks
// should be performed using `guard` events.
$this->denyAccessUnlessGranted($task, TaskVoter::SHOW);
$workflow = $registry->get($task);
if ($workflow->can($task, $transition)) {
$workflow->apply($task, $transition);
$em->flush();
$this->addFlash('success', 'The transition is sucessfully applyed');
} else {
$this->addFlash('error', 'The transition could not be applyed');
}
return $this->redirect($request->query->get('return_path', $defaultReturnPath));
}
}