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.

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