You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 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\Config\Action;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
  9. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  11. use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
  12. use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
  14. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
  16. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  17. use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
  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\Container\User\UserMerchantContainer;
  22. use Lc\CaracoleBundle\Controller\ControllerTrait;
  23. use Lc\CaracoleBundle\Definition\ActionDefinition;
  24. use Lc\CaracoleBundle\Factory\User\UserFactory;
  25. use Lc\CaracoleBundle\Form\Credit\CreditHistoryFormType;
  26. use Lc\CaracoleBundle\Form\User\UserMerchantFormType;
  27. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  28. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  29. use Lc\SovBundle\Container\User\UserContainer;
  30. use Lc\SovBundle\Controller\AbstractAdminController;
  31. use Lc\SovBundle\Field\BooleanField;
  32. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  33. use Lc\SovBundle\Translation\TranslatorAdmin;
  34. use Symfony\Component\HttpFoundation\Response;
  35. abstract class UserMerchantAdminController extends AbstractAdminController
  36. {
  37. use ControllerTrait;
  38. public function getRepositoryQuery(): RepositoryQueryInterface
  39. {
  40. return $this->getUserMerchantContainer()->getRepositoryQuery();
  41. }
  42. public function createEntity(string $entityFqcn)
  43. {
  44. return $this->getUserMerchantContainer()->getFactory()->createBase($this->getMerchantCurrent());
  45. }
  46. public function overrideEntitiesActions(?EntityCollection $entities): void
  47. {
  48. $context = $this->get(AdminContextProvider::class)->getContext();
  49. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  50. $creditControllerFqcn = $context->getCrudControllers()->findCrudFqcnByEntityFqcn(
  51. $this->get(EntityManagerInterface::class)->getEntityName(CreditHistoryInterface::class)
  52. );
  53. if ($entities) {
  54. foreach ($entities as $entity) {
  55. foreach ($entity->getActions() as $action) {
  56. if ($action->getName() == 'credit_history') {
  57. $url = $adminUrlGenerator
  58. ->setController($creditControllerFqcn)
  59. ->set('userMerchantId', $entity->getInstance()->getId())
  60. ->generateUrl();
  61. $action->setLinkUrl($url);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. public function configureActions(Actions $actions): Actions
  68. {
  69. $actions = parent::configureActions($actions);
  70. $creditAction = Action::new('credit_history', false, 'fa fa-cash-register')
  71. ->linkToCrudAction('credit_history')
  72. ->setHtmlAttributes(
  73. array(
  74. 'data-toggle' => 'tooltip',
  75. 'title' => $this->get(TranslatorAdmin::class)->transAction('credit'),
  76. )
  77. )
  78. ->setCssClass('btn btn-sm btn-success');
  79. $actions->add(Crud::PAGE_INDEX, $creditAction);
  80. return $actions;
  81. }
  82. public function configureFields(string $pageName): iterable
  83. {
  84. $fields = [
  85. IntegerField::new('id')->onlyOnIndex()->setSortable(true),
  86. TextField::new('user.lastname')->setSortable(true),
  87. TextField::new('user.firstname')->setSortable(true),
  88. TextField::new('user.email')->setSortable(true),
  89. BooleanField::new('active')->setSortable(true),
  90. BooleanField::new('creditActive')->hideOnIndex(),
  91. AssociationField::new('user'),
  92. ];
  93. if ($this->isGranted('ROLE_SUPER_ADMIN')) {
  94. $fields[] = ArrayField::new('roles');
  95. }
  96. return $fields;
  97. }
  98. public function configureFilters(Filters $filters): Filters
  99. {
  100. return $filters
  101. ->add(BooleanFilter::new('active'));
  102. }
  103. public function new(AdminContext $context): Response
  104. {
  105. $entityManager = $this->get(EntityManagerInterface::class);
  106. $merchantResolver = $this->get(MerchantResolver::class);
  107. $userMerchant = $this->get(UserMerchantContainer::class)
  108. ->getFactory()
  109. ->create($merchantResolver->getCurrent());
  110. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  111. $form->handleRequest($context->getRequest());
  112. if ($form->isSubmitted() && $form->isValid()) {
  113. $userMerchant = $form->getData();
  114. $existingUser = $this->get(UserContainer::class)->getStore()->getOneByEmail($form->get('email')->getData());
  115. //Le user n'existe pas, on le créer
  116. if ($existingUser == null) {
  117. $param['email'] = $form->get('email')->getData();
  118. $param['lastname'] = $form->get('lastname')->getData();
  119. $param['lastname'] = $form->get('firstname')->getData();
  120. $param['roles'] = array();
  121. // @TODO : à adapter l'array de valeur passer au factory
  122. $userFactory = new UserFactory();
  123. $user = $userFactory->create($param);
  124. $entityManager->create($user);
  125. $userMerchant->setUser($user);
  126. $entityManager->create($userMerchant);
  127. $entityManager->flush();
  128. $this->addFlashTranslator('success', 'created');
  129. $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::INDEX)->generateUrl();
  130. return $this->redirect($url);
  131. } else {
  132. //Le user existe, on vérifie si le usemerchant existe aussi
  133. $existingUserMerchant = $merchantResolver->getUserMerchant($existingUser);
  134. if ($existingUserMerchant == null) {
  135. $userMerchant->setUser($existingUser);
  136. $entityManager->create($userMerchant);
  137. $entityManager->flush();
  138. $this->addFlashTranslator('success', 'linked');
  139. $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::INDEX)->generateUrl();
  140. return $this->redirect($url);
  141. } else {
  142. $this->addFlashTranslator('error', 'already_exist');
  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->addFlashTranslator('success', 'updated');
  168. $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::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' => ActionDefinition::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. }