選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

99 行
3.7KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use App\Entity\MerchantConfig;
  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. $availableOptions = MerchantConfig::getAvailableOptions() ;
  23. foreach($availableOptions as $key => $option) {
  24. $optionExist = false ;
  25. foreach($merchantConfigs as $merchantConfig) {
  26. if($merchantConfig->getName() == $key) {
  27. $optionExist = true ;
  28. }
  29. }
  30. if(!$optionExist) {
  31. $merchantConfig = new MerchantConfig();
  32. $merchantConfig->setName($key)->setValue($merchantConfig->getOptionValue('default'))->setMerchant($entity) ;
  33. $this->em->persist($merchantConfig);
  34. }
  35. }
  36. $this->em->flush() ;
  37. }
  38. public function editAction()
  39. {
  40. // paramètres (admin)
  41. $isSettings = $this->request->query->get('is_settings') ;
  42. if($isSettings) {
  43. $idMerchant = $this->security->getUser()->getMerchant()->getId() ;
  44. $this->request->query->set('id', $idMerchant) ;
  45. $easyadmin = $this->request->attributes->get('easyadmin');
  46. $merchant = $this->getDoctrine()
  47. ->getRepository(MerchantInterface::class)
  48. ->find($idMerchant);
  49. $easyadmin['item'] = $merchant;
  50. $this->request->attributes->set('easyadmin', $easyadmin) ;
  51. $response = parent::editAction() ;
  52. if ($response instanceof RedirectResponse) {
  53. $referer = $this->request->headers->get('referer');
  54. return new RedirectResponse($referer);
  55. }
  56. else {
  57. return $response ;
  58. }
  59. }
  60. // édition des Merchant (super admin)
  61. else {
  62. return parent::editAction() ;
  63. }
  64. }
  65. public function switchMerchantAction(Request $request): RedirectResponse
  66. {
  67. $em = $this->getDoctrine()->getManager();
  68. $user = $this->security->getUser() ;
  69. if($user->hasRole('ROLE_SUPER_ADMIN')) {
  70. $idMerchant = $request->request->get('id_merchant');
  71. $merchant = $this->getDoctrine()
  72. ->getRepository(MerchantInterface::class)
  73. ->find($idMerchant);
  74. if ($merchant) {
  75. $user->setMerchant($merchant);
  76. $em->persist($user);
  77. $em->flush();
  78. }
  79. }
  80. return $this->redirect('admin/dashboard') ;
  81. }
  82. }