Feat: Implement availabilities
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Teamer\Profile\Availability;
|
||||
|
||||
use App\Entity\Availability;
|
||||
use App\Entity\User;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class AddController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route('/teamer/profile/availability/add/{id}', name: 'app_teamer_profile_availability_add')]
|
||||
#[IsGranted('ROLE_TEAMER')]
|
||||
public function index(Availability $availability): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
$teamer = $user->getTeamer();
|
||||
|
||||
$teamer->addAvailability($availability);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->logger->info('Add availability', [
|
||||
'user' => $user->getUserIdentifier(),
|
||||
'availability' => $availability,
|
||||
]);
|
||||
|
||||
return $this->redirectToRoute('app_teamer_profile_availability_index');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Teamer\Profile\Availability;
|
||||
|
||||
use App\Entity\Availability;
|
||||
use App\Entity\User;
|
||||
use App\Form\AvailabilityType;
|
||||
use App\Model\AjaxModalResponseDto;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class CreateController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route('/teamer/profile/availability/create', name: 'app_teamer_profile_availability_create')]
|
||||
#[IsGranted('ROLE_TEAMER')]
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$response = new AjaxModalResponseDto();
|
||||
$action = $this->generateUrl('app_teamer_profile_availability_create');
|
||||
|
||||
$availability = new Availability();
|
||||
|
||||
$form = $this->createForm(AvailabilityType::class, $availability, ['action' => $action, 'ajax_submit' => true]);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
$teamer = $user->getTeamer();
|
||||
$teamer
|
||||
->addAvailability($availability)
|
||||
->addCustomAvailability($availability)
|
||||
;
|
||||
|
||||
$this->entityManager->persist($availability);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Der Zeitraum wurde hinzugefügt.');
|
||||
$this->logger->info('Add availability', [
|
||||
'availability' => $availability,
|
||||
]);
|
||||
|
||||
$redirectUrl = $this->generateUrl('app_teamer_profile_availability_index');
|
||||
$response->setCloseAndRedirect($redirectUrl);
|
||||
} else {
|
||||
$content = $this->renderView('teamer/profile/availability/create.html.twig', [
|
||||
'form' => $form,
|
||||
]);
|
||||
$response->setContent($content);
|
||||
}
|
||||
|
||||
return $this->json($response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Teamer\Profile\Availability;
|
||||
|
||||
use App\Entity\Availability;
|
||||
use App\Entity\User;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class DeleteController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route('/teamer/profile/availability/delete/{id}', name: 'app_teamer_profile_availability_delete')]
|
||||
#[IsGranted('DELETE', subject: 'availability')]
|
||||
public function index(Availability $availability): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
$teamer = $user->getTeamer();
|
||||
|
||||
if (null === $availability->getOwner()) {
|
||||
$teamer->removeAvailability($availability);
|
||||
} elseif ($teamer === $availability->getOwner()) {
|
||||
$this->entityManager->remove($availability);
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Der Zeitraum wurde entfernt/gelöscht.');
|
||||
$this->logger->info('Delete availability', [
|
||||
'user' => $this->getUser()->getUserIdentifier(),
|
||||
]);
|
||||
|
||||
return $this->redirectToRoute('app_teamer_profile_availability_index');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Teamer\Profile\Availability;
|
||||
|
||||
use App\Entity\Availability;
|
||||
use App\Entity\User;
|
||||
use App\Repository\AvailabilityRepository;
|
||||
use Carbon\Carbon;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly AvailabilityRepository $availabilityRepository)
|
||||
{}
|
||||
|
||||
#[Route('/teamer/profile/availabilities', name: 'app_teamer_profile_availability_index')]
|
||||
#[IsGranted('ROLE_TEAMER')]
|
||||
public function index(): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
$teamer = $user->getTeamer();
|
||||
|
||||
$availabilities = $teamer->getAvailabilities()->toArray();
|
||||
$selectedAvailabilities = $this->groupRecords($availabilities);
|
||||
|
||||
$availabilities = $this->availabilityRepository->getSelectableForTeamer($teamer);
|
||||
$selectableAvailabilities = $this->groupRecords($availabilities);
|
||||
|
||||
return $this->render('teamer/profile/availability/index.html.twig', [
|
||||
'selectedAvailabilities' => $selectedAvailabilities,
|
||||
'selectableAvailabilities' => $selectableAvailabilities,
|
||||
]);
|
||||
}
|
||||
|
||||
private function groupRecords(array $records): array
|
||||
{
|
||||
$selectableAvailabilities = [];
|
||||
|
||||
foreach ($records as $record) {
|
||||
/** @var Availability $record */
|
||||
$dateFrom = $record->getDateFrom();
|
||||
$year = $dateFrom->format('Y');
|
||||
$month = (new Carbon($dateFrom))->locale('de_DE')->monthName;
|
||||
|
||||
if (false === isset($selectableAvailabilities[$year])) {
|
||||
$selectableAvailabilities[$year] = [];
|
||||
}
|
||||
|
||||
if (false === isset($selectableAvailabilities[$year][$month])) {
|
||||
$selectableAvailabilities[$year][$month] = [];
|
||||
}
|
||||
|
||||
$selectableAvailabilities[$year][$month][] = $record;
|
||||
}
|
||||
|
||||
return $selectableAvailabilities;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user