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.

101 satır
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. dump($event);
  47. /* $this->setUpdated($entity);
  48. $this->setAddressCreatedBy($entity) ;*/
  49. }
  50. private function setStatusProperty($entity){
  51. $entity->setStatus(1);
  52. return $entity;
  53. }
  54. private function setSortableProperty($entity, $entityRepo){
  55. if ($entity instanceof TreeInterface) {
  56. if ($entity->getParent()) {
  57. $total = $entityRepo->count(array('status' => 1, 'parent' => $entity->getParent()->getId()));
  58. $entity->setPosition($entity->getParent()->getId() . '_' . $total);
  59. } else {
  60. $total = $entityRepo->count(array('status' => 1, 'parent' => null));
  61. $entity->setPosition($total);
  62. }
  63. } else {
  64. $total = $entityRepo->count(array('status' => 1));
  65. $entity->setPosition($total);
  66. }
  67. return $entity;
  68. }
  69. private function setMerchantProperty($entity){
  70. $entity->setMerchant($this->globalParam->getCurrentMerchant());
  71. }
  72. private function setMultipleMerchantProperty($entity){
  73. if ($entity->getMerchants()->isEmpty()) {
  74. $entity->addMerchant($this->globalParam->getCurrentMerchant());
  75. }
  76. }
  77. }