feat: add admin crud for resources and rooms
This commit is contained in:
1 parent
ad5d5acb00
commit
3d67942c62
19 files changed
+1059
-4
No files matched your search
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller\Admin\Resource;
|
||||||
|
|
||||||
|
use App\Entity\Resource;
|
||||||
|
use App\Enum\ResourceType;
|
||||||
|
use App\Form\Admin\ResourceFormType;
|
||||||
|
use App\Htmx\HxRedirectResponse;
|
||||||
|
use App\Security\Role;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||||
|
|
||||||
|
#[IsGranted(Role::ADMIN)]
|
||||||
|
final class CreateController extends AbstractController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $em,
|
||||||
|
private readonly LoggerInterface $logger,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET renders the modal, POST saves it. An invalid submit re-renders the modal as a 422, which
|
||||||
|
* htmx swaps in place; a valid one navigates back to the list.
|
||||||
|
*/
|
||||||
|
#[Route('/admin/resources/new/{type}', name: 'app_admin_resource_new', methods: ['GET', 'POST'])]
|
||||||
|
public function index(Request $request, ResourceType $type): Response
|
||||||
|
{
|
||||||
|
$resource = new Resource($type, '');
|
||||||
|
$form = $this->createForm(ResourceFormType::class, $resource, [
|
||||||
|
'action' => $this->generateUrl('app_admin_resource_new', ['type' => $type->value]),
|
||||||
|
]);
|
||||||
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$this->em->persist($resource);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$this->logger->info('Admin created a resource.', [
|
||||||
|
'admin' => $this->getUser()?->getUserIdentifier(),
|
||||||
|
'resource_id' => $resource->getId(),
|
||||||
|
'type' => $type->value,
|
||||||
|
'label' => $resource->getLabel(),
|
||||||
|
]);
|
||||||
|
$this->addFlash('success', \sprintf('%s angelegt.', $resource->getLabel()));
|
||||||
|
|
||||||
|
return new HxRedirectResponse($this->generateUrl('app_admin_resource'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('admin/_modal_resource.html.twig', [
|
||||||
|
'form' => $form,
|
||||||
|
'resource' => $resource,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller\Admin\Resource;
|
||||||
|
|
||||||
|
use App\Entity\Resource;
|
||||||
|
use App\Form\Admin\ResourceFormType;
|
||||||
|
use App\Htmx\HxRedirectResponse;
|
||||||
|
use App\Repository\BookingRepository;
|
||||||
|
use App\Security\Role;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renaming, moving to another room, and retiring or reactivating.
|
||||||
|
*
|
||||||
|
* Retiring is a checkbox here rather than a confirmation modal of its own: it deletes nothing and
|
||||||
|
* is undone by ticking the box again. What it does leave behind -- bookings that still point at
|
||||||
|
* the resource -- is named in the flash, so the administrator can release them from
|
||||||
|
* /admin/bookings if they should not stand.
|
||||||
|
*/
|
||||||
|
#[IsGranted(Role::ADMIN)]
|
||||||
|
final class EditController extends AbstractController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $em,
|
||||||
|
private readonly BookingRepository $bookings,
|
||||||
|
private readonly LoggerInterface $logger,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route('/admin/resources/{id}/edit', name: 'app_admin_resource_edit', methods: ['GET', 'POST'])]
|
||||||
|
public function index(Request $request, Resource $resource): Response
|
||||||
|
{
|
||||||
|
$wasActive = $resource->isActive();
|
||||||
|
|
||||||
|
$form = $this->createForm(ResourceFormType::class, $resource, [
|
||||||
|
'action' => $this->generateUrl('app_admin_resource_edit', ['id' => $resource->getId()]),
|
||||||
|
'with_active' => true,
|
||||||
|
]);
|
||||||
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$this->logger->info('Admin edited a resource.', [
|
||||||
|
'admin' => $this->getUser()?->getUserIdentifier(),
|
||||||
|
'resource_id' => $resource->getId(),
|
||||||
|
'label' => $resource->getLabel(),
|
||||||
|
'group' => $resource->getGroup()?->getLabel(),
|
||||||
|
'active' => $resource->isActive(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$remaining = $wasActive && !$resource->isActive()
|
||||||
|
? $this->bookings->countFromDate($resource, new \DateTimeImmutable('today'))
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
if ($remaining > 0) {
|
||||||
|
$this->addFlash('warning', \sprintf(
|
||||||
|
'%s ist nicht mehr buchbar. %d bestehende Buchungen ab heute bleiben erhalten.',
|
||||||
|
$resource->getLabel(),
|
||||||
|
$remaining,
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
$this->addFlash('success', \sprintf('%s gespeichert.', $resource->getLabel()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HxRedirectResponse($this->generateUrl('app_admin_resource'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('admin/_modal_resource.html.twig', [
|
||||||
|
'form' => $form,
|
||||||
|
'resource' => $resource,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller\Admin\Resource;
|
||||||
|
|
||||||
|
use App\Enum\ResourceType;
|
||||||
|
use App\Repository\ResourceGroupRepository;
|
||||||
|
use App\Repository\ResourceRepository;
|
||||||
|
use App\Security\Role;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Everything that can be booked, per type and room, retired resources included.
|
||||||
|
*
|
||||||
|
* Creating and editing happen in modals that navigate back here once saved. Nothing is ever
|
||||||
|
* deleted except an empty room: a resource with history is retired instead, so its bookings keep
|
||||||
|
* resolving.
|
||||||
|
*/
|
||||||
|
#[IsGranted(Role::ADMIN)]
|
||||||
|
final class IndexController extends AbstractController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly ResourceRepository $resources,
|
||||||
|
private readonly ResourceGroupRepository $groups,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route('/admin/resources', name: 'app_admin_resource', methods: ['GET'])]
|
||||||
|
public function index(): Response
|
||||||
|
{
|
||||||
|
$sections = [];
|
||||||
|
|
||||||
|
foreach (ResourceType::cases() as $type) {
|
||||||
|
$byGroup = [];
|
||||||
|
$ungrouped = [];
|
||||||
|
|
||||||
|
foreach ($this->resources->findAllByType($type) as $resource) {
|
||||||
|
if (null === $resource->getGroup()) {
|
||||||
|
$ungrouped[] = $resource;
|
||||||
|
} else {
|
||||||
|
$byGroup[$resource->getGroup()->getId()][] = $resource;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$sections[] = [
|
||||||
|
'type' => $type,
|
||||||
|
// Every group, empty ones too -- an empty room is the only thing that can be
|
||||||
|
// deleted, so it has to be visible.
|
||||||
|
'groups' => array_map(
|
||||||
|
static fn ($group): array => ['group' => $group, 'resources' => $byGroup[$group->getId()] ?? []],
|
||||||
|
$this->groups->findByType($type),
|
||||||
|
),
|
||||||
|
'ungrouped' => $ungrouped,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('admin/resources.html.twig', [
|
||||||
|
'sections' => $sections,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller\Admin\ResourceGroup;
|
||||||
|
|
||||||
|
use App\Entity\ResourceGroup;
|
||||||
|
use App\Enum\ResourceType;
|
||||||
|
use App\Form\Admin\ResourceGroupFormType;
|
||||||
|
use App\Htmx\HxRedirectResponse;
|
||||||
|
use App\Repository\ResourceGroupRepository;
|
||||||
|
use App\Security\Role;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||||
|
|
||||||
|
#[IsGranted(Role::ADMIN)]
|
||||||
|
final class CreateController extends AbstractController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $em,
|
||||||
|
private readonly ResourceGroupRepository $groups,
|
||||||
|
private readonly LoggerInterface $logger,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route('/admin/resource-groups/new/{type}', name: 'app_admin_resource_group_new', methods: ['GET', 'POST'])]
|
||||||
|
public function index(Request $request, ResourceType $type): Response
|
||||||
|
{
|
||||||
|
// Prefilled to come after the existing ones, which is where a new room usually belongs.
|
||||||
|
$existing = $this->groups->findByType($type);
|
||||||
|
$position = [] === $existing ? 0 : max(array_map(static fn (ResourceGroup $group): int => $group->getPosition(), $existing)) + 1;
|
||||||
|
|
||||||
|
$group = new ResourceGroup($type, '', $position);
|
||||||
|
$form = $this->createForm(ResourceGroupFormType::class, $group, [
|
||||||
|
'action' => $this->generateUrl('app_admin_resource_group_new', ['type' => $type->value]),
|
||||||
|
]);
|
||||||
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$this->em->persist($group);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$this->logger->info('Admin created a resource group.', [
|
||||||
|
'admin' => $this->getUser()?->getUserIdentifier(),
|
||||||
|
'group_id' => $group->getId(),
|
||||||
|
'type' => $type->value,
|
||||||
|
'label' => $group->getLabel(),
|
||||||
|
]);
|
||||||
|
$this->addFlash('success', \sprintf('%s angelegt.', $group->getLabel()));
|
||||||
|
|
||||||
|
return new HxRedirectResponse($this->generateUrl('app_admin_resource'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('admin/_modal_resource_group.html.twig', [
|
||||||
|
'form' => $form,
|
||||||
|
'group' => $group,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller\Admin\ResourceGroup;
|
||||||
|
|
||||||
|
use App\Controller\Traits\ConfirmedActionTrait;
|
||||||
|
use App\Entity\ResourceGroup;
|
||||||
|
use App\Htmx\HxRedirectResponse;
|
||||||
|
use App\Repository\ResourceRepository;
|
||||||
|
use App\Security\Role;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only an empty room can be deleted -- retired resources count, since they still sit in it.
|
||||||
|
*
|
||||||
|
* The foreign key would allow more (ON DELETE SET NULL), but silently turning a room's desks into
|
||||||
|
* ungrouped ones is never what deleting a room means. Move them first.
|
||||||
|
*/
|
||||||
|
#[IsGranted(Role::ADMIN)]
|
||||||
|
final class DeleteController extends AbstractController
|
||||||
|
{
|
||||||
|
use ConfirmedActionTrait;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $em,
|
||||||
|
private readonly ResourceRepository $resources,
|
||||||
|
private readonly LoggerInterface $logger,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route('/admin/resource-groups/{id}/delete', name: 'app_admin_resource_group_delete', methods: ['GET', 'POST'])]
|
||||||
|
public function index(Request $request, ResourceGroup $group): Response
|
||||||
|
{
|
||||||
|
if ($this->resources->count(['group' => $group]) > 0) {
|
||||||
|
throw $this->createAccessDeniedException('Only an empty resource group can be deleted.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$request->isMethod(Request::METHOD_POST)) {
|
||||||
|
return $this->render('admin/_modal_resource_group_delete.html.twig', [
|
||||||
|
'group' => $group,
|
||||||
|
'csrf_token_id' => 'delete-resource-group-'.$group->getId(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->denyUnlessCsrfValid($request, 'delete-resource-group-'.$group->getId());
|
||||||
|
|
||||||
|
$this->logger->info('Admin deleted a resource group.', [
|
||||||
|
'admin' => $this->getUser()?->getUserIdentifier(),
|
||||||
|
'group_id' => $group->getId(),
|
||||||
|
'label' => $group->getLabel(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$label = $group->getLabel();
|
||||||
|
$this->em->remove($group);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$this->addFlash('success', \sprintf('%s gelöscht.', $label));
|
||||||
|
|
||||||
|
return new HxRedirectResponse($this->generateUrl('app_admin_resource'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller\Admin\ResourceGroup;
|
||||||
|
|
||||||
|
use App\Entity\ResourceGroup;
|
||||||
|
use App\Form\Admin\ResourceGroupFormType;
|
||||||
|
use App\Htmx\HxRedirectResponse;
|
||||||
|
use App\Security\Role;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||||
|
|
||||||
|
#[IsGranted(Role::ADMIN)]
|
||||||
|
final class EditController extends AbstractController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $em,
|
||||||
|
private readonly LoggerInterface $logger,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route('/admin/resource-groups/{id}/edit', name: 'app_admin_resource_group_edit', methods: ['GET', 'POST'])]
|
||||||
|
public function index(Request $request, ResourceGroup $group): Response
|
||||||
|
{
|
||||||
|
$form = $this->createForm(ResourceGroupFormType::class, $group, [
|
||||||
|
'action' => $this->generateUrl('app_admin_resource_group_edit', ['id' => $group->getId()]),
|
||||||
|
]);
|
||||||
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$this->logger->info('Admin edited a resource group.', [
|
||||||
|
'admin' => $this->getUser()?->getUserIdentifier(),
|
||||||
|
'group_id' => $group->getId(),
|
||||||
|
'label' => $group->getLabel(),
|
||||||
|
'position' => $group->getPosition(),
|
||||||
|
]);
|
||||||
|
$this->addFlash('success', \sprintf('%s gespeichert.', $group->getLabel()));
|
||||||
|
|
||||||
|
return new HxRedirectResponse($this->generateUrl('app_admin_resource'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('admin/_modal_resource_group.html.twig', [
|
||||||
|
'form' => $form,
|
||||||
|
'group' => $group,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ namespace App\Entity;
|
|||||||
use App\Enum\ResourceType;
|
use App\Enum\ResourceType;
|
||||||
use App\Repository\ResourceRepository;
|
use App\Repository\ResourceRepository;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||||
use Symfony\Component\Validator\Constraints as Assert;
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -14,6 +15,7 @@ use Symfony\Component\Validator\Constraints as Assert;
|
|||||||
*/
|
*/
|
||||||
#[ORM\Entity(repositoryClass: ResourceRepository::class)]
|
#[ORM\Entity(repositoryClass: ResourceRepository::class)]
|
||||||
#[ORM\UniqueConstraint(name: 'uniq_resource_label', columns: ['type', 'label'])]
|
#[ORM\UniqueConstraint(name: 'uniq_resource_label', columns: ['type', 'label'])]
|
||||||
|
#[UniqueEntity(fields: ['type', 'label'], message: 'Diese Bezeichnung ist bereits vergeben.', errorPath: 'label')]
|
||||||
class Resource
|
class Resource
|
||||||
{
|
{
|
||||||
#[ORM\Id]
|
#[ORM\Id]
|
||||||
@@ -26,6 +28,7 @@ class Resource
|
|||||||
|
|
||||||
#[ORM\Column(length: 50)]
|
#[ORM\Column(length: 50)]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank]
|
||||||
|
#[Assert\Length(max: 50)]
|
||||||
private string $label;
|
private string $label;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ namespace App\Entity;
|
|||||||
use App\Enum\ResourceType;
|
use App\Enum\ResourceType;
|
||||||
use App\Repository\ResourceGroupRepository;
|
use App\Repository\ResourceGroupRepository;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||||
use Symfony\Component\Validator\Constraints as Assert;
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Where a bookable thing physically is: a room of the office, an area of the car park.
|
* Where a bookable thing physically is: a room of the office, an area of the car park.
|
||||||
*
|
*
|
||||||
* A group belongs to exactly one ResourceType, because "Raum Nord" means nothing for parking.
|
* A group belongs to exactly one ResourceType, because "Raum Nord" means nothing for parking.
|
||||||
* It is a description of the building, so it is configuration seeded alongside the resources
|
* It is a description of the building, maintained by an administrator under /admin/resources.
|
||||||
* rather than something a user creates.
|
|
||||||
*
|
*
|
||||||
* Note there is deliberately no method named `group`, `label` or `position` other than the plain
|
* Note there is deliberately no method named `group`, `label` or `position` other than the plain
|
||||||
* accessors: Twig resolves `x.label` to a method of that literal name before trying getLabel(),
|
* accessors: Twig resolves `x.label` to a method of that literal name before trying getLabel(),
|
||||||
@@ -22,6 +22,7 @@ use Symfony\Component\Validator\Constraints as Assert;
|
|||||||
*/
|
*/
|
||||||
#[ORM\Entity(repositoryClass: ResourceGroupRepository::class)]
|
#[ORM\Entity(repositoryClass: ResourceGroupRepository::class)]
|
||||||
#[ORM\UniqueConstraint(name: 'uniq_resource_group_label', columns: ['type', 'label'])]
|
#[ORM\UniqueConstraint(name: 'uniq_resource_group_label', columns: ['type', 'label'])]
|
||||||
|
#[UniqueEntity(fields: ['type', 'label'], message: 'Diese Bezeichnung ist bereits vergeben.', errorPath: 'label')]
|
||||||
class ResourceGroup
|
class ResourceGroup
|
||||||
{
|
{
|
||||||
#[ORM\Id]
|
#[ORM\Id]
|
||||||
@@ -34,6 +35,7 @@ class ResourceGroup
|
|||||||
|
|
||||||
#[ORM\Column(length: 50)]
|
#[ORM\Column(length: 50)]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank]
|
||||||
|
#[Assert\Length(max: 50)]
|
||||||
private string $label;
|
private string $label;
|
||||||
|
|
||||||
/** Draw and list order within the type. */
|
/** Draw and list order within the type. */
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ namespace App\Enum;
|
|||||||
* The kinds of thing that can be booked.
|
* The kinds of thing that can be booked.
|
||||||
*
|
*
|
||||||
* **This enum is the extension point.** The dashboard builds one tile per case, the board route
|
* **This enum is the extension point.** The dashboard builds one tile per case, the board route
|
||||||
* accepts one backing value per case, and the floor plan renders every kind the same way.
|
* accepts one backing value per case, and the floor plan and the admin screen render every kind
|
||||||
* Adding a third bookable kind is therefore: a case here and rows in `app:seed`.
|
* the same way. Adding a third bookable kind is therefore: a case here.
|
||||||
* Nothing else needs editing — in particular no template lists the kinds by hand.
|
* Nothing else needs editing — in particular no template lists the kinds by hand.
|
||||||
*
|
*
|
||||||
* The backing value is both what is persisted in `resource.type` and what appears in a URL, so
|
* The backing value is both what is persisted in `resource.type` and what appears in a URL, so
|
||||||
@@ -45,6 +45,17 @@ enum ResourceType: string
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What a ResourceGroup of this kind is called: an office has rooms, a car park has areas.
|
||||||
|
*/
|
||||||
|
public function groupLabel(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::DESK => 'Raum',
|
||||||
|
self::PARKING => 'Bereich',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public function icon(): string
|
public function icon(): string
|
||||||
{
|
{
|
||||||
return match ($this) {
|
return match ($this) {
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Form\Admin;
|
||||||
|
|
||||||
|
use App\Entity\Resource;
|
||||||
|
use App\Entity\ResourceGroup;
|
||||||
|
use App\Enum\ResourceType;
|
||||||
|
use Doctrine\ORM\EntityRepository;
|
||||||
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A desk or parking spot. The type is not a field: it is fixed by the route that creates the
|
||||||
|
* resource and never changes afterwards, which is what lets the group choices be limited to it.
|
||||||
|
*/
|
||||||
|
final class ResourceFormType extends AbstractType
|
||||||
|
{
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||||
|
{
|
||||||
|
$resource = $builder->getData();
|
||||||
|
\assert($resource instanceof Resource);
|
||||||
|
$type = $resource->getType();
|
||||||
|
|
||||||
|
$builder
|
||||||
|
->add('label', TextType::class, [
|
||||||
|
'label' => 'Bezeichnung',
|
||||||
|
])
|
||||||
|
->add('group', EntityType::class, [
|
||||||
|
'label' => $type->groupLabel(),
|
||||||
|
'class' => ResourceGroup::class,
|
||||||
|
'required' => false,
|
||||||
|
'placeholder' => 'Ohne '.$type->groupLabel(),
|
||||||
|
'choice_label' => 'label',
|
||||||
|
// A group of another type would be tampering, and the choice list rejects it.
|
||||||
|
'query_builder' => static fn (EntityRepository $repository) => $repository
|
||||||
|
->createQueryBuilder('g')
|
||||||
|
->andWhere('g.type = :type')
|
||||||
|
->setParameter('type', $type)
|
||||||
|
->orderBy('g.position', 'ASC')
|
||||||
|
->addOrderBy('g.label', 'ASC'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Only on edit: a resource is created bookable, and retiring it is a later decision.
|
||||||
|
if ($options['with_active']) {
|
||||||
|
$builder->add('active', CheckboxType::class, [
|
||||||
|
'label' => 'Buchbar',
|
||||||
|
'help' => 'Nicht buchbare Plätze erscheinen nicht mehr im Plan. Bestehende Buchungen bleiben erhalten.',
|
||||||
|
'required' => false,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function configureOptions(OptionsResolver $resolver): void
|
||||||
|
{
|
||||||
|
$resolver->setDefaults([
|
||||||
|
'data_class' => Resource::class,
|
||||||
|
'with_active' => false,
|
||||||
|
]);
|
||||||
|
$resolver->setAllowedTypes('with_active', 'bool');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Form\Admin;
|
||||||
|
|
||||||
|
use App\Entity\ResourceGroup;
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A room or area. Like a resource, its type is fixed by the route that creates it.
|
||||||
|
*/
|
||||||
|
final class ResourceGroupFormType extends AbstractType
|
||||||
|
{
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||||
|
{
|
||||||
|
$builder
|
||||||
|
->add('label', TextType::class, [
|
||||||
|
'label' => 'Bezeichnung',
|
||||||
|
])
|
||||||
|
->add('position', IntegerType::class, [
|
||||||
|
'label' => 'Reihenfolge',
|
||||||
|
'help' => 'Kleinere Zahlen stehen im Plan weiter oben.',
|
||||||
|
// The setter takes an int, so a cleared field must not reach it as null.
|
||||||
|
'empty_data' => '0',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function configureOptions(OptionsResolver $resolver): void
|
||||||
|
{
|
||||||
|
$resolver->setDefaults([
|
||||||
|
'data_class' => ResourceGroup::class,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -205,6 +205,21 @@ class BookingRepository extends ServiceEntityRepository
|
|||||||
return $qb->getQuery()->getResult();
|
return $qb->getQuery()->getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bookings from a day onwards, so retiring a resource can say what it leaves behind.
|
||||||
|
*/
|
||||||
|
public function countFromDate(Resource $resource, \DateTimeImmutable $from): int
|
||||||
|
{
|
||||||
|
return (int) $this->createQueryBuilder('b')
|
||||||
|
->select('COUNT(b.id)')
|
||||||
|
->andWhere('b.resource = :resource')
|
||||||
|
->andWhere('b.date >= :from')
|
||||||
|
->setParameter('resource', $resource)
|
||||||
|
->setParameter('from', $from->setTime(0, 0))
|
||||||
|
->getQuery()
|
||||||
|
->getSingleScalarResult();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The occurrences an "end this series" would remove.
|
* The occurrences an "end this series" would remove.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -42,6 +42,23 @@ class ResourceRepository extends ServiceEntityRepository
|
|||||||
->getResult();
|
->getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retired ones included, for the admin screen -- which is where they are reactivated.
|
||||||
|
*
|
||||||
|
* @return list<Resource>
|
||||||
|
*/
|
||||||
|
public function findAllByType(ResourceType $type): array
|
||||||
|
{
|
||||||
|
return $this->createQueryBuilder('r')
|
||||||
|
->leftJoin('r.group', 'g')
|
||||||
|
->addSelect('g')
|
||||||
|
->andWhere('r.type = :type')
|
||||||
|
->setParameter('type', $type)
|
||||||
|
->orderBy('r.label', 'ASC')
|
||||||
|
->getQuery()
|
||||||
|
->getResult();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return list<Resource>
|
* @return list<Resource>
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
{#
|
||||||
|
Creating and editing a desk or parking spot. Same double duty as the booking modal: the form
|
||||||
|
targets the modal itself, so an invalid submit (422) replaces it with the errors showing, and a
|
||||||
|
valid one answers with HX-Redirect back to the list.
|
||||||
|
#}
|
||||||
|
{% extends 'htmx_modal.html.twig' %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
{%- if resource.id -%}
|
||||||
|
{{ resource.label }} bearbeiten
|
||||||
|
{%- else -%}
|
||||||
|
{{ resource.type.label }} hinzufügen
|
||||||
|
{%- endif -%}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{{ form_start(form, { attr: {
|
||||||
|
'hx-post': form.vars.action,
|
||||||
|
'hx-target': '#htmx-modal',
|
||||||
|
'hx-swap': 'outerHTML',
|
||||||
|
} }) }}
|
||||||
|
{{ form_errors(form) }}
|
||||||
|
|
||||||
|
<div class="space-y-4">
|
||||||
|
{{ form_row(form.label) }}
|
||||||
|
{{ form_row(form.group) }}
|
||||||
|
{% if form.active is defined %}
|
||||||
|
{{ form_row(form.active) }}
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex justify-end gap-2 pt-6">
|
||||||
|
<button type="button" class="button button--neutral" {{ stimulus_action('modal', 'close') }}>
|
||||||
|
Abbrechen
|
||||||
|
</button>
|
||||||
|
<button type="submit" class="button button--primary">
|
||||||
|
Speichern
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{{ form_end(form) }}
|
||||||
|
{% endblock %}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
{# Creating and editing a room or area. See admin/_modal_resource.html.twig for how it closes. #}
|
||||||
|
{% extends 'htmx_modal.html.twig' %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
{%- if group.id -%}
|
||||||
|
{{ group.label }} bearbeiten
|
||||||
|
{%- else -%}
|
||||||
|
{{ group.type.groupLabel }} hinzufügen
|
||||||
|
{%- endif -%}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{{ form_start(form, { attr: {
|
||||||
|
'hx-post': form.vars.action,
|
||||||
|
'hx-target': '#htmx-modal',
|
||||||
|
'hx-swap': 'outerHTML',
|
||||||
|
} }) }}
|
||||||
|
{{ form_errors(form) }}
|
||||||
|
|
||||||
|
<div class="space-y-4">
|
||||||
|
{{ form_row(form.label) }}
|
||||||
|
{{ form_row(form.position) }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex justify-end gap-2 pt-6">
|
||||||
|
<button type="button" class="button button--neutral" {{ stimulus_action('modal', 'close') }}>
|
||||||
|
Abbrechen
|
||||||
|
</button>
|
||||||
|
<button type="submit" class="button button--primary">
|
||||||
|
Speichern
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{{ form_end(form) }}
|
||||||
|
{% endblock %}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{% extends 'htmx_confirmation_modal.html.twig' %}
|
||||||
|
|
||||||
|
{% block title %}{{ group.type.groupLabel }} löschen?{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<p>
|
||||||
|
{{ group.type.groupLabel }} <em>{{ group.label }}</em> wird gelöscht.
|
||||||
|
</p>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block button_confirm %}Löschen{% endblock %}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
{#
|
||||||
|
Everything bookable, per type and room. Every write opens a modal via htmx and navigates back
|
||||||
|
here once saved -- the same shape as the sibling apps' admin lists.
|
||||||
|
|
||||||
|
Retired resources stay listed, muted, because this is the only place they can be reactivated.
|
||||||
|
Only an empty room offers "Löschen"; DeleteController refuses the rest regardless.
|
||||||
|
#}
|
||||||
|
{% extends 'layout.html.twig' %}
|
||||||
|
|
||||||
|
{% block title %}Plätze & Räume · ephub{% endblock %}
|
||||||
|
|
||||||
|
{% macro opener(url, label, variant = 'neutral') %}
|
||||||
|
<button type="button" class="button button--small button--{{ variant }}"
|
||||||
|
hx-get="{{ url }}"
|
||||||
|
hx-target="body"
|
||||||
|
hx-swap="beforeend">{{ label }}</button>
|
||||||
|
{% endmacro %}
|
||||||
|
|
||||||
|
{% macro resource_list(resources) %}
|
||||||
|
{% if resources is empty %}
|
||||||
|
<p class="py-3 text-sm text-gray-500">Noch nichts angelegt.</p>
|
||||||
|
{% else %}
|
||||||
|
<ul class="divide-y divide-gray-200 border-y border-gray-200">
|
||||||
|
{% for resource in resources %}
|
||||||
|
<li class="flex items-center justify-between gap-4 py-2" data-resource>
|
||||||
|
<span class="{{ resource.active ? '' : 'text-gray-400' }}">
|
||||||
|
{{ resource.label }}
|
||||||
|
{% if not resource.active %}
|
||||||
|
<span class="badge bg-gray-100 text-gray-600 ml-1">nicht buchbar</span>
|
||||||
|
{% endif %}
|
||||||
|
</span>
|
||||||
|
{{ _self.opener(path('app_admin_resource_edit', { id: resource.id }), 'Bearbeiten') }}
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% endif %}
|
||||||
|
{% endmacro %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<a class="text-sm text-gray-500 hover:text-gray-900" href="{{ path('app_dashboard') }}">← Übersicht</a>
|
||||||
|
|
||||||
|
<h1 class="pt-2">Plätze & Räume</h1>
|
||||||
|
<p class="subline">Was gebucht werden kann und wo es steht.</p>
|
||||||
|
|
||||||
|
{% for section in sections %}
|
||||||
|
{% set type = section.type %}
|
||||||
|
|
||||||
|
<section class="pt-8" data-type="{{ type.value }}">
|
||||||
|
<div class="flex flex-wrap items-baseline justify-between gap-2">
|
||||||
|
<h2>{{ type.plural }}</h2>
|
||||||
|
<div class="flex gap-2">
|
||||||
|
{{ _self.opener(path('app_admin_resource_group_new', { type: type.value }), type.groupLabel ~ ' hinzufügen') }}
|
||||||
|
{{ _self.opener(path('app_admin_resource_new', { type: type.value }), type.label ~ ' hinzufügen', 'primary') }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% for entry in section.groups %}
|
||||||
|
<div class="pt-4" data-group>
|
||||||
|
<div class="flex flex-wrap items-center justify-between gap-2 pb-1">
|
||||||
|
<h3 class="!mb-0 text-base md:text-base">
|
||||||
|
{{ entry.group.label }}
|
||||||
|
</h3>
|
||||||
|
<div class="flex gap-2">
|
||||||
|
{{ _self.opener(path('app_admin_resource_group_edit', { id: entry.group.id }), 'Bearbeiten') }}
|
||||||
|
{% if entry.resources is empty %}
|
||||||
|
{{ _self.opener(path('app_admin_resource_group_delete', { id: entry.group.id }), 'Löschen') }}
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{{ _self.resource_list(entry.resources) }}
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
{# Only when there is something to show, or when there are no groups at all -- then this
|
||||||
|
is simply the list, as for the car park. #}
|
||||||
|
{% if section.ungrouped is not empty or section.groups is empty %}
|
||||||
|
<div class="pt-4" data-group>
|
||||||
|
{% if section.groups is not empty %}
|
||||||
|
<h3 class="!mb-0 pb-1 text-base md:text-base">Ohne {{ type.groupLabel }}</h3>
|
||||||
|
{% endif %}
|
||||||
|
{{ _self.resource_list(section.ungrouped) }}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
</section>
|
||||||
|
{% endfor %}
|
||||||
|
{% endblock %}
|
||||||
@@ -63,6 +63,17 @@
|
|||||||
Buchungen aller Mitarbeitenden einsehen und ggf. freigeben.
|
Buchungen aller Mitarbeitenden einsehen und ggf. freigeben.
|
||||||
</p>
|
</p>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
<a href="{{ path('app_admin_resource') }}"
|
||||||
|
class="group flex flex-col rounded-md border border-gray-200 p-6 hover:border-primary hover:bg-gray-50">
|
||||||
|
<div class="flex items-center gap-3 pb-2">
|
||||||
|
{{ ux_icon('lucide:layout-grid', { class: 'h-6 w-6 shrink-0 text-primary' }) }}
|
||||||
|
<span class="text-lg font-bold">Plätze & Räume</span>
|
||||||
|
</div>
|
||||||
|
<p class="text-sm text-gray-500 pb-4">
|
||||||
|
Schreibtische, Parkplätze und Räume anlegen und bearbeiten.
|
||||||
|
</p>
|
||||||
|
</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,322 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Tests\Controller\Admin;
|
||||||
|
|
||||||
|
use App\Entity\Booking;
|
||||||
|
use App\Entity\Resource;
|
||||||
|
use App\Entity\ResourceGroup;
|
||||||
|
use App\Entity\User;
|
||||||
|
use App\Enum\ResourceType;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maintaining what can be booked: resources and the rooms they sit in.
|
||||||
|
*/
|
||||||
|
class ResourceAdminControllerTest extends WebTestCase
|
||||||
|
{
|
||||||
|
private KernelBrowser $client;
|
||||||
|
private EntityManagerInterface $em;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->client = self::createClient();
|
||||||
|
$this->client->disableReboot();
|
||||||
|
$this->em = self::getContainer()->get(EntityManagerInterface::class);
|
||||||
|
$this->em->getConnection()->beginTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
$this->em->getConnection()->rollBack();
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAnEmployeeCannotReachIt(): void
|
||||||
|
{
|
||||||
|
$this->client->loginUser($this->user('[email protected]'));
|
||||||
|
|
||||||
|
$this->client->request('GET', '/admin/resources');
|
||||||
|
self::assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
|
||||||
|
|
||||||
|
// Not as htmx: an htmx request gets the error modal instead of the bare status.
|
||||||
|
$this->client->request('GET', '/admin/resources/new/desk');
|
||||||
|
self::assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTheListShowsRoomsAndRetiredResources(): void
|
||||||
|
{
|
||||||
|
$room = $this->group('Raum Nord');
|
||||||
|
$this->resource('Schreibtisch 01', $room);
|
||||||
|
$this->resource('Schreibtisch 02', $room)->setActive(false);
|
||||||
|
$this->em->flush();
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$crawler = $this->client->request('GET', '/admin/resources');
|
||||||
|
|
||||||
|
self::assertResponseIsSuccessful();
|
||||||
|
$desks = $crawler->filter('[data-type="desk"]')->text();
|
||||||
|
self::assertStringContainsString('Raum Nord', $desks);
|
||||||
|
self::assertStringContainsString('Schreibtisch 01', $desks);
|
||||||
|
self::assertStringContainsString('Schreibtisch 02 nicht buchbar', preg_replace('/\s+/', ' ', $desks) ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreatingAResourceInARoom(): void
|
||||||
|
{
|
||||||
|
$room = $this->group('Raum Nord');
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$response = $this->submit('/admin/resources/new/desk', 'resource_form', ['label' => 'Schreibtisch 13', 'group' => (string) $room->getId()]);
|
||||||
|
|
||||||
|
self::assertSame('/admin/resources', $response->headers->get('HX-Redirect'));
|
||||||
|
$created = $this->em->getRepository(Resource::class)->findOneBy(['label' => 'Schreibtisch 13']);
|
||||||
|
self::assertNotNull($created);
|
||||||
|
self::assertSame(ResourceType::DESK, $created->getType());
|
||||||
|
self::assertSame($room->getId(), $created->getGroup()?->getId());
|
||||||
|
self::assertTrue($created->isActive());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testADuplicateLabelIsRejectedInTheModal(): void
|
||||||
|
{
|
||||||
|
$this->resource('Schreibtisch 01');
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$response = $this->submit('/admin/resources/new/desk', 'resource_form', ['label' => 'Schreibtisch 01']);
|
||||||
|
|
||||||
|
self::assertSame(Response::HTTP_UNPROCESSABLE_ENTITY, $response->getStatusCode());
|
||||||
|
self::assertStringContainsString('bereits vergeben', (string) $response->getContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The same label is fine for another type -- uniqueness is per type, like the database's.
|
||||||
|
*/
|
||||||
|
public function testTheSameLabelMayExistForAnotherType(): void
|
||||||
|
{
|
||||||
|
$this->resource('A1');
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$response = $this->submit('/admin/resources/new/parking', 'resource_form', ['label' => 'A1']);
|
||||||
|
|
||||||
|
self::assertSame('/admin/resources', $response->headers->get('HX-Redirect'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The group choices are limited to the resource's type, so a parking area cannot be forced
|
||||||
|
* onto a desk by editing the request.
|
||||||
|
*/
|
||||||
|
public function testARoomOfAnotherTypeIsRejected(): void
|
||||||
|
{
|
||||||
|
$area = $this->group('Tiefgarage', ResourceType::PARKING);
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$response = $this->submit('/admin/resources/new/desk', 'resource_form', ['label' => 'Schreibtisch 13', 'group' => (string) $area->getId()]);
|
||||||
|
|
||||||
|
self::assertSame(Response::HTTP_UNPROCESSABLE_ENTITY, $response->getStatusCode());
|
||||||
|
self::assertNull($this->em->getRepository(Resource::class)->findOneBy(['label' => 'Schreibtisch 13']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAnUnknownTypeIsNotFound(): void
|
||||||
|
{
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$this->client->request('GET', '/admin/resources/new/boat');
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retiring keeps the bookings and says how many are left, and the resource leaves the plan.
|
||||||
|
*/
|
||||||
|
public function testRetiringAResourceWithUpcomingBookings(): void
|
||||||
|
{
|
||||||
|
$desk = $this->resource('Schreibtisch 01');
|
||||||
|
$this->em->persist(new Booking($this->user('[email protected]'), $desk, new \DateTimeImmutable('+1 day')));
|
||||||
|
$this->em->flush();
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
// An unticked checkbox is simply absent from the submit.
|
||||||
|
$response = $this->submit(\sprintf('/admin/resources/%d/edit', $desk->getId()), 'resource_form', ['label' => 'Schreibtisch 01']);
|
||||||
|
|
||||||
|
self::assertSame('/admin/resources', $response->headers->get('HX-Redirect'));
|
||||||
|
$desk = $this->reload($desk);
|
||||||
|
self::assertFalse($desk->isActive());
|
||||||
|
self::assertCount(1, $this->em->getRepository(Booking::class)->findBy(['resource' => $desk]));
|
||||||
|
|
||||||
|
$this->client->request('GET', '/admin/resources');
|
||||||
|
self::assertStringContainsString('1 bestehende Buchungen ab heute bleiben erhalten', (string) $this->client->getResponse()->getContent());
|
||||||
|
|
||||||
|
$board = $this->client->request('GET', '/book/desk');
|
||||||
|
self::assertCount(0, $board->filter('[data-spot]'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReactivatingAResource(): void
|
||||||
|
{
|
||||||
|
$desk = $this->resource('Schreibtisch 01');
|
||||||
|
$desk->setActive(false);
|
||||||
|
$this->em->flush();
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$this->submit(\sprintf('/admin/resources/%d/edit', $desk->getId()), 'resource_form', ['label' => 'Schreibtisch 01', 'active' => '1']);
|
||||||
|
|
||||||
|
self::assertTrue($this->reload($desk)->isActive());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testANewRoomIsPrefilledToComeLast(): void
|
||||||
|
{
|
||||||
|
$this->group('Raum Nord', position: 0);
|
||||||
|
$this->group('Raum Süd', position: 4);
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$crawler = $this->client->request('GET', '/admin/resource-groups/new/desk', server: ['HTTP_HX-Request' => 'true']);
|
||||||
|
|
||||||
|
self::assertSame('5', $crawler->filter('#resource_group_form_position')->attr('value'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreatingAndRenamingARoom(): void
|
||||||
|
{
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$response = $this->submit('/admin/resource-groups/new/desk', 'resource_group_form', ['label' => 'Raum West', 'position' => '2']);
|
||||||
|
self::assertSame('/admin/resources', $response->headers->get('HX-Redirect'));
|
||||||
|
|
||||||
|
$room = $this->em->getRepository(ResourceGroup::class)->findOneBy(['label' => 'Raum West']);
|
||||||
|
self::assertNotNull($room);
|
||||||
|
self::assertSame(ResourceType::DESK, $room->getType());
|
||||||
|
|
||||||
|
$this->submit(\sprintf('/admin/resource-groups/%d/edit', $room->getId()), 'resource_group_form', ['label' => 'Raum Ost', 'position' => '1']);
|
||||||
|
|
||||||
|
$room = $this->reload($room);
|
||||||
|
self::assertSame('Raum Ost', $room->getLabel());
|
||||||
|
self::assertSame(1, $room->getPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDeletingAnEmptyRoom(): void
|
||||||
|
{
|
||||||
|
$room = $this->group('Raum Nord');
|
||||||
|
$id = $room->getId();
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
$uri = \sprintf('/admin/resource-groups/%d/delete', $id);
|
||||||
|
|
||||||
|
$this->client->request('POST', $uri, ['_token' => $this->confirmationToken($uri)], server: ['HTTP_HX-Request' => 'true']);
|
||||||
|
|
||||||
|
self::assertSame('/admin/resources', $this->client->getResponse()->headers->get('HX-Redirect'));
|
||||||
|
$this->em->clear();
|
||||||
|
self::assertNull($this->em->find(ResourceGroup::class, $id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A retired resource still sits in the room, so it counts.
|
||||||
|
*/
|
||||||
|
public function testARoomWithResourcesCannotBeDeleted(): void
|
||||||
|
{
|
||||||
|
$room = $this->group('Raum Nord');
|
||||||
|
$this->resource('Schreibtisch 01', $room)->setActive(false);
|
||||||
|
$this->em->flush();
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
|
||||||
|
$crawler = $this->client->request('GET', '/admin/resources');
|
||||||
|
self::assertCount(0, $crawler->filter(\sprintf('[hx-get="/admin/resource-groups/%d/delete"]', $room->getId())));
|
||||||
|
|
||||||
|
$this->client->request('POST', \sprintf('/admin/resource-groups/%d/delete', $room->getId()), ['_token' => 'whatever']);
|
||||||
|
self::assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTheDashboardLinksHereForAdminsOnly(): void
|
||||||
|
{
|
||||||
|
$this->client->loginUser($this->user('[email protected]'));
|
||||||
|
self::assertCount(0, $this->client->request('GET', '/')->filter('a[href="/admin/resources"]'));
|
||||||
|
|
||||||
|
$this->client->loginUser($this->admin());
|
||||||
|
self::assertCount(1, $this->client->request('GET', '/')->filter('a[href="/admin/resources"]'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens the modal, then posts the given fields with its CSRF token -- the round trip a
|
||||||
|
* browser makes.
|
||||||
|
*
|
||||||
|
* @param array<string, string> $fields
|
||||||
|
*/
|
||||||
|
private function submit(string $uri, string $formName, array $fields): Response
|
||||||
|
{
|
||||||
|
$crawler = $this->client->request('GET', $uri, server: ['HTTP_HX-Request' => 'true']);
|
||||||
|
self::assertResponseIsSuccessful();
|
||||||
|
|
||||||
|
$fields['_token'] = $crawler->filter(\sprintf('#%s__token', $formName))->attr('value');
|
||||||
|
$this->client->request('POST', $uri, [$formName => $fields], server: ['HTTP_HX-Request' => 'true']);
|
||||||
|
|
||||||
|
return $this->client->getResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function confirmationToken(string $uri): string
|
||||||
|
{
|
||||||
|
$crawler = $this->client->request('GET', $uri, server: ['HTTP_HX-Request' => 'true']);
|
||||||
|
self::assertResponseIsSuccessful();
|
||||||
|
|
||||||
|
$vals = $crawler->filter('#htmx-confirmation-modal [hx-post]')->attr('hx-vals');
|
||||||
|
|
||||||
|
return json_decode((string) $vals, true, flags: \JSON_THROW_ON_ERROR)['_token'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Each request resets the entity manager, so what a test holds is detached afterwards.
|
||||||
|
*
|
||||||
|
* @template T of Resource|ResourceGroup
|
||||||
|
*
|
||||||
|
* @param T $entity
|
||||||
|
*
|
||||||
|
* @return T
|
||||||
|
*/
|
||||||
|
private function reload(Resource|ResourceGroup $entity): Resource|ResourceGroup
|
||||||
|
{
|
||||||
|
$fresh = $this->em->find($entity::class, $entity->getId());
|
||||||
|
self::assertNotNull($fresh);
|
||||||
|
|
||||||
|
return $fresh;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function group(string $label, ResourceType $type = ResourceType::DESK, int $position = 0): ResourceGroup
|
||||||
|
{
|
||||||
|
$group = new ResourceGroup($type, $label, $position);
|
||||||
|
$this->em->persist($group);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
return $group;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resource(string $label, ?ResourceGroup $group = null): Resource
|
||||||
|
{
|
||||||
|
$resource = new Resource(ResourceType::DESK, $label);
|
||||||
|
$resource->setGroup($group);
|
||||||
|
$this->em->persist($resource);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
return $resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function admin(): User
|
||||||
|
{
|
||||||
|
return $this->user('[email protected]', ['ROLE_ADMIN']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<string> $roles
|
||||||
|
*/
|
||||||
|
private function user(string $email, array $roles = ['ROLE_EMPLOYEE']): User
|
||||||
|
{
|
||||||
|
$existing = $this->em->getRepository(User::class)->findOneBy(['email' => $email]);
|
||||||
|
if (null !== $existing) {
|
||||||
|
return $existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = new User($email, 'Test');
|
||||||
|
$user->setRoles($roles);
|
||||||
|
$this->em->persist($user);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user