Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

261 lines
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\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\Controller\AdminControllerTrait;
  25. use Lc\CaracoleBundle\Form\Credit\CreditHistoryFormType;
  26. use Lc\CaracoleBundle\Form\User\UserMerchantFormType;
  27. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  28. use Lc\SovBundle\Controller\AbstractAdminController;
  29. use Lc\SovBundle\Field\BooleanField;
  30. use Lc\SovBundle\Field\ToggleField;
  31. use Lc\SovBundle\Model\User\UserInterface;
  32. use Lc\SovBundle\Translation\TranslatorAdmin;
  33. use Symfony\Component\HttpFoundation\Response;
  34. abstract class UserMerchantAdminController extends AbstractAdminController
  35. {
  36. use AdminControllerTrait;
  37. protected $em;
  38. protected $translatorAdmin;
  39. public function __construct(EntityManagerInterface $entityManager, TranslatorAdmin $translatorAdmin)
  40. {
  41. $this->em = $entityManager;
  42. $this->translatorAdmin = $translatorAdmin;
  43. }
  44. public function overrideEntitiesActions(?EntityCollection $entities): void
  45. {
  46. $context = $this->get(AdminContextProvider::class)->getContext();
  47. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  48. $creditControllerFqcn = $context->getCrudControllers()->findCrudFqcnByEntityFqcn(
  49. $this->em->getEntityName(CreditHistoryInterface::class)
  50. );
  51. if ($entities) {
  52. foreach ($entities as $entity) {
  53. foreach ($entity->getActions() as $action) {
  54. if ($action->getName() == 'credit_history') {
  55. $url = $adminUrlGenerator
  56. ->setController($creditControllerFqcn)
  57. ->set('userMerchantId', $entity->getInstance()->getId())
  58. ->generateUrl();
  59. $action->setLinkUrl($url);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. public function configureActions(Actions $actions): Actions
  66. {
  67. $actions = parent::configureActions($actions);
  68. $creditAction = Action::new('credit_history', false, 'fa fa-cash-register')
  69. ->linkToCrudAction('credit_history')
  70. ->setHtmlAttributes(
  71. array(
  72. 'data-toggle' => 'tooltip',
  73. 'title' => $this->translatorAdmin->transAction('credit'),
  74. )
  75. )
  76. ->setCssClass('btn btn-sm btn-success');
  77. $actions->add(Crud::PAGE_INDEX, $creditAction);
  78. return $actions;
  79. }
  80. public function configureFields(string $pageName): iterable
  81. {
  82. $fields = [
  83. TextField::new('user.email'),
  84. TextField::new('user.lastname'),
  85. TextField::new('user.firstname'),
  86. BooleanField::new('active'),
  87. BooleanField::new('creditActive'),
  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. }
  101. public function new(AdminContext $context): Response
  102. {
  103. $entityManager = $this->get('em');
  104. $userFactory = $this->get('user_factory');
  105. $userMerchantFactory = $this->get('user_merchant_factory');
  106. $userRepository = $entityManager->getRepository(UserInterface::class);
  107. $merchantResolver = $this->get('merchant_resolver');
  108. $userMerchant = $userMerchantFactory->create();
  109. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  110. $form->handleRequest($context->getRequest());
  111. if ($form->isSubmitted() && $form->isValid()) {
  112. $userMerchant = $form->getData();
  113. $existingUser = $userRepository->findOneByEmail($form->get('email')->getData());
  114. //Le user n'existe pas, on le créer
  115. if ($existingUser == null) {
  116. $param['email'] = $form->get('email')->getData();
  117. $param['lastname'] = $form->get('lastname')->getData();
  118. $param['lastname'] = $form->get('firstname')->getData();
  119. $param['roles'] = array();
  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('translator_admin')->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('translator_admin')->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('error', $this->get('translator_admin')->trans('form.user_merchant.already_exist'));
  140. }
  141. }
  142. }
  143. return $this->render(
  144. '@LcCaracole/admin/user/new_usermerchant.html.twig',
  145. [
  146. 'form' => $form->createView(),
  147. ]
  148. );
  149. }
  150. public function edit(AdminContext $context): Response
  151. {
  152. $entityManager = $this->get('em');
  153. $userRepository = $entityManager->getRepository(UserInterface::class);
  154. $merchantResolver = $this->get('merchant_resolver');
  155. $userMerchant = $context->getEntity()->getInstance();
  156. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  157. $form->handleRequest($context->getRequest());
  158. if ($form->isSubmitted() && $form->isValid()) {
  159. $userMerchant = $form->getData();
  160. $userMerchant->getUser()->setEmail($form->get('email')->getData());
  161. $userMerchant->getUser()->setLastName($form->get('lastname')->getData());
  162. $userMerchant->getUser()->setFirstname($form->get('firstname')->getData());
  163. $entityManager->update($userMerchant);
  164. $entityManager->update($userMerchant->getUser());
  165. $entityManager->flush();
  166. $this->addFlash('success', $this->get('translator_admin')->trans('form.user_merchant.update'));
  167. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  168. return $this->redirect($url);
  169. } else {
  170. $form->get('email')->setData($userMerchant->getUser()->getEmail());
  171. $form->get('lastname')->setData($userMerchant->getUser()->getLastname());
  172. $form->get('firstname')->setData($userMerchant->getUser()->getFirstname());
  173. }
  174. return $this->render(
  175. '@LcCaracole/admin/user/edit_usermerchant.html.twig',
  176. [
  177. 'form' => $form->createView(),
  178. ]
  179. );
  180. }
  181. public function credit(AdminContext $context, AdminUrlGenerator $adminUrlGenerator): Response
  182. {
  183. $event = new BeforeCrudActionEvent($context);
  184. $this->get('event_dispatcher')->dispatch($event);
  185. if ($event->isPropagationStopped()) {
  186. return $event->getResponse();
  187. }
  188. if (!$this->isGranted(
  189. Permission::EA_EXECUTE_ACTION,
  190. ['action' => Action::DETAIL, 'entity' => $context->getEntity()]
  191. )) {
  192. throw new ForbiddenActionException($context);
  193. }
  194. if (!$context->getEntity()->isAccessible()) {
  195. throw new InsufficientEntityPermissionException($context);
  196. }
  197. $options['action'] = $adminUrlGenerator
  198. ->setController($context->getCrud()->getControllerFqcn())
  199. ->setAction('add_credit')
  200. ->setEntityId($context->getEntity()->getInstance()->getId())
  201. ->set('userMerchant', 'niche')
  202. ->generateUrl();
  203. $addCreditHistoryForm = $this->createForm(CreditHistoryFormType::class, null, $options)->createView();
  204. return $this->render(
  205. '@LcCaracole/admin/credit/credit_detail.html.twig',
  206. [
  207. 'pageName' => Crud::PAGE_DETAIL,
  208. 'entity' => $context->getEntity(),
  209. 'batch_actions' => array(),
  210. 'entities' => array(),
  211. 'filters' => array(),
  212. 'paginator' => array(),
  213. 'add_credit_history_form' => $addCreditHistoryForm,
  214. ]
  215. );
  216. }
  217. }