-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathArticleLinkProvider.php
150 lines (126 loc) · 4.16 KB
/
ArticleLinkProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Sulu\Bundle\ArticleBundle\Markup;
use ONGR\ElasticsearchBundle\Service\Manager;
use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery;
use ONGR\ElasticsearchDSL\Search;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkConfigurationBuilder;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkItem;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderInterface;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Integrates articles into link-system.
*/
class ArticleLinkProvider implements LinkProviderInterface
{
use ArticleViewDocumentIdTrait;
/**
* @var Manager
*/
private $liveManager;
/**
* @var Manager
*/
private $defaultManager;
/**
* @var WebspaceManagerInterface
*/
protected $webspaceManager;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var array
*/
private $types;
/**
* @var string
*/
private $articleViewClass;
/**
* @var string
*/
private $environment;
public function __construct(
Manager $liveManager,
Manager $defaultManager,
WebspaceManagerInterface $webspaceManager,
RequestStack $requestStack,
TranslatorInterface $translator,
array $types,
$articleViewClass,
$environment
) {
$this->liveManager = $liveManager;
$this->defaultManager = $defaultManager;
$this->webspaceManager = $webspaceManager;
$this->requestStack = $requestStack;
$this->translator = $translator;
$this->types = $types;
$this->articleViewClass = $articleViewClass;
$this->environment = $environment;
}
public function getConfiguration()
{
// TODO implement tabs again?
return LinkConfigurationBuilder::create()
->setTitle($this->translator->trans('sulu_article.articles', [], 'admin'))
->setResourceKey('articles')
->setListAdapter('table')
->setDisplayProperties(['title'])
->setOverlayTitle($this->translator->trans('sulu_article.single_selection_overlay_title', [], 'admin'))
->setEmptyText($this->translator->trans('sulu_article.no_article_selected', [], 'admin'))
->setIcon('su-newspaper')
->getLinkConfiguration();
}
public function preload(array $hrefs, $locale, $published = true)
{
$request = $this->requestStack->getCurrentRequest();
$scheme = 'http';
if ($request) {
$scheme = $request->getScheme();
}
$search = new Search();
$search->addQuery(new IdsQuery($this->getViewDocumentIds($hrefs, $locale)));
$search->setSize(\count($hrefs));
$repository = $this->liveManager->getRepository($this->articleViewClass);
if (!$published) {
$repository = $this->defaultManager->getRepository($this->articleViewClass);
}
$documents = $repository->findDocuments($search);
$result = [];
/** @var ArticleViewDocumentInterface $document */
foreach ($documents as $document) {
$result[] = $this->createLinkItem($document, $locale, $scheme);
}
return $result;
}
protected function createLinkItem(ArticleViewDocumentInterface $document, string $locale, string $scheme): LinkItem
{
$url = $this->webspaceManager->findUrlByResourceLocator(
$document->getRoutePath(),
$this->environment,
$locale,
$document->getTargetWebspace(),
null,
$scheme
);
return new LinkItem($document->getUuid(), $document->getTitle(), $url, $document->getPublishedState());
}
}