1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2015 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Loader; |
18
|
|
|
|
19
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
20
|
|
|
use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface; |
21
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
23
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteInterface; |
24
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
25
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Context\Context; |
26
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface; |
27
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface; |
28
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection; |
29
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class ArticleLoader. |
33
|
|
|
*/ |
34
|
|
|
class ArticleLoader extends PaginatedLoader implements LoaderInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var ArticleProviderInterface |
38
|
|
|
*/ |
39
|
|
|
protected $articleProvider; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var RouteProviderInterface |
43
|
|
|
*/ |
44
|
|
|
protected $routeProvider; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ObjectManager |
48
|
|
|
*/ |
49
|
|
|
protected $dm; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $routeBasepaths; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var MetaFactoryInterface |
58
|
|
|
*/ |
59
|
|
|
protected $metaFactory; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var Context |
63
|
|
|
*/ |
64
|
|
|
protected $context; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* ArticleLoader constructor. |
68
|
|
|
* |
69
|
|
|
* @param ArticleProviderInterface $articleProvider |
70
|
|
|
* @param RouteProviderInterface $routeProvider |
71
|
|
|
* @param ObjectManager $dm |
72
|
|
|
* @param MetaFactoryInterface $metaFactory |
73
|
|
|
* @param Context $context |
74
|
|
|
*/ |
75
|
|
|
public function __construct( |
76
|
111 |
|
ArticleProviderInterface $articleProvider, |
77
|
|
|
RouteProviderInterface $routeProvider, |
78
|
|
|
ObjectManager $dm, |
79
|
|
|
MetaFactoryInterface $metaFactory, |
80
|
|
|
Context $context |
81
|
|
|
) { |
82
|
|
|
$this->articleProvider = $articleProvider; |
83
|
111 |
|
$this->routeProvider = $routeProvider; |
84
|
111 |
|
$this->dm = $dm; |
85
|
111 |
|
$this->metaFactory = $metaFactory; |
86
|
111 |
|
$this->context = $context; |
87
|
111 |
|
} |
88
|
111 |
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE) |
93
|
|
|
{ |
94
|
|
|
$criteria = new Criteria(); |
95
|
|
|
if ('article' === $type && LoaderInterface::SINGLE === $responseType) { |
96
|
|
|
$article = null; |
97
|
|
|
if (array_key_exists('article', $parameters) && $parameters['article'] instanceof ArticleInterface) { |
98
|
|
|
$this->dm->detach($parameters['article']); |
99
|
|
|
$criteria->set('id', $parameters['article']->getId()); |
100
|
|
|
unset($parameters['article']); |
101
|
|
|
} elseif (array_key_exists('slug', $parameters)) { |
102
|
|
|
$criteria->set('slug', $parameters['slug']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
try { |
106
|
|
|
$article = $this->articleProvider->getOneByCriteria($criteria); |
107
|
|
|
} catch (NotFoundHttpException $e) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
10 |
|
|
111
|
|
|
try { |
112
|
10 |
|
return $this->getArticleMeta($article); |
113
|
10 |
|
} catch (NotFoundHttpException $e) { |
114
|
9 |
|
return; |
115
|
|
|
} |
116
|
|
|
} elseif ('articles' === $type && LoaderInterface::COLLECTION === $responseType) { |
117
|
9 |
|
$currentPage = $this->context['route']; |
118
|
9 |
|
$route = null; |
119
|
|
|
|
120
|
|
|
if ($currentPage) { |
121
|
|
|
$route = $currentPage->getValues(); |
122
|
9 |
|
} |
123
|
2 |
|
|
124
|
2 |
|
if (array_key_exists('route', $parameters)) { |
125
|
|
|
if (null === $route || ($route instanceof RouteInterface && $route->getId() !== $parameters['route'])) { |
126
|
2 |
|
if (is_int($parameters['route'])) { |
127
|
2 |
|
$route = $this->routeProvider->getOneById($parameters['route']); |
128
|
2 |
|
} elseif (is_string($parameters['route'])) { |
129
|
|
|
$route = $this->routeProvider->getOneByStaticPrefix($parameters['route']); |
130
|
2 |
|
} elseif (is_array($parameters['route'])) { |
131
|
2 |
|
$loadByStaticPrefix = true; |
132
|
|
|
foreach ($parameters['route'] as $key => $providedRoute) { |
133
|
|
|
if (!is_string($providedRoute)) { |
134
|
2 |
|
$loadByStaticPrefix = false; |
135
|
2 |
|
|
136
|
2 |
|
break; |
137
|
|
|
} |
138
|
2 |
|
} |
139
|
2 |
|
|
140
|
|
|
if ($loadByStaticPrefix) { |
141
|
|
|
$route = $this->routeProvider->getWithChildrenByStaticPrefix($parameters['route']); |
142
|
2 |
|
} else { |
143
|
|
|
$route = $parameters['route']; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (null === $route) { |
148
|
|
|
// if Route parameter was passed but it was not found - don't return articles not filtered by route |
149
|
2 |
|
return; |
150
|
2 |
|
} |
151
|
2 |
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (null !== $route && ($route instanceof RouteInterface && RouteInterface::TYPE_COLLECTION === $route->getType() || is_array($route))) { |
155
|
2 |
|
$criteria->set('route', $route); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach (['metadata', 'keywords', 'source', 'author'] as $item) { |
159
|
2 |
|
if (isset($parameters[$item])) { |
160
|
2 |
|
$criteria->set($item, $parameters[$item]); |
161
|
2 |
|
} |
162
|
2 |
|
|
163
|
2 |
|
if (isset($withoutParameters[$item])) { |
164
|
2 |
|
$criteria->set('exclude_'.$item, $withoutParameters[$item]); |
165
|
2 |
|
} |
166
|
2 |
|
} |
167
|
2 |
|
|
168
|
|
|
$this->applyPaginationToCriteria($criteria, $parameters); |
169
|
|
|
$this->setDateRangeToCriteria($criteria, $parameters); |
170
|
2 |
|
$countCriteria = clone $criteria; |
171
|
|
|
$articlesCollection = $this->articleProvider->getManyByCriteria($criteria, $criteria->get('order', [])); |
172
|
2 |
|
|
173
|
|
|
if ($articlesCollection->count() > 0) { |
174
|
|
|
$metaCollection = new MetaCollection(); |
175
|
|
|
$metaCollection->setTotalItemsCount($this->articleProvider->getCountByCriteria($countCriteria)); |
176
|
|
|
foreach ($articlesCollection as $article) { |
177
|
|
|
$articleMeta = $this->getArticleMeta($article); |
178
|
|
|
if (null !== $articleMeta) { |
179
|
|
|
$metaCollection->add($articleMeta); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
unset($articlesCollection, $route, $criteria); |
183
|
|
|
|
184
|
|
|
return $metaCollection; |
185
|
|
|
} |
186
|
14 |
|
} |
187
|
|
|
|
188
|
14 |
|
return; |
189
|
|
|
} |
190
|
|
|
|
191
|
8 |
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
8 |
|
*/ |
194
|
8 |
|
public function isSupported(string $type): bool |
195
|
|
|
{ |
196
|
|
|
return in_array($type, ['articles', 'article']); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param Criteria $criteria |
201
|
|
|
* @param array $parameters |
202
|
|
|
*/ |
203
|
|
|
private function setDateRangeToCriteria(Criteria $criteria, array $parameters) |
204
|
|
|
{ |
205
|
|
|
if (isset($parameters['date_range']) && is_array($parameters['date_range']) && 2 === count($parameters['date_range'])) { |
206
|
|
|
$criteria->set('dateRange', $parameters['date_range']); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param ArticleInterface|null $article |
212
|
|
|
* |
213
|
|
|
* @return \SWP\Component\TemplatesSystem\Gimme\Meta\Meta|void |
214
|
|
|
*/ |
215
|
|
|
private function getArticleMeta($article) |
216
|
|
|
{ |
217
|
|
|
if (null !== $article) { |
218
|
|
|
return $this->metaFactory->create($article); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|