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.

132 satır
4.7KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\Ticket;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  8. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  9. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  10. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  11. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  12. use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  14. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  15. use Lc\SovBundle\Container\Ticket\TicketContainer;
  16. use Lc\SovBundle\Container\Ticket\TicketMessageContainer;
  17. use Lc\SovBundle\Definition\ActionDefinition;
  18. use Lc\SovBundle\Factory\Ticket\TicketFactory;
  19. use Lc\SovBundle\Factory\Ticket\TicketFactoryInterface;
  20. use Lc\SovBundle\Factory\Ticket\TicketMessageFactory;
  21. use Lc\SovBundle\Factory\Ticket\TicketMessageFactoryInterface;
  22. use Lc\SovBundle\Form\Ticket\TicketFormType;
  23. use Lc\SovBundle\Form\Ticket\TicketMessageFormType;
  24. use Lc\SovBundle\Form\Ticket\TicketStatusType;
  25. use Lc\SovBundle\Model\Ticket\TicketInterface;
  26. use Lc\SovBundle\Controller\AbstractAdminController;
  27. use Lc\SovBundle\Model\Ticket\TicketModel;
  28. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  29. use Lc\SovBundle\Translation\TranslatorAdmin;
  30. use Symfony\Component\HttpFoundation\JsonResponse;
  31. use Symfony\Component\HttpFoundation\RequestStack;
  32. abstract class TicketAdminController extends AbstractAdminController
  33. {
  34. public function getRepositoryQuery(): RepositoryQueryInterface
  35. {
  36. return $this->getTicketContainer()->getRepositoryQuery();
  37. }
  38. public function createEntity(string $entityFqcn)
  39. {
  40. return $this->getTicketContainer()->getFactory()->create();
  41. }
  42. public function configureFields(string $pageName): iterable
  43. {
  44. return $this->getTicketContainer()->getFieldDefinition()->getFields($pageName);
  45. }
  46. public function configureAssets(Assets $assets): Assets
  47. {
  48. $assets = parent::configureAssets($assets);
  49. $assets->addWebpackEncoreEntry('sov-ticket');
  50. return $assets;
  51. }
  52. public function configureActions(Actions $actions): Actions
  53. {
  54. $actions
  55. ->add(Crud::PAGE_INDEX, ActionDefinition::DETAIL)
  56. ->remove(Crud::PAGE_INDEX, ActionDefinition::EDIT);
  57. return parent::configureActions($actions);
  58. }
  59. public function detail(AdminContext $context)
  60. {
  61. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  62. $ticket = $context->getEntity()->getInstance();
  63. $url = $adminUrlGenerator
  64. ->setAction('ticketStatusAction')
  65. ->generateUrl();
  66. $formTicketStatus = $this->createForm(
  67. TicketStatusType::class,
  68. $ticket,
  69. [
  70. 'action' => $url,
  71. 'method' => 'POST',
  72. ]
  73. );
  74. $ticketMessage = $this->get(TicketMessageContainer::class)->getFactory()->create($ticket);
  75. $formAddTicketMessage = $this->createForm(TicketMessageFormType::class, $ticketMessage);
  76. $formAddTicketMessage->handleRequest($this->get(RequestStack::class)->getMainRequest());
  77. if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
  78. $ticketMessage = $formAddTicketMessage->getData();
  79. $ticketMessage->setTicket($ticket);
  80. $ticketMessage->setAnswerByAdmin(true);
  81. $this->get(EntityManagerInterface::class)->create($ticketMessage);
  82. $this->get(EntityManagerInterface::class)->flush();
  83. }
  84. return $this->render(
  85. '@LcSov/admin/ticket/detail.html.twig',
  86. [
  87. 'form_ticket_status' => $formTicketStatus->createView(),
  88. 'form_add_ticket_message' => $formAddTicketMessage->createView(),
  89. 'ticket' => $ticket,
  90. ]
  91. );
  92. }
  93. public function ticketStatusAction()
  94. {
  95. $entityManager = $this->getEntityManager();
  96. $request = $this->getRequestStack()->getMainRequest();
  97. $ticket = $request->attributes->get('easyadmin_context')->getEntity()->getInstance();
  98. $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
  99. $formTicketStatusForm->handleRequest($request);
  100. $success = false;
  101. if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
  102. $entityManager->persist($ticket);
  103. $entityManager->flush();
  104. $success = true;
  105. }
  106. return new JsonResponse(['success' => $success]);
  107. }
  108. }