|
- <?php
-
- namespace Lc\SovBundle\Controller\Ticket;
-
- use Doctrine\ORM\EntityManagerInterface;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
- use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
- use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
- use Lc\SovBundle\Container\Ticket\TicketContainer;
- use Lc\SovBundle\Container\Ticket\TicketMessageContainer;
- use Lc\SovBundle\Definition\ActionDefinition;
- use Lc\SovBundle\Factory\Ticket\TicketFactory;
- use Lc\SovBundle\Factory\Ticket\TicketFactoryInterface;
- use Lc\SovBundle\Factory\Ticket\TicketMessageFactory;
- use Lc\SovBundle\Factory\Ticket\TicketMessageFactoryInterface;
- use Lc\SovBundle\Form\Ticket\TicketFormType;
- use Lc\SovBundle\Form\Ticket\TicketMessageFormType;
- use Lc\SovBundle\Form\Ticket\TicketStatusType;
- use Lc\SovBundle\Model\Ticket\TicketInterface;
- use Lc\SovBundle\Controller\AbstractAdminController;
- use Lc\SovBundle\Model\Ticket\TicketModel;
- use Lc\SovBundle\Translation\TranslatorAdmin;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\HttpFoundation\RequestStack;
-
- class TicketAdminController extends AbstractAdminController
- {
-
- public static function getEntityFqcn(): string
- {
- return TicketInterface::class;
- }
-
- public function createEntity(string $entityFqcn)
- {
- return $this->get(TicketContainer::class)->getFactory()->create();
- }
-
- public function configureAssets(Assets $assets): Assets
- {
- $assets = parent::configureAssets($assets);
-
- $assets->addWebpackEncoreEntry('sov-ticket');
-
- return $assets;
- }
-
- public function configureFields(string $pageName): iterable
- {
- $translatorAdmin = $this->get(TranslatorAdmin::class);
- return [
- IntegerField::new('id')
- ->hideOnForm(),
- DateField::new('createdAt')->setFormat('short')
- ->hideOnForm(),
- TextField::new('visitorFirstName')
- ->setTemplatePath('@LcSov/admin/ticket/field/username.html.twig')
- ->hideOnForm(),
- TextField::new('visitorEmail')
- ->setTemplatePath('@LcSov/admin/ticket/field/email.html.twig')
- ->hideOnForm(),
- AssociationField::new('user')
- ->hideOnIndex(),
- TextField::new('subject'),
- TextField::new('lastMessage')
- ->setTemplatePath('@LcSov/admin/ticket/field/lastmessage.html.twig')
- ->hideOnForm(),
- ChoiceField::new('type')
- ->autocomplete()
- ->setChoices(
- $translatorAdmin->transChoices($this->get(TicketContainer::class)->getSolver()->getTypeChoices(), 'Ticket', 'type')
- ),
- ChoiceField::new('status')
- ->autocomplete()
- ->setChoices(
- $translatorAdmin->transChoices($this->get(TicketContainer::class)->getSolver()->getStatusChoices(), 'Ticket', 'status')
-
- )
- ->setTemplatePath('@LcSov/admin/ticket/field/status.html.twig')
- ->hideOnForm(),
- ];
- }
-
- public function configureActions(Actions $actions): Actions
- {
- $actions
- ->add(Crud::PAGE_INDEX, ActionDefinition::DETAIL)
- ->remove(Crud::PAGE_INDEX, ActionDefinition::EDIT);
-
- return parent::configureActions($actions);
- }
-
- public function new(AdminContext $context)
- {
- $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
-
- $ticket = $this->createEntity($context->getEntity()->getFqcn());
-
- $form = $this->createForm(TicketFormType::class, $ticket);
-
- $form->handleRequest($context->getRequest());
- if ($form->isSubmitted() && $form->isValid()) {
- $ticket = $form->getData();
-
- $this->get(EntityManagerInterface::class)->create($ticket);
- $this->get(EntityManagerInterface::class)->flush();
-
- $url = $adminUrlGenerator
- ->setAction('index')
- ->generateUrl();
-
- return $this->redirect($url);
- }
-
- return $this->render(
- '@LcSov/admin/ticket/new.html.twig',
- [
- 'form' => $form->createView(),
- ]
- );
- }
-
- public function detail(AdminContext $context)
- {
- $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
- $ticket = $context->getEntity()->getInstance();
-
- $url = $adminUrlGenerator
- ->setAction('ticketStatusAction')
- ->generateUrl();
-
- $formTicketStatus = $this->createForm(
- TicketStatusType::class,
- $ticket,
- [
- 'action' => $url,
- 'method' => 'POST',
- ]
- );
-
- $ticketMessage = $this->get(TicketMessageContainer::class)->getFactory()->create($ticket);
- $formAddTicketMessage = $this->createForm(TicketMessageFormType::class, $ticketMessage);
- $formAddTicketMessage->handleRequest($this->get(RequestStack::class)->getMainRequest());
-
- if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
- $ticketMessage = $formAddTicketMessage->getData();
- $ticketMessage->setTicket($ticket);
- $ticketMessage->setAnswerByAdmin(true);
- $this->get(EntityManagerInterface::class)->create($ticketMessage);
- $this->get(EntityManagerInterface::class)->flush();
- }
-
- return $this->render(
- '@LcSov/admin/ticket/detail.html.twig',
- [
- 'form_ticket_status' => $formTicketStatus->createView(),
- 'form_add_ticket_message' => $formAddTicketMessage->createView(),
- 'ticket' => $ticket,
- ]
- );
- }
-
-
- public function ticketStatusAction()
- {
- $request = $this->get('request')->getMasterRequest();
- $ticket = $request->attributes->get('easyadmin_context')->getEntity()->getInstance();
-
- $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
- $formTicketStatusForm->handleRequest($request);
-
- $success = false;
- if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
- $this->get('em')->persist($ticket);
- $this->get('em')->flush();
- $success = true;
- }
-
- return new JsonResponse(['success' => $success]);
- }
-
- }
|