vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Teaser/PHPCRPageTeaserProvider.php line 97

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\PageBundle\Teaser;
  11. use Sulu\Bundle\MediaBundle\Api\Media;
  12. use Sulu\Bundle\PageBundle\Admin\PageAdmin;
  13. use Sulu\Bundle\PageBundle\Teaser\Configuration\TeaserConfiguration;
  14. use Sulu\Bundle\PageBundle\Teaser\Provider\TeaserProviderInterface;
  15. use Sulu\Component\Content\Compat\PropertyParameter;
  16. use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
  17. use Sulu\Component\Content\Metadata\StructureMetadata;
  18. use Sulu\Component\Content\Query\ContentQueryBuilderInterface;
  19. use Sulu\Component\Content\Query\ContentQueryExecutorInterface;
  20. use Sulu\Component\Security\Authorization\PermissionTypes;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. class PHPCRPageTeaserProvider implements TeaserProviderInterface
  23. {
  24.     /**
  25.      * @var ContentQueryExecutorInterface
  26.      */
  27.     private $contentQueryExecutor;
  28.     /**
  29.      * @var ContentQueryBuilderInterface
  30.      */
  31.     private $contentQueryBuilder;
  32.     /**
  33.      * @var StructureMetadataFactoryInterface
  34.      */
  35.     private $structureMetadataFactory;
  36.     /**
  37.      * @var TranslatorInterface
  38.      */
  39.     private $translator;
  40.     /**
  41.      * @var bool
  42.      */
  43.     private $showDrafts;
  44.     /**
  45.      * @var array<string, int>
  46.      */
  47.     private $permissions;
  48.     /**
  49.      * @param bool $showDrafts Parameter "sulu_document_manager.show_drafts"
  50.      * @param array<string, int> $permissions Parameter "sulu_security.permissions"
  51.      */
  52.     public function __construct(
  53.         ContentQueryExecutorInterface $contentQueryExecutor,
  54.         ContentQueryBuilderInterface $contentQueryBuilder,
  55.         StructureMetadataFactoryInterface $structureMetadataFactory,
  56.         TranslatorInterface $translator,
  57.         bool $showDrafts,
  58.         array $permissions
  59.     ) {
  60.         $this->contentQueryExecutor $contentQueryExecutor;
  61.         $this->contentQueryBuilder $contentQueryBuilder;
  62.         $this->structureMetadataFactory $structureMetadataFactory;
  63.         $this->translator $translator;
  64.         $this->showDrafts $showDrafts;
  65.         $this->permissions $permissions;
  66.     }
  67.     public function getConfiguration(): TeaserConfiguration
  68.     {
  69.         return new TeaserConfiguration(
  70.             $this->translator->trans('sulu_page.page', [], 'admin'),
  71.             'pages',
  72.             'column_list',
  73.             ['title'],
  74.             $this->translator->trans('sulu_page.single_selection_overlay_title', [], 'admin'),
  75.             PageAdmin::EDIT_FORM_VIEW,
  76.             ['id' => 'id''attributes/webspaceKey' => 'webspace']
  77.         );
  78.     }
  79.     /**
  80.      * @param string[] $ids
  81.      * @param string $locale
  82.      *
  83.      * @return Teaser[]
  84.      */
  85.     public function find(array $ids$locale): array
  86.     {
  87.         if (=== \count($ids)) {
  88.             return [];
  89.         }
  90.         $pages $this->loadPages($ids$locale);
  91.         $result = [];
  92.         foreach ($pages as $pageData) {
  93.             $teaser $this->createTeaser($pageData$locale);
  94.             if (null === $teaser) {
  95.                 continue;
  96.             }
  97.             $result[] = $teaser;
  98.         }
  99.         return $result;
  100.     }
  101.     /**
  102.      * @param string[] $ids
  103.      *
  104.      * @return array<array<string, mixed>>
  105.      */
  106.     protected function loadPages(array $idsstring $locale): array
  107.     {
  108.         $this->contentQueryBuilder->init(
  109.             [
  110.                 'ids' => $ids,
  111.                 'properties' => $this->getPropertiesToLoad(),
  112.                 'published' => !$this->showDrafts,
  113.             ]
  114.         );
  115.         return $this->contentQueryExecutor->execute(
  116.             null,
  117.             [$locale],
  118.             $this->contentQueryBuilder,
  119.             true,
  120.             -1,
  121.             null,
  122.             null,
  123.             false,
  124.             $this->permissions[PermissionTypes::VIEW]
  125.         );
  126.     }
  127.     /**
  128.      * @return PropertyParameter[]
  129.      */
  130.     protected function getPropertiesToLoad(): array
  131.     {
  132.         return \array_merge(
  133.             $this->getTeaserPropertiesToLoad(),
  134.             [
  135.                 new PropertyParameter('excerptTitle''excerpt.title'),
  136.                 new PropertyParameter('excerptDescription''excerpt.description'),
  137.                 new PropertyParameter('excerptMore''excerpt.more'),
  138.                 new PropertyParameter('excerptImages''excerpt.images'),
  139.             ]
  140.         );
  141.     }
  142.     /**
  143.      * @return PropertyParameter[]
  144.      */
  145.     protected function getTeaserPropertiesToLoad(): array
  146.     {
  147.         $allMetadata $this->structureMetadataFactory->getStructures('page');
  148.         $properties = [];
  149.         foreach ($allMetadata as $metadata) {
  150.             if ($teaserProperty $this->getTeaserProperty($metadata'sulu.teaser.description''teaserDescription')) {
  151.                 $properties[] = $teaserProperty;
  152.             }
  153.             if ($teaserProperty $this->getTeaserProperty($metadata'sulu.teaser.media''teaserMedia')) {
  154.                 $properties[] = $teaserProperty;
  155.             }
  156.         }
  157.         return $properties;
  158.     }
  159.     private function getTeaserProperty(StructureMetadata $metadatastring $tagNamestring $propertyName): ?PropertyParameter
  160.     {
  161.         if ($metadata->hasPropertyWithTagName($tagName)) {
  162.             $property $metadata->getPropertyByTagName($tagName);
  163.             return new PropertyParameter($metadata->getName() . '_' $propertyName$property->getName());
  164.         }
  165.         return null;
  166.     }
  167.     /**
  168.      * @param array<string, mixed> $pageData
  169.      */
  170.     protected function createTeaser(array $pageDatastring $locale): ?Teaser
  171.     {
  172.         return new Teaser(
  173.             $this->getId($pageData),
  174.             'pages',
  175.             $locale,
  176.             $this->getTitle($pageData),
  177.             $this->getDescription($pageData),
  178.             $this->getMoreText($pageData),
  179.             $this->getUrl($pageData),
  180.             $this->getMediaId($pageData),
  181.             $this->getAttributes($pageData)
  182.         );
  183.     }
  184.     /**
  185.      * @param array<string, mixed> $pageData
  186.      */
  187.     protected function getId(array $pageData): ?string
  188.     {
  189.         return $pageData['id'] ?? null;
  190.     }
  191.     /**
  192.      * @param array<string, mixed> $pageData
  193.      */
  194.     protected function getTitle(array $pageData): ?string
  195.     {
  196.         $excerptTitle $pageData['excerptTitle'] ?? null;
  197.         $pageTitle $pageData['title'] ?? null;
  198.         return $excerptTitle ?: $pageTitle;
  199.     }
  200.     /**
  201.      * @param array<string, mixed> $pageData
  202.      */
  203.     protected function getDescription(array $pageData): ?string
  204.     {
  205.         $excerptDescription $pageData['excerptDescription'];
  206.         $structureType $this->getStructureType($pageData);
  207.         $teaserDescription $pageData[$structureType '_teaserDescription'] ?? null;
  208.         return $excerptDescription ?: $teaserDescription;
  209.     }
  210.     /**
  211.      * @param array<string, mixed> $pageData
  212.      */
  213.     protected function getMoreText(array $pageData): ?string
  214.     {
  215.         return $pageData['excerptMore'] ?? null;
  216.     }
  217.     /**
  218.      * @param array<string, mixed> $pageData
  219.      */
  220.     protected function getUrl(array $pageData): ?string
  221.     {
  222.         return $pageData['url'] ?? null;
  223.     }
  224.     /**
  225.      * @param array<string, mixed> $pageData
  226.      */
  227.     protected function getMediaId(array $pageData): ?int
  228.     {
  229.         $excerptMedia $pageData['excerptImages'][0] ?? null;
  230.         if ($excerptMedia instanceof Media) {
  231.             return $excerptMedia->getId();
  232.         }
  233.         $structureType $this->getStructureType($pageData);
  234.         $teaserMedia $pageData[$structureType '_teaserMedia'] ?? null;
  235.         if (\is_array($teaserMedia)) {
  236.             $teaserMedia $teaserMedia[0] ?? null;
  237.         }
  238.         if ($teaserMedia instanceof Media) {
  239.             return $teaserMedia->getId();
  240.         }
  241.         return null;
  242.     }
  243.     /**
  244.      * @param array<string, mixed> $pageData
  245.      *
  246.      * @return array<string, mixed>
  247.      */
  248.     protected function getAttributes(array $pageData): array
  249.     {
  250.         return [
  251.             'structureType' => $this->getStructureType($pageData),
  252.             'webspaceKey' => $this->getWebspaceKey($pageData),
  253.         ];
  254.     }
  255.     /**
  256.      * @param array<string, mixed> $pageData
  257.      */
  258.     protected function getStructureType(array $pageData): ?string
  259.     {
  260.         return $pageData['template'] ?? null;
  261.     }
  262.     /**
  263.      * @param array<string, mixed> $pageData
  264.      */
  265.     protected function getWebspaceKey(array $pageData): ?string
  266.     {
  267.         return $pageData['webspaceKey'] ?? null;
  268.     }
  269. }