diff --git a/src/Bundle/ChillPersonBundle/Controller/ReassignAccompanyingPeriodController.php b/src/Bundle/ChillPersonBundle/Controller/ReassignAccompanyingPeriodController.php index eb8dcc70e..05dd70cb4 100644 --- a/src/Bundle/ChillPersonBundle/Controller/ReassignAccompanyingPeriodController.php +++ b/src/Bundle/ChillPersonBundle/Controller/ReassignAccompanyingPeriodController.php @@ -16,10 +16,12 @@ use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Repository\UserRepository; use Chill\MainBundle\Templating\Entity\UserRender; use Chill\PersonBundle\Repository\AccompanyingPeriodACLAwareRepositoryInterface; +use Chill\PersonBundle\Repository\AccompanyingPeriodRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\FormType; -use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; @@ -45,7 +47,21 @@ class ReassignAccompanyingPeriodController extends AbstractController private UserRepository $userRepository; - public function __construct(AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository, UserRepository $userRepository, EngineInterface $engine, FormFactoryInterface $formFactory, PaginatorFactory $paginatorFactory, Security $security, UserRender $userRender) + private AccompanyingPeriodRepository $courseRepository; + + private EntityManagerInterface $em; + + public function __construct( + AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository, + UserRepository $userRepository, + AccompanyingPeriodRepository $courseRepository, + EngineInterface $engine, + FormFactoryInterface $formFactory, + PaginatorFactory $paginatorFactory, + Security $security, + UserRender $userRender, + EntityManagerInterface $em + ) { $this->accompanyingPeriodACLAwareRepository = $accompanyingPeriodACLAwareRepository; $this->engine = $engine; @@ -54,6 +70,8 @@ class ReassignAccompanyingPeriodController extends AbstractController $this->security = $security; $this->userRepository = $userRepository; $this->userRender = $userRender; + $this->courseRepository = $courseRepository; + $this->em = $em; } /** @@ -65,9 +83,7 @@ class ReassignAccompanyingPeriodController extends AbstractController throw new AccessDeniedException(); } - $periodIds = []; - - $form = $this->buildFilterForm($periodIds); + $form = $this->buildFilterForm(); $form->handleRequest($request); @@ -83,14 +99,66 @@ class ReassignAccompanyingPeriodController extends AbstractController $paginator->getCurrentPageFirstItemNumber() ); - foreach ($periods as $period) { - $periodIds[] = $period->getId(); + // foreach ($periods as $period) { + // $periodIds[] = $period->getId(); + // } + + // $assignForm= $this->buildReassignForm($periods); + $assignData = []; + $assignForm = $this->createFormBuilder($assignData) + ->add('periods', ChoiceType::class, [ + // 'data' => serialize($periods), + 'choices' => $periods, + 'multiple' => true, + 'expanded' => true + ]) + ->add('user', EntityType::class, [ + 'class' => User::class, + 'choices' => $this->userRepository->findByActive(['username' => 'ASC']), + 'choice_label' => function (User $u) { + return $this->userRender->renderString($u, []); + }, + 'placeholder' => 'Choose a user to reassign to', + 'multiple' => false, + 'label' => 'User', + 'required' => true, + ]) + ->getForm(); + + $assignForm->handleRequest($request); + + if ($assignForm->isSubmitted()) { + + $periods = $assignForm->get('periods')->getData(); + $userAssign = $assignForm->get('user')->getData(); + + foreach($periods as $periodId) { + $reassignPeriod = $this->courseRepository->find($periodId); + $reassignPeriod->setUser($userAssign); + $this->em->persist($reassignPeriod); + } + + $this->em->flush(); + + $remainingPeriods = $this->accompanyingPeriodACLAwareRepository + ->findByUserOpenedAccompanyingPeriod( + $form['user']->getData(), + ['openingDate' => 'ASC'], + $paginator->getItemsPerPage(), + $paginator->getCurrentPageFirstItemNumber() + ); + + return new Response( + $this->engine->render('@ChillPerson/AccompanyingPeriod/reassign_list.html.twig', [ + 'paginator' => $paginator, + 'periods' => $remainingPeriods, + 'form' => $form->createView(), + 'assignForm' => $assignForm->createView() + ]) + ); + } - $assignForm= $this->buildFilterForm($periodIds); - - dump($assignForm->get('periods')); - return new Response( $this->engine->render('@ChillPerson/AccompanyingPeriod/reassign_list.html.twig', [ 'paginator' => $paginator, @@ -101,7 +169,7 @@ class ReassignAccompanyingPeriodController extends AbstractController ); } - private function buildFilterForm(array $periodIds): FormInterface + private function buildFilterForm(): FormInterface { $data = [ 'user' => null, @@ -119,9 +187,40 @@ class ReassignAccompanyingPeriodController extends AbstractController 'multiple' => false, 'label' => 'User', 'required' => false, + ]); + // ->add('periods', HiddenType::class, [ + // 'data' => serialize($periodIds), + // ]); + + return $builder->getForm(); + } + + private function buildReassignForm(array $periods): FormInterface + { + $defaultData = [ + 'user' => [], + 'periods' => $periods + ]; + + $builder = $this->formFactory->createBuilder(FormType::class, $defaultData, ['csrf_protection' => false, ]); + + $builder + ->add('periods', ChoiceType::class, [ + // 'data' => serialize($periods), + 'choices' => $periods, + 'multiple' => true, + 'expanded' => true ]) - ->add('periods', HiddenType::class, [ - 'data' => serialize($periodIds), + ->add('user', EntityType::class, [ + 'class' => User::class, + 'choices' => $this->userRepository->findByActive(['username' => 'ASC']), + 'choice_label' => function (User $u) { + return $this->userRender->renderString($u, []); + }, + 'placeholder' => 'Choose a user to reassign to', + 'multiple' => false, + 'label' => 'User', + 'required' => true, ]); return $builder->getForm(); diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingPeriod/reassign_list.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingPeriod/reassign_list.html.twig index 2b36a175f..201925188 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingPeriod/reassign_list.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingPeriod/reassign_list.html.twig @@ -59,15 +59,37 @@ {{ form_end(form) }} -
-

{{ 'Attribute all parcours in this list to the following users,'|trans }}

+ {% if form.user.vars.value is empty %} +

{{ 'period_by_user_list.Pick a user'|trans }}

+ {% elseif periods|length == 0 and form.user.vars.value is not empty %} +

{{ 'period_by_user_list.Any course or no authorization to see them'|trans }}

+ {% else %} +

{{ 'Attribute parcours in this list to the following user,'|trans }}

+

{{ paginator.totalItems }} parcours à réassigner (calculé ce jour à {{ null|format_time('medium') }})

+ +
+
+ {{ form_label(assignForm.user ) }} + {{ form_widget(assignForm.user, {'attr': {'class': 'select2'}}) }} +
+
+ {{ form_start(assignForm) }} -
-
- {{ form_label(assignForm.user ) }} - {{ form_widget(assignForm.user, {'attr': {'class': 'select2'}}) }} +
+
+ {% for choice in assignForm.periods.vars.choices %} +
+ + +
+ {% endfor %}
+ {% do assignForm.periods.setRendered() %}
  • @@ -77,22 +99,12 @@
{{ form_end(assignForm) }} -
- - {% if form.user.vars.value is empty %} -

{{ 'period_by_user_list.Pick a user'|trans }}

- {% elseif periods|length == 0 and form.user.vars.value is not empty %} -

{{ 'period_by_user_list.Any course or no authorization to see them'|trans }}

- - {% else %} -

{{ paginator.totalItems }} parcours à réassigner (calculé ce jour à {{ null|format_time('medium') }})

- -
+ {#
{% for period in periods %} {% include '@ChillPerson/AccompanyingPeriod/_list_item.html.twig' with {'period': period, 'recordAction': m.period_actions(period), 'itemMeta': m.period_meta(period) } %} {% endfor %} -
+
#} {% endif %} {{ chill_pagination(paginator) }}