|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
-
- namespace Lc\ShopBundle\EventSubscriber;
-
- use Lc\ShopBundle\Context\FilterMerchantInterface;
- use Lc\ShopBundle\Context\FilterMultipleMerchantsInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\SortableInterface;
- use Lc\ShopBundle\Context\StatusInterface;
- use Lc\ShopBundle\Context\TreeInterface;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\EventDispatcher\GenericEvent;
-
-
- class EditEventSubscriber implements EventSubscriberInterface
- {
- public $merchantUtils;
-
- public function __construct(MerchantUtilsInterface $merchantUtils)
- {
- $this->merchantUtils = $merchantUtils;
- }
-
- public static function getSubscribedEvents()
- {
- return array(
- 'easy_admin.pre_persist' => array('persistCommonProperty'),
- 'easy_admin.post_update' => array('updateCommonProperty'),
- );
- }
-
-
- public function persistCommonProperty(GenericEvent $event)
- {
- $entity = $event->getSubject();
- $em = $event->getArgument('em');
-
- $entityRepo = $em->getRepository(get_class($entity));
-
- if ($entity instanceof StatusInterface) {
- $this->setStatusProperty($entity);
- }
-
- if ($entity instanceof SortableInterface) {
- $this->setSortableProperty($entity, $entityRepo);
- }
-
- if ($entity instanceof FilterMerchantInterface) {
- $this->setMerchantProperty($entity);
- }
-
- if ($entity instanceof FilterMultipleMerchantsInterface) {
- $this->setMultipleMerchantProperty($entity);
- }
- }
-
-
- public function updateCommonProperty(GenericEvent $event)
- {
- /* $this->setUpdated($entity);
- $this->setAddressCreatedBy($entity) ;*/
- }
-
- private function setStatusProperty($entity){
- $entity->setStatus(1);
-
- return $entity;
- }
-
- private function setSortableProperty($entity, $entityRepo){
- if ($entity instanceof TreeInterface) {
- if ($entity->getParent()) {
- $total = $entityRepo->count(array('status' => 1, 'parent' => $entity->getParent()->getId()));
- $entity->setPosition($entity->getParent()->getId() . '_' . $total);
- } else {
- if ($entity instanceof FilterMerchantInterface) {
- $total = $entityRepo->count(array('status' => 1, 'parent' => null, 'merchant' => $this->merchantUtils->getMerchantCurrent()));
- }else{
- $total = $entityRepo->count(array('status' => 1, 'parent' => null));
- }
- $entity->setPosition($total);
- }
- } else {
- if ($entity instanceof FilterMerchantInterface) {
- $total = $entityRepo->count(array('status' => 1, 'merchant' => $this->merchantUtils->getMerchantCurrent()));
- }else{
- $total = $entityRepo->count(array('status' => 1));
- }
- $entity->setPosition($total);
- }
- return $entity;
- }
-
- private function setMerchantProperty($entity){
- $entity->setMerchant($this->merchantUtils->getMerchantCurrent());
- }
-
- private function setMultipleMerchantProperty($entity){
- if ($entity->getMerchants()->isEmpty()) {
- $entity->addMerchant($this->merchantUtils->getMerchantCurrent());
- }
- }
-
- }
|