Commit b856647b authored by Julien Fastré's avatar Julien Fastré

add logic to filter tasks

parent 38cc4f84
......@@ -25,6 +25,7 @@ use Chill\TaskBundle\Event\TaskEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
class SingleTaskController extends Controller
{
......@@ -329,7 +330,8 @@ class SingleTaskController extends Controller
Request $request,
PaginatorFactory $paginatorFactory,
SingleTaskRepository $taskRepository,
PersonRepository $personRepository
PersonRepository $personRepository,
FormFactoryInterface $formFactory
) {
/* @var $viewParams array The parameters for the view */
/* @var $params array The parameters for the query */
......@@ -340,10 +342,10 @@ class SingleTaskController extends Controller
$params['user'] = null;
$viewParams['center'] = null;
$params['center'] = null;
// Get parameters from url
if ($request->query->has('person_id')) {
$personId = $request->query->getInt('person_id', null);
if (!empty($request->query->get('person_id', NULL))) {
$personId = $request->query->getInt('person_id');
$person = $personRepository->find($personId);
if ($person === null) {
......@@ -437,74 +439,22 @@ class SingleTaskController extends Controller
// total number of tasks
$viewParams['tasks_count'] = $tasks_count;
if ($request->query->has('person_id')){
if ($viewParams['person'] !== null){
$viewParams['layout'] = 'ChillPersonBundle::layout.html.twig';
} else {
$viewParams['layout'] = 'ChillMainBundle::layout.html.twig';
}
// Form for filtering tasks
$form = $this->createForm(SingleTaskListType::class, null, [
//'center' => $task->getCenter()
$form = $formFactory->createNamed(null, SingleTaskListType::class, null, [
'method' => Request::METHOD_GET,
'csrf_protection' => false
  • si on laisse true, on a un _token=xxxxxxx dans l'url, et quand on ne le met pas, ça ne marche pas.

Please register or sign in to reply
]);
$form->add('submit', SubmitType::class);
$form->handleRequest($request);
if ($form->isSubmitted()) {
  • Quand on crée un formulaire "nommé", l'url devient ceci:

    single_task_list[person_id]=123&single_task_list[status][]=ended

    Mais quand on a un formulaire anonyme, l'URL devient ceci:

    person_id=123&status[]=ended

    Du coup, il ne faut pas ré-écrire le filtre avec les données du formulaire => moins de code.

Please register or sign in to reply
if ($form->isValid()) {
$formData = $form->getData();
$status = $statuses[$formData['status']];
$viewParamsForm['user'] = $formData['user'];
$viewParamsForm['isSingleStatus'] = $singleStatus = count($statuses) === 1;
// herit some parameters from the page
$viewParamsForm['person'] = $viewParams['person'];
$viewParamsForm['layout'] = $viewParams['layout'];
// different query if regarding to date or 'closed'
if (in_array($status, SingleTaskRepository::DATE_STATUSES)) {
$params['date_status'] = $status;
$params['is_closed'] = false;
} else {
$params['date_status'] = null;
$params['is_closed'] = true;
}
$count = $taskRepository
->countByParameters($params, $formData['user'])
;
$paginator = $paginatorFactory->create($count);
$viewParamsForm['single_task_'.$status.'_count'] = $count;
$viewParamsForm['single_task_'.$status.'_paginator'] = $paginator;
$viewParamsForm['single_task_'.$status.'_tasks'] = $taskRepository
->findByParameters($params, $formData['user'],
$singleStatus ? $paginator->getCurrentPage()->getFirstItemNumber() : 0,
$singleStatus ? $paginator->getItemsPerPage() : 10)
;
// total number of tasks
$viewParamsForm['tasks_count'] = $count;
return $this->render('ChillTaskBundle:SingleTask:index.html.twig', array(
'view' => $viewParamsForm,
'form' => $form->createView()
));
} else {
$this->addFlash('error', "wrong test");
}
}
dump($viewParams);
return $this->render('ChillTaskBundle:SingleTask:index.html.twig', array(
'view' => $viewParams,
'form' => $form->createView()
));
return $this->render('ChillTaskBundle:SingleTask:index.html.twig',
\array_merge($viewParams, [ 'form' => $form->createView() ]));
}
......
......@@ -35,6 +35,7 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface
$loader->load('services/event.yml');
$loader->load('services/timeline.yml');
$loader->load('services/fixtures.yml');
$loader->load('services/form.yml');
}
public function prepend(ContainerBuilder $container)
......
......@@ -27,8 +27,14 @@ use Chill\MainBundle\Form\Type\ScopePickerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Chill\TaskBundle\Repository\SingleTaskRepository;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Symfony\Component\Security\Core\Role\Role;
use Chill\TaskBundle\Entity\SingleTask;
use Chill\PersonBundle\Form\Type\PickPersonType;
/**
*
......@@ -37,19 +43,105 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
*/
class SingleTaskListType extends AbstractType
{
/**
*
* @var EntityManagerInterface
*/
protected $em;
/**
*
* @var SingleTaskRepository
*/
protected $singleTaskRepository;
/**
*
* @var TokenStorageInterface
*/
protected $tokenStorage;
/**
*
* @var AuthorizationHelper
*/
protected $authorizationHelper;
public function __construct(
EntityManagerInterface $em,
TokenStorageInterface $tokenStorage,
AuthorizationHelper $authorizationHelper
) {
$this->em = $em;
$this->tokenStorage = $tokenStorage;
$this->authorizationHelper = $authorizationHelper;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$statuses = \array_merge(SingleTaskRepository::DATE_STATUSES, [ 'closed' ]);
$builder
->add('user', EntityType::class, [
'class' => 'Chill\MainBundle\Entity\User'
->add('user_id', EntityType::class, [
'class' => User::class,
'choices' => $this->getUsersAssigneedToTask(),
'placeholder' => 'Choose a user',
'required' => false
])
->add('status', ChoiceType::class, [
'choices' => array_flip(\array_merge(SingleTaskRepository::DATE_STATUSES, [ 'closed' ])),
'expanded' => false,
'multiple' => false
'choices' => array_combine($statuses, $statuses),
'expanded' => true,
'multiple' => true
])
->add('person_id', PickPersonType::class, [
'centers' => $this->authorizationHelper
->getReachableCenters(
$this->tokenStorage->getToken()->getUser(),
new Role(TaskVoter::SHOW)
),
'required' => false
])
;
}
/**
* Return a list of user having a task assigned.
*
* @return User[]
*/
protected function getUsersAssigneedToTask()
  • Ici, on fait une requete pour obtenir les utilisateurs qui sont assignés à une tâche. C'est plus simple que d'obtenir les utilisateurs qui sont associés aux centres.

Please register or sign in to reply
{
$qb = $this->em->createQueryBuilder();
$user = $this->tokenStorage->getToken()->getUser();
$role = new Role(TaskVoter::SHOW);
$centers = $this->authorizationHelper->getReachableCenters($user, $role);
$qb->select('DISTINCT user')
->from(User::class, 'user')
->join(SingleTask::class, 'task', \Doctrine\ORM\Query\Expr\Join::WITH, 'task.assignee = user')
->join('task.person', 'person')
->where("user.enabled = 'TRUE'")
;
$i = 0;
foreach ($centers as $center) {
$circles = $this->authorizationHelper->getReachableCircles($user, $role, $center);
// add condition about person and circle
$qb->andWhere(
$qb->expr()->andX()
->add($qb->expr()->eq('person.center', ':center_'.$i))
->add($qb->expr()->in('task.circle', ':circles_'.$i))
);
$qb->setParameter('center_'.$i, $center)
->setParameter('circles_'.$i, $circles)
;
// increase counter
$i++;
}
return $qb->getQuery()->getResult();
}
public function configureOptions(OptionsResolver $resolver)
......
......@@ -118,7 +118,6 @@ class SingleTaskRepository extends \Doctrine\ORM\EntityRepository
$qb->setParameter('scope', $params['scope']);
}
if (\array_key_exists('date_status', $params) and !empty($params['date_status'])) {
$this->addTypeFilter($qb, $params);
}
......
services:
Chill\TaskBundle\Form\SingleTaskListType:
arguments:
$em: '@Doctrine\ORM\EntityManagerInterface'
$authorizationHelper: '@Chill\MainBundle\Security\Authorization\AuthorizationHelper'
$tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
tags:
- { name: form.type }
......@@ -143,55 +143,59 @@
<p>Filter the tasks</p>
{{ form_start(form) }}
{{ form_row(form.user) }}
{{ form_row(form.user_id) }}
{{ form_row(form.status) }}
{{ form_row(form.person_id) }}
<button type="submit">{{ 'Filter'|trans }}</button>
{{ form_end(form)}}
{% if view.tasks_count == 0 %}
  • Je me suis permis de remettre de sortir les variables de la variable "view". Ca me paraissait plus conventionnel comme approche, même si je comprends pourquoi tu avais mis tout dans view.

Please register or sign in to reply
{% if tasks_count == 0 %}
<p class="chill-no-data-statement">{{ "There is no tasks."|trans }}</p>
{% if view.person is not null %}
<a href="{{ path('chill_task_single_task_new', {'person_id': view.person.id}) }}" class="sc-button bt-create">
{% if person is not null %}
<a href="{{ path('chill_task_single_task_new', {'person_id': person.id}) }}" class="sc-button bt-create">
{{ 'Add a new task' | trans }}
</a>
{% endif %}
<!-- Pour l'instant, il n'est pas encore possible d'ajouter une tache sans personne liée -->
<!-- {% if view.person is null %}
<!-- {% if person is null %}
<a href="{{ path('chill_task_single_task_new') }}" class="sc-button bt-create">
{{ 'Add a new task' | trans }}
</a>
{% endif %} -->
{% else %}
{% if view.single_task_ended_tasks is defined %}
{{ helper.date_status('Tasks with expired deadline', view.single_task_ended_tasks, view.single_task_ended_count, view.single_task_ended_paginator, 'ended', view.isSingleStatus, view.person) }}
{% if single_task_ended_tasks is defined %}
{{ helper.date_status('Tasks with expired deadline', single_task_ended_tasks, single_task_ended_count, single_task_ended_paginator, 'ended', isSingleStatus, person) }}
{% endif %}
{% if view.single_task_warning_tasks is defined %}
{{ helper.date_status('Tasks with warning deadline reached', view.single_task_warning_tasks, view.single_task_warning_count, view.single_task_warning_paginator, 'warning', view.isSingleStatus, view.person) }}
{% if single_task_warning_tasks is defined %}
{{ helper.date_status('Tasks with warning deadline reached', single_task_warning_tasks, single_task_warning_count, single_task_warning_paginator, 'warning', isSingleStatus, person) }}
{% endif %}
{% if view.single_task_current_tasks is defined %}
{{ helper.date_status('Current tasks', view.single_task_current_tasks, view.single_task_current_count, view.single_task_current_paginator, 'current', view.isSingleStatus, view.person) }}
{% if single_task_current_tasks is defined %}
{{ helper.date_status('Current tasks', single_task_current_tasks, single_task_current_count, single_task_current_paginator, 'current', isSingleStatus, person) }}
{% endif %}
{% if view.single_task_not_started_tasks is defined %}
{{ helper.date_status('Tasks not started', view.single_task_not_started_tasks, view.single_task_not_started_count, view.single_task_not_started_paginator, 'not_started', view.isSingleStatus, view.person) }}
{% if single_task_not_started_tasks is defined %}
{{ helper.date_status('Tasks not started', single_task_not_started_tasks, single_task_not_started_count, single_task_not_started_paginator, 'not_started', isSingleStatus, person) }}
{% endif %}
{% if view.single_task_closed_tasks is defined %}
{{ helper.date_status('Closed', view.single_task_closed_tasks, view.single_task_closed_count, view.single_task_closed_paginator, 'closed', view.isSingleStatus, view.person) }}
{% if single_task_closed_tasks is defined %}
{{ helper.date_status('Closed', single_task_closed_tasks, single_task_closed_count, single_task_closed_paginator, 'closed', isSingleStatus, person) }}
{% endif %}
{% if view.isSingleStatus == false %}
{% if isSingleStatus == false %}
<ul class="record_actions">
<li>
{% if view.person is not null %}
{% if person is not null %}
<a href="{{ path('chill_task_single_task_new', {'person_id': person.id}) }}" class="sc-button bt-create">
{{ 'Add a new task' | trans }}
</a>
{% endif %}
<!-- Pour l'instant, il n'est pas encore possible d'ajouter une tache sans personne liée -->
<!-- {% if view.person is null %}
<!-- {% if person is null %}
<a href="{{ path('chill_task_single_task_new') }}" class="sc-button bt-create">
{{ 'Add a new task' | trans }}
</a>
......
......@@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
#}
{% extends view.layout %}
{% extends layout %}
{% set activeRouteKey = 'chill_task_single_task_new' %}
......@@ -32,7 +32,7 @@
{# filter tasks #}
{% if view.person is not null %}
{% if person is not null %}
{% block personcontent %}
{% include 'ChillTaskBundle:SingleTask:_list.html.twig' %}
{% endblock %}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment