Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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\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\Model\User\UserInterface;
  31. use Lc\SovBundle\Translation\TranslatorAdmin;
  32. use Symfony\Component\HttpFoundation\Response;
  33. abstract class UserMerchantAdminController extends AbstractAdminController
  34. {
  35. use AdminControllerTrait;
  36. protected $em;
  37. protected $translatorAdmin;
  38. public function __construct(EntityManagerInterface $entityManager, TranslatorAdmin $translatorAdmin)
  39. {
  40. $this->em = $entityManager;
  41. $this->translatorAdmin = $translatorAdmin;
  42. }
  43. public function overrideEntitiesActions(?EntityCollection $entities): void
  44. {
  45. $context = $this->get(AdminContextProvider::class)->getContext();
  46. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  47. $creditControllerFqcn = $context->getCrudControllers()->findCrudFqcnByEntityFqcn(
  48. $this->em->getEntityName(CreditHistoryInterface::class)
  49. );
  50. if ($entities) {
  51. foreach ($entities as $entity) {
  52. foreach ($entity->getActions() as $action) {
  53. if ($action->getName() == 'credit_history') {
  54. $url = $adminUrlGenerator
  55. ->setController($creditControllerFqcn)
  56. ->set('userMerchantId', $entity->getInstance()->getId())
  57. ->generateUrl();
  58. $action->setLinkUrl($url);
  59. }
  60. }
  61. }
  62. }
  63. }
  64. public function configureActions(Actions $actions): Actions
  65. {
  66. $actions = parent::configureActions($actions);
  67. $creditAction = Action::new('credit_history', false, 'fa fa-cash-register')
  68. ->linkToCrudAction('credit_history')
  69. ->setHtmlAttributes(
  70. array(
  71. 'data-toggle' => 'tooltip',
  72. 'title' => $this->translatorAdmin->transAction('credit'),
  73. )
  74. )
  75. ->setCssClass('btn btn-sm btn-success');
  76. $actions->add(Crud::PAGE_INDEX, $creditAction);
  77. return $actions;
  78. }
  79. public function configureFields(string $pageName): iterable
  80. {
  81. $fields = [
  82. TextField::new('user.email'),
  83. TextField::new('user.lastname'),
  84. TextField::new('user.firstname'),
  85. BooleanField::new('active'),
  86. BooleanField::new('creditActive'),
  87. AssociationField::new('user'),
  88. ];
  89. if ($this->isGranted('ROLE_SUPER_ADMIN')) {
  90. $fields[] = ArrayField::new('roles');
  91. }
  92. return $fields;
  93. }
  94. public function configureFilters(Filters $filters): Filters
  95. {
  96. return $filters
  97. ->add(BooleanFilter::new('active'))
  98. ;
  99. }
  100. public function new(AdminContext $context): Response
  101. {
  102. $entityManager = $this->get('em');
  103. $userFactory = $this->get('user_factory');
  104. $userMerchantFactory = $this->get('user_merchant_factory');
  105. $userRepository = $entityManager->getRepository(UserInterface::class);
  106. $merchantResolver = $this->get('merchant_resolver');
  107. $userMerchant = $userMerchantFactory->create();
  108. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  109. $form->handleRequest($context->getRequest());
  110. if ($form->isSubmitted() && $form->isValid()) {
  111. $userMerchant = $form->getData();
  112. $existingUser = $userRepository->findOneByEmail($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. $user = $userFactory->create($param);
  120. $entityManager->create($user);
  121. $userMerchant->setUser($user);
  122. $entityManager->create($userMerchant);
  123. $entityManager->flush();
  124. $this->addFlash('success', $this->get('translator_admin')->trans('form.user_merchant.create'));
  125. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  126. return $this->redirect($url);
  127. } else {
  128. //Le user existe, on vérifie si le usemerchant existe aussi
  129. $existingUserMerchant = $merchantResolver->getUserMerchant($existingUser);
  130. if ($existingUserMerchant == null) {
  131. $userMerchant->setUser($existingUser);
  132. $entityManager->create($userMerchant);
  133. $entityManager->flush();
  134. $this->addFlash('success', $this->get('translator_admin')->trans('form.user_merchant.linked'));
  135. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  136. return $this->redirect($url);
  137. } else {
  138. $this->addFlash('error', $this->get('translator_admin')->trans('form.user_merchant.already_exist'));
  139. }
  140. }
  141. }
  142. return $this->render(
  143. '@LcCaracole/admin/user/new_usermerchant.html.twig',
  144. [
  145. 'form' => $form->createView(),
  146. ]
  147. );
  148. }
  149. public function edit(AdminContext $context): Response
  150. {
  151. $entityManager = $this->get('em');
  152. $userRepository = $entityManager->getRepository(UserInterface::class);
  153. $merchantResolver = $this->get('merchant_resolver');
  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->addFlash('success', $this->get('translator_admin')->trans('form.user_merchant.update'));
  166. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::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/usermerchant_edit.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' => Action::DETAIL, 'entity' => $context->getEntity()]
  190. )) {
  191. throw new ForbiddenActionException($context);
  192. }
  193. if (!$context->getEntity()->isAccessible()) {
  194. throw new InsufficientEntityPermissionException($context);
  195. }
  196. dump($context->getCrud()->getControllerFqcn());
  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. }