Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

261 linhas
10KB

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