Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

245 lines
9.9KB

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