src/Admin/ImportacaoDadosAdmin.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Admin;
  4. use Sonata\AdminBundle\Admin\AbstractAdmin;
  5. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  6. use Sonata\AdminBundle\Datagrid\ListMapper;
  7. use Sonata\AdminBundle\Form\FormMapper;
  8. use Sonata\AdminBundle\Show\ShowMapper;
  9. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  10. use App\Enums\TipoImportacaoEnum;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use Symfony\Component\Form\Extension\Core\Type\FileType;
  13. use Sonata\AdminBundle\Route\RouteCollection;
  14. final class ImportacaoDadosAdmin extends BaseAdmin
  15. {
  16.     
  17.     protected function configureRoutes(RouteCollection $collection)
  18.     {
  19.         $collection->add('processaImportacao');
  20.     }
  21.     protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
  22.     {
  23.         $datagridMapper
  24.             ->add('id')
  25.             ->add('tipo')
  26.             ->add('observacoes')
  27.             ->add('createdAt')
  28.             ;
  29.     }
  30.     protected function configureListFields(ListMapper $listMapper): void
  31.     {
  32.         $listMapper
  33.             ->add('tipo')
  34.             ->add('createdAt'null, ['label'=>'Data cadastro'])
  35.             //->add('processadoEm', null, ['label'=>'Data processamento'])
  36.             ->add('_action'null, [
  37.                 'actions' => [
  38.                     'show' => [],
  39.                     'edit' => [],
  40.                     'delete' => [],
  41.                     'processaImportacao' => [
  42.                         'template' => 'Importacao/list__action_processaImportacao.html.twig',
  43.                     ],
  44.                 ],
  45.             ]);
  46.     }
  47.     protected function configureFormFields(FormMapper $formMapper): void
  48.     {
  49.         
  50.         $obj $this->getSubject();
  51.         $fileFieldOptionsDocumentoImportacao = [
  52.             'data_class' => null,
  53.             'required' => false,
  54.             'label' => 'Documento para importação',
  55.             'help' => '',
  56.         ];
  57.         if (!is_null($obj) && $obj->getDocumentoImportacao() != '') {
  58.             $fileFieldOptionsDocumentoImportacao['help'] = '<a href="/'.$obj->getDocumentoImportacao().'" target="_blank" class="btn btn-warning"/><i class="fa fa-eye" aria-hidden="true"></i> Visualizar</a>';
  59.         }
  60.         
  61.         $formMapper
  62.             ->add('tipo'ChoiceType::class, [
  63.                 'label' => 'Tipo de importação',
  64.                 'placeholder' => '-- Selecione --',
  65.                 'choices' => TipoImportacaoEnum::getAssociatedValues(),
  66.             ])
  67.             ->add('documentoImportacaoFile'FileType::class, [
  68.                 'constraints' => [new Assert\File([
  69.                     'mimeTypes' => [
  70.                         'text/csv'// Tipo mais comum
  71.                         'text/plain'// Pode ser usado para alguns arquivos CSV
  72.                         'application/vnd.ms-excel'// Alguns browsers mais antigos identificam CSV assim
  73.                     ],
  74.                     'mimeTypesMessage' => 'O sistema só aceita arquivos no formato .csv'
  75.                 ])],
  76.                 'label' => 'Documento para importacao',
  77.                 'help' => $fileFieldOptionsDocumentoImportacao['help'],
  78.                 'required' => false,
  79.             ])
  80.             ->add('observacoes'null, ['label'=>'Observações para importação'])
  81.             ;
  82.     }
  83.     protected function configureShowFields(ShowMapper $showMapper): void
  84.     {
  85.         $showMapper
  86.             ->add('id')
  87.             ->add('tipo')
  88.             ->add('observacoes')
  89.             ->add('createdAt')
  90.             ;
  91.     }
  92.     
  93.     public function prePersist($object)
  94.     {
  95.         ini_set('upload_max_filesize''10M');
  96.         $object->setCreatedAt(new \DateTime('now'));
  97.         //$object->setCadastradoPor($this->getUserLogado());
  98.         if($object->getDocumentoImportacaoFile()){
  99.             $this->manageFilesUploadPdfDocumentoImportacao($object);
  100.         }
  101.         
  102.         return $object;
  103.     }
  104.     
  105.     public function preUpdate($object)
  106.     {
  107.         ini_set('upload_max_filesize''10M');
  108.         if($object->getDocumentoImportacaoFile()){
  109.             $this->manageFilesUploadPdfDocumentoImportacao($object);
  110.         }
  111.         
  112.         return $object;
  113.     }
  114.     
  115.     public function postRemove($object)
  116.     {
  117.         parent::postRemove($object); // TODO: Change the autogenerated stub
  118.         if($object->getDocumentoImportacaoFile()){
  119.             if (file_exists($object->getDocumentoImportacaoFile())) {
  120.                 @unlink($object->getDocumentoImportacaoFile());
  121.             }
  122.         }
  123.     }
  124.     
  125.     private function manageFilesUploadPdfDocumentoImportacao($obj)
  126.     {
  127.         $file $obj->getDocumentoImportacaoFile();
  128.         $nameFile $filename md5(date('Y-m-d H:i:s:u')).".".$file->getClientOriginalExtension();
  129.         $uploadPath "uploads/documento-importacao";
  130.         $file->move($uploadPath$nameFile);
  131.         $obj->setDocumentoImportacao($uploadPath "/" $filename);
  132.         $obj->setDocumentoImportacaoOriginal($uploadPath "/" $filename);
  133.     }
  134. }