您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TicketAdminController.php 6.3KB

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