creditUtils = $creditUtils; } public function addCreditHistoryAction(){ $easyadmin = $this->request->attributes->get('easyadmin'); $userMerchant = $easyadmin['item']; $creditHistoryClass = $this->em->getClassMetadata(CreditHistoryInterface::class); $creditHistory = new $creditHistoryClass->name; $addCreditHistoryForm = $this->createForm(CreditHistoryType::class, $creditHistory); $addCreditHistoryForm->handleRequest($this->request); if ($addCreditHistoryForm->isSubmitted() && $addCreditHistoryForm->isValid()) { $creditHistory->setUserMerchant($userMerchant); dump($creditHistory); if($this->creditUtils->saveCreditHistory($creditHistory)){ $this->utils->addFlash('warning', 'success.credit.added'); }else{ $this->utils->addFlash('warning', 'error.credit.notAdded'); } } else { $this->utils->addFlash('error', $addCreditHistoryForm->getErrors()); } return $this->redirectToRoute('easyadmin', [ 'action' => 'show', 'entity' => $this->request->query->get('entity'), 'id' => $userMerchant->getId() ]); } protected function newAction() { $this->dispatch(EasyAdminEvents::PRE_NEW); $entity = $this->executeDynamicMethod('createNewEntity'); $easyadmin = $this->request->attributes->get('easyadmin'); $easyadmin['item'] = $entity; $this->request->attributes->set('easyadmin', $easyadmin); $fields = $this->entity['new']['fields']; $newForm = $this->executeDynamicMethod('createNewForm', [$entity, $fields]); $newForm->handleRequest($this->request); //ETAPE 1 Choix de l'utilisateur $user = $newForm->get('user')->getData(); if ($user == null) { $user = $this->getUserViaFirstStepForm($entity); } if (!$user instanceof UserInterface) return $user; else { $userMerchant = $this->creditUtils->activeCredit($user); return $this->redirectToRoute('easyadmin', [ 'action' => 'edit', 'entity' => $this->entity['name'], 'id' => $userMerchant->getId() ]); } } public function getUserViaFirstStepForm($entity) { $userClass = $this->em->getClassMetadata(UserInterface::class); $userChoiceForm = $this->createFormBuilder($entity) ->add('user', EntityType::class, array( 'class' => $userClass->name )) ->add('nextStep', SubmitType::class) ->getForm(); $userChoiceForm->handleRequest($this->request); if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) { return $userChoiceForm->get('user')->getData(); } $parameters = [ 'form' => $userChoiceForm->createView(), 'formView' => 'default', 'entity' => $entity, ]; return $this->executeDynamicMethod('renderTemplate', ['new', $this->entity['templates']['new'], $parameters]); } /** * The method that is executed when the user performs a 'show' action on an entity. * * @return Response */ public function showAction() { $this->dispatch(EasyAdminEvents::PRE_SHOW); $id = $this->request->query->get('id'); $easyadmin = $this->request->attributes->get('easyadmin'); $entity = $easyadmin['item']; $fields = $this->entity['show']['fields']; $this->dispatch(EasyAdminEvents::POST_SHOW, [ //'deleteForm' => $deleteForm, 'fields' => $fields, 'entity' => $entity, ]); $parameters = [ 'entity' => $entity, 'fields' => $fields ]; return $this->executeDynamicMethod('renderTemplate', ['show', $this->entity['templates']['show'], $parameters]); } public function renderUserMerchantTemplate($actionName, $templatePath, array $parameters = []){ if ($actionName == 'show') { $parameters['add_credit_history_form'] = $this->createCustomForm(CreditHistoryType::class, 'addCreditHistory', $parameters, false)->createView(); } return parent::renderTemplate($actionName, $templatePath, $parameters); } /** * Réécriture de edit action pr rediriger vers le show */ public function editAction() { $id = $this->request->query->get('id'); $entity = $this->request->query->get('entity'); return $this->redirectToRoute('easyadmin', [ 'action' => 'show', 'entity' => $entity, 'id' => $id ]); } }