mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
improve single task list
- move to a new controller - refactor to be more adaptive to context - partial autowiring
This commit is contained in:
parent
196fc2c38f
commit
60f6cd95b4
@ -14,28 +14,14 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|||||||
use Symfony\Component\Form\FormFactoryInterface;
|
use Symfony\Component\Form\FormFactoryInterface;
|
||||||
use Chill\TaskBundle\Security\Authorization\TaskVoter;
|
use Chill\TaskBundle\Security\Authorization\TaskVoter;
|
||||||
use Symfony\Component\Security\Core\Role\Role;
|
use Symfony\Component\Security\Core\Role\Role;
|
||||||
|
use Chill\MainBundle\Entity\User;
|
||||||
|
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
||||||
|
use Chill\TaskBundle\Repository\SingleTaskRepository;
|
||||||
|
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||||
|
|
||||||
class SingleTaskController extends Controller
|
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")
|
* @Route("/{_locale}/task/single-task/new")
|
||||||
@ -73,10 +59,15 @@ class SingleTaskController extends Controller
|
|||||||
if ($form->isValid()) {
|
if ($form->isValid()) {
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$em->persist($task);
|
$em->persist($task);
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
$this->addFlash('success', "The task is created");
|
$this->addFlash('success', "The task is created");
|
||||||
|
|
||||||
$em->flush();
|
return $this->redirectToRoute('chill_task_task_list', [
|
||||||
|
'personId' => $task->getPerson()->getId()
|
||||||
|
]);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$this->addFlash('error', "This form contains errors");
|
$this->addFlash('error', "This form contains errors");
|
||||||
}
|
}
|
||||||
@ -104,5 +95,94 @@ class SingleTaskController extends Controller
|
|||||||
|
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route(
|
||||||
|
* "/{_locale}/task/task/list/person/{personId}",
|
||||||
|
* name="chill_task_task_list"
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,92 +14,6 @@ use Symfony\Component\HttpFoundation\Response;
|
|||||||
|
|
||||||
class TaskController extends Controller
|
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
|
||||||
|
|
||||||
$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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,9 +33,22 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface
|
|||||||
public function prepend(ContainerBuilder $container)
|
public function prepend(ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
$this->prependAuthorization($container);
|
$this->prependAuthorization($container);
|
||||||
|
$this->prependRoute($container);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function prependAuthorization(ContainerBuilder $container)
|
protected function prependRoute(ContainerBuilder $container)
|
||||||
|
{
|
||||||
|
//declare routes for person bundle
|
||||||
|
$container->prependExtensionConfig('chill_main', array(
|
||||||
|
'routing' => array(
|
||||||
|
'resources' => array(
|
||||||
|
'@ChillTaskBundle/Resources/config/routing.yml'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function prependAuthorization(ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
$container->prependExtensionConfig('security', array(
|
$container->prependExtensionConfig('security', array(
|
||||||
'role_hierarchy' => array(
|
'role_hierarchy' => array(
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
services:
|
services:
|
||||||
#chill_task.single_task_controller:
|
Chill\TaskBundle\Controller\:
|
||||||
# class: Chill\TaskBundle\Controller\SingleTaskController
|
#autowire: true
|
||||||
# autowire: true
|
resource: '../../../Controller'
|
||||||
|
tags: ['controller.service_arguments']
|
||||||
|
@ -8,3 +8,5 @@ services:
|
|||||||
- method: setAuthorizationHelper
|
- method: setAuthorizationHelper
|
||||||
arguments:
|
arguments:
|
||||||
- "@chill.main.security.authorization.helper"
|
- "@chill.main.security.authorization.helper"
|
||||||
|
|
||||||
|
Chill\TaskBundle\Repository\SingleTaskRepository: '@chill_task.single_task_repository'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user