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

173 lines
5.8KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\Ticket;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  5. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  9. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  11. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  12. use EasyCorp\Bundle\EasyAdminBundle\Registry\TemplateRegistry;
  13. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  14. use Lc\SovBundle\Container\Ticket\TicketMessageContainer;
  15. use Lc\SovBundle\Definition\ActionDefinition;
  16. use Lc\SovBundle\Event\Ticket\TicketEvent;
  17. use Lc\SovBundle\Form\Ticket\TicketMessageAdminFormType;
  18. use Lc\SovBundle\Form\Ticket\TicketStatusType;
  19. use Lc\SovBundle\Controller\AbstractAdminController;
  20. use Lc\SovBundle\Model\Ticket\TicketModel;
  21. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  22. use Symfony\Component\HttpFoundation\JsonResponse;
  23. use Symfony\Component\HttpFoundation\RequestStack;
  24. abstract class TicketAdminController extends AbstractAdminController
  25. {
  26. public function getRepositoryQuery(): RepositoryQueryInterface
  27. {
  28. return $this->getTicketContainer()->getRepositoryQuery();
  29. }
  30. public function createEntity(string $entityFqcn)
  31. {
  32. $ticket = $this->getTicketContainer()->getFactory()->create();
  33. return $ticket;
  34. }
  35. public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
  36. {
  37. parent::persistEntity($entityManager, $entityInstance);
  38. $this->getEventDispatcher()->dispatch(new TicketEvent($entityInstance), TicketEvent::NEW_TICKET_EVENT);
  39. }
  40. public function configureCrud(Crud $crud): Crud
  41. {
  42. $crud = parent::configureCrud($crud); // TODO: Change the autogenerated stub
  43. $crud->setDefaultSort(array('updatedAt' => 'DESC'));
  44. $crud->overrideTemplate('crud/detail', '@LcSov/admin/ticket/detail.html.twig');
  45. return $crud;
  46. }
  47. public function configureFields(string $pageName): iterable
  48. {
  49. return $this->getTicketContainer()->getFieldDefinition()->getFields($pageName);
  50. }
  51. public function configureAssets(Assets $assets): Assets
  52. {
  53. $assets = parent::configureAssets($assets);
  54. $assets->addWebpackEncoreEntry('sov-ticket');
  55. return $assets;
  56. }
  57. public function configureActions(Actions $actions): Actions
  58. {
  59. $actions->add(Crud::PAGE_INDEX, ActionDefinition::DETAIL);
  60. $actions = parent::configureActions($actions);
  61. $actions->disable( ActionDefinition::EDIT, ActionDefinition::DUPLICATE);
  62. return $actions;
  63. }
  64. public function createIndexRepositoryQuery(
  65. SearchDto $searchDto,
  66. EntityDto $entityDto,
  67. FieldCollection $fields,
  68. FilterCollection $filters
  69. ): RepositoryQueryInterface {
  70. $repositoryQuery = parent::createIndexRepositoryQuery(
  71. $searchDto,
  72. $entityDto,
  73. $fields,
  74. $filters
  75. );
  76. if(!$this->isRepositoryQueryFiltered()){
  77. $repositoryQuery->filterByStatus(array(
  78. TicketModel::TICKET_STATUS_OPEN,
  79. TicketModel::TICKET_STATUS_BEING_PROCESSED,
  80. TicketModel::TICKET_STATUS_PROCESSED
  81. ));
  82. }
  83. return $repositoryQuery;
  84. }
  85. public function detail(AdminContext $context)
  86. {
  87. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  88. $ticket = $context->getEntity()->getInstance();
  89. $url = $adminUrlGenerator
  90. ->setAction('ticketStatusAction')
  91. ->generateUrl();
  92. $formTicketStatus = $this->createForm(
  93. TicketStatusType::class,
  94. $ticket,
  95. [
  96. 'action' => $url,
  97. 'method' => 'POST',
  98. ]
  99. );
  100. $ticketMessage = $this->get(TicketMessageContainer::class)->getFactory()->create($ticket);
  101. $formAddTicketMessage = $this->createForm(TicketMessageAdminFormType::class, $ticketMessage);
  102. $formAddTicketMessage->handleRequest($this->get(RequestStack::class)->getMainRequest());
  103. if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
  104. $ticketMessage = $formAddTicketMessage->getData();
  105. $ticketMessage->setTicket($ticket);
  106. $ticketMessage->setAnswerByAdmin(true);
  107. $this->get(EntityManagerInterface::class)->create($ticketMessage);
  108. $this->get(EntityManagerInterface::class)->flush();
  109. $this->getEventDispatcher()->dispatch(new TicketEvent($ticket), TicketEvent::NEW_MESSAGE_EVENT);
  110. return $this->redirect($this->generateEaUrl());
  111. }
  112. return $this->render(
  113. $context->getTemplatePath('crud/detail'),
  114. [
  115. 'form_ticket_status' => $formTicketStatus->createView(),
  116. 'form_add_ticket_message' => $formAddTicketMessage->createView(),
  117. 'ticket' => $ticket,
  118. ]
  119. );
  120. }
  121. public function ticketStatusAction(AdminContext $context, EntityManagerInterface $entityManager)
  122. {
  123. $ticket = $context->getEntity()->getInstance();
  124. $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
  125. $formTicketStatusForm->handleRequest($context->getRequest());
  126. $success = false;
  127. if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
  128. $entityManager->update($ticket);
  129. $entityManager->flush();
  130. $success = true;
  131. }
  132. return new JsonResponse(['success' => $success]);
  133. }
  134. }