Merge remote-tracking branch 'origin/master' into issue596_admin_user

This commit is contained in:
2022-05-13 15:08:49 +02:00
47 changed files with 916 additions and 212 deletions

View File

@@ -247,7 +247,7 @@ final class AccompanyingCourseApiController extends ApiController
}
if (null === $requestor) {
throw new BadRequestHttpException('Could not find any person or thirdparty', 0, null);
throw new BadRequestHttpException('Could not find any person or thirdparty');
}
$accompanyingPeriod->setRequestor($requestor);

View File

@@ -12,12 +12,15 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Controller;
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\CreationPersonType;
use Chill\PersonBundle\Form\PersonType;
use Chill\PersonBundle\Privacy\PrivacyEvent;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Search\SimilarPersonMatcher;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
@@ -31,8 +34,8 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function count;
use function hash;
use function implode;
@@ -248,6 +251,31 @@ final class PersonController extends AbstractController
$this->em->flush();
$this->lastPostDataReset();
$address = $form->get('address')->getData();
$addressForm = (bool) $form->get('addressForm')->getData();
if (null !== $address && $addressForm) {
$household = new Household();
$member = new HouseholdMember();
$member->setPerson($person);
$member->setStartDate(new DateTimeImmutable());
$household->addMember($member);
$household->setForceAddress($address);
$this->em->persist($member);
$this->em->persist($household);
$this->em->flush();
if ($form->get('createHousehold')->isClicked()) {
return $this->redirectToRoute('chill_person_household_members_editor', [
'persons' => [$person->getId()],
'household' => $household->getId(),
]);
}
}
if ($form->get('createPeriod')->isClicked()) {
return $this->redirectToRoute('chill_person_accompanying_course_new', [
'person_id' => [$person->getId()],

View File

@@ -12,13 +12,18 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\PickUserDynamicType;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Templating\Entity\UserRender;
use Chill\PersonBundle\Repository\AccompanyingPeriodACLAwareRepositoryInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;
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;
@@ -27,11 +32,18 @@ use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Validator\Constraints\NotIdenticalTo;
use Symfony\Component\Validator\Constraints\NotNull;
use function is_int;
class ReassignAccompanyingPeriodController extends AbstractController
{
private AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository;
private AccompanyingPeriodRepository $courseRepository;
private EntityManagerInterface $em;
private EngineInterface $engine;
private FormFactoryInterface $formFactory;
@@ -44,8 +56,17 @@ class ReassignAccompanyingPeriodController extends AbstractController
private UserRepository $userRepository;
public function __construct(AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository, UserRepository $userRepository, EngineInterface $engine, FormFactoryInterface $formFactory, PaginatorFactory $paginatorFactory, Security $security, UserRender $userRender)
{
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;
$this->formFactory = $formFactory;
@@ -53,6 +74,8 @@ class ReassignAccompanyingPeriodController extends AbstractController
$this->security = $security;
$this->userRepository = $userRepository;
$this->userRender = $userRender;
$this->courseRepository = $courseRepository;
$this->em = $em;
}
/**
@@ -68,23 +91,55 @@ class ReassignAccompanyingPeriodController extends AbstractController
$form->handleRequest($request);
$total = $this->accompanyingPeriodACLAwareRepository->countByUserOpenedAccompanyingPeriod(
$form['user']->getData()
);
$userFrom = $form['user']->getData();
$total = $this->accompanyingPeriodACLAwareRepository->countByUserOpenedAccompanyingPeriod($userFrom);
$paginator = $this->paginatorFactory->create($total);
$periods = $this->accompanyingPeriodACLAwareRepository
->findByUserOpenedAccompanyingPeriod(
$form['user']->getData(),
$userFrom,
['openingDate' => 'ASC'],
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
);
$periodIds = [];
foreach ($periods as $period) {
$periodIds[] = $period->getId();
}
// Create an array of period id's to pass into assignForm hiddenfield
$assignForm = $this->buildReassignForm($periodIds, $userFrom);
$assignForm->handleRequest($request);
if ($assignForm->isSubmitted() && $assignForm->isValid()) {
$assignPeriodIds = json_decode($assignForm->get('periods')->getData(), true);
$userTo = $assignForm->get('userTo')->getData();
$userFrom = $assignForm->get('userFrom')->getData();
foreach ($assignPeriodIds as $periodId) {
$period = $this->courseRepository->find($periodId);
if ($period->getUser() === $userFrom) {
$period->setUser($userTo);
}
}
$this->em->flush();
// redirect to the first page
return $this->redirectToRoute('chill_course_list_reassign', $request->query->all());
}
return new Response(
$this->engine->render('@ChillPerson/AccompanyingPeriod/reassign_list.html.twig', [
'assignForm' => $assignForm->createView(),
'form' => $form->createView(),
'paginator' => $paginator,
'periods' => $periods,
'form' => $form->createView(),
'userFrom' => $userFrom,
])
);
}
@@ -98,17 +153,63 @@ class ReassignAccompanyingPeriodController extends AbstractController
'method' => 'get', 'csrf_protection' => false, ]);
$builder
->add('user', EntityType::class, [
'class' => User::class,
'choices' => $this->userRepository->findByActive(['username' => 'ASC']),
'choice_label' => function (User $u) {
return $this->userRender->renderString($u, []);
},
->add('user', PickUserDynamicType::class, [
'multiple' => false,
'label' => 'User',
'label' => 'reassign.Current user',
'required' => false,
'help' => 'reassign.Choose a user and click on "Filter" to apply',
]);
return $builder->getForm();
}
private function buildReassignForm(array $periodIds, ?User $userFrom): FormInterface
{
$defaultData = [
'userFrom' => $userFrom,
'periods' => json_encode($periodIds),
];
$builder = $this->formFactory->createNamedBuilder('reassign', FormType::class, $defaultData);
if (null !== $userFrom) {
$constraints = [new NotIdenticalTo(['value' => $userFrom])];
} else {
$constraints = [];
}
$builder
->add('periods', HiddenType::class)
->add('userFrom', HiddenType::class)
->add('userTo', PickUserDynamicType::class, [
'multiple' => false,
'label' => 'reassign.Next user',
'required' => true,
'help' => 'reassign.All periods on this list will be reassigned to this user, excepted the one you manually reassigned before',
'constraints' => [new NotNull()],
]);
$builder->get('userFrom')->addModelTransformer(new CallbackTransformer(
static 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();
}
}