Feat: Add functionality to duplicate availabilities
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller\Admin\System\Availability;
|
||||||
|
|
||||||
|
use App\Entity\Availability;
|
||||||
|
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 DuplicateController extends AbstractController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $entityManager,
|
||||||
|
private readonly LoggerInterface $logger
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route('/admin/system/availability/duplicate/{id}', name: 'app_admin_system_availability_duplicate')]
|
||||||
|
#[IsGranted('ROLE_ADMIN')]
|
||||||
|
public function index(Availability $availability, Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
$copy = Availability::duplicate($availability);
|
||||||
|
|
||||||
|
$response = new AjaxModalResponseDto();
|
||||||
|
$action = $this->generateUrl('app_admin_system_availability_duplicate', ['id' => $availability->getId()]);
|
||||||
|
|
||||||
|
$form = $this->createForm(AvailabilityType::class, $copy, ['action' => $action, 'ajax_submit' => true]);
|
||||||
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$this->entityManager->persist($copy);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
$this->addFlash('success', 'Der Zeitraum wurde dupliziert.');
|
||||||
|
$this->logger->info('Duplicate availability', [
|
||||||
|
'availability' => $availability,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$redirectUrl = $this->generateUrl('app_admin_system_availability_index');
|
||||||
|
$response->setCloseAndRedirect($redirectUrl);
|
||||||
|
} else {
|
||||||
|
$content = $this->renderView('admin/system/availability/form.html.twig', [
|
||||||
|
'form' => $form,
|
||||||
|
]);
|
||||||
|
$response->setContent($content);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->json($response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,6 +45,19 @@ class Availability implements BlameableEntityInterface, TimestampableEntityInter
|
|||||||
$this->teamers = new ArrayCollection();
|
$this->teamers = new ArrayCollection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function duplicate(Availability $availability): static
|
||||||
|
{
|
||||||
|
$instance = new static();
|
||||||
|
|
||||||
|
$instance
|
||||||
|
->setDateFrom($availability->getDateFrom())
|
||||||
|
->setDateTo($availability->getDateTo())
|
||||||
|
->setDescription($availability->getDescription())
|
||||||
|
;
|
||||||
|
|
||||||
|
return $instance;
|
||||||
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
{
|
{
|
||||||
return sprintf('%s - %s', $this->getDateFrom()->format('d.m.Y'), $this->getDateTo()->format('d.m.Y'));
|
return sprintf('%s - %s', $this->getDateFrom()->format('d.m.Y'), $this->getDateTo()->format('d.m.Y'));
|
||||||
|
|||||||
@@ -55,6 +55,14 @@
|
|||||||
}) }}>
|
}) }}>
|
||||||
{{ icon('edit') }}
|
{{ icon('edit') }}
|
||||||
</button>
|
</button>
|
||||||
|
<button type="button"
|
||||||
|
{{ stimulus_controller('modal-button', [], [], {'ajax-modal': '#ajax-modal'}) }}
|
||||||
|
{{ stimulus_action('modal-button', 'ajax', null, {
|
||||||
|
'title': 'Verfügbarkeit duplizieren',
|
||||||
|
'url': path('app_admin_system_availability_duplicate', { 'id': availability.id })
|
||||||
|
}) }}>
|
||||||
|
{{ icon('copy') }}
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user