選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

198 行
6.6KB

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