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\Bundle\ContentBundle\Twig\Cache\CacheBlockTagsCollectorInterface; |
22
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
23
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
24
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteInterface; |
25
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
26
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Context\Context; |
27
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface; |
28
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface; |
29
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
31
|
|
|
|
32
|
|
|
class ArticleLoader extends PaginatedLoader implements LoaderInterface |
33
|
|
|
{ |
34
|
|
|
protected $articleProvider; |
35
|
|
|
|
36
|
|
|
protected $routeProvider; |
37
|
|
|
|
38
|
|
|
protected $dm; |
39
|
|
|
|
40
|
|
|
protected $metaFactory; |
41
|
|
|
|
42
|
|
|
protected $context; |
43
|
|
|
|
44
|
|
|
private $cacheBlockTagsCollector; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
ArticleProviderInterface $articleProvider, |
48
|
|
|
RouteProviderInterface $routeProvider, |
49
|
|
|
ObjectManager $dm, |
50
|
|
|
MetaFactoryInterface $metaFactory, |
51
|
|
|
Context $context, |
52
|
|
|
CacheBlockTagsCollectorInterface $cacheBlockTagsCollector |
53
|
|
|
) { |
54
|
|
|
$this->articleProvider = $articleProvider; |
55
|
|
|
$this->routeProvider = $routeProvider; |
56
|
|
|
$this->dm = $dm; |
57
|
|
|
$this->metaFactory = $metaFactory; |
58
|
|
|
$this->context = $context; |
59
|
|
|
$this->cacheBlockTagsCollector = $cacheBlockTagsCollector; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE) |
63
|
|
|
{ |
64
|
|
|
$criteria = new Criteria(); |
65
|
|
|
if ('article' === $type && LoaderInterface::SINGLE === $responseType) { |
66
|
|
|
$article = null; |
67
|
|
|
if (array_key_exists('article', $parameters) && $parameters['article'] instanceof ArticleInterface) { |
68
|
|
|
try { |
69
|
|
|
return $this->getArticleMeta($parameters['article']); |
70
|
|
|
} catch (NotFoundHttpException $e) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} elseif (array_key_exists('slug', $parameters)) { |
74
|
|
|
if ('' === $parameters['slug']) { |
75
|
|
|
$parameters['slug'] = null; |
76
|
111 |
|
} |
77
|
|
|
$criteria->set('slug', $parameters['slug']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$article = $this->articleProvider->getOneByCriteria($criteria); |
82
|
|
|
|
83
|
111 |
|
return $this->getArticleMeta($article); |
84
|
111 |
|
} catch (NotFoundHttpException $e) { |
85
|
111 |
|
return false; |
86
|
111 |
|
} |
87
|
111 |
|
} elseif ('articles' === $type && LoaderInterface::COLLECTION === $responseType) { |
88
|
111 |
|
$currentPage = $this->context['route']; |
89
|
|
|
$route = null; |
90
|
|
|
|
91
|
|
|
if ($currentPage) { |
92
|
|
|
$route = $currentPage->getValues(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (array_key_exists('route', $parameters)) { |
96
|
|
|
if (null === $route || ($route instanceof RouteInterface && $route->getId() !== $parameters['route'])) { |
97
|
|
|
$route = $this->routeProvider->getByMixed($parameters['route']); |
98
|
|
|
|
99
|
|
|
if (null === $route) { |
100
|
|
|
// if Route parameter was passed but it was not found - don't return articles not filtered by route |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (null !== $route && (($route instanceof RouteInterface && RouteInterface::TYPE_COLLECTION === $route->getType()) || is_array($route))) { |
107
|
|
|
$criteria->set('route', $route); |
108
|
|
|
} |
109
|
|
|
|
110
|
10 |
|
foreach (['metadata', 'extra', 'keywords', 'source', 'author', 'article', 'publishedAfter', 'publishedBefore'] as $item) { |
111
|
|
|
if (isset($parameters[$item])) { |
112
|
10 |
|
$criteria->set($item, $parameters[$item]); |
113
|
10 |
|
} |
114
|
9 |
|
|
115
|
|
|
if (isset($withoutParameters[$item])) { |
116
|
|
|
$criteria->set('exclude_'.$item, $withoutParameters[$item]); |
117
|
9 |
|
} |
118
|
9 |
|
} |
119
|
|
|
|
120
|
|
|
$this->applyPaginationToCriteria($criteria, $parameters); |
121
|
|
|
$this->setDateRangeToCriteria($criteria, $parameters); |
122
|
9 |
|
$countCriteria = clone $criteria; |
123
|
2 |
|
$articlesCollection = $this->articleProvider->getManyByCriteria($criteria, $criteria->get('order', [])); |
124
|
2 |
|
if ($articlesCollection->count() > 0) { |
125
|
|
|
$metaCollection = new MetaCollection(); |
126
|
2 |
|
$metaCollection->setTotalItemsCount($this->articleProvider->getCountByCriteria($countCriteria)); |
127
|
2 |
|
foreach ($articlesCollection as $article) { |
128
|
2 |
|
$articleMeta = $this->getArticleMeta($article); |
129
|
|
|
if (null !== $articleMeta) { |
130
|
2 |
|
$metaCollection->add($articleMeta); |
131
|
2 |
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
unset($articlesCollection, $route, $criteria); |
135
|
2 |
|
|
136
|
2 |
|
return $metaCollection; |
137
|
|
|
} |
138
|
2 |
|
|
139
|
2 |
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function isSupported(string $type): bool |
146
|
|
|
{ |
147
|
|
|
return in_array($type, ['articles', 'article']); |
148
|
|
|
} |
149
|
2 |
|
|
150
|
2 |
|
protected function setDateRangeToCriteria(Criteria $criteria, array $parameters): void |
151
|
2 |
|
{ |
152
|
|
|
if (isset($parameters['date_range']) && is_array($parameters['date_range']) && 2 === count($parameters['date_range'])) { |
153
|
|
|
$criteria->set('dateRange', $parameters['date_range']); |
154
|
|
|
} |
155
|
2 |
|
} |
156
|
|
|
|
157
|
|
|
protected function getArticleMeta(?ArticleInterface $article) |
158
|
|
|
{ |
159
|
2 |
|
if (null !== $article) { |
160
|
2 |
|
$this->cacheBlockTagsCollector->addTagToCurrentCacheBlock('a-'.$article->getId()); |
161
|
2 |
|
|
162
|
2 |
|
return $this->metaFactory->create($article); |
163
|
2 |
|
} |
164
|
2 |
|
|
165
|
2 |
|
return false; |
166
|
2 |
|
} |
167
|
|
|
} |
168
|
|
|
|