Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

112 lines
4.0KB

  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. {
  54. if ($entity instanceof TreeInterface) {
  55. if ($entity->getParent()) {
  56. $total = $entityRepo->count(array('status' => 1, 'parent' => $entity->getParent()->getId()));
  57. $entity->setPosition((float) $entity->getParent()->getId() . '.' . str_pad($total, 2, '0', STR_PAD_LEFT));
  58. }
  59. else {
  60. if ($entity instanceof FilterMerchantInterface) {
  61. $total = $entityRepo->count(array('status' => 1, 'parent' => null, 'merchant' => $this->merchantUtils->getMerchantCurrent()));
  62. }else{
  63. $total = $entityRepo->count(array('status' => 1, 'parent' => null));
  64. }
  65. $entity->setPosition($total);
  66. }
  67. }
  68. else {
  69. if ($entity instanceof FilterMerchantInterface) {
  70. $total = $entityRepo->count(array('status' => 1, 'merchant' => $this->merchantUtils->getMerchantCurrent()));
  71. }
  72. else {
  73. $total = $entityRepo->count(array('status' => 1));
  74. }
  75. $entity->setPosition($total);
  76. }
  77. return $entity;
  78. }
  79. private function setMerchantProperty($entity)
  80. {
  81. $entity->setMerchant($this->merchantUtils->getMerchantCurrent());
  82. }
  83. private function setMultipleMerchantProperty($entity)
  84. {
  85. if ($entity->getMerchants()->isEmpty()) {
  86. $entity->addMerchant($this->merchantUtils->getMerchantCurrent());
  87. }
  88. }
  89. }