109 líneas
4.3KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use Lc\ShopBundle\Context\MerchantConfigInterface;
  4. use Lc\ShopBundle\Context\MerchantInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class MerchantController extends AdminController
  8. {
  9. public function persistEntity($entity)
  10. {
  11. $this->createAvailableOptions($entity) ;
  12. parent::persistEntity($entity);
  13. }
  14. public function updateEntity($entity)
  15. {
  16. $this->createAvailableOptions($entity) ;
  17. parent::updateEntity($entity);
  18. }
  19. public function createAvailableOptions($entity)
  20. {
  21. $merchantConfigs = $entity->getMerchantConfigs() ;
  22. $classMerchantConfig = $this->em->getClassMetadata(MerchantConfigInterface::class)->getName() ;
  23. $availableOptions = $classMerchantConfig::getAvailableOptions() ;
  24. $addConfig = false ;
  25. foreach($availableOptions as $key => $option) {
  26. $optionExist = false ;
  27. foreach($merchantConfigs as $merchantConfig) {
  28. if($merchantConfig->getName() == $key) {
  29. $optionExist = true ;
  30. }
  31. }
  32. if(!$optionExist) {
  33. $newMerchantConfig = new $classMerchantConfig ;
  34. $newMerchantConfig
  35. ->setName($key)
  36. ->setMerchant($entity) ;
  37. if(isset($option['default'])) {
  38. $newMerchantConfig->setValue($option['default']) ;
  39. }
  40. $addConfig = true ;
  41. $this->em->persist($newMerchantConfig);
  42. }
  43. }
  44. if($addConfig) {
  45. $this->em->flush() ;
  46. }
  47. }
  48. public function editAction()
  49. {
  50. // paramètres (admin)
  51. $isSettings = $this->request->query->get('is_settings') ;
  52. if($isSettings) {
  53. $idMerchant = $this->security->getUser()->getMerchant()->getId() ;
  54. $this->request->query->set('id', $idMerchant) ;
  55. $easyadmin = $this->request->attributes->get('easyadmin');
  56. $merchant = $this->getDoctrine()
  57. ->getRepository(MerchantInterface::class)
  58. ->find($idMerchant);
  59. $easyadmin['item'] = $merchant;
  60. $this->request->attributes->set('easyadmin', $easyadmin) ;
  61. $response = parent::editAction() ;
  62. if ($response instanceof RedirectResponse) {
  63. $referer = $this->request->headers->get('referer');
  64. return new RedirectResponse($referer);
  65. }
  66. else {
  67. return $response ;
  68. }
  69. }
  70. // édition des Merchant (super admin)
  71. else {
  72. return parent::editAction() ;
  73. }
  74. }
  75. public function switchMerchantAction(Request $request): RedirectResponse
  76. {
  77. $em = $this->getDoctrine()->getManager();
  78. $user = $this->security->getUser() ;
  79. if($user->hasRole('ROLE_SUPER_ADMIN')) {
  80. $idMerchant = $request->request->get('id_merchant');
  81. $merchant = $this->getDoctrine()
  82. ->getRepository(MerchantInterface::class)
  83. ->find($idMerchant);
  84. if ($merchant) {
  85. $user->setMerchant($merchant);
  86. $em->persist($user);
  87. $em->flush();
  88. }
  89. }
  90. return $this->redirect('/admin/dashboard') ;
  91. }
  92. }