Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

EditEventSubscriber.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if ($entity instanceof FilterMerchantInterface) {
  59. $total = $entityRepo->count(array('status' => 1, 'parent' => null, 'merchant' => $this->merchantUtils->getMerchantCurrent()));
  60. }else{
  61. $total = $entityRepo->count(array('status' => 1, 'parent' => null));
  62. }
  63. $entity->setPosition($total);
  64. }
  65. } else {
  66. if ($entity instanceof FilterMerchantInterface) {
  67. $total = $entityRepo->count(array('status' => 1, 'merchant' => $this->merchantUtils->getMerchantCurrent()));
  68. }else{
  69. $total = $entityRepo->count(array('status' => 1));
  70. }
  71. $entity->setPosition($total);
  72. }
  73. return $entity;
  74. }
  75. private function setMerchantProperty($entity){
  76. $entity->setMerchant($this->merchantUtils->getMerchantCurrent());
  77. }
  78. private function setMultipleMerchantProperty($entity){
  79. if ($entity->getMerchants()->isEmpty()) {
  80. $entity->addMerchant($this->merchantUtils->getMerchantCurrent());
  81. }
  82. }
  83. }