Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

650 Zeilen
32KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\EntityRepository;
  5. use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  7. use FOS\UserBundle\Model\UserManagerInterface;
  8. use Lc\ShopBundle\Context\FilterMultipleMerchantsInterface;
  9. use Lc\ShopBundle\Context\ImageInterface;
  10. use Lc\ShopBundle\Context\MerchantInterface;
  11. use Lc\ShopBundle\Context\ReminderInterface;
  12. use Lc\ShopBundle\Context\SeoInterface;
  13. use Lc\ShopBundle\Context\StatusInterface;
  14. use Lc\ShopBundle\Context\TreeInterface;
  15. use Lc\ShopBundle\Form\Backend\Common\AbstractEditPositionType;
  16. use Lc\ShopBundle\Form\Backend\Filters\ListFilterType;
  17. use Lc\ShopBundle\Model\User;
  18. use Lc\ShopBundle\Services\UtilsManager;
  19. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  20. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  21. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  22. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  23. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  24. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  25. use Symfony\Component\Form\Extension\Core\Type\TextType;
  26. use Symfony\Component\Form\FormEvent;
  27. use Symfony\Component\Form\FormEvents;
  28. use Symfony\Component\HttpFoundation\JsonResponse;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\Security\Core\Security;
  31. use Symfony\Contracts\Translation\TranslatorInterface;
  32. class AdminController extends EasyAdminController
  33. {
  34. protected $security;
  35. protected $userManager;
  36. protected $em;
  37. protected $utils;
  38. protected $merchantUtils;
  39. protected $mailjetTransport;
  40. protected $orderUtils;
  41. protected $mailUtils ;
  42. protected $translator;
  43. protected $utilsProcess;
  44. protected $sectionUtils;
  45. protected $filtersForm = null;
  46. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em,
  47. MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator)
  48. {
  49. $this->security = $security;
  50. $this->userManager = $userManager;
  51. $this->em = $em;
  52. $this->mailjetTransport = $mailjetTransport;
  53. $this->utils = $utilsManager->getUtils();
  54. $this->merchantUtils = $utilsManager->getMerchantUtils();
  55. $this->orderUtils = $utilsManager->getOrderUtils();;
  56. $this->mailUtils = $utilsManager->getMailUtils() ;
  57. $this->utilsProcess = $utilsManager->getUtilsProcess() ;
  58. $this->sectionUtils = $utilsManager->getSectionUtils() ;
  59. $this->translator = $translator;
  60. }
  61. public function createCustomForm($class, $action, $parameters, $data = true, $options = array())
  62. {
  63. if ($data) $options['data'] = $parameters['entity'];
  64. $options['action'] = $this->generateUrl('easyadmin', array(
  65. 'action' => $action,
  66. 'entity' => $this->entity['name'],
  67. 'id' => $parameters['entity']->getId()
  68. ));
  69. return $this->createForm($class, null, $options);
  70. }
  71. public function autocompleteAction()
  72. {
  73. $entityName = $this->request->query->get('entity');
  74. $valueSearched = $this->request->query->get('q');
  75. $field = $this->request->query->get('field');
  76. $class = $this->entity['class'];
  77. $repo = $this->em->getRepository($class);
  78. $values = $repo->findByTerm($field, $valueSearched);
  79. $response = array();
  80. foreach ($values as $value) {
  81. $response[] = $value[$field];
  82. }
  83. return new JsonResponse($response);
  84. }
  85. /**
  86. * Réécriture de show action pr rediriger vers l'édition
  87. */
  88. public function showAction()
  89. {
  90. $id = $this->request->query->get('id');
  91. $entity = $this->request->query->get('entity');
  92. return $this->redirectToRoute('easyadmin', [
  93. 'action' => 'edit',
  94. 'entity' => $entity,
  95. 'id' => $id
  96. ]);
  97. }
  98. /**
  99. * Réécriture de removeEntity pour passer les status à -1
  100. */
  101. protected function removeEntity($entity)
  102. {
  103. if ($entity instanceof StatusInterface) {
  104. $entity->setStatus(-1);
  105. $this->em->persist($entity);
  106. $this->em->flush();
  107. } else {
  108. parent::removeEntity($entity);
  109. }
  110. }
  111. protected function commonDqlFilterQueryBuilder($entityClass, $dqlFilter)
  112. {
  113. if ($pos = strpos($dqlFilter, 'currentMerchant')) {
  114. $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
  115. }
  116. if ($pos = strpos($dqlFilter, 'sectionLunch')) {
  117. $dqlFilter = sprintf(str_replace('sectionLunch', $this->sectionUtils->getSection('lunch')->getId(), $dqlFilter));
  118. }else if ($pos = strpos($dqlFilter, 'sectionMarket')) {
  119. $dqlFilter = sprintf(str_replace('sectionMarket', $this->sectionUtils->getSection('market')->getId(), $dqlFilter));
  120. }
  121. if (new $entityClass instanceof StatusInterface && strpos($dqlFilter, 'entity.status') === false) {
  122. if ($dqlFilter) $dqlFilter .= sprintf(' AND entity.status > = 0');
  123. else $dqlFilter .= sprintf(' entity.status > = 0');
  124. }
  125. return $dqlFilter;
  126. }
  127. protected function commonQueryFilter($entityClass, $queryBuilder)
  128. {
  129. if (new $entityClass instanceof FilterMultipleMerchantsInterface) {
  130. $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchants')
  131. ->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
  132. }
  133. }
  134. protected function createSearchQueryBuilder($entityClass, $searchQuery, array $searchableFields, $sortField = null, $sortDirection = null, $dqlFilter = null)
  135. {
  136. $dqlFilter = $this->commonDqlFilterQueryBuilder($entityClass, $dqlFilter);
  137. $queryBuilder = parent::createSearchQueryBuilder($entityClass, $searchQuery, $searchableFields, $sortField, $sortDirection, $dqlFilter);
  138. $this->commonQueryFilter($entityClass, $queryBuilder);
  139. return $queryBuilder;
  140. }
  141. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  142. {
  143. $dqlFilter = $this->commonDqlFilterQueryBuilder($entityClass, $dqlFilter);
  144. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  145. $this->commonQueryFilter($entityClass, $queryBuilder);
  146. $listFields = $this->entity['list']['fields'];
  147. $this->filtersForm = $this->createForm(ListFilterType::class, null, array(
  148. 'fields' => $listFields,
  149. 'method' => 'get',
  150. 'entity_name' => $this->entity['name'],
  151. //'entityClass' => $this->entity['class'],
  152. 'entity_class' => $this->entity['class']
  153. ));
  154. $this->filtersForm->handleRequest($this->request);
  155. if ($this->filtersForm->isSubmitted() && $this->filtersForm->isValid()) {
  156. foreach ($listFields as $field) {
  157. if ($this->filtersForm->has($field['property'])) {
  158. switch ($field['dataType']) {
  159. case 'option':
  160. case 'integer':
  161. case 'text':
  162. case 'string':
  163. case 'toggle':
  164. $filter = $this->filtersForm->get($field['property'])->getData();
  165. if ($filter !== null) {
  166. $queryBuilder->andWhere('entity.' . $field['property'] . ' LIKE :' . $field['property'] . '');
  167. $queryBuilder->setParameter($field['property'], '%' . $filter . '%');
  168. }
  169. break;
  170. case 'association' :
  171. $filter = $this->filtersForm->get($field['property'])->getData();
  172. if ($filter !== null) {
  173. if ($field['type_options']['multiple']) {
  174. $queryBuilder->andWhere(':' . $field['property'] . ' MEMBER OF entity.' . $field['property'] . '');
  175. } else {
  176. $queryBuilder->andWhere('entity.' . $field['property'] . ' = :' . $field['property'] . '');
  177. }
  178. if($filter instanceof TreeInterface && $filter->getParent() == null) {
  179. $queryBuilder->setParameter($field['property'], array_merge(array($filter), $filter->getChildrens()->toArray()));
  180. }else{
  181. $queryBuilder->setParameter($field['property'], $filter);
  182. }
  183. }
  184. break;
  185. case 'datetime':
  186. case 'date':
  187. $dateStart = $this->filtersForm->get($field['property'])->get('dateStart')->getData();
  188. $dateEnd = $this->filtersForm->get($field['property'])->get('dateEnd')->getData();
  189. if ($dateStart) $queryBuilder->andWhere('entity.' . $field['property'] . ' >= :dateStart')->setParameter('dateStart', $dateStart);
  190. if ($dateEnd) $queryBuilder->andWhere('entity.' . $field['property'] . ' <= :dateEnd')->setParameter('dateEnd', $dateEnd);
  191. }
  192. }
  193. }
  194. }
  195. return $queryBuilder;
  196. }
  197. public function renderTemplate($actionName, $templatePath, array $parameters = [])
  198. {
  199. $reminderRepo = $this->em->getRepository(ReminderInterface::class);
  200. $easyadmin = $this->request->attributes->get('easyadmin');
  201. $entityName = $easyadmin['entity']['name'];
  202. $id = null;
  203. if ($easyadmin['item']) $id = $easyadmin['item']->getId();
  204. $user = $this->security->getUser();
  205. $reminders = array('reminders' => $reminderRepo->findByEasyAdminConfigAndUser($actionName, $entityName, $user, $id));
  206. if ($actionName == 'list') {
  207. if ($this->filtersForm === null) {
  208. $options['fields'] = $parameters['fields'];
  209. $options['method'] = 'get';
  210. $this->filtersForm = $this->createForm(ListFilterType::class, null, $options);
  211. }
  212. $parameters = array_merge(
  213. $parameters,
  214. array(
  215. 'filters_form' => $this->filtersForm->createView()
  216. )
  217. );
  218. }
  219. $parameters = array_merge(
  220. $parameters,
  221. $this->getTwigExtraParameters(),
  222. $reminders
  223. );
  224. return parent::renderTemplate($actionName, $templatePath, $parameters);
  225. }
  226. public function getTwigExtraParameters()
  227. {
  228. $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class);
  229. $merchants = $repositoryMerchant->findAll();
  230. $user = $this->security->getUser();
  231. return [
  232. 'merchants' => $merchants,
  233. 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null,
  234. ];
  235. }
  236. public function listChildrenAction()
  237. {
  238. $this->dispatch(EasyAdminEvents::PRE_LIST);
  239. $id = $this->request->query->get('id');
  240. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  241. $easyadmin = $this->request->attributes->get('easyadmin');
  242. $entity = $easyadmin['item'];
  243. $fields = $this->entity['list']['fields'];
  244. $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $this->entity['list']['max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']);
  245. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  246. $parameters = [
  247. 'paginator' => $paginator,
  248. 'fields' => $fields,
  249. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  250. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  251. 'entity' => $entity,
  252. ];
  253. return $this->executeDynamicMethod('render<EntityName>Template', ['list', $this->entity['templates']['list'], $parameters]);
  254. }
  255. public function sortAction()
  256. {
  257. $this->dispatch(EasyAdminEvents::PRE_LIST);
  258. $entity = null;
  259. //Replace this with query builder function, do not use finAll of easyAdmin
  260. if ($this->request->query->get('id')) {
  261. $id = $this->request->query->get('id');
  262. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  263. $easyadmin = $this->request->attributes->get('easyadmin');
  264. $entity = $easyadmin['item'];
  265. }
  266. if ($this->entity['list']['dql_filter']) $this->entity['list']['dql_filter'] .= sprintf(' AND entity.status = 1');
  267. else $this->entity['list']['dql_filter'] .= sprintf(' entity.status = 1');
  268. $fields = $this->entity['list']['fields'];
  269. $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), 500, $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']);
  270. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  271. $positionForm = $this->createFormBuilder(array('entities', $paginator->getCurrentPageResults()))
  272. ->add('entities', CollectionType::class, array(
  273. 'required' => true,
  274. 'allow_add' => true,
  275. 'entry_type' => AbstractEditPositionType::class,
  276. ))
  277. ->getForm();
  278. $positionForm->handleRequest($this->request);
  279. if ($positionForm->isSubmitted() && $positionForm->isValid()) {
  280. $class = $this->entity['class'];
  281. $repo = $this->em->getRepository($class);
  282. $latsPos = 0;
  283. foreach ($positionForm->get('entities')->getData() as $elm) {
  284. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  285. $entity = $repo->find($elm['id']);
  286. $entity->setPosition($elm['position']);
  287. $this->em->persist($entity);
  288. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  289. $latsPos = $elm['position'];
  290. }
  291. //die();
  292. //die();
  293. //to do récupérer les élements hors ligne et incrémenter position
  294. /*foreach ($repo->findBy(array('status'=> false)) as $offlineEntity) {
  295. $latsPos++;
  296. $offlineEntity->setPosition($latsPos);
  297. $this->em->persist($offlineEntity);
  298. }*/
  299. $this->em->flush();
  300. $this->addFlash('success', 'Position modifié', array(), 'mweb');
  301. return $this->redirectToReferrer();
  302. }
  303. $parameters = [
  304. 'paginator' => $paginator,
  305. 'fields' => $fields,
  306. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  307. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  308. 'postion_form' => $positionForm->createView(),
  309. 'entity' => $entity,
  310. 'sortable' => true
  311. ];
  312. return $this->executeDynamicMethod('render<EntityName>Template', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]);
  313. }
  314. public function createEntityFormBuilder($entity, $view, $override = true)
  315. {
  316. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  317. if ($override) $formBuilder = $this->overrideFormBuilder($formBuilder, $entity, $view);
  318. return $formBuilder;
  319. }
  320. public function overrideFormBuilder($formBuilder, $entity, $view)
  321. {
  322. $id = (null !== $entity->getId()) ? $entity->getId() : 0;
  323. if ($entity instanceof SeoInterface) {
  324. $formBuilder->add('metaTitle', TextType::class, array(
  325. 'required' => false,
  326. 'translation_domain' => 'lcshop'
  327. )
  328. );
  329. $formBuilder->add('metaDescription', TextareaType::class, array(
  330. 'required' => false,
  331. 'translation_domain' => 'lcshop'
  332. )
  333. );
  334. /*$formBuilder->add('oldUrls', TextareaType::class, array(
  335. 'required' => false,
  336. 'translation_domain' => 'lcshop'
  337. )
  338. );*/
  339. }
  340. if ($entity instanceof StatusInterface) {
  341. $formBuilder->add('status', ChoiceType::class, array(
  342. 'choices' => array(
  343. 'field.default.statusOptions.offline' => '0',
  344. 'field.default.statusOptions.online' => '1'
  345. ),
  346. 'expanded' => true,
  347. 'required' => true,
  348. 'translation_domain' => 'lcshop'
  349. )
  350. );
  351. }
  352. if ($entity instanceof TreeInterface) {
  353. $formBuilder->add('parent', EntityType::class, array(
  354. 'class' => $this->entity['class'],
  355. 'query_builder' => function (EntityRepository $repo) use ($id) {
  356. return $repo->createQueryBuilder('e')
  357. ->where('e.parent is NULL')
  358. ->andWhere('e.status >= 0')
  359. ->orderBy('e.status', "DESC");
  360. },
  361. 'required' => false,
  362. )
  363. );
  364. }
  365. $formBuilder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
  366. $form = $event->getForm();
  367. $childrens = $form->all();
  368. $this->loopFormField($form, $childrens);
  369. });
  370. return $formBuilder;
  371. }
  372. private function loopFormField($form, $childrens)
  373. {
  374. foreach ($childrens as $child) {
  375. $statusInterface = false;
  376. $treeInterface = false;
  377. $type = $child->getConfig()->getType()->getInnerType();;
  378. if ($type instanceof EntityType) {
  379. $passedOptions = $child->getConfig()->getOptions();
  380. $classImplements = class_implements($passedOptions['class']);
  381. $isFilterMerchantInterface = in_array('Lc\ShopBundle\Context\FilterMerchantInterface', $classImplements);
  382. $isFilterMultipleMerchantsInterface = in_array('Lc\ShopBundle\Context\FilterMultipleMerchantsInterface', $classImplements);
  383. if ($isFilterMerchantInterface || $isFilterMultipleMerchantsInterface) {
  384. if (in_array('Lc\ShopBundle\Context\StatusInterface', $classImplements)) {
  385. $statusInterface = true;
  386. }
  387. if (in_array('Lc\ShopBundle\Context\TreeInterface', $classImplements)) {
  388. $treeInterface = true;
  389. }
  390. $propertyMerchant = 'merchant';
  391. if ($isFilterMultipleMerchantsInterface) {
  392. $propertyMerchant .= 's';
  393. }
  394. $form->add($child->getName(), EntityType::class, array(
  395. 'class' => $this->em->getClassMetadata($passedOptions['class'])->getName(),
  396. 'label' => $passedOptions['label'],
  397. 'expanded' => isset($passedOptions['expanded']) ? $passedOptions['expanded'] : false,
  398. 'multiple' => isset($passedOptions['multiple']) ? $passedOptions['multiple'] : false,
  399. 'placeholder' => '--',
  400. 'translation_domain' => 'lcshop',
  401. 'query_builder' => function (EntityRepository $repo) use ($passedOptions, $propertyMerchant, $statusInterface, $treeInterface, $child) {
  402. $queryBuilder = $repo->createQueryBuilder('e');
  403. $propertyMerchant = 'e.' . $propertyMerchant;
  404. if ($passedOptions['class'] == 'App\Entity\PointSale') {
  405. $queryBuilder->where(':currentMerchant MEMBER OF ' . $propertyMerchant);
  406. } else {
  407. $queryBuilder->where($propertyMerchant . ' = :currentMerchant');
  408. }
  409. if ($statusInterface) {
  410. $queryBuilder->andWhere('e.status >= 0');
  411. }
  412. if ($treeInterface && $child->getName() == 'parent') {
  413. $queryBuilder->andWhere('e.parent is null');
  414. }
  415. $queryBuilder->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
  416. return $queryBuilder;
  417. },
  418. 'choice_label' => function ($choice) {
  419. if ($choice instanceof StatusInterface && $choice->getStatus() == 0) {
  420. return $choice . ' [hors ligne]';
  421. }
  422. return $choice;
  423. },
  424. 'required' => $passedOptions['required'],
  425. 'choice_attr' => $passedOptions['choice_attr'],
  426. )
  427. );
  428. }
  429. } else {
  430. $subChildrens = $child->all();
  431. if (count($subChildrens)) {
  432. $this->loopFormField($child, $subChildrens);
  433. }
  434. }
  435. }
  436. }
  437. protected function editAction()
  438. {
  439. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  440. $id = $this->request->query->get('id');
  441. $easyadmin = $this->request->attributes->get('easyadmin');
  442. $entity = $easyadmin['item'];
  443. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  444. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  445. $fieldsMetadata = $this->entity['list']['fields'];
  446. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  447. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  448. }
  449. $this->updateEntityProperty($entity, $property, $newValue);
  450. $this->utils->addFlash('success', 'success.common.fieldChange');
  451. $response['flashMessages'] = $this->utils->getFlashMessages();
  452. return new Response(json_encode($response));
  453. }
  454. $fields = $this->entity['edit']['fields'];
  455. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  456. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  457. $editForm->handleRequest($this->request);
  458. if ($editForm->isSubmitted() && $editForm->isValid()) {
  459. $this->processUploadedFiles($editForm);
  460. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  461. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  462. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  463. return $this->redirectToReferrer();
  464. }
  465. $this->dispatch(EasyAdminEvents::POST_EDIT);
  466. $parameters = [
  467. 'form' => $editForm->createView(),
  468. 'entity_fields' => $fields,
  469. 'entity' => $entity,
  470. 'delete_form' => $deleteForm->createView(),
  471. ];
  472. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  473. }
  474. /* public function createNewEntity(){
  475. $idDuplicate = $this->request->query->get('duplicate', null);
  476. if($idDuplicate){
  477. $easyadmin = $this->request->attributes->get('easyadmin');
  478. $entity= $this->em->getRepository($easyadmin['entity']['class'])->find($idDuplicate);
  479. $newProductFamily = clone $entity ;
  480. $this->em->persist($newProductFamily) ;
  481. $this->em->flush() ;
  482. }else{
  483. $entityFullyQualifiedClassName = $this->entity['class'];
  484. return new $entityFullyQualifiedClassName();
  485. }
  486. }*/
  487. public function duplicateAction(){
  488. $id = $this->request->query->get('id');
  489. $refererUrl = $this->request->query->get('referer', '');
  490. $easyadmin = $this->request->attributes->get('easyadmin');
  491. $entity= $this->em->getRepository($easyadmin['entity']['class'])->find($id);
  492. $newEntity = $this->utilsProcess->duplicateEntity($entity);
  493. return $this->redirectToRoute('easyadmin', ['entity' => $easyadmin['entity']['name'], 'action' => 'edit', 'id' =>$newEntity->getId(), 'referer' =>$refererUrl ]) ;
  494. }
  495. public function duplicateOtherHubAction(){
  496. $id = $this->request->query->get('id');
  497. $hubAlias = $this->request->query->get('hub');
  498. $refererUrl = $this->request->query->get('referer', '');
  499. $user = $this->security->getUser() ;
  500. $easyadmin = $this->request->attributes->get('easyadmin');
  501. $entity= $this->em->getRepository($easyadmin['entity']['class'])->find($id);
  502. $hub= $this->em->getRepository(MerchantInterface::class)->findOneByDevAlias($hubAlias);
  503. $newEntity = $this->utilsProcess->duplicateEntityToOtherHub($entity,$hub);
  504. $user->setMerchant($hub);
  505. $this->em->persist($user);
  506. $this->em->flush();
  507. $redirectUrl = $this->generateUrl('easyadmin', ['entity' => $easyadmin['entity']['name'], 'action' => 'edit', 'id' =>$newEntity->getId(), 'referer' =>$refererUrl ]).'&hubredirection=true';
  508. return $this->redirectToOtherHub($hub, $redirectUrl) ;
  509. }
  510. public function redirectToOtherHub($hub, $url){
  511. if(strpos($_SERVER['HTTP_HOST'], 'localhost')!==false){
  512. return $this->redirect($url);
  513. }else{
  514. return $this->redirect($hub->getMerchantConfig('url').substr($url,1));
  515. }
  516. }
  517. }