|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
-
- namespace Lc\SovBundle\EventSubscriber;
-
- use Doctrine\ORM\EntityManagerInterface;
-
- use Lc\SovBundle\Doctrine\EntityInterface;
- use Lc\SovBundle\Doctrine\Extension\SortableInterface;
- use Lc\SovBundle\Doctrine\Extension\StatusInterface;
- use Lc\SovBundle\Doctrine\Extension\TreeInterface;
- use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
- use Lc\SovBundle\Repository\AbstractRepositoryInterface;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
- class SortablePropertyEventSubscriber implements EventSubscriberInterface
- {
- protected $em;
- protected $adminUrlGenerator;
-
- public function __construct(EntityManagerInterface $entityManager)
- {
- $this->em = $entityManager;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- EntityManagerEvent::PRE_CREATE_EVENT => ['setCommonProperty'],
- ];
- }
-
- public function setCommonProperty(EntityManagerEvent $event)
- {
- $entity = $event->getEntity();
- $entityRepository = $this->em->getRepository(get_class($entity));
- if ($entity instanceof SortableInterface) {
- $this->setSortableProperty($entity, $entityRepository);
- }
- }
-
-
- private function setSortableProperty(EntityInterface $entity, AbstractRepositoryInterface $entityRepository)
- {
- if ($entity instanceof StatusInterface) {
- $countParam['status'] = 1;
- }
-
- if ($entity instanceof TreeInterface) {
- if($entity->getParent()){
- $countParam['parent'] = $entity->getParent()->getId();
- }else{
- $countParam['parent'] = null;
- }
- }
-
- $total = $entityRepository->count($countParam);
-
- $entity->setPosition($total);
- }
-
-
- }
|