Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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