src/Controller/Admin/ReportCrudController.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Controller\Admin\Filter\VinFilter;
  4. use App\Entity\Report;
  5. use App\Message\LoadSourceReportMessage;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
  10. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  11. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
  12. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
  14. use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  16. use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
  17. use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
  18. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  19. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\Messenger\MessageBusInterface;
  22. use Symfony\Component\Serializer\SerializerInterface;
  23. use function Symfony\Component\Translation\t;
  24. class ReportCrudController extends AbstractCrudController
  25. {
  26.     public function __construct(private AdminUrlGenerator $adminUrlGenerator, private SerializerInterface $serializer)
  27.     {
  28.     }
  29.     public static function getEntityFqcn(): string
  30.     {
  31.         return Report::class;
  32.     }
  33.     public function configureFields(string $pageName): iterable
  34.     {
  35.         yield IdField::new('id')->setDisabled();
  36.         yield AssociationField::new('order')->setDisabled();
  37.         yield TextField::new('order.vin')->setSortable(true)->setDisabled()->setLabel('VIN');
  38.         yield DateTimeField::new('order.createdAt')->setSortable(true)->setDisabled()->setLabel('Дата');
  39.         yield AssociationField::new('reportType');
  40.         yield IntegerField::new('price');
  41.         yield BooleanField::new('handled');
  42.         yield BooleanField::new('loaded');
  43.         yield TextareaField::new('content')->onlyOnForms();
  44.     }
  45.     public function configureFilters(Filters $filters): Filters
  46.     {
  47.         return $filters
  48.             ->add('reportType')
  49.             ->add('price')
  50.             ->add('handled')
  51.             ->add('loaded')
  52.             ->add('order')
  53.             ->add(VinFilter::new('vin'))
  54.             ;
  55.     }
  56.     public function configureCrud(Crud $crud): Crud
  57.     {
  58.         return $crud
  59.             ->setDefaultSort(['order.createdAt' => 'DESC'])
  60.         ;
  61.     }
  62.     public function configureActions(Actions $actions): Actions
  63.     {
  64.         return $actions
  65.             ->add(
  66.                 Crud::PAGE_INDEX,
  67.                 Action::new('loadReport't('Load report'), 'fas fa-download')
  68.                     ->linkToCrudAction('loadReport')
  69.                     ->displayIf(static function (Report $entity) {
  70.                         return !$entity->getLoaded();
  71.                     })
  72.             );
  73.     }
  74.     public function loadReport(AdminContext $contextMessageBusInterface $bus)
  75.     {
  76.         $report $context->getEntity()->getInstance();
  77.         $bus->dispatch(new LoadSourceReportMessage($report->getReportType()->getName() ?? 'unknownType', (string) $report->getId(), $report->getOrder()->getVin()->getVin()));
  78.         if ($context->getRequest()->isXmlHttpRequest()) {
  79.             new JsonResponse($this->serializer->serialize(['report' => $report], 'json', ['groups' => 'index']));
  80.         }
  81.         $url $this->adminUrlGenerator
  82.             ->setController(static::class)
  83.             ->setAction(Action::INDEX)
  84.             ->generateUrl();
  85.         return $this->redirect($url);
  86.     }    
  87. }