mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
infinite loop bug on flush
This commit is contained in:
parent
084d77c8f4
commit
12df38d32d
@ -20,6 +20,8 @@ use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
|
|||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\Form\CallbackTransformer;
|
||||||
|
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||||
use Symfony\Component\Form\FormFactoryInterface;
|
use Symfony\Component\Form\FormFactoryInterface;
|
||||||
@ -30,6 +32,7 @@ use Symfony\Component\Routing\Annotation\Route;
|
|||||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||||
use Symfony\Component\Security\Core\Security;
|
use Symfony\Component\Security\Core\Security;
|
||||||
use Symfony\Component\Templating\EngineInterface;
|
use Symfony\Component\Templating\EngineInterface;
|
||||||
|
use Symfony\Component\Validator\Constraints\NotEqualTo;
|
||||||
|
|
||||||
class ReassignAccompanyingPeriodController extends AbstractController
|
class ReassignAccompanyingPeriodController extends AbstractController
|
||||||
{
|
{
|
||||||
@ -92,70 +95,75 @@ class ReassignAccompanyingPeriodController extends AbstractController
|
|||||||
$total = $this->accompanyingPeriodACLAwareRepository->countByUserOpenedAccompanyingPeriod($userFrom);
|
$total = $this->accompanyingPeriodACLAwareRepository->countByUserOpenedAccompanyingPeriod($userFrom);
|
||||||
$paginator = $this->paginatorFactory->create($total);
|
$paginator = $this->paginatorFactory->create($total);
|
||||||
$periods = $this->accompanyingPeriodACLAwareRepository
|
$periods = $this->accompanyingPeriodACLAwareRepository
|
||||||
->findByUserOpenedAccompanyingPeriod(
|
->findByUserOpenedAccompanyingPeriod(
|
||||||
$userFrom,
|
$userFrom,
|
||||||
['openingDate' => 'ASC'],
|
['openingDate' => 'ASC'],
|
||||||
$paginator->getItemsPerPage(),
|
$paginator->getItemsPerPage(),
|
||||||
$paginator->getCurrentPageFirstItemNumber()
|
$paginator->getCurrentPageFirstItemNumber()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create an array of period id's to pass into assignForm hiddenfield
|
|
||||||
$periodIds = [];
|
$periodIds = [];
|
||||||
|
|
||||||
foreach ($periods as $period) {
|
foreach ($periods as $period) {
|
||||||
$periodIds[] = $period->getId();
|
$periodIds[] = $period->getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
$userFromId = null !== $userFrom ? $userFrom->getId() : null;
|
// Create an array of period id's to pass into assignForm hiddenfield
|
||||||
|
$assignForm = $this->buildReassignForm($periodIds, $userFrom);
|
||||||
$assignForm = $this->buildReassignForm($periodIds, $userFromId);
|
|
||||||
|
|
||||||
$assignForm->handleRequest($request);
|
$assignForm->handleRequest($request);
|
||||||
|
|
||||||
|
$userFromId = null !== $userFrom ? $userFrom->getId() : null;
|
||||||
|
dump($userFrom);
|
||||||
if ($assignForm->isSubmitted()) {
|
if ($assignForm->isSubmitted()) {
|
||||||
|
dump(' assign submitted');
|
||||||
|
|
||||||
$userFromId = (int) $assignForm->get('userFrom')->getData();
|
|
||||||
$assignPeriodIds = json_decode($assignForm->get('periods')->getData(), true);
|
if ($assignForm->isSubmitted()) {
|
||||||
$userTo = $assignForm->get('userTo')->getData();
|
dump('reassign');
|
||||||
|
|
||||||
$userFrom = $this->userRepository->find($userFromId);
|
$assignPeriodIds = json_decode($assignForm->get('periods')->getData(), true);
|
||||||
|
$userTo = $assignForm->get('userTo')->getData();
|
||||||
|
$userFrom = $assignForm->get('userFrom')->getData();
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
|
||||||
|
dump($assignPeriodIds);
|
||||||
|
foreach($assignPeriodIds as $periodId) {
|
||||||
|
$period = $this->courseRepository->find($periodId);
|
||||||
|
|
||||||
|
if ($userFrom === $period->getUser()) {
|
||||||
|
$period->setUser($userTo);
|
||||||
|
}
|
||||||
|
|
||||||
$remainingPeriods = $this->accompanyingPeriodACLAwareRepository
|
|
||||||
->findByUserOpenedAccompanyingPeriod(
|
if ($i === 10) {
|
||||||
$userFrom,
|
// dd($i);
|
||||||
['openingDate' => 'ASC'],
|
}
|
||||||
$paginator->getItemsPerPage(),
|
|
||||||
$paginator->getCurrentPageFirstItemNumber()
|
|
||||||
);
|
|
||||||
|
|
||||||
|
dump($period);
|
||||||
foreach($remainingPeriods as $period) {
|
$i++;
|
||||||
$id = $period->getId();
|
|
||||||
|
|
||||||
if (in_array($id, $assignPeriodIds)) {
|
|
||||||
$period->setUser($userTo);
|
|
||||||
$this->em->persist($period);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//$this->em->flush();
|
||||||
|
|
||||||
|
// redirect to the first page
|
||||||
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
$this->em->flush();
|
dump('no assign form');
|
||||||
|
|
||||||
return new Response(
|
|
||||||
$this->engine->render('@ChillPerson/AccompanyingPeriod/reassign_list.html.twig', [
|
|
||||||
'paginator' => $paginator,
|
|
||||||
'form' => $form->createView(),
|
|
||||||
'assignForm' => $assignForm->createView()
|
|
||||||
])
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
$this->engine->render('@ChillPerson/AccompanyingPeriod/reassign_list.html.twig', [
|
$this->engine->render('@ChillPerson/AccompanyingPeriod/reassign_list.html.twig', [
|
||||||
'paginator' => $paginator,
|
'paginator' => $paginator,
|
||||||
'periods' => $periods,
|
'periods' => $periods,
|
||||||
'form' => $form->createView(),
|
'form' => $form->createView(),
|
||||||
'assignForm' => $assignForm->createView()
|
'assignForm' => isset($assignForm) ? $assignForm->createView() : null,
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -170,7 +178,7 @@ class ReassignAccompanyingPeriodController extends AbstractController
|
|||||||
|
|
||||||
$builder
|
$builder
|
||||||
->add('user', EntityType::class, [
|
->add('user', EntityType::class, [
|
||||||
'class' => User::class,
|
'class' => User::class, // pickUserType or PickDyamicUserType
|
||||||
'choices' => $this->userRepository->findByActive(['username' => 'ASC']),
|
'choices' => $this->userRepository->findByActive(['username' => 'ASC']),
|
||||||
'choice_label' => function (User $u) {
|
'choice_label' => function (User $u) {
|
||||||
return $this->userRender->renderString($u, []);
|
return $this->userRender->renderString($u, []);
|
||||||
@ -183,27 +191,21 @@ class ReassignAccompanyingPeriodController extends AbstractController
|
|||||||
return $builder->getForm();
|
return $builder->getForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildReassignForm(array $periodIds, ?int $userFromId): FormInterface
|
private function buildReassignForm(array $periodIds, ?User $userFrom): FormInterface
|
||||||
{
|
{
|
||||||
$defaultData = [
|
$defaultData = [
|
||||||
'userFrom' => $userFromId,
|
'userFrom' => $userFrom,
|
||||||
'periods' => [],
|
'periods' => json_encode($periodIds),
|
||||||
'assignTo' => null
|
'assignTo' => null
|
||||||
];
|
];
|
||||||
|
|
||||||
$periodsJson = json_encode($periodIds);
|
$builder = $this->formFactory->createBuilder(FormType::class, $defaultData);
|
||||||
|
|
||||||
$builder = $this->formFactory->createBuilder(FormType::class, $defaultData, ['csrf_protection' => false, ]);
|
|
||||||
|
|
||||||
$builder
|
$builder
|
||||||
->add('periods', HiddenType::class, [
|
->add('periods', HiddenType::class)
|
||||||
'data' => $periodsJson,
|
->add('userFrom', HiddenType::class)
|
||||||
])
|
|
||||||
->add('userFrom', HiddenType::class, [
|
|
||||||
'data' => $userFromId
|
|
||||||
])
|
|
||||||
->add('userTo', EntityType::class, [
|
->add('userTo', EntityType::class, [
|
||||||
'class' => User::class,
|
'class' => User::class, // PickUserType
|
||||||
'choices' => $this->userRepository->findByActive(['username' => 'ASC']),
|
'choices' => $this->userRepository->findByActive(['username' => 'ASC']),
|
||||||
'choice_label' => function (User $u) {
|
'choice_label' => function (User $u) {
|
||||||
return $this->userRender->renderString($u, []);
|
return $this->userRender->renderString($u, []);
|
||||||
@ -212,9 +214,28 @@ class ReassignAccompanyingPeriodController extends AbstractController
|
|||||||
'multiple' => false,
|
'multiple' => false,
|
||||||
'label' => 'User',
|
'label' => 'User',
|
||||||
'required' => true,
|
'required' => true,
|
||||||
|
// add a constraint: userFrom is not equal to userTo
|
||||||
|
//'constraints' => NotEqualToh
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// $builder->get('periods')->resetViewTransformers();
|
$builder->get('userFrom')->addModelTransformer(new CallbackTransformer(
|
||||||
|
function (?User $user) {
|
||||||
|
if (null === $user) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return $user->getId();
|
||||||
|
},
|
||||||
|
function (?string $id) {
|
||||||
|
if (null === $id) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!is_int((int) $id)) {
|
||||||
|
throw new TransformationFailedException("the user id is not a numeric");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->userRepository->find((int) $id);
|
||||||
|
}
|
||||||
|
));
|
||||||
|
|
||||||
return $builder->getForm();
|
return $builder->getForm();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user