vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Teaser/PageTeaserProvider.php line 90

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 Massive\Bundle\SearchBundle\Search\Document;
  12. use Massive\Bundle\SearchBundle\Search\QueryHit;
  13. use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
  14. use Sulu\Bundle\PageBundle\Admin\PageAdmin;
  15. use Sulu\Bundle\PageBundle\Search\Metadata\StructureProvider;
  16. use Sulu\Bundle\PageBundle\Teaser\Configuration\TeaserConfiguration;
  17. use Sulu\Bundle\PageBundle\Teaser\Provider\TeaserProviderInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. /**
  20.  * @deprecated This class will be replaced with the `PHPCRPageTeaserProvider`
  21.  */
  22. class PageTeaserProvider implements TeaserProviderInterface
  23. {
  24.     /**
  25.      * @var SearchManagerInterface
  26.      */
  27.     private $searchManager;
  28.     /**
  29.      * @var TranslatorInterface
  30.      */
  31.     private $translator;
  32.     /**
  33.      * @var bool
  34.      */
  35.     private $showDrafts;
  36.     /**
  37.      * @var TeaserProviderInterface|null
  38.      */
  39.     private $phpcrPageTeaserProvider;
  40.     /**
  41.      * @param PHPCRPageTeaserProvider|null $phpcrPageTeaserProvider
  42.      */
  43.     public function __construct(
  44.         SearchManagerInterface $searchManager,
  45.         TranslatorInterface $translator,
  46.         bool $showDrafts false,
  47.         ?TeaserProviderInterface $phpcrPageTeaserProvider null
  48.     ) {
  49.         $this->searchManager $searchManager;
  50.         $this->translator $translator;
  51.         $this->showDrafts $showDrafts;
  52.         $this->phpcrPageTeaserProvider $phpcrPageTeaserProvider;
  53.         if (null === $this->phpcrPageTeaserProvider) {
  54.             @\trigger_error(
  55.                 'Instantiating a PageTeaserProvider without the $phpcrPageTeaserProvider argument is deprecated!',
  56.                 \E_USER_DEPRECATED
  57.             );
  58.         }
  59.     }
  60.     public function getConfiguration()
  61.     {
  62.         if (null !== $this->phpcrPageTeaserProvider) {
  63.             return $this->phpcrPageTeaserProvider->getConfiguration();
  64.         }
  65.         return new TeaserConfiguration(
  66.             $this->translator->trans('sulu_page.page', [], 'admin'),
  67.             'pages',
  68.             'column_list',
  69.             ['title'],
  70.             $this->translator->trans('sulu_page.single_selection_overlay_title', [], 'admin'),
  71.             PageAdmin::EDIT_FORM_VIEW,
  72.             ['id' => 'id''attributes/webspaceKey' => 'webspace']
  73.         );
  74.     }
  75.     public function find(array $ids$locale)
  76.     {
  77.         if (null !== $this->phpcrPageTeaserProvider) {
  78.             return $this->phpcrPageTeaserProvider->find($ids$locale);
  79.         }
  80.         $statements \array_map(
  81.             function($item) {
  82.                 return \sprintf('__id:"%s"'$item);
  83.             },
  84.             $ids
  85.         );
  86.         $result = [];
  87.         $searchResult $this->searchManager
  88.             ->createSearch(\implode(' OR '$statements))
  89.             ->indexes($this->getPageIndexes())
  90.             ->locale($locale)
  91.             ->setLimit(\count($ids))
  92.             ->execute();
  93.         /** @var QueryHit $item */
  94.         foreach ($searchResult as $item) {
  95.             $document $item->getDocument();
  96.             $title $this->getTitleFromDocument($document);
  97.             $excerptTitle $this->getExcerptTitleFromDocument($document);
  98.             $excerptDescription $this->getExcerptDescritionFromDocument($document);
  99.             $excerptMedia $this->getMedia($document'excerptImages');
  100.             $teaserDescription $document->hasField(StructureProvider::FIELD_TEASER_DESCRIPTION) ?
  101.                 $document->getField(StructureProvider::FIELD_TEASER_DESCRIPTION)->getValue() : '';
  102.             $teaserMedia $document->hasField(StructureProvider::FIELD_TEASER_MEDIA) ?
  103.                 $this->getMedia($documentStructureProvider::FIELD_TEASER_MEDIA) : null;
  104.             $result[] = new Teaser(
  105.                 $item->getId(),
  106.                 'pages',
  107.                 $locale,
  108.                 ('' !== $excerptTitle $excerptTitle $title),
  109.                 ('' !== $excerptDescription $excerptDescription $teaserDescription),
  110.                 $document->getField('excerptMore')->getValue(),
  111.                 $document->getField('__url')->getValue(),
  112.                 (null !== $excerptMedia $excerptMedia $teaserMedia),
  113.                 $this->getAttributes($document)
  114.             );
  115.         }
  116.         return $result;
  117.     }
  118.     /**
  119.      * @deprecated
  120.      */
  121.     protected function getTitleFromDocument(Document $document)
  122.     {
  123.         return $document->getField('title')->getValue();
  124.     }
  125.     /**
  126.      * @deprecated
  127.      */
  128.     protected function getExcerptTitleFromDocument(Document $document)
  129.     {
  130.         return $document->getField('excerptTitle')->getValue();
  131.     }
  132.     /**
  133.      * @deprecated
  134.      */
  135.     protected function getExcerptDescritionFromDocument(Document $document)
  136.     {
  137.         return $document->getField('excerptDescription')->getValue();
  138.     }
  139.     /**
  140.      * Returns media-id.
  141.      *
  142.      * @param string $field
  143.      *
  144.      * @return int|null
  145.      */
  146.     private function getMedia(Document $document$field)
  147.     {
  148.         $images \json_decode($document->getField($field)->getValue(), true);
  149.         if (!$images || !\array_key_exists('ids'$images) || === \count($images['ids'])) {
  150.             return;
  151.         }
  152.         return $images['ids'][0];
  153.     }
  154.     /**
  155.      * Returns page indexes.
  156.      *
  157.      * @return array
  158.      */
  159.     private function getPageIndexes()
  160.     {
  161.         $allPageIndexNames \array_filter(
  162.             $this->searchManager->getIndexNames(),
  163.             function($index) {
  164.                 return \preg_match('/page_(.+)/'$index) > 0;
  165.             }
  166.         );
  167.         $publishedPageIndexNames \array_filter(
  168.             $allPageIndexNames,
  169.             function($index) {
  170.                 return \preg_match('/page_(.+)_published/'$index) > 0;
  171.             }
  172.         );
  173.         return $this->showDrafts
  174.             \array_values(\array_diff($allPageIndexNames$publishedPageIndexNames))
  175.             : \array_values($publishedPageIndexNames);
  176.     }
  177.     /**
  178.      * Returns attributes for teaser.
  179.      *
  180.      * @deprecated
  181.      *
  182.      * @return array
  183.      */
  184.     protected function getAttributes(Document $document)
  185.     {
  186.         return [
  187.             'structureType' => $document->getField(StructureProvider::FIELD_STRUCTURE_TYPE)->getValue(),
  188.             'webspaceKey' => $document->getField(StructureProvider::FIELD_WEBSPACE_KEY)->getValue(),
  189.         ];
  190.     }
  191. }