|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\ArticleBundle\Document\Serializer; |
|
13
|
|
|
|
|
14
|
|
|
use JMS\Serializer\EventDispatcher\Events; |
|
15
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
|
16
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
17
|
|
|
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; |
|
18
|
|
|
use Sulu\Bundle\ArticleBundle\Document\ArticleInterface; |
|
19
|
|
|
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument; |
|
20
|
|
|
use Sulu\Bundle\ArticleBundle\Document\Structure\ContentProxyFactory; |
|
21
|
|
|
use Sulu\Component\Content\Compat\StructureManagerInterface; |
|
22
|
|
|
use Sulu\Component\Content\Extension\ExtensionManagerInterface; |
|
23
|
|
|
use Sulu\Component\Util\SortUtils; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Extends serializer with additional functionality to prepare article(-page) data. |
|
27
|
|
|
*/ |
|
28
|
|
|
class ArticleWebsiteSubscriber implements EventSubscriberInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var StructureManagerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $structureManager; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ExtensionManagerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $extensionManager; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ContentProxyFactory |
|
42
|
|
|
*/ |
|
43
|
|
|
private $contentProxyFactory; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct( |
|
46
|
|
|
StructureManagerInterface $structureManager, |
|
47
|
|
|
ExtensionManagerInterface $extensionManager, |
|
48
|
|
|
ContentProxyFactory $contentProxyFactory |
|
49
|
|
|
) { |
|
50
|
|
|
$this->structureManager = $structureManager; |
|
51
|
|
|
$this->extensionManager = $extensionManager; |
|
52
|
|
|
$this->contentProxyFactory = $contentProxyFactory; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function getSubscribedEvents() |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
|
|
[ |
|
62
|
|
|
'event' => Events::POST_SERIALIZE, |
|
63
|
|
|
'format' => 'array', |
|
64
|
|
|
'method' => 'resolveContentOnPostSerialize', |
|
65
|
|
|
], |
|
66
|
1 |
|
[ |
|
67
|
|
|
'event' => Events::POST_SERIALIZE, |
|
68
|
|
|
'format' => 'array', |
|
69
|
|
|
'method' => 'resolveContentForArticleOnPostSerialize', |
|
70
|
1 |
|
], |
|
71
|
1 |
|
[ |
|
72
|
1 |
|
'event' => Events::POST_SERIALIZE, |
|
73
|
|
|
'format' => 'array', |
|
74
|
|
|
'method' => 'resolveContentForArticlePageOnPostSerialize', |
|
75
|
1 |
|
], |
|
76
|
1 |
|
[ |
|
77
|
1 |
|
'event' => Events::POST_SERIALIZE, |
|
78
|
|
|
'format' => 'array', |
|
79
|
|
|
'method' => 'appendPageData', |
|
80
|
1 |
|
], |
|
81
|
1 |
|
]; |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
1 |
|
* Resolve content on serialization. |
|
86
|
1 |
|
* |
|
87
|
1 |
|
* @param ObjectEvent $event |
|
88
|
|
|
*/ |
|
89
|
|
|
public function resolveContentOnPostSerialize(ObjectEvent $event) |
|
90
|
|
|
{ |
|
91
|
|
|
$article = $event->getObject(); |
|
92
|
|
|
$visitor = $event->getVisitor(); |
|
93
|
|
|
$context = $event->getContext(); |
|
94
|
|
|
|
|
95
|
|
|
if (!$article instanceof ArticleInterface || !$context->attributes->containsKey('website')) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$visitor->addData('uuid', $context->accept($article->getArticleUuid())); |
|
100
|
|
|
$visitor->addData('pageUuid', $context->accept($article->getPageUuid())); |
|
101
|
|
|
|
|
102
|
|
|
$extensionData = $article->getExtensionsData()->toArray(); |
|
103
|
|
|
$extension = []; |
|
104
|
|
|
foreach ($extensionData as $name => $data) { |
|
105
|
|
|
$extension[$name] = $this->extensionManager->getExtension('article', $name)->getContentData($data); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$visitor->addData('extension', $extension); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Append page data. |
|
113
|
|
|
* |
|
114
|
|
|
* @param ObjectEvent $event |
|
115
|
|
|
*/ |
|
116
|
|
|
public function appendPageData(ObjectEvent $event) |
|
117
|
|
|
{ |
|
118
|
|
|
$article = $event->getObject(); |
|
119
|
|
|
$visitor = $event->getVisitor(); |
|
120
|
|
|
$context = $event->getContext(); |
|
121
|
|
|
|
|
122
|
|
|
if ($article instanceof ArticlePageDocument) { |
|
123
|
|
|
$article = $article->getParent(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (!$article instanceof ArticleDocument || !$context->attributes->containsKey('website')) { |
|
127
|
|
|
return; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$pageNumber = 1; |
|
131
|
|
|
if ($context->attributes->containsKey('pageNumber')) { |
|
132
|
|
|
$pageNumber = $context->attributes->get('pageNumber')->get(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$visitor->addData('page', $pageNumber); |
|
136
|
|
|
$visitor->addData('pages', $context->accept($article->getPages())); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Resolve content on serialization. |
|
141
|
|
|
* |
|
142
|
|
|
* @param ObjectEvent $event |
|
143
|
|
|
*/ |
|
144
|
|
|
public function resolveContentForArticleOnPostSerialize(ObjectEvent $event) |
|
145
|
|
|
{ |
|
146
|
|
|
$article = $event->getObject(); |
|
147
|
|
|
$visitor = $event->getVisitor(); |
|
148
|
|
|
$context = $event->getContext(); |
|
149
|
|
|
|
|
150
|
|
|
if (!$article instanceof ArticleDocument || !$context->attributes->containsKey('website')) { |
|
151
|
|
|
return; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$children = $article->getChildren(); |
|
155
|
|
|
|
|
156
|
|
|
if (null !== $children && $context->attributes->containsKey('pageNumber')) { |
|
157
|
|
|
$pages = array_values(is_array($children) ? $children : iterator_to_array($children)); |
|
158
|
|
|
$pages = SortUtils::multisort($pages, 'pageNumber'); |
|
159
|
|
|
|
|
160
|
|
|
$pageNumber = $context->attributes->get('pageNumber')->get(); |
|
161
|
|
|
if ($pageNumber !== 1) { |
|
162
|
|
|
$article = $pages[$pageNumber - 2]; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$content = $this->resolve($article); |
|
167
|
|
|
foreach ($content as $name => $value) { |
|
168
|
|
|
$visitor->addData($name, $value); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Resolve content on serialization. |
|
174
|
|
|
* |
|
175
|
|
|
* @param ObjectEvent $event |
|
176
|
|
|
*/ |
|
177
|
|
|
public function resolveContentForArticlePageOnPostSerialize(ObjectEvent $event) |
|
178
|
|
|
{ |
|
179
|
|
|
$article = $event->getObject(); |
|
180
|
|
|
$visitor = $event->getVisitor(); |
|
181
|
|
|
$context = $event->getContext(); |
|
182
|
|
|
|
|
183
|
|
|
if (!$article instanceof ArticlePageDocument || !$context->attributes->containsKey('website')) { |
|
184
|
|
|
return; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$content = $this->resolve($article); |
|
188
|
|
|
foreach ($content as $name => $value) { |
|
189
|
|
|
$visitor->addData($name, $value); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Returns content and view of article. |
|
195
|
|
|
* |
|
196
|
|
|
* @param ArticleInterface $article |
|
197
|
|
|
* |
|
198
|
|
|
* @return array |
|
199
|
|
|
*/ |
|
200
|
|
|
private function resolve(ArticleInterface $article) |
|
201
|
|
|
{ |
|
202
|
|
|
$structure = $this->structureManager->getStructure($article->getStructureType(), 'article'); |
|
203
|
|
|
$structure->setDocument($article); |
|
204
|
|
|
|
|
205
|
|
|
$data = $article->getStructure()->toArray(); |
|
206
|
|
|
|
|
207
|
|
|
return [ |
|
208
|
|
|
'content' => $this->contentProxyFactory->createContentProxy($structure, $data), |
|
209
|
|
|
'view' => $this->contentProxyFactory->createViewProxy($structure, $data), |
|
210
|
|
|
]; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|