Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

62 lines
1.7KB

  1. <?php
  2. namespace Lc\SovBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Doctrine\EntityInterface;
  5. use Lc\SovBundle\Doctrine\Extension\SortableInterface;
  6. use Lc\SovBundle\Doctrine\Extension\StatusInterface;
  7. use Lc\SovBundle\Doctrine\Extension\TreeInterface;
  8. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  9. use Lc\SovBundle\Repository\AbstractRepositoryInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CreateEntityEventSubscriber implements EventSubscriberInterface
  12. {
  13. protected $em;
  14. protected $adminUrlGenerator;
  15. public function __construct(EntityManagerInterface $entityManager)
  16. {
  17. $this->em = $entityManager;
  18. }
  19. public static function getSubscribedEvents()
  20. {
  21. return [
  22. EntityManagerEvent::CREATE_EVENT => ['createEntity'],
  23. ];
  24. }
  25. public function createEntity(EntityManagerEvent $event)
  26. {
  27. $entity = $event->getEntity();
  28. $entityRepository = $this->em->getRepository(get_class($entity));
  29. if ($entity instanceof SortableInterface) {
  30. $this->setSortableProperty($entity, $entityRepository);
  31. }
  32. }
  33. private function setSortableProperty(EntityInterface $entity, AbstractRepositoryInterface $entityRepository)
  34. {
  35. if ($entity instanceof StatusInterface) {
  36. $countParam['status'] = 1;
  37. }
  38. if ($entity instanceof TreeInterface) {
  39. if($entity->getParent()){
  40. $countParam['parent'] = $entity->getParent()->getId();
  41. }else{
  42. $countParam['parent'] = null;
  43. }
  44. }
  45. $total = $entityRepository->count($countParam);
  46. $entity->setPosition($total);
  47. }
  48. }