Feat: Add assignment filter for admins
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Common;
|
||||
|
||||
use App\Entity\Assignment;
|
||||
use App\Entity\Destination;
|
||||
use App\Entity\JobProfile;
|
||||
use App\Form\AssignmentFilterType;
|
||||
use App\Model\AssignmentFilterDto;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
|
||||
class AssignmentFilterHandler
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RequestStack $requestStack,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly FormFactoryInterface $formFactory,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getForm(): FormInterface
|
||||
{
|
||||
$filterDto = $this->getFilterSettings();
|
||||
|
||||
$assignmentRepository = $this->entityManager->getRepository(Assignment::class);
|
||||
$filterOptions = $assignmentRepository->getFilterOptions();
|
||||
|
||||
return $this->formFactory->create(AssignmentFilterType::class, $filterDto, [
|
||||
'min_date' => $filterOptions['minDate'],
|
||||
'max_date' => $filterOptions['maxDate'],
|
||||
'job_profiles' => $filterOptions['jobProfiles'],
|
||||
'destinations' => $filterOptions['destinations'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function handleRequest(FormInterface $form, Request $request): AssignmentFilterDto
|
||||
{
|
||||
$form->handleRequest($request);
|
||||
$filterDto = $form->getData();
|
||||
|
||||
if ($form->has('reset') && $form->get('reset')->isClicked()) {
|
||||
$filterDto = new AssignmentFilterDto();
|
||||
$filterDto->setReset(true);
|
||||
$this->getSession()->remove('assignment_filter');
|
||||
} elseif ($form->has('apply') && $form->get('apply')->isClicked()) {
|
||||
$this->saveFilterSettings($filterDto);
|
||||
}
|
||||
|
||||
return $filterDto;
|
||||
}
|
||||
|
||||
public function getFilterSettings(): AssignmentFilterDto
|
||||
{
|
||||
if (null === $this->getSession()->get('assignment_filter')) {
|
||||
return new AssignmentFilterDto();
|
||||
}
|
||||
|
||||
return $this->loadFilterSettings();
|
||||
}
|
||||
|
||||
public function loadFilterSettings(): AssignmentFilterDto
|
||||
{
|
||||
$data = $this->getSession()->get('assignment_filter');
|
||||
|
||||
$filterDto = new AssignmentFilterDto();
|
||||
|
||||
if (isset($data['date_from'])) {
|
||||
$filterDto->setDateFrom(new \DateTimeImmutable($data['date_from']));
|
||||
}
|
||||
if (isset($data['date_to'])) {
|
||||
$filterDto->setDateTo(new \DateTimeImmutable($data['date_to']));
|
||||
}
|
||||
if (isset($data['job_profile']) && 0 < (int) $data['job_profile']) {
|
||||
$jobProfile = $this
|
||||
->entityManager
|
||||
->getRepository(JobProfile::class)
|
||||
->find($data['job_profile'])
|
||||
;
|
||||
$filterDto->setJobProfile($jobProfile);
|
||||
}
|
||||
if (isset($data['destination']) && 0 < (int) $data['destination']) {
|
||||
$destination = $this
|
||||
->entityManager
|
||||
->getRepository(Destination::class)
|
||||
->find($data['destination'])
|
||||
;
|
||||
$filterDto->setDestination($destination);
|
||||
}
|
||||
if (isset($data['duration']) && 0 < (int) $data['duration']) {
|
||||
$filterDto->setDuration($data['duration']);
|
||||
}
|
||||
|
||||
return $filterDto;
|
||||
}
|
||||
|
||||
public function saveFilterSettings(AssignmentFilterDto $filterDto): void
|
||||
{
|
||||
$this->getSession()->set('assignment_filter', [
|
||||
'date_from' => $filterDto->getDateFrom()?->format('Y-m-d'),
|
||||
'date_to' => $filterDto->getDateTo()?->format('Y-m-d'),
|
||||
'job_profile' => $filterDto->getJobProfile()?->getId(),
|
||||
'destination' => $filterDto->getDestination()?->getId(),
|
||||
'duration' => $filterDto->getDuration(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function getSession(): SessionInterface
|
||||
{
|
||||
return $this->requestStack->getSession();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user