No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

704 líneas
34KB

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