Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

UserMerchantAdminController.php 10KB

vor 3 Jahren
vor 3 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\User;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
  5. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
  10. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  11. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  12. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
  13. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  14. use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
  15. use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
  16. use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
  17. use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
  18. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  19. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  20. use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
  21. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  22. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  23. use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
  24. use Lc\CaracoleBundle\Container\User\UserMerchantContainer;
  25. use Lc\CaracoleBundle\Controller\AdminControllerTrait;
  26. use Lc\CaracoleBundle\Factory\User\UserFactory;
  27. use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
  28. use Lc\CaracoleBundle\Form\Credit\CreditHistoryFormType;
  29. use Lc\CaracoleBundle\Form\User\UserMerchantFormType;
  30. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  31. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  32. use Lc\SovBundle\Container\User\UserContainer;
  33. use Lc\SovBundle\Controller\AbstractAdminController;
  34. use Lc\SovBundle\Field\BooleanField;
  35. use Lc\SovBundle\Field\ToggleField;
  36. use Lc\SovBundle\Model\User\UserInterface;
  37. use Lc\SovBundle\Translation\TranslatorAdmin;
  38. use Symfony\Component\HttpFoundation\Response;
  39. abstract class UserMerchantAdminController extends AbstractAdminController
  40. {
  41. use AdminControllerTrait;
  42. public function overrideEntitiesActions(?EntityCollection $entities): void
  43. {
  44. $context = $this->get(AdminContextProvider::class)->getContext();
  45. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  46. $creditControllerFqcn = $context->getCrudControllers()->findCrudFqcnByEntityFqcn(
  47. $this->get(EntityManagerInterface::class)->getEntityName(CreditHistoryInterface::class)
  48. );
  49. if ($entities) {
  50. foreach ($entities as $entity) {
  51. foreach ($entity->getActions() as $action) {
  52. if ($action->getName() == 'credit_history') {
  53. $url = $adminUrlGenerator
  54. ->setController($creditControllerFqcn)
  55. ->set('userMerchantId', $entity->getInstance()->getId())
  56. ->generateUrl();
  57. $action->setLinkUrl($url);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. public function configureActions(Actions $actions): Actions
  64. {
  65. $actions = parent::configureActions($actions);
  66. $creditAction = Action::new('credit_history', false, 'fa fa-cash-register')
  67. ->linkToCrudAction('credit_history')
  68. ->setHtmlAttributes(
  69. array(
  70. 'data-toggle' => 'tooltip',
  71. 'title' => $this->get(TranslatorAdmin::class)->transAction('credit'),
  72. )
  73. )
  74. ->setCssClass('btn btn-sm btn-success');
  75. $actions->add(Crud::PAGE_INDEX, $creditAction);
  76. return $actions;
  77. }
  78. public function configureFields(string $pageName): iterable
  79. {
  80. $fields = [
  81. TextField::new('user.email'),
  82. TextField::new('user.lastname'),
  83. TextField::new('user.firstname'),
  84. BooleanField::new('active'),
  85. BooleanField::new('creditActive'),
  86. AssociationField::new('user'),
  87. ];
  88. if ($this->isGranted('ROLE_SUPER_ADMIN')) {
  89. $fields[] = ArrayField::new('roles');
  90. }
  91. return $fields;
  92. }
  93. public function configureFilters(Filters $filters): Filters
  94. {
  95. return $filters
  96. ->add(BooleanFilter::new('active'));
  97. }
  98. public function new(AdminContext $context): Response
  99. {
  100. $entityManager = $this->get(EntityManagerInterface::class);
  101. $merchantResolver = $this->get(MerchantResolver::class);
  102. $userMerchant = $this->get(UserMerchantContainer::class)
  103. ->getFactory()
  104. ->create($merchantResolver->getCurrent());
  105. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  106. $form->handleRequest($context->getRequest());
  107. if ($form->isSubmitted() && $form->isValid()) {
  108. $userMerchant = $form->getData();
  109. $existingUser = $this->get(UserContainer::class)->getStore()->getOneByEmail($form->get('email')->getData());
  110. //Le user n'existe pas, on le créer
  111. if ($existingUser == null) {
  112. $param['email'] = $form->get('email')->getData();
  113. $param['lastname'] = $form->get('lastname')->getData();
  114. $param['lastname'] = $form->get('firstname')->getData();
  115. $param['roles'] = array();
  116. // @TODO : à adapter l'array de valeur passer au factory
  117. $userFactory = new UserFactory();
  118. $user = $userFactory->create($param);
  119. $entityManager->create($user);
  120. $userMerchant->setUser($user);
  121. $entityManager->create($userMerchant);
  122. $entityManager->flush();
  123. $this->addFlash('success', $this->get(TranslatorAdmin::class)->trans('form.user_merchant.create'));
  124. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  125. return $this->redirect($url);
  126. } else {
  127. //Le user existe, on vérifie si le usemerchant existe aussi
  128. $existingUserMerchant = $merchantResolver->getUserMerchant($existingUser);
  129. if ($existingUserMerchant == null) {
  130. $userMerchant->setUser($existingUser);
  131. $entityManager->create($userMerchant);
  132. $entityManager->flush();
  133. $this->addFlash('success', $this->get(TranslatorAdmin::class)->trans('form.user_merchant.linked'));
  134. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  135. return $this->redirect($url);
  136. } else {
  137. $this->addFlash('error', $this->get(TranslatorAdmin::class)->trans('form.user_merchant.already_exist'));
  138. }
  139. }
  140. }
  141. return $this->render(
  142. '@LcCaracole/admin/user/new_usermerchant.html.twig',
  143. [
  144. 'form' => $form->createView(),
  145. ]
  146. );
  147. }
  148. public function edit(AdminContext $context): Response
  149. {
  150. $entityManager = $this->get(EntityManagerInterface::class);
  151. $userMerchant = $context->getEntity()->getInstance();
  152. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  153. $form->handleRequest($context->getRequest());
  154. if ($form->isSubmitted() && $form->isValid()) {
  155. $userMerchant = $form->getData();
  156. $userMerchant->getUser()->setEmail($form->get('email')->getData());
  157. $userMerchant->getUser()->setLastName($form->get('lastname')->getData());
  158. $userMerchant->getUser()->setFirstname($form->get('firstname')->getData());
  159. $entityManager->update($userMerchant);
  160. $entityManager->update($userMerchant->getUser());
  161. $entityManager->flush();
  162. $this->addFlash('success', $this->get(TranslatorAdmin::class)->trans('form.user_merchant.update'));
  163. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  164. return $this->redirect($url);
  165. } else {
  166. $form->get('email')->setData($userMerchant->getUser()->getEmail());
  167. $form->get('lastname')->setData($userMerchant->getUser()->getLastname());
  168. $form->get('firstname')->setData($userMerchant->getUser()->getFirstname());
  169. }
  170. return $this->render(
  171. '@LcCaracole/admin/user/edit_usermerchant.html.twig',
  172. [
  173. 'form' => $form->createView(),
  174. ]
  175. );
  176. }
  177. public function credit(AdminContext $context, AdminUrlGenerator $adminUrlGenerator): Response
  178. {
  179. $event = new BeforeCrudActionEvent($context);
  180. $this->get('event_dispatcher')->dispatch($event);
  181. if ($event->isPropagationStopped()) {
  182. return $event->getResponse();
  183. }
  184. if (!$this->isGranted(
  185. Permission::EA_EXECUTE_ACTION,
  186. ['action' => Action::DETAIL, 'entity' => $context->getEntity()]
  187. )) {
  188. throw new ForbiddenActionException($context);
  189. }
  190. if (!$context->getEntity()->isAccessible()) {
  191. throw new InsufficientEntityPermissionException($context);
  192. }
  193. $options['action'] = $adminUrlGenerator
  194. ->setController($context->getCrud()->getControllerFqcn())
  195. ->setAction('add_credit')
  196. ->setEntityId($context->getEntity()->getInstance()->getId())
  197. ->set('userMerchant', 'niche')
  198. ->generateUrl();
  199. $addCreditHistoryForm = $this->createForm(CreditHistoryFormType::class, null, $options)->createView();
  200. return $this->render(
  201. '@LcCaracole/admin/credit/credit_detail.html.twig',
  202. [
  203. 'pageName' => Crud::PAGE_DETAIL,
  204. 'entity' => $context->getEntity(),
  205. 'batch_actions' => array(),
  206. 'entities' => array(),
  207. 'filters' => array(),
  208. 'paginator' => array(),
  209. 'add_credit_history_form' => $addCreditHistoryForm,
  210. ]
  211. );
  212. }
  213. }