vendor/sulu/article-bundle/Markup/ArticleLinkProvider.php line 126

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\ArticleBundle\Markup;
  11. use ONGR\ElasticsearchBundle\Service\Manager;
  12. use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery;
  13. use ONGR\ElasticsearchDSL\Search;
  14. use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
  15. use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
  16. use Sulu\Bundle\MarkupBundle\Markup\Link\LinkConfigurationBuilder;
  17. use Sulu\Bundle\MarkupBundle\Markup\Link\LinkItem;
  18. use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderInterface;
  19. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. /**
  23.  * Integrates articles into link-system.
  24.  */
  25. class ArticleLinkProvider implements LinkProviderInterface
  26. {
  27.     use ArticleViewDocumentIdTrait;
  28.     /**
  29.      * @var Manager
  30.      */
  31.     private $liveManager;
  32.     /**
  33.      * @var Manager
  34.      */
  35.     private $defaultManager;
  36.     /**
  37.      * @var WebspaceManagerInterface
  38.      */
  39.     protected $webspaceManager;
  40.     /**
  41.      * @var RequestStack
  42.      */
  43.     private $requestStack;
  44.     /**
  45.      * @var TranslatorInterface
  46.      */
  47.     private $translator;
  48.     /**
  49.      * @var array
  50.      */
  51.     private $types;
  52.     /**
  53.      * @var string
  54.      */
  55.     private $articleViewClass;
  56.     /**
  57.      * @var string
  58.      */
  59.     private $environment;
  60.     public function __construct(
  61.         Manager $liveManager,
  62.         Manager $defaultManager,
  63.         WebspaceManagerInterface $webspaceManager,
  64.         RequestStack $requestStack,
  65.         TranslatorInterface $translator,
  66.         array $types,
  67.         $articleViewClass,
  68.         $environment
  69.     ) {
  70.         $this->liveManager $liveManager;
  71.         $this->defaultManager $defaultManager;
  72.         $this->webspaceManager $webspaceManager;
  73.         $this->requestStack $requestStack;
  74.         $this->translator $translator;
  75.         $this->types $types;
  76.         $this->articleViewClass $articleViewClass;
  77.         $this->environment $environment;
  78.     }
  79.     public function getConfiguration()
  80.     {
  81.         // TODO implement tabs again?
  82.         return LinkConfigurationBuilder::create()
  83.             ->setTitle($this->translator->trans('sulu_article.articles', [], 'admin'))
  84.             ->setResourceKey('articles')
  85.             ->setListAdapter('table')
  86.             ->setDisplayProperties(['title'])
  87.             ->setOverlayTitle($this->translator->trans('sulu_article.single_selection_overlay_title', [], 'admin'))
  88.             ->setEmptyText($this->translator->trans('sulu_article.no_article_selected', [], 'admin'))
  89.             ->setIcon('su-newspaper')
  90.             ->getLinkConfiguration();
  91.     }
  92.     public function preload(array $hrefs$locale$published true)
  93.     {
  94.         $request $this->requestStack->getCurrentRequest();
  95.         $scheme 'http';
  96.         if ($request) {
  97.             $scheme $request->getScheme();
  98.         }
  99.         $search = new Search();
  100.         $search->addQuery(new IdsQuery($this->getViewDocumentIds($hrefs$locale)));
  101.         $search->setSize(\count($hrefs));
  102.         $repository $this->liveManager->getRepository($this->articleViewClass);
  103.         if (!$published) {
  104.             $repository $this->defaultManager->getRepository($this->articleViewClass);
  105.         }
  106.         $documents $repository->findDocuments($search);
  107.         $result = [];
  108.         /** @var ArticleViewDocumentInterface $document */
  109.         foreach ($documents as $document) {
  110.             $result[] = $this->createLinkItem($document$locale$scheme);
  111.         }
  112.         return $result;
  113.     }
  114.     protected function createLinkItem(ArticleViewDocumentInterface $documentstring $localestring $scheme): LinkItem
  115.     {
  116.         $url $this->webspaceManager->findUrlByResourceLocator(
  117.             $document->getRoutePath(),
  118.             $this->environment,
  119.             $locale,
  120.             $document->getTargetWebspace(),
  121.             null,
  122.             $scheme
  123.         );
  124.         return new LinkItem($document->getUuid(), $document->getTitle(), $url$document->getPublishedState());
  125.     }
  126. }