get(AdminContextProvider::class)->getContext(); $adminUrlGenerator = $this->get(AdminUrlGenerator::class); $creditControllerFqcn = $context->getCrudControllers()->findCrudFqcnByEntityFqcn( $this->get(EntityManagerInterface::class)->getEntityName(CreditHistoryInterface::class) ); if ($entities) { foreach ($entities as $entity) { foreach ($entity->getActions() as $action) { if ($action->getName() == 'credit_history') { $url = $adminUrlGenerator ->setController($creditControllerFqcn) ->set('userMerchantId', $entity->getInstance()->getId()) ->generateUrl(); $action->setLinkUrl($url); } } } } } public function configureActions(Actions $actions): Actions { $actions = parent::configureActions($actions); $creditAction = Action::new('credit_history', false, 'fa fa-cash-register') ->linkToCrudAction('credit_history') ->setHtmlAttributes( array( 'data-toggle' => 'tooltip', 'title' => $this->get(TranslatorAdmin::class)->transAction('credit'), ) ) ->setCssClass('btn btn-sm btn-success'); $actions->add(Crud::PAGE_INDEX, $creditAction); return $actions; } public function configureFields(string $pageName): iterable { $fields = [ TextField::new('user.email'), TextField::new('user.lastname'), TextField::new('user.firstname'), BooleanField::new('active'), BooleanField::new('creditActive'), AssociationField::new('user'), ]; if ($this->isGranted('ROLE_SUPER_ADMIN')) { $fields[] = ArrayField::new('roles'); } return $fields; } public function configureFilters(Filters $filters): Filters { return $filters ->add(BooleanFilter::new('active')); } public function new(AdminContext $context): Response { $entityManager = $this->get(EntityManagerInterface::class); $merchantResolver = $this->get(MerchantResolver::class); $userMerchant = $this->get(UserMerchantContainer::class) ->getFactory() ->create($merchantResolver->getCurrent()); $form = $this->createForm(UserMerchantFormType::class, $userMerchant); $form->handleRequest($context->getRequest()); if ($form->isSubmitted() && $form->isValid()) { $userMerchant = $form->getData(); $existingUser = $this->get(UserContainer::class)->getStore()->getOneByEmail($form->get('email')->getData()); //Le user n'existe pas, on le créer if ($existingUser == null) { $param['email'] = $form->get('email')->getData(); $param['lastname'] = $form->get('lastname')->getData(); $param['lastname'] = $form->get('firstname')->getData(); $param['roles'] = array(); // @TODO : à adapter l'array de valeur passer au factory $userFactory = new UserFactory(); $user = $userFactory->create($param); $entityManager->create($user); $userMerchant->setUser($user); $entityManager->create($userMerchant); $entityManager->flush(); $this->addFlash('success', $this->get(TranslatorAdmin::class)->trans('form.user_merchant.create')); $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl(); return $this->redirect($url); } else { //Le user existe, on vérifie si le usemerchant existe aussi $existingUserMerchant = $merchantResolver->getUserMerchant($existingUser); if ($existingUserMerchant == null) { $userMerchant->setUser($existingUser); $entityManager->create($userMerchant); $entityManager->flush(); $this->addFlash('success', $this->get(TranslatorAdmin::class)->trans('form.user_merchant.linked')); $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl(); return $this->redirect($url); } else { $this->addFlash('error', $this->get(TranslatorAdmin::class)->trans('form.user_merchant.already_exist')); } } } return $this->render( '@LcCaracole/admin/user/new_usermerchant.html.twig', [ 'form' => $form->createView(), ] ); } public function edit(AdminContext $context): Response { $entityManager = $this->get(EntityManagerInterface::class); $userMerchant = $context->getEntity()->getInstance(); $form = $this->createForm(UserMerchantFormType::class, $userMerchant); $form->handleRequest($context->getRequest()); if ($form->isSubmitted() && $form->isValid()) { $userMerchant = $form->getData(); $userMerchant->getUser()->setEmail($form->get('email')->getData()); $userMerchant->getUser()->setLastName($form->get('lastname')->getData()); $userMerchant->getUser()->setFirstname($form->get('firstname')->getData()); $entityManager->update($userMerchant); $entityManager->update($userMerchant->getUser()); $entityManager->flush(); $this->addFlash('success', $this->get(TranslatorAdmin::class)->trans('form.user_merchant.update')); $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl(); return $this->redirect($url); } else { $form->get('email')->setData($userMerchant->getUser()->getEmail()); $form->get('lastname')->setData($userMerchant->getUser()->getLastname()); $form->get('firstname')->setData($userMerchant->getUser()->getFirstname()); } return $this->render( '@LcCaracole/admin/user/edit_usermerchant.html.twig', [ 'form' => $form->createView(), ] ); } public function credit(AdminContext $context, AdminUrlGenerator $adminUrlGenerator): Response { $event = new BeforeCrudActionEvent($context); $this->get('event_dispatcher')->dispatch($event); if ($event->isPropagationStopped()) { return $event->getResponse(); } if (!$this->isGranted( Permission::EA_EXECUTE_ACTION, ['action' => Action::DETAIL, 'entity' => $context->getEntity()] )) { throw new ForbiddenActionException($context); } if (!$context->getEntity()->isAccessible()) { throw new InsufficientEntityPermissionException($context); } $options['action'] = $adminUrlGenerator ->setController($context->getCrud()->getControllerFqcn()) ->setAction('add_credit') ->setEntityId($context->getEntity()->getInstance()->getId()) ->set('userMerchant', 'niche') ->generateUrl(); $addCreditHistoryForm = $this->createForm(CreditHistoryFormType::class, null, $options)->createView(); return $this->render( '@LcCaracole/admin/credit/credit_detail.html.twig', [ 'pageName' => Crud::PAGE_DETAIL, 'entity' => $context->getEntity(), 'batch_actions' => array(), 'entities' => array(), 'filters' => array(), 'paginator' => array(), 'add_credit_history_form' => $addCreditHistoryForm, ] ); } }