You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.4KB

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