Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

SortablePropertyEventSubscriber.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 SortablePropertyEventSubscriber 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::PRE_CREATE_EVENT => ['setCommonProperty'],
  23. ];
  24. }
  25. public function setCommonProperty(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. }