Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

237 lines
9.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\User;
  3. use App\Controller\Credit\CreditHistoryAdminController;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
  6. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  7. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  10. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  11. use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
  12. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  13. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  14. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  15. use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
  16. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  17. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  18. use Lc\CaracoleBundle\Container\User\UserMerchantContainer;
  19. use Lc\CaracoleBundle\Controller\AbstractAdminController;
  20. use Lc\CaracoleBundle\Definition\ActionDefinition;
  21. use Lc\CaracoleBundle\Form\User\UserMerchantActiveCreditFormType;
  22. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  23. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  24. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  25. use Lc\SovBundle\Translation\TranslatorAdmin;
  26. use Symfony\Component\HttpFoundation\Response;
  27. abstract class UserMerchantAdminController extends AbstractAdminController
  28. {
  29. public function getRepositoryQuery(): RepositoryQueryInterface
  30. {
  31. return $this->getUserMerchantContainer()->getRepositoryQuery();
  32. }
  33. public function createEntity(string $entityFqcn)
  34. {
  35. return $this->getUserMerchantContainer()->getFactory()->createBase($this->getMerchantCurrent());
  36. }
  37. public function createIndexRepositoryQuery(
  38. SearchDto $searchDto,
  39. EntityDto $entityDto,
  40. FieldCollection $fields,
  41. FilterCollection $filters
  42. ): RepositoryQueryInterface {
  43. $repositoryQuery = parent::createIndexRepositoryQuery($searchDto, $entityDto, $fields, $filters);
  44. $repositoryQuery->filterIsCreditActive();
  45. return $repositoryQuery;
  46. }
  47. public function overrideEntitiesActions(?EntityCollection $entities): void
  48. {
  49. $context = $this->get(AdminContextProvider::class)->getContext();
  50. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  51. $creditControllerFqcn = $context->getCrudControllers()->findCrudFqcnByEntityFqcn(
  52. $this->get(EntityManagerInterface::class)->getEntityName(CreditHistoryInterface::class)
  53. );
  54. if ($entities) {
  55. foreach ($entities as $entity) {
  56. foreach ($entity->getActions() as $action) {
  57. if ($action->getName() == 'credit_history') {
  58. $url = $adminUrlGenerator
  59. ->setController($creditControllerFqcn)
  60. ->set('userMerchantId', $entity->getInstance()->getId())
  61. ->generateUrl();
  62. $action->setLinkUrl($url);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. public function configureActions(Actions $actions): Actions
  69. {
  70. $actions = parent::configureActions($actions);
  71. $creditAction = Action::new('credit_history', false, 'fa fa-cash-register')
  72. ->linkToCrudAction('credit_history')
  73. ->setHtmlAttributes(
  74. array(
  75. 'data-toggle' => 'tooltip',
  76. 'title' => $this->get(TranslatorAdmin::class)->transAction('credit'),
  77. )
  78. )
  79. ->setCssClass('btn btn-sm btn-success');
  80. $actions->add(Crud::PAGE_INDEX, $creditAction);
  81. $actions->disable(
  82. ActionDefinition::EDIT,
  83. ActionDefinition::DUPLICATE,
  84. ActionDefinition::DELETE,
  85. ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT
  86. );
  87. return $actions;
  88. }
  89. public function configureFields(string $pageName): iterable
  90. {
  91. return $this->getUserMerchantContainer()->getFieldDefinition()->getFields($pageName);
  92. }
  93. public function configureFilters(Filters $filters): Filters
  94. {
  95. return $filters
  96. ->add(BooleanFilter::new('active'));
  97. }
  98. public function new(AdminContext $context): Response
  99. {
  100. $entityManager = $this->get(EntityManagerInterface::class);
  101. $merchantResolver = $this->get(MerchantResolver::class);
  102. $userMerchant = $this->get(UserMerchantContainer::class)
  103. ->getFactory()
  104. ->create($merchantResolver->getCurrent());
  105. $form = $this->createForm(
  106. UserMerchantActiveCreditFormType::class,
  107. $userMerchant,
  108. array(
  109. 'merchant' => $this->getMerchantCurrent()
  110. )
  111. );
  112. $form->handleRequest($context->getRequest());
  113. if ($form->isSubmitted() && $form->isValid()) {
  114. $user = $form->get('user')->getData();
  115. $userMerchant = $merchantResolver->getUserMerchant($user);
  116. $userMerchant->setCreditActive(true);
  117. $entityManager->update($userMerchant);
  118. $entityManager->flush();
  119. $this->addFlashTranslator('success', 'creditActive');
  120. return $this->redirect($this->generateEaUrl(CreditHistoryAdminController::class, ActionDefinition::INDEX, null, array(
  121. 'userMerchantId'=> $userMerchant->getId()
  122. )));
  123. }
  124. return $this->render(
  125. '@LcCaracole/admin/user/new_usermerchant.html.twig',
  126. [
  127. 'form' => $form->createView(),
  128. ]
  129. );
  130. }
  131. // public function edit(AdminContext $context): Response
  132. // {
  133. // $entityManager = $this->get(EntityManagerInterface::class);
  134. //
  135. // $userMerchant = $context->getEntity()->getInstance();
  136. //
  137. // $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  138. //
  139. // $form->handleRequest($context->getRequest());
  140. //
  141. // if ($form->isSubmitted() && $form->isValid()) {
  142. // $userMerchant = $form->getData();
  143. //
  144. // $userMerchant->getUser()->setEmail($form->get('email')->getData());
  145. // $userMerchant->getUser()->setLastName($form->get('lastname')->getData());
  146. // $userMerchant->getUser()->setFirstname($form->get('firstname')->getData());
  147. //
  148. // $entityManager->update($userMerchant);
  149. // $entityManager->update($userMerchant->getUser());
  150. // $entityManager->flush();
  151. // $this->addFlashTranslator('success', 'updated');
  152. // $url = $this->get(AdminUrlGenerator::class)->setAction(ActionDefinition::INDEX)->generateUrl();
  153. //
  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. //
  161. // return $this->render(
  162. // '@LcCaracole/admin/user/edit_usermerchant.html.twig',
  163. // [
  164. // 'form' => $form->createView(),
  165. // ]
  166. // );
  167. // }
  168. // public function credit(AdminContext $context, AdminUrlGenerator $adminUrlGenerator): Response
  169. // {
  170. // $event = new BeforeCrudActionEvent($context);
  171. // $this->get('event_dispatcher')->dispatch($event);
  172. // if ($event->isPropagationStopped()) {
  173. // return $event->getResponse();
  174. // }
  175. //
  176. // if (!$this->isGranted(
  177. // Permission::EA_EXECUTE_ACTION,
  178. // ['action' => ActionDefinition::DETAIL, 'entity' => $context->getEntity()]
  179. // )) {
  180. // throw new ForbiddenActionException($context);
  181. // }
  182. // if (!$context->getEntity()->isAccessible()) {
  183. // throw new InsufficientEntityPermissionException($context);
  184. // }
  185. //
  186. // $options['action'] = $adminUrlGenerator
  187. // ->setController($context->getCrud()->getControllerFqcn())
  188. // ->setAction('add_credit')
  189. // ->setEntityId($context->getEntity()->getInstance()->getId())
  190. // ->set('userMerchant', 'niche')
  191. // ->generateUrl();
  192. // $addCreditHistoryForm = $this->createForm(CreditHistoryFormType::class, null, $options)->createView();
  193. //
  194. //
  195. // return $this->render(
  196. // '@LcCaracole/admin/credit/credit_detail.html.twig',
  197. // [
  198. // 'pageName' => Crud::PAGE_DETAIL,
  199. // 'entity' => $context->getEntity(),
  200. // 'batch_actions' => array(),
  201. // 'entities' => array(),
  202. // 'filters' => array(),
  203. // 'paginator' => array(),
  204. // 'add_credit_history_form' => $addCreditHistoryForm,
  205. // ]
  206. // );
  207. // }
  208. }