src/EventSubscriber/PageFormSubscriber.php line 110

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\EventSubscriber;
  3. use App\Entity\PageInterface;
  4. use App\Entity\FormPageInterface;
  5. use Symfony\Component\Finder\Finder;
  6. use Vich\UploaderBundle\Event\Event;
  7. use Symfony\Component\Form\FormEvent;
  8. use Vich\UploaderBundle\Event\Events;
  9. use Symfony\Component\Form\FormEvents;
  10. use Symfony\Component\Filesystem\Filesystem;
  11. use Symfony\Component\HttpFoundation\File\UploadedFile;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. class PageFormSubscriber implements EventSubscriberInterface
  15. {
  16.     private $filesystem;
  17.     private $params;
  18.     private $dirPath '';
  19.     public function __construct(Filesystem $filesystemParameterBagInterface $params)
  20.     {
  21.         $this->filesystem $filesystem;
  22.         $this->params $params;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             FormEvents::PRE_SUBMIT => ['onPreSubmit'],
  28.             Events::PRE_UPLOAD => ['onVichUploaderPreUpload'],
  29.             Events::POST_UPLOAD => ['onVichUploaderPostUpload'],
  30.         ];
  31.     }
  32.     public function onPreSubmit(FormEvent $event): void
  33.     {
  34.         $page $event->getForm()->getData();
  35.         $data $event->getData();
  36.         if (!$page instanceof PageInterface) {
  37.             return;
  38.         }
  39.         // Checks if the machine name or the site will be modified submitting the form,
  40.         // if not, we don't have to reupload files
  41.         if (
  42.             (!isset($data['machineName']) || isset($data['machineName']) && $page->getMachineName() == $data['machineName'])
  43.             && (!isset($data['site']) || isset($data['site']) && $page->getSite()->getId() == $data['site'])
  44.         ) {
  45.             return;
  46.         }
  47.         if (($fileName $page->getTemplateFileName()) && !isset($data['templateFile']['delete'])) {
  48.             $data['templateFile'] = $this->updateFilePath(
  49.                 $page,
  50.                 $this->params->get('public_templates_path'),
  51.                 $fileName,
  52.                 'text/html'
  53.             );
  54.         }
  55.         if (($fileName $page->getAssetsArchiveFilename()) && !isset($data['assetsArchiveFile']['delete'])) {
  56.             $data['assetsArchiveFile'] = $this->updateFilePath(
  57.                 $page,
  58.                 $this->params->get('public_archives_path'),
  59.                 $fileName,
  60.                 'application/zip');
  61.         }
  62.         if ($page instanceof FormPageInterface) {
  63.             if (($fileName $page->getConfirmationTemplateFilename()) && !isset($data['confirmationTemplateFile']['delete'])) {
  64.                 $data['confirmationTemplateFile'] = $this->updateFilePath(
  65.                     $page,
  66.                     $this->params->get('public_templates_path'),
  67.                     $fileName,
  68.                     'text/html'
  69.                 );
  70.             }
  71.             if (($fileName $page->getLeadEmailTemplateFilename()) && !isset($data['leadEmailTemplateFile']['delete'])) {
  72.                 $data['leadEmailTemplateFile'] = $this->updateFilePath(
  73.                     $page,
  74.                     $this->params->get('public_templates_path'),
  75.                     $fileName,
  76.                     'text/html'
  77.                 );
  78.             }
  79.         }
  80.         $event->setData($data);
  81.     }
  82.     private function updateFilePath(PageInterface $pagestring $dirPathstring $fileNamestring $fileMimeType): array
  83.     {
  84.         return [
  85.             'file' => new UploadedFile(
  86.                 implode(DIRECTORY_SEPARATOR, [$dirPath$page->getFilesystemPath(), $fileName]),
  87.                 $fileName,
  88.                 $fileMimeType,
  89.                 null,
  90.                 true
  91.             )
  92.         ];
  93.     }
  94.     public function onVichUploaderPreUpload(Event $event): void
  95.     {
  96.         $page $event->getObject();
  97.         switch ($event->getMapping()->getMappingName()) {
  98.             case 'page_template':
  99.                 $this->dirPath $page->getTemplateFile()->getPath();
  100.                 break;
  101.             case 'page_lead_email':
  102.                 $this->dirPath $page->getLeadEmailTemplateFile()->getPath();
  103.                 break;
  104.             case 'page_confirmation_template':
  105.                 $this->dirPath $page->getConfirmationTemplateFile()->getPath();
  106.                 break;
  107.             case 'page_assets_archive':
  108.                 $this->dirPath $page->getAssetsArchiveFile()->getPath();
  109.                 break;
  110.         }
  111.     }
  112.     public function onVichUploaderPostUpload(Event $event): void
  113.     {
  114.         $finder = new Finder();
  115.         // Checks if the path exists otherwise Finder can't find files in it
  116.         if (!$this->filesystem->exists($this->dirPath)) {
  117.             return;
  118.         }
  119.         // Changes assets path in templates (in case of links to PDF files)
  120.         $page $event->getObject();
  121.         switch ($event->getMapping()->getMappingName()) {
  122.             case 'page_template':
  123.                 $postUploadedFile $page->getTemplateFile();
  124.                 break;
  125.             case 'page_lead_email':
  126.                 $postUploadedFile $page->getLeadEmailTemplateFile();
  127.                 break;
  128.             case 'page_confirmation_template':
  129.                 $postUploadedFile $page->getConfirmationTemplateFile();
  130.                 break;
  131.         }
  132.         if (isset($postUploadedFile)) {
  133.             $fileContents file_get_contents($postUploadedFile->getRealPath());
  134.             $old_file_system_path  str_replace(
  135.                 $this->params->get('public_templates_path').DIRECTORY_SEPARATOR,
  136.                 '',
  137.                 $this->dirPath,
  138.             );
  139.             $updated_file str_replace($old_file_system_path $page->getFilesystemPath(), $fileContents);
  140.             file_put_contents($postUploadedFile->getPathname(), $updated_file);
  141.         }
  142.         $finder->files()->in($this->dirPath);
  143.         // Checks if the old directory is empty to remove it
  144.         if (!$finder->hasResults()) {
  145.             // Deletes old templates and archives folders
  146.             $this->filesystem->remove($this->dirPath);
  147.             $mapping $event->getMapping();
  148.             // Deletes old extracted assets folder
  149.             if ($mapping->getMappingName() == 'page_assets_archive') {
  150.                 // Creates assets path by replacing 'archives' by 'assets' in the archive path
  151.                 $assetsPath str_replace('archives''assets'$this->dirPath);
  152.                 $this->filesystem->remove($assetsPath);
  153.             } else { // Deletes old updated templates folder
  154.                 // Creates assets path by replacing 'public/uploads/templates' by 'templates/uploads' in the template path
  155.                 $updatedTemplatesPath str_replace(
  156.                     $mapping->getUploadDestination(),
  157.                     $this->params->get('private_templates_path'),
  158.                     $this->dirPath,
  159.                 );
  160.                 $this->filesystem->remove($updatedTemplatesPath);
  161.             }
  162.         }
  163.     }
  164. }