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\Collections\ArrayCollection; |
20
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
21
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface; |
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\Meta; |
30
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection; |
31
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class ArticleLoader. |
35
|
|
|
*/ |
36
|
|
|
class ArticleLoader extends PaginatedLoader implements LoaderInterface |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var ArticleRepositoryInterface |
40
|
|
|
*/ |
41
|
|
|
protected $articleRepository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var RouteProviderInterface |
45
|
|
|
*/ |
46
|
|
|
protected $routeProvider; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ObjectManager |
50
|
|
|
*/ |
51
|
|
|
protected $dm; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $routeBasepaths; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var MetaFactoryInterface |
60
|
|
|
*/ |
61
|
|
|
protected $metaFactory; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Context |
65
|
|
|
*/ |
66
|
|
|
protected $context; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* ArticleLoader constructor. |
70
|
|
|
* |
71
|
|
|
* @param ArticleRepositoryInterface $articleRepository |
72
|
|
|
* @param RouteProviderInterface $routeProvider |
73
|
|
|
* @param ObjectManager $dm |
74
|
|
|
* @param MetaFactoryInterface $metaFactory |
75
|
|
|
* @param Context $context |
76
|
111 |
|
*/ |
77
|
|
|
public function __construct( |
78
|
|
|
ArticleRepositoryInterface $articleRepository, |
79
|
|
|
RouteProviderInterface $routeProvider, |
80
|
|
|
ObjectManager $dm, |
81
|
|
|
MetaFactoryInterface $metaFactory, |
82
|
|
|
Context $context |
83
|
111 |
|
) { |
84
|
111 |
|
$this->articleRepository = $articleRepository; |
85
|
111 |
|
$this->routeProvider = $routeProvider; |
86
|
111 |
|
$this->dm = $dm; |
87
|
111 |
|
$this->metaFactory = $metaFactory; |
88
|
111 |
|
$this->context = $context; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Load meta object by provided type and parameters. |
93
|
|
|
* |
94
|
|
|
* @MetaLoaderDoc( |
95
|
|
|
* description="Article Loader loads articles from Content Repository", |
96
|
|
|
* parameters={ |
97
|
|
|
* contentPath="SINGLE|required content path", |
98
|
|
|
* slug="SINGLE|required content slug", |
99
|
|
|
* pageName="COLLECTiON|name of Page for required articles" |
100
|
|
|
* } |
101
|
|
|
* ) |
102
|
|
|
* |
103
|
|
|
* @param string $type object type |
104
|
|
|
* @param array $parameters parameters needed to load required object type |
105
|
|
|
* @param int $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION) |
106
|
|
|
* |
107
|
|
|
* @return Meta|Meta[]|bool false if meta cannot be loaded, a Meta instance otherwise |
108
|
|
|
* |
109
|
|
|
* @throws \Exception |
110
|
10 |
|
*/ |
111
|
|
|
public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE) |
112
|
10 |
|
{ |
113
|
10 |
|
$criteria = new Criteria(); |
114
|
9 |
|
if ($type === 'article' && $responseType === LoaderInterface::SINGLE) { |
115
|
|
|
$article = null; |
116
|
|
|
if (array_key_exists('article', $parameters) && $parameters['article'] instanceof ArticleInterface) { |
117
|
9 |
|
$this->dm->detach($parameters['article']); |
118
|
9 |
|
$article = $this->articleRepository->findOneBy(['id' => $parameters['article']->getId()]); |
119
|
|
|
unset($parameters['article']); |
120
|
|
|
} elseif (array_key_exists('slug', $parameters)) { |
121
|
|
|
$article = $this->articleRepository->findOneBySlug($parameters['slug']); |
122
|
9 |
|
} |
123
|
2 |
|
|
124
|
2 |
|
try { |
125
|
|
|
return $this->getArticleMeta($article); |
126
|
2 |
|
} catch (NotFoundHttpException $e) { |
127
|
2 |
|
return; |
128
|
2 |
|
} |
129
|
|
|
} elseif ($type === 'articles' && $responseType === LoaderInterface::COLLECTION) { |
130
|
2 |
|
$currentPage = $this->context['route']; |
131
|
2 |
|
$route = null; |
132
|
|
|
|
133
|
|
|
if ($currentPage) { |
134
|
2 |
|
$route = $currentPage->getValues(); |
135
|
2 |
|
} |
136
|
2 |
|
|
137
|
|
|
if (array_key_exists('route', $parameters)) { |
138
|
2 |
|
if (null === $route || ($route instanceof RouteInterface && $route->getId() !== $parameters['route'])) { |
139
|
2 |
|
if (is_int($parameters['route'])) { |
140
|
|
|
$route = $this->routeProvider->getOneById($parameters['route']); |
141
|
|
|
} elseif (is_string($parameters['route'])) { |
142
|
2 |
|
$route = $this->routeProvider->getOneByStaticPrefix($parameters['route']); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (null === $route) { |
146
|
|
|
// if Route parameter was passed but it was not found - don't return articles not filtered by route |
147
|
|
|
return; |
148
|
|
|
} |
149
|
2 |
|
} |
150
|
2 |
|
} |
151
|
2 |
|
|
152
|
|
|
if (null !== $route) { |
153
|
|
|
if ($route instanceof RouteInterface && RouteInterface::TYPE_COLLECTION === $route->getType()) { |
154
|
|
|
$criteria->set('route', $route); |
155
|
2 |
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (isset($parameters['metadata'])) { |
159
|
2 |
|
$criteria->set('metadata', $parameters['metadata']); |
160
|
2 |
|
} |
161
|
2 |
|
|
162
|
2 |
|
if (isset($parameters['keywords'])) { |
163
|
2 |
|
$criteria->set('keywords', $parameters['keywords']); |
164
|
2 |
|
} |
165
|
2 |
|
|
166
|
2 |
|
$criteria = $this->applyPaginationToCriteria($criteria, $parameters); |
167
|
2 |
|
$countCriteria = clone $criteria; |
168
|
|
|
$articles = $this->articleRepository->findArticlesByCriteria($criteria, $criteria->get('order', [])); |
169
|
|
|
$articlesCollection = new ArrayCollection($articles); |
170
|
2 |
|
if ($articlesCollection->count() > 0) { |
171
|
|
|
$metaCollection = new MetaCollection(); |
172
|
2 |
|
$metaCollection->setTotalItemsCount($this->articleRepository->countByCriteria($countCriteria)); |
173
|
|
|
foreach ($articlesCollection as $article) { |
174
|
|
|
$articleMeta = $this->getArticleMeta($article); |
175
|
|
|
if (null !== $articleMeta) { |
176
|
|
|
$metaCollection->add($articleMeta); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
unset($articlesCollection, $route, $criteria); |
180
|
|
|
|
181
|
|
|
return $metaCollection; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return; |
186
|
14 |
|
} |
187
|
|
|
|
188
|
14 |
|
/** |
189
|
|
|
* Checks if Loader supports provided type. |
190
|
|
|
* |
191
|
8 |
|
* @param string $type |
192
|
|
|
* |
193
|
8 |
|
* @return bool |
194
|
8 |
|
*/ |
195
|
|
|
public function isSupported(string $type): bool |
196
|
|
|
{ |
197
|
|
|
return in_array($type, ['articles', 'article']); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function getArticleMeta($article) |
201
|
|
|
{ |
202
|
|
|
if (null !== $article) { |
203
|
|
|
return $this->metaFactory->create($article); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|