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.

UserMerchantAdminController.php 10KB

3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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\Definition\ActionDefinition;
  28. use Lc\CaracoleBundle\Factory\User\UserFactory;
  29. use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
  30. use Lc\CaracoleBundle\Form\Credit\CreditHistoryFormType;
  31. use Lc\CaracoleBundle\Form\User\UserMerchantFormType;
  32. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  33. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  34. use Lc\SovBundle\Container\User\UserContainer;
  35. use Lc\SovBundle\Controller\AbstractAdminController;
  36. use Lc\SovBundle\Field\BooleanField;
  37. use Lc\SovBundle\Field\ToggleField;
  38. use Lc\SovBundle\Model\User\UserInterface;
  39. use Lc\SovBundle\Translation\TranslatorAdmin;
  40. use Symfony\Component\HttpFoundation\Response;
  41. abstract class UserMerchantAdminController extends AbstractAdminController
  42. {
  43. use AdminControllerTrait;
  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->get(EntityManagerInterface::class)->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->get(TranslatorAdmin::class)->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. IntegerField::new('id')->onlyOnIndex()->setSortable(true),
  84. TextField::new('user.lastname')->setSortable(true),
  85. TextField::new('user.firstname')->setSortable(true),
  86. TextField::new('user.email')->setSortable(true),
  87. BooleanField::new('active')->setSortable(true),
  88. BooleanField::new('creditActive')->hideOnIndex(),
  89. AssociationField::new('user'),
  90. ];
  91. if ($this->isGranted('ROLE_SUPER_ADMIN')) {
  92. $fields[] = ArrayField::new('roles');
  93. }
  94. return $fields;
  95. }
  96. public function configureFilters(Filters $filters): Filters
  97. {
  98. return $filters
  99. ->add(BooleanFilter::new('active'));
  100. }
  101. public function new(AdminContext $context): Response
  102. {
  103. $entityManager = $this->get(EntityManagerInterface::class);
  104. $merchantResolver = $this->get(MerchantResolver::class);
  105. $userMerchant = $this->get(UserMerchantContainer::class)
  106. ->getFactory()
  107. ->create($merchantResolver->getCurrent());
  108. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  109. $form->handleRequest($context->getRequest());
  110. if ($form->isSubmitted() && $form->isValid()) {
  111. $userMerchant = $form->getData();
  112. $existingUser = $this->get(UserContainer::class)->getStore()->getOneByEmail($form->get('email')->getData());
  113. //Le user n'existe pas, on le créer
  114. if ($existingUser == null) {
  115. $param['email'] = $form->get('email')->getData();
  116. $param['lastname'] = $form->get('lastname')->getData();
  117. $param['lastname'] = $form->get('firstname')->getData();
  118. $param['roles'] = array();
  119. // @TODO : à adapter l'array de valeur passer au factory
  120. $userFactory = new UserFactory();
  121. $user = $userFactory->create($param);
  122. $entityManager->create($user);
  123. $userMerchant->setUser($user);
  124. $entityManager->create($userMerchant);
  125. $entityManager->flush();
  126. $this->addFlashTranslator('success','created');
  127. $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::INDEX)->generateUrl();
  128. return $this->redirect($url);
  129. } else {
  130. //Le user existe, on vérifie si le usemerchant existe aussi
  131. $existingUserMerchant = $merchantResolver->getUserMerchant($existingUser);
  132. if ($existingUserMerchant == null) {
  133. $userMerchant->setUser($existingUser);
  134. $entityManager->create($userMerchant);
  135. $entityManager->flush();
  136. $this->addFlashTranslator('success', 'linked');
  137. $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::INDEX)->generateUrl();
  138. return $this->redirect($url);
  139. } else {
  140. $this->addFlashTranslator('error','already_exist');
  141. }
  142. }
  143. }
  144. return $this->render(
  145. '@LcCaracole/admin/user/new_usermerchant.html.twig',
  146. [
  147. 'form' => $form->createView(),
  148. ]
  149. );
  150. }
  151. public function edit(AdminContext $context): Response
  152. {
  153. $entityManager = $this->get(EntityManagerInterface::class);
  154. $userMerchant = $context->getEntity()->getInstance();
  155. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  156. $form->handleRequest($context->getRequest());
  157. if ($form->isSubmitted() && $form->isValid()) {
  158. $userMerchant = $form->getData();
  159. $userMerchant->getUser()->setEmail($form->get('email')->getData());
  160. $userMerchant->getUser()->setLastName($form->get('lastname')->getData());
  161. $userMerchant->getUser()->setFirstname($form->get('firstname')->getData());
  162. $entityManager->update($userMerchant);
  163. $entityManager->update($userMerchant->getUser());
  164. $entityManager->flush();
  165. $this->addFlashTranslator('success', 'updated');
  166. $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::INDEX)->generateUrl();
  167. return $this->redirect($url);
  168. } else {
  169. $form->get('email')->setData($userMerchant->getUser()->getEmail());
  170. $form->get('lastname')->setData($userMerchant->getUser()->getLastname());
  171. $form->get('firstname')->setData($userMerchant->getUser()->getFirstname());
  172. }
  173. return $this->render(
  174. '@LcCaracole/admin/user/edit_usermerchant.html.twig',
  175. [
  176. 'form' => $form->createView(),
  177. ]
  178. );
  179. }
  180. public function credit(AdminContext $context, AdminUrlGenerator $adminUrlGenerator): Response
  181. {
  182. $event = new BeforeCrudActionEvent($context);
  183. $this->get('event_dispatcher')->dispatch($event);
  184. if ($event->isPropagationStopped()) {
  185. return $event->getResponse();
  186. }
  187. if (!$this->isGranted(
  188. Permission::EA_EXECUTE_ACTION,
  189. ['action' => ActionDefinition::DETAIL, 'entity' => $context->getEntity()]
  190. )) {
  191. throw new ForbiddenActionException($context);
  192. }
  193. if (!$context->getEntity()->isAccessible()) {
  194. throw new InsufficientEntityPermissionException($context);
  195. }
  196. $options['action'] = $adminUrlGenerator
  197. ->setController($context->getCrud()->getControllerFqcn())
  198. ->setAction('add_credit')
  199. ->setEntityId($context->getEntity()->getInstance()->getId())
  200. ->set('userMerchant', 'niche')
  201. ->generateUrl();
  202. $addCreditHistoryForm = $this->createForm(CreditHistoryFormType::class, null, $options)->createView();
  203. return $this->render(
  204. '@LcCaracole/admin/credit/credit_detail.html.twig',
  205. [
  206. 'pageName' => Crud::PAGE_DETAIL,
  207. 'entity' => $context->getEntity(),
  208. 'batch_actions' => array(),
  209. 'entities' => array(),
  210. 'filters' => array(),
  211. 'paginator' => array(),
  212. 'add_credit_history_form' => $addCreditHistoryForm,
  213. ]
  214. );
  215. }
  216. }