EditEventSubscriber.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Lc\ShopBundle\EventSubscriber;
  3. use Lc\ShopBundle\Context\FilterMerchantInterface;
  4. use Lc\ShopBundle\Context\FilterMultipleMerchantsInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\SortableInterface;
  7. use Lc\ShopBundle\Context\StatusInterface;
  8. use Lc\ShopBundle\Context\TreeInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\EventDispatcher\GenericEvent;
  11. class EditEventSubscriber implements EventSubscriberInterface
  12. {
  13. public $merchantUtils;
  14. public function __construct(MerchantUtilsInterface $merchantUtils)
  15. {
  16. $this->merchantUtils = $merchantUtils;
  17. }
  18. public static function getSubscribedEvents()
  19. {
  20. return array(
  21. 'easy_admin.pre_persist' => array('persistCommonProperty'),
  22. 'easy_admin.post_update' => array('updateCommonProperty'),
  23. );
  24. }
  25. public function persistCommonProperty(GenericEvent $event)
  26. {
  27. $entity = $event->getSubject();
  28. $em = $event->getArgument('em');
  29. $entityRepo = $em->getRepository(get_class($entity));
  30. if ($entity instanceof StatusInterface) {
  31. $this->setStatusProperty($entity);
  32. }
  33. if ($entity instanceof SortableInterface) {
  34. $this->setSortableProperty($entity, $entityRepo);
  35. }
  36. if ($entity instanceof FilterMerchantInterface) {
  37. $this->setMerchantProperty($entity);
  38. }
  39. if ($entity instanceof FilterMultipleMerchantsInterface) {
  40. $this->setMultipleMerchantProperty($entity);
  41. }
  42. }
  43. public function updateCommonProperty(GenericEvent $event)
  44. {
  45. /* $this->setUpdated($entity);
  46. $this->setAddressCreatedBy($entity) ;*/
  47. }
  48. private function setStatusProperty($entity){
  49. $entity->setStatus(1);
  50. return $entity;
  51. }
  52. private function setSortableProperty($entity, $entityRepo){
  53. if ($entity instanceof TreeInterface) {
  54. if ($entity->getParent()) {
  55. $total = $entityRepo->count(array('status' => 1, 'parent' => $entity->getParent()->getId()));
  56. $entity->setPosition($entity->getParent()->getId() . '_' . $total);
  57. } else {
  58. $total = $entityRepo->count(array('status' => 1, 'parent' => null));
  59. $entity->setPosition($total);
  60. }
  61. } else {
  62. $total = $entityRepo->count(array('status' => 1));
  63. $entity->setPosition($total);
  64. }
  65. return $entity;
  66. }
  67. private function setMerchantProperty($entity){
  68. $entity->setMerchant($this->merchantUtils->getMerchantCurrent());
  69. }
  70. private function setMultipleMerchantProperty($entity){
  71. if ($entity->getMerchants()->isEmpty()) {
  72. $entity->addMerchant($this->merchantUtils->getMerchantCurrent());
  73. }
  74. }
  75. }