<?php
namespace App\EventSubscriber;
use App\Entity\Booking;
use App\Entity\BookingConstraint;
use App\Entity\User;
use App\Entity\Profile;
use Cocur\Slugify\Slugify;
use App\Entity\HobbySingle;
use App\Entity\ServiceSingle;
use App\Entity\GalleryCategory;
use App\Repository\BookingConstraintRepository;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $encoder;
public function __construct(UserPasswordHasherInterface $encoder, Security $security, SessionInterface $session, BookingConstraintRepository $constraintRepo)
{
$this->encoder = $encoder;
$this->locales = ['fr', 'en', 'es', 'de'];
$this->security = $security;
$this->session = $session;
$this->constraintRepo = $constraintRepo;
}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['setOptionNew'],
BeforeEntityUpdatedEvent::class => ['setOptionEdit'],
AfterEntityPersistedEvent::class => ['afterPersist'],
AfterEntityUpdatedEvent::class => ['afterUpdate'],
AfterEntityDeletedEvent::class => ['afterDelete']
];
}
/**
* Permet de gerer un formulaire avant son envoi pour la création
*
* @param BeforeEntityPersistedEvent $event
* @return void
*/
public function setOptionNew(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
# USER PASSWORD HASH
if ($entity instanceof User) {
$hash = $this->encoder->hashPassword($entity, $entity->getPassword());
$entity->setPassword($hash);
}
# PROFILE USER
if ($entity instanceof Profile) {
$entity->setUser($this->security->getUser());
}
# ITEM SLUG
if ($entity instanceof GalleryCategory || $entity instanceof ServiceSingle || $entity instanceof HobbySingle) {
$slugify = new Slugify();
if ($entity instanceof GalleryCategory) {
foreach ($this->locales as $locale) {
$entity->translate($locale)->setSlug($slugify->slugify($entity->translate($locale)->getName()));
}
}else {
foreach ($this->locales as $locale) {
$entity->translate($locale)->setSlug($slugify->slugify($entity->translate($locale)->getTitle()));
}
}
}
# BOOKING
if ($entity instanceof Booking) {
$entity
->setBookingConstraint($this->constraintRepo->findOneBy([]))
->setUniqId(uniqid("R"))
->setApproval(true)
->setRgpd(true)
;
}
return;
}
/**
* Permet de gerer un formulaire avant son envoi pour la mise à jour
*
* @param BeforeEntityUpdatedEvent $event
*/
public function setOptionEdit(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof User) {
$hash = $this->encoder->hashPassword($entity, $entity->getPassword());
$entity->setPassword($hash);
}
if ($entity instanceof GalleryCategory || $entity instanceof ServiceSingle) {
$slugify = new Slugify();
if ($entity instanceof GalleryCategory) {
foreach ($this->locales as $locale) {
$entity->translate($locale)->setSlug($slugify->slugify($entity->translate($locale)->getName()));
}
}else {
foreach ($this->locales as $locale) {
$entity->translate($locale)->setSlug($slugify->slugify($entity->translate($locale)->getTitle()));
}
}
}
return;
}
public function afterPersist(AfterEntityPersistedEvent $event): void
{
$this->session->getFlashBag()->add('success', 'Ajouté avec succes');
}
public function afterUpdate(AfterEntityUpdatedEvent $event): void
{
$this->session->getFlashBag()->add('success', 'Mis à jour avec succes');
}
public function afterDelete(AfterEntityDeletedEvent $event): void
{
$this->session->getFlashBag()->add('success','Supprimé avec succes');
}
}