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.

189 lines
6.6KB

  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\Factory\Ticket\TicketFactory;
  17. use Lc\SovBundle\Factory\Ticket\TicketFactoryInterface;
  18. use Lc\SovBundle\Factory\Ticket\TicketMessageFactory;
  19. use Lc\SovBundle\Factory\Ticket\TicketMessageFactoryInterface;
  20. use Lc\SovBundle\Form\Ticket\TicketFormType;
  21. use Lc\SovBundle\Form\Ticket\TicketMessageFormType;
  22. use Lc\SovBundle\Form\Ticket\TicketStatusType;
  23. use Lc\SovBundle\Model\Ticket\TicketInterface;
  24. use Lc\SovBundle\Controller\AbstractAdminController;
  25. use Lc\SovBundle\Model\Ticket\TicketModel;
  26. use Lc\SovBundle\Translation\TranslatorAdmin;
  27. use Symfony\Component\HttpFoundation\JsonResponse;
  28. use Symfony\Component\HttpFoundation\RequestStack;
  29. class TicketAdminController extends AbstractAdminController
  30. {
  31. public static function getEntityFqcn(): string
  32. {
  33. return TicketInterface::class;
  34. }
  35. public function createEntity(string $entityFqcn)
  36. {
  37. return $this->get(TicketContainer::class)->getFactory()->create();
  38. }
  39. public function configureAssets(Assets $assets): Assets
  40. {
  41. $assets = parent::configureAssets($assets);
  42. $assets->addWebpackEncoreEntry('sov-ticket');
  43. return $assets;
  44. }
  45. public function configureFields(string $pageName): iterable
  46. {
  47. $translatorAdmin = $this->get(TranslatorAdmin::class);
  48. return [
  49. IntegerField::new('id')
  50. ->hideOnForm(),
  51. DateField::new('createdAt')->setFormat('short')
  52. ->hideOnForm(),
  53. TextField::new('visitorFirstName')
  54. ->setTemplatePath('@LcSov/admin/ticket/field/username.html.twig')
  55. ->hideOnForm(),
  56. TextField::new('visitorEmail')
  57. ->setTemplatePath('@LcSov/admin/ticket/field/email.html.twig')
  58. ->hideOnForm(),
  59. AssociationField::new('user')
  60. ->hideOnIndex(),
  61. TextField::new('subject'),
  62. TextField::new('lastMessage')
  63. ->setTemplatePath('@LcSov/admin/ticket/field/lastmessage.html.twig')
  64. ->hideOnForm(),
  65. ChoiceField::new('type')
  66. ->autocomplete()
  67. ->setChoices(
  68. $translatorAdmin->transChoices($this->get(TicketContainer::class)->getSolver()->getTypeChoices(), 'Ticket', 'type')
  69. ),
  70. ChoiceField::new('status')
  71. ->autocomplete()
  72. ->setChoices(
  73. $translatorAdmin->transChoices($this->get(TicketContainer::class)->getSolver()->getStatusChoices(), 'Ticket', 'status')
  74. )
  75. ->setTemplatePath('@LcSov/admin/ticket/field/status.html.twig')
  76. ->hideOnForm(),
  77. ];
  78. }
  79. public function configureActions(Actions $actions): Actions
  80. {
  81. $actions
  82. ->add(Crud::PAGE_INDEX, Action::DETAIL)
  83. ->remove(Crud::PAGE_INDEX, Action::EDIT);
  84. return parent::configureActions($actions);
  85. }
  86. public function new(AdminContext $context)
  87. {
  88. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  89. $ticket = $this->createEntity($context->getEntity()->getFqcn());
  90. $form = $this->createForm(TicketFormType::class, $ticket);
  91. $form->handleRequest($context->getRequest());
  92. if ($form->isSubmitted() && $form->isValid()) {
  93. $ticket = $form->getData();
  94. $this->get(EntityManagerInterface::class)->create($ticket);
  95. $this->get(EntityManagerInterface::class)->flush();
  96. $url = $adminUrlGenerator
  97. ->setAction('index')
  98. ->generateUrl();
  99. return $this->redirect($url);
  100. }
  101. return $this->render(
  102. '@LcSov/admin/ticket/new.html.twig',
  103. [
  104. 'form' => $form->createView(),
  105. ]
  106. );
  107. }
  108. public function detail(AdminContext $context)
  109. {
  110. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  111. $ticket = $context->getEntity()->getInstance();
  112. $url = $adminUrlGenerator
  113. ->setAction('ticketStatusAction')
  114. ->generateUrl();
  115. $formTicketStatus = $this->createForm(
  116. TicketStatusType::class,
  117. $ticket,
  118. [
  119. 'action' => $url,
  120. 'method' => 'POST',
  121. ]
  122. );
  123. $ticketMessage = $this->get(TicketContainer::class)->getFactory()->create();
  124. $formAddTicketMessage = $this->createForm(TicketMessageFormType::class, $ticketMessage);
  125. $formAddTicketMessage->handleRequest($this->get(RequestStack::class)->getMainRequest());
  126. if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
  127. $ticketMessage = $formAddTicketMessage->getData();
  128. $ticketMessage->setTicket($ticket);
  129. $ticketMessage->setAnswerByAdmin(true);
  130. $this->get(EntityManagerInterface::class)->create($ticketMessage);
  131. $this->get(EntityManagerInterface::class)->flush();
  132. }
  133. return $this->render(
  134. '@LcSov/admin/ticket/detail.html.twig',
  135. [
  136. 'form_ticket_status' => $formTicketStatus->createView(),
  137. 'form_add_ticket_message' => $formAddTicketMessage->createView(),
  138. 'ticket' => $ticket,
  139. ]
  140. );
  141. }
  142. public function ticketStatusAction()
  143. {
  144. $request = $this->get('request')->getMasterRequest();
  145. $ticket = $request->attributes->get('easyadmin_context')->getEntity()->getInstance();
  146. $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
  147. $formTicketStatusForm->handleRequest($request);
  148. $success = false;
  149. if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
  150. $this->get('em')->persist($ticket);
  151. $this->get('em')->flush();
  152. $success = true;
  153. }
  154. return new JsonResponse(['success' => $success]);
  155. }
  156. }