您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AdminController.php 32KB

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