Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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