@@ -3,6 +3,7 @@ | |||
namespace Lc\CaracoleBundle\Controller\User; | |||
use Doctrine\ORM\EntityManagerInterface; | |||
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; | |||
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; | |||
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField; | |||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; | |||
@@ -40,6 +41,13 @@ abstract class UserAdminController extends SovUserAdminController | |||
parent::__construct($session, $request, $em, $translatorAdmin, $rolesDefinition); | |||
} | |||
public function configureCrud(Crud $crud): Crud | |||
{ | |||
$crud->overrideTemplate('crud/new', '@LcCaracole/admin/user/user_new.html.twig'); | |||
return parent::configureCrud($crud); // TODO: Change the autogenerated stub | |||
} | |||
public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void | |||
{ | |||
$this->updateUserMerchantRoles($entityManager, $entityInstance); |
@@ -0,0 +1,108 @@ | |||
<?php | |||
namespace Lc\CaracoleBundle\Controller\User; | |||
use EasyCorp\Bundle\EasyAdminBundle\Config\Action; | |||
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; | |||
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField; | |||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; | |||
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator; | |||
use Lc\CaracoleBundle\Factory\User\UserMerchantFactory; | |||
use Lc\CaracoleBundle\Form\User\UserMerchantFormType; | |||
use Lc\CaracoleBundle\Resolver\MerchantResolver; | |||
use Lc\CaracoleBundle\Resolver\SectionResolver; | |||
use Lc\SovBundle\Controller\AbstractAdminController; | |||
use Lc\SovBundle\Factory\User\UserFactory; | |||
use Lc\SovBundle\Field\BooleanField; | |||
use Lc\SovBundle\Model\User\UserInterface; | |||
use Symfony\Component\HttpFoundation\Response; | |||
abstract class UserMerchantAdminController extends AbstractAdminController | |||
{ | |||
public static function getSubscribedServices() | |||
{ | |||
return array_merge(parent::getSubscribedServices(), [ | |||
'user_factory' => UserFactory::class, | |||
'user_merchant_factory' => UserMerchantFactory::class, | |||
'merchant_resolver' => MerchantResolver::class, | |||
'section_resolver' => SectionResolver::class, | |||
]); | |||
} | |||
public function configureFields(string $pageName): iterable | |||
{ | |||
return [ | |||
TextField::new('user.email'), | |||
TextField::new('user.lastname'), | |||
TextField::new('user.firstname'), | |||
BooleanField::new('active'), | |||
BooleanField::new('creditActive'), | |||
ArrayField::new('roles'), | |||
]; | |||
} | |||
public function new(AdminContext $context): Response | |||
{ | |||
$userFactory = $this->get('user_factory'); | |||
$userMerchantFactory = $this->get('user_merchant_factory'); | |||
$userRepository = $this->em->getRepository(UserInterface::class); | |||
$merchantResolver = $this->get('merchant_resolver'); | |||
$userMerchant= $userMerchantFactory->create(); | |||
$form = $this->createForm(UserMerchantFormType::class, $userMerchant); | |||
$form->handleRequest($context->getRequest()); | |||
if ($form->isSubmitted() && $form->isValid()) { | |||
$userMerchant = $form->getData(); | |||
$existingUser = $userRepository->findOneByEmail($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(); | |||
$user = $userFactory->create($param); | |||
$this->em->create($user); | |||
$userMerchant->setUser($user); | |||
$this->em->create($userMerchant); | |||
$this->em->flush(); | |||
$this->addFlash('success', $this->translatorAdmin->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); | |||
$this->em->create($userMerchant); | |||
$this->em->flush(); | |||
$this->addFlash('success', $this->translatorAdmin->trans('form.user_merchant.linked')); | |||
$url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl(); | |||
return $this->redirect($url); | |||
}else{ | |||
$this->addFlash('error', $this->translatorAdmin->trans('form.user_merchant.already_exist')); | |||
} | |||
} | |||
} | |||
return $this->render( | |||
'@LcCaracole/admin/user/usermerchant_new.html.twig', | |||
[ | |||
'form' => $form->createView(), | |||
] | |||
); | |||
} | |||
} |
@@ -31,6 +31,11 @@ class UserMerchantFactory extends AbstractFactory | |||
if(!isset($params['creditActive'])){ | |||
$params['creditActive'] =false; | |||
} | |||
if(!isset($params['active'])){ | |||
$params['active'] = true; | |||
} | |||
return parent::create($params); // TODO: Change the autogenerated stub | |||
} | |||
} |
@@ -0,0 +1,102 @@ | |||
<?php | |||
namespace Lc\CaracoleBundle\Form\User; | |||
use Lc\CaracoleBundle\Model\User\UserMerchantInterface; | |||
use Lc\SovBundle\Definition\RolesDefinitionInterface; | |||
use Lc\SovBundle\Doctrine\EntityManager; | |||
use Lc\SovBundle\Model\User\UserInterface; | |||
use Lc\SovBundle\Translation\TranslatorAdmin; | |||
use Symfony\Component\Form\AbstractType; | |||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | |||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | |||
use Symfony\Component\Form\Extension\Core\Type\EmailType; | |||
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | |||
use Symfony\Component\Form\Extension\Core\Type\TextType; | |||
use Symfony\Component\Form\FormBuilderInterface; | |||
use Symfony\Component\OptionsResolver\OptionsResolver; | |||
class UserMerchantFormType extends AbstractType | |||
{ | |||
protected $em; | |||
protected $rolesDefinition; | |||
protected $translatorAdmin; | |||
public function __construct( | |||
EntityManager $em, | |||
TranslatorAdmin $translatorAdmin, | |||
RolesDefinitionInterface $rolesDefinition | |||
) { | |||
$this->em = $em; | |||
$this->rolesDefinition = $rolesDefinition; | |||
$this->translatorAdmin = $translatorAdmin; | |||
} | |||
public function buildForm(FormBuilderInterface $builder, array $options) | |||
{ | |||
$entityName = $this->em->getEntityName(UserInterface::class); | |||
$builder->add( | |||
'firstname', | |||
TextType::class, | |||
array( | |||
'mapped' => false, | |||
) | |||
); | |||
$builder->add( | |||
'lastname', | |||
TextType::class, | |||
array( | |||
'mapped' => false, | |||
) | |||
); | |||
$builder->add( | |||
'email', | |||
EmailType::class, | |||
array( | |||
'mapped' => false, | |||
) | |||
); | |||
$builder->add( | |||
'creditActive', | |||
CheckboxType::class, | |||
array( | |||
'required'=>false | |||
) | |||
); | |||
$builder->add( | |||
'roles', | |||
ChoiceType::class, | |||
array( | |||
'choices' => $this->rolesDefinition->getRolesList(), | |||
'multiple'=> true | |||
) | |||
); | |||
$builder->add( | |||
'submit', | |||
SubmitType::class, | |||
[ | |||
'label' => $this->translatorAdmin->transAction('save'), | |||
] | |||
); | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function configureOptions(OptionsResolver $resolver) | |||
{ | |||
$resolver->setDefaults( | |||
[ | |||
'data_class' => $this->em->getEntityName(UserMerchantInterface::class), | |||
] | |||
); | |||
} | |||
} |
@@ -27,4 +27,18 @@ entity: | |||
TaxRate: | |||
label: Règle de taxe | |||
label_plurial: Règles de taxes | |||
UserMerchant: | |||
label: Utilisateur | |||
label_plurial: Utilisateurs | |||
fields: | |||
user: | |||
email: Email | |||
lastname: Nom | |||
firstname: Prénom | |||
creditActive: Crédit activer | |||
form: | |||
user_merchant: | |||
create: L'utilisateur a bien été créé | |||
linked: L'utilisateur existait déjà et a été lié à votre espace. | |||
already_exist: L'utilisateur existe déjà |
@@ -0,0 +1,13 @@ | |||
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #} | |||
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #} | |||
{% extends '@LcSov/adminlte/crud/new.html.twig' %} | |||
{% import '@LcSov/adminlte/macro/callout.html.twig' as cm %} | |||
{% block aside %} | |||
{{ cm.callout('info', 'Il est possible que l\'utilisateur existe déjà dans notre sytème, si c\'est le cas, importez le !', 'Utilisateur déjà existant ?') }} | |||
{% set url = ea_url() | |||
.setController('App\\Controller\\User\\UserMerchantAdminController') | |||
.setAction('new') | |||
%} | |||
<a href="{{ url }}">Importer</a> | |||
{% endblock %} |
@@ -0,0 +1,18 @@ | |||
{% extends '@LcCaracole/adminlte/layout.html.twig' %} | |||
{% block content_title %} | |||
{{ 'new'|lc_trans_admin_title(ea.getEntity().getFqcn()) }} | |||
{% endblock %} | |||
{% block main %} | |||
<div class="col-8"> | |||
{% embed '@LcSov/adminlte/embed/card.html.twig' %} | |||
{% block header_wrapper %}{% endblock %} | |||
{% block body %} | |||
{% form_theme form '@LcSov/adminlte/crud/form_theme.html.twig' %} | |||
{{ form(form) }} | |||
{% endblock %} | |||
{% block footer_wrapper %}{% endblock %} | |||
{% endembed %} | |||
</div> | |||
{% endblock main %} |