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

195 lines
7.0KB

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