src/Cms/Modules/GalleryBundle/Form/Type/Gallery/GalleryDataType.php line 40

Open in your IDE?
  1. <?php
  2. namespace Cms\Modules\GalleryBundle\Form\Type\Gallery;
  3. use Cms\ContainerBundle\Entity\Container;
  4. use Cms\ContainerBundle\Entity\Containers\GenericContainer;
  5. use Cms\ContainerBundle\Entity\Containers\IntranetContainer;
  6. use Cms\ContainerBundle\Entity\Containers\PersonalContainer;
  7. use Cms\ContainerBundle\Service\ContainerService;
  8. use Cms\CoreBundle\Form\Type\DateTimeType;
  9. use Cms\CoreBundle\Form\Type\HtmlType;
  10. use Cms\CoreBundle\Form\Type\SlugType;
  11. use Cms\CoreBundle\Model\Url;
  12. use Cms\CoreBundle\Service\Transcoding\Transcoder;
  13. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  14. use Cms\DomainBundle\Entity\Domain;
  15. use Cms\FileBundle\Entity\Nodes\File;
  16. use Cms\FileBundle\Entity\Nodes\Folder;
  17. use Cms\FileBundle\Form\Type\FileModalType;
  18. use Cms\ModuleBundle\Form\StandardMetadataType;
  19. use Cms\ModuleBundle\Form\Type\AbstractModuleDataType;
  20. use Cms\ModuleBundle\Validator\Constraints\ModuleReservedSlugs;
  21. use Cms\ModuleBundle\Validator\Constraints\ModuleSlug;
  22. use Cms\Modules\GalleryBundle\Form\Type\GalleryEditorType;
  23. use Cms\Modules\GalleryBundle\Model\Gallery\GalleryData;
  24. use Cms\Modules\GalleryBundle\Model\GalleryEntry;
  25. use Cms\TagBundle\Form\Type\TaggableType;
  26. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  27. use Symfony\Component\Form\FormBuilderInterface;
  28. use Symfony\Component\Form\Extension\Core\Type\TextType;
  29. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  30. use Symfony\Component\Form\FormEvent;
  31. use Symfony\Component\Form\FormEvents;
  32. use Symfony\Component\Validator\Constraints\NotBlank;
  33. /**
  34.  * Class GalleryDataType
  35.  * @package Cms\Modules\GalleryBundle\Form\Type\Gallery
  36.  */
  37. final class GalleryDataType extends AbstractModuleDataType
  38. {
  39.     /**
  40.      * @var ParameterBagInterface
  41.      */
  42.     private ParameterBagInterface $params;
  43.     /**
  44.      * @var EntityManager
  45.      */
  46.     private EntityManager $em;
  47.     /**
  48.      * @var ContainerService
  49.      */
  50.     private ContainerService $containerService;
  51.     /**
  52.      * @param ParameterBagInterface $params
  53.      * @param EntityManager $em
  54.      * @param ContainerService $containerService
  55.      */
  56.     public function __construct(
  57.         ParameterBagInterface $params,
  58.         EntityManager $em,
  59.         ContainerService $containerService
  60.     )
  61.     {
  62.         $this->params $params;
  63.         $this->em $em;
  64.         $this->containerService $containerService;
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     public function buildForm(FormBuilderInterface $builder, array $options): void
  70.     {
  71.         /** @var Container $container */
  72.         $container $options['container'];
  73.         $builder
  74.             ->add('title'TextType::class, array(
  75.                 'constraints' => array(
  76.                     new NotBlank(),
  77.                 ),
  78.             ))
  79.             ->add('slug'SlugType::class, array(
  80.                 'dependency' => array('title'),
  81.                 'helpText' => 'Attention! Changing the URL can cause broken links across your site. Proceed with caution.',
  82.                 'constraints' => array(
  83.                     new ModuleSlug(),
  84.                     new ModuleReservedSlugs(),
  85.                 ),
  86.                 'slugPrefix' => sprintf(
  87.                     '%s/gallery/{id}/',
  88.                     rtrim($this->containerService->getFrontPath($container), '/')
  89.                 ),
  90.             ))
  91.             ->add('timestamp'DateTimeType::class, array(
  92.                 'constraints' => array(
  93.                     new NotBlank(),
  94.                 ),
  95.             ))
  96.             // not showing image field as this is set via featured setting in gallery items
  97.             ->add('abstract'TextareaType::class, array(
  98.                 'label' => 'Teaser',
  99.             ))
  100.             ->add('description'HtmlType::class, array(
  101.                 'baseHref' => $this->containerService->getFrontLink($container),
  102.             ))
  103.             ->add('tags'TaggableType::class, [])
  104.             ->add('entries'GalleryEditorType::class, array(
  105.                 'modalParams' => array(
  106.                     'containerId' => $container->getId()
  107.                 ),
  108.                 'wscCustomerId' => $this->params->get('wsc.customer_id'),
  109.                 'baseHref' => $this->containerService->getFrontLink($container),
  110.             ))
  111.             ->add('standardMetadata'StandardMetadataType::class, [])
  112.             ->add('socialImage'FileModalType::class, array(
  113.                 'container' => $container->getId(),
  114.             ))
  115.             ->addEventListener(
  116.                 FormEvents::POST_SUBMIT,
  117.                 function (FormEvent $event) use ($container$options) {
  118.                     /** @var GalleryData $data */
  119.                     $data $event->getForm()->getData();
  120.                     $entries Transcoder::fromStorage($data->getEntries());
  121.                     $featured null;
  122.                     /** @var GalleryEntry $entry */
  123.                     foreach ($entries as $entry) {
  124.                         if ($entry->getFeatured() === true) {
  125.                             $featured $entry;
  126.                             break;
  127.                         }
  128.                     }
  129.                     if ( ! $featured && count($entries) > 0) {
  130.                         $featured $entries[0];
  131.                     }
  132.                     $file null;
  133.                     if ( ! empty($featured) && ! empty($featured->getMedia())) {
  134.                         try {
  135.                             $file $this->fileUrlToEntity($featured$container$options);
  136.                         } catch (\Exception $e) {
  137.                             // NOOP
  138.                         }
  139.                     }
  140.                     $data->setImage($file);
  141.                 }
  142.             )
  143.         ;
  144.     }
  145.     private function fileUrlToEntity(GalleryEntry $entryContainer $rootContainer)
  146.     {
  147.         // TODO: SYMFONY: fix this?
  148.         $url Url::factory($entry->getMedia());
  149.         $value $url->getPath();
  150.         $hostname $url->getHost();
  151.         if (preg_match('/^(.*?)\\/files\\/(.+)/'$value$matches) !== 1) {
  152.             throw new \RuntimeException();
  153.         }
  154.         $containers array_values(array_filter(explode('/'$matches[1])));
  155.         // handle intranet requests
  156.         switch (true) {
  157.             case (count($containers) > && strpos($containers[0], '~') === 0):
  158.                 $site $this->em->getRepository(IntranetContainer::class)->findOneBy(array(
  159.                     'parent' => null,
  160.                     'slug' => ltrim($containers[0], '~'),
  161.                 ));
  162.                 $containers array_slice($containers1);
  163.                 break;
  164.             case empty($hostname):
  165.                 $cntr null;
  166.                 switch (true) {
  167.                     case $rootContainer instanceof GenericContainer:
  168.                     case $rootContainer instanceof IntranetContainer:
  169.                         $cntr $rootContainer;
  170.                         break;
  171.                     case $rootContainer instanceof PersonalContainer:
  172.                         $cntr $rootContainer->getContainer();
  173.                         break;
  174.                     default:
  175.                         throw new \RuntimeException();
  176.                 }
  177.                 while ($cntr->getParent()) {
  178.                     $cntr $cntr->getParent();
  179.                 }
  180.                 $site $cntr;
  181.                 break;
  182.             default:
  183.                 $fixedhost preg_replace('/^https?:\\/\\/([^\\/]+).*$/''$1'$hostname);
  184.                 if (preg_match('/^([-a-zA-Z0-9]+)\\.([-a-zA-Z0-9]+)\\.campussuite\\.site$/'$fixedhost$hostmatches) === 1) {
  185.                     $site $this->em->getRepository(GenericContainer::class)->findOneBy(array(
  186.                         'parent' => null,
  187.                         'slug' => $hostmatches[1],
  188.                     ));
  189.                 } else {
  190.                     $domain $this->em->getRepository(Domain::class)->findOneByHost(
  191.                         $fixedhost
  192.                     );
  193.                     $site $this->em->getRepository(GenericContainer::class)->findOneByDomain(
  194.                         $domain
  195.                     );
  196.                 }
  197.         }
  198.         $container $site;
  199.         foreach ($containers as $slug) {
  200.             $container $this->em->getRepository(Container::class)->findOneBy(array(
  201.                 'parent' => $container,
  202.                 'slug' => urldecode($slug),
  203.             ));
  204.             if (empty($container)) {
  205.                 throw new \RuntimeException();
  206.             }
  207.         }
  208.         $folders array_values(array_filter(explode('/'$matches[2])));
  209.         $fileparts = [];
  210.         if (preg_match('/(.*)\\.([a-zA-Z0-9]+)(?:[?].*)?$/'array_pop($folders), $fileparts) !== 1) {
  211.             throw new \RuntimeException();
  212.         }
  213.         $folder null;
  214.         foreach ($folders as $name) {
  215.             $folder $this->em->getRepository(Folder::class)->findOneBy(array(
  216.                 'container' => $container,
  217.                 'parent' => $folder,
  218.                 'name' => urldecode($name),
  219.             ));
  220.             if (empty($folder)) {
  221.                 throw new \RuntimeException();
  222.             }
  223.         }
  224.         $file $this->em->getRepository(File::class)->findOneBy(array(
  225.             'parent' => $folder,
  226.             'name' => urldecode($fileparts[1]),
  227.             'extension' => urldecode($fileparts[2]),
  228.         ));
  229.         if (empty($file)) {
  230.             throw new \RuntimeException();
  231.         }
  232.         return $file;
  233.     }
  234. }