Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

141 lines
5.1KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
  4. use FOS\UserBundle\Model\UserManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantInterface;
  6. use Symfony\Component\Security\Core\Security;
  7. class AdminController extends EasyAdminController
  8. {
  9. protected $security;
  10. protected $userManager;
  11. public function __construct(Security $security, UserManagerInterface $userManager)
  12. {
  13. $this->security = $security;
  14. $this->userManager = $userManager ;
  15. }
  16. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  17. {
  18. if ($pos = strpos($dqlFilter, 'currentMerchant')) {
  19. $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
  20. }
  21. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  22. if($entityClass == 'App\Entity\PointSale') {
  23. $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchant')
  24. ->setParameter(':currentMerchant',$this->getUser()->getMerchant()->getId()) ;
  25. }
  26. return $queryBuilder ;
  27. }
  28. public function renderTemplate($actionName, $templatePath, array $parameters = [])
  29. {
  30. $parameters = array_merge(
  31. $parameters,
  32. $this->getTwigExtraParameters()
  33. ) ;
  34. return parent::renderTemplate($actionName, $templatePath, $parameters) ;
  35. }
  36. public function getTwigExtraParameters()
  37. {
  38. $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class);
  39. $merchants = $repositoryMerchant->findAll() ;
  40. $user = $this->security->getUser() ;
  41. return [
  42. 'merchants' => $merchants,
  43. 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null,
  44. ] ;
  45. }
  46. public function updateEntity($entity)
  47. {
  48. $this->setUpdated($entity);
  49. parent::updateEntity($entity);
  50. }
  51. public function persistEntity($entity)
  52. {
  53. if (method_exists($entity, 'setMerchant')) {
  54. $entity->setMerchant($this->security->getUser()->getMerchant());
  55. }
  56. if (method_exists($entity, 'addMerchant')) {
  57. if($entity->getMerchant()->isEmpty()) {
  58. $entity->addMerchant($this->security->getUser()->getMerchant());
  59. }
  60. }
  61. if (method_exists($entity, 'setCreatedAt')) {
  62. $entity->setCreatedAt(new \DateTime());
  63. }
  64. if (method_exists($entity, 'setCreatedAt')) {
  65. $entity->setCreatedBy($this->security->getUser());
  66. }
  67. if (method_exists($entity, 'getAddress')) {
  68. $entity->getAddress()->setCreatedBy($this->security->getUser());
  69. $entity->getAddress()->setCreatedAt(new \DateTime());
  70. }
  71. if (method_exists($entity, 'getAddresses')) {
  72. foreach($entity->getAddresses() as $address) {
  73. $address->setCreatedBy($this->security->getUser()) ;
  74. $address->setCreatedAt(new \DateTime()) ;
  75. }
  76. }
  77. $this->setUpdated($entity);
  78. if (method_exists($entity, 'setPosition')) {
  79. $entity->setPosition(0);
  80. }
  81. if (method_exists($entity, 'setStatus')) {
  82. $entity->setStatus(1);
  83. }
  84. parent::persistEntity($entity);
  85. }
  86. public function setUpdated($entity)
  87. {
  88. if (method_exists($entity, 'setUpdatedAt')) {
  89. $entity->setUpdatedAt(new \DateTime());
  90. }
  91. if (method_exists($entity, 'setUpdatedBy')) {
  92. $entity->setUpdatedBy($this->security->getUser());
  93. }
  94. if (method_exists($entity, 'getAddress')) {
  95. $entity->getAddress()->setUpdatedBy($this->security->getUser());
  96. }
  97. if (method_exists($entity, 'getAddresses')) {
  98. foreach($entity->getAddresses() as $address) {
  99. $address->setUpdatedBy($this->security->getUser()) ;
  100. $address->setUpdatedAt(new \DateTime()) ;
  101. if(!$address->getCreatedBy()) {
  102. $address->setCreatedBy($this->security->getUser()) ;
  103. $address->setCreatedAt(new \DateTime()) ;
  104. }
  105. }
  106. }
  107. }
  108. }