|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
-
- namespace Lc\CaracoleBundle\Controller\User;
-
- use Doctrine\ORM\EntityManagerInterface;
- use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
- use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
- use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
- use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
- use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
- use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
- use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
- use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
- use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
- use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
- use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
- use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
- use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
- use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
- use Lc\CaracoleBundle\Container\User\UserMerchantContainer;
- use Lc\CaracoleBundle\Controller\AdminControllerTrait;
- use Lc\CaracoleBundle\Definition\ActionDefinition;
- use Lc\CaracoleBundle\Factory\User\UserFactory;
- use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
- use Lc\CaracoleBundle\Form\Credit\CreditHistoryFormType;
- use Lc\CaracoleBundle\Form\User\UserMerchantFormType;
- use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
- use Lc\CaracoleBundle\Resolver\MerchantResolver;
- use Lc\SovBundle\Container\User\UserContainer;
- use Lc\SovBundle\Controller\AbstractAdminController;
- use Lc\SovBundle\Field\BooleanField;
- use Lc\SovBundle\Field\ToggleField;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\SovBundle\Translation\TranslatorAdmin;
- use Symfony\Component\HttpFoundation\Response;
-
- abstract class UserMerchantAdminController extends AbstractAdminController
- {
- use AdminControllerTrait;
-
-
- public function overrideEntitiesActions(?EntityCollection $entities): void
- {
- $context = $this->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 = [
- IntegerField::new('id')->onlyOnIndex()->setSortable(true),
- TextField::new('user.lastname')->setSortable(true),
- TextField::new('user.firstname')->setSortable(true),
- TextField::new('user.email')->setSortable(true),
- BooleanField::new('active')->setSortable(true),
- BooleanField::new('creditActive')->hideOnIndex(),
- 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->addFlashTranslator('success','created');
- $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::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->addFlashTranslator('success', 'linked');
- $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::INDEX)->generateUrl();
-
- return $this->redirect($url);
- } else {
- $this->addFlashTranslator('error','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->addFlashTranslator('success', 'updated');
- $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::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' => ActionDefinition::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,
- ]
- );
- }
-
- }
|