Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

188 lines
6.3KB

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