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.

AdminController.php 3.6KB

5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. return parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  20. }
  21. public function renderTemplate($actionName, $templatePath, array $parameters = [])
  22. {
  23. $parameters = array_merge(
  24. $parameters,
  25. $this->getTwigExtraParameters()
  26. ) ;
  27. return parent::renderTemplate($actionName, $templatePath, $parameters) ;
  28. }
  29. public function getTwigExtraParameters()
  30. {
  31. $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class);
  32. $merchants = $repositoryMerchant->findAll() ;
  33. $user = $this->security->getUser() ;
  34. return [
  35. 'merchants' => $merchants,
  36. 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null,
  37. ] ;
  38. }
  39. public function updateEntity($entity)
  40. {
  41. $this->setUpdated($entity);
  42. parent::updateEntity($entity);
  43. }
  44. public function persistEntity($entity)
  45. {
  46. if (method_exists($entity, 'setMerchant')) {
  47. $entity->setMerchant($this->security->getUser()->getMerchant());
  48. }
  49. if (method_exists($entity, 'setCreatedAt')) {
  50. $entity->setCreatedAt(new \DateTime());
  51. }
  52. if (method_exists($entity, 'setCreatedAt')) {
  53. $entity->setCreatedBy($this->security->getUser());
  54. }
  55. if (method_exists($entity, 'getAddress')
  56. && method_exists($entity->getAddress(), 'setCreatedBy')) {
  57. $entity->getAddress()->setCreatedBy($this->security->getUser());
  58. }
  59. $this->setUpdated($entity);
  60. if (method_exists($entity, 'setPosition')) {
  61. $entity->setPosition(0);
  62. }
  63. if (method_exists($entity, 'setStatus')) {
  64. $entity->setStatus(1);
  65. }
  66. parent::persistEntity($entity);
  67. }
  68. public function setUpdated($entity)
  69. {
  70. if (method_exists($entity, 'setUpdatedAt')) {
  71. $entity->setUpdatedAt(new \DateTime());
  72. }
  73. if (method_exists($entity, 'setUpdatedAt')) {
  74. $entity->setUpdatedBy($this->security->getUser());
  75. }
  76. if (method_exists($entity, 'getAddress')
  77. && method_exists($entity->getAddress(), 'setUpdatedBy')) {
  78. $entity->getAddress()->setUpdatedBy($this->security->getUser());
  79. }
  80. }
  81. }