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.

126 lines
4.4KB

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