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ů.

123 lines
4.2KB

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