|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2020 Sourcefabric z.ú. 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 2020 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\AppleNews\Converter; |
|
18
|
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\Model\SlideshowItemInterface; |
|
20
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Byline; |
|
21
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Caption; |
|
22
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Gallery; |
|
23
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\GalleryItem; |
|
24
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Intro; |
|
25
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Photo; |
|
26
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Title; |
|
27
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\ArticleDocument; |
|
28
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\ComponentLayout; |
|
29
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\ComponentLayouts; |
|
30
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\ComponentTextStyle; |
|
31
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\ComponentTextStyles; |
|
32
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\InlineTextStyle; |
|
33
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\Layout; |
|
34
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\LinkedArticle; |
|
35
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\Margin; |
|
36
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\Metadata; |
|
37
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Document\TextStyle; |
|
38
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Serializer\AppleNewsFormatSerializer; |
|
39
|
|
|
use SWP\Bundle\CoreBundle\Factory\VersionFactory; |
|
40
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleInterface; |
|
41
|
|
|
use SWP\Bundle\CoreBundle\Model\TenantInterface; |
|
42
|
|
|
use SWP\Bundle\CoreBundle\Routing\TenantAwareAbsoluteUrlRouter; |
|
43
|
|
|
|
|
44
|
|
|
final class ArticleToAppleNewsFormatConverter |
|
45
|
|
|
{ |
|
46
|
|
|
private $serializer; |
|
47
|
|
|
|
|
48
|
|
|
private $router; |
|
49
|
|
|
|
|
50
|
|
|
private $versionFactory; |
|
51
|
|
|
|
|
52
|
|
|
private $articleBodyConverter; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct( |
|
55
|
|
|
VersionFactory $versionFactory, |
|
56
|
|
|
AppleNewsFormatSerializer $serializer, |
|
57
|
|
|
TenantAwareAbsoluteUrlRouter $router, |
|
58
|
|
|
ArticleBodyToComponentsConverter $articleBodyConverter |
|
59
|
|
|
) { |
|
60
|
|
|
$this->versionFactory = $versionFactory; |
|
61
|
|
|
$this->serializer = $serializer; |
|
62
|
|
|
$this->router = $router; |
|
63
|
|
|
$this->articleBodyConverter = $articleBodyConverter; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function convert(ArticleInterface $article, TenantInterface $tenant): string |
|
67
|
|
|
{ |
|
68
|
|
|
$version = $this->versionFactory->create(); |
|
69
|
|
|
|
|
70
|
|
|
$metadata = new Metadata(); |
|
71
|
|
|
$articleDocument = new ArticleDocument(); |
|
72
|
|
|
$articleDocument->setTitle($article->getTitle()); |
|
73
|
|
|
$articleDocument->setIdentifier((string) $article->getId()); |
|
74
|
|
|
$articleDocument->setLanguage($article->getLocale()); |
|
75
|
|
|
|
|
76
|
|
|
$articleDocument->addComponent(new Title($article->getTitle(), 'halfMarginBelowLayout')); |
|
77
|
|
|
if (null !== $article->getLead()) { |
|
78
|
|
|
$articleDocument->addComponent(new Intro($article->getLead(), 'halfMarginBelowLayout')); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$featureMedia = $article->getFeatureMedia(); |
|
82
|
|
|
|
|
83
|
|
|
if (null !== $featureMedia) { |
|
84
|
|
|
$featureMediaUrl = $this->router->generate( |
|
85
|
|
|
'swp_media_get', |
|
86
|
|
|
$tenant, |
|
87
|
|
|
[ |
|
88
|
|
|
'mediaId' => $featureMedia->getImage()->getAssetId(), |
|
89
|
|
|
'extension' => $featureMedia->getImage()->getFileExtension(), |
|
90
|
|
|
] |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$articleDocument->addComponent(new Photo($featureMediaUrl, (string) $featureMedia->getDescription())); |
|
94
|
|
|
|
|
95
|
|
|
$featureMediaCaptionText = $featureMedia->getDescription(); |
|
96
|
|
|
|
|
97
|
|
|
$imageCopyright = null; |
|
98
|
|
|
|
|
99
|
|
|
if (null !== ($byline = $featureMedia->getByLine())) { |
|
100
|
|
|
$imageCopyright = $byline; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$extra = $article->getExtra(); |
|
104
|
|
|
if (isset($extra['feature_media_credits']) && null !== $extra['feature_media_credits']) { |
|
105
|
|
|
$imageCopyright = $extra['feature_media_credits']; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (null !== $imageCopyright) { |
|
109
|
|
|
$featureMediaCaptionText .= ' (photo: '.$imageCopyright.')'; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$articleDocument->addComponent(new Caption($featureMediaCaptionText, 'marginBetweenComponents')); |
|
113
|
|
|
$metadata->setThumbnailURL($featureMediaUrl); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$articleDocument->addComponent($this->createBylineComponent($article)); |
|
117
|
|
|
$components = $this->articleBodyConverter->convert($article->getBody()); |
|
118
|
|
|
$components = $this->processGalleries($components, $article, $tenant); |
|
119
|
|
|
$links = $this->processRelatedArticles($article, $tenant); |
|
120
|
|
|
|
|
121
|
|
|
foreach ($components as $component) { |
|
122
|
|
|
$articleDocument->addComponent($component); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$articleDocument->setLayout(new Layout(20, 1024, 20, 60)); |
|
126
|
|
|
|
|
127
|
|
|
$componentTextStyles = $this->configureComponentTextStyles(); |
|
128
|
|
|
$articleDocument->setComponentTextStyles($componentTextStyles); |
|
129
|
|
|
|
|
130
|
|
|
$componentLayouts = $this->configureComponentLayouts(); |
|
131
|
|
|
$articleDocument->setComponentLayouts($componentLayouts); |
|
132
|
|
|
|
|
133
|
|
|
$metadata->setAuthors($article->getAuthorsNames()); |
|
134
|
|
|
|
|
135
|
|
|
$canonicalUrl = $this->router->generate( |
|
136
|
|
|
$article->getRoute()->getRouteName(), |
|
137
|
|
|
$tenant, |
|
138
|
|
|
[ |
|
139
|
|
|
'slug' => $article->getSlug(), |
|
140
|
|
|
] |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$metadata->setCanonicalUrl($canonicalUrl); |
|
144
|
|
|
$metadata->setDateCreated($article->getCreatedAt()); |
|
145
|
|
|
$metadata->setDatePublished($article->getPublishedAt()); |
|
146
|
|
|
|
|
147
|
|
|
$metadata->setExcerpt($article->getLead() ?? ''); |
|
148
|
|
|
|
|
149
|
|
|
$metadata->setGeneratorIdentifier('publisher'); |
|
150
|
|
|
$metadata->setGeneratorName('Publisher'); |
|
151
|
|
|
$metadata->setGeneratorVersion($version->getVersion()); |
|
152
|
|
|
|
|
153
|
|
|
$metadata->setKeywords($article->getKeywordsNames()); |
|
154
|
|
|
$metadata->setLinks($links); |
|
155
|
|
|
|
|
156
|
|
|
$articleDocument->setMetadata($metadata); |
|
157
|
|
|
|
|
158
|
|
|
return $this->serializer->serialize($articleDocument); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
private function processGalleries(array $components, ArticleInterface $article, TenantInterface $tenant): array |
|
162
|
|
|
{ |
|
163
|
|
|
if ($article->getSlideshows()->count() > 0) { |
|
164
|
|
|
foreach ($article->getSlideshows() as $slideshow) { |
|
165
|
|
|
$galleryComponent = new Gallery(); |
|
166
|
|
|
/** @var SlideshowItemInterface $slideshowItem */ |
|
167
|
|
|
foreach ($slideshow->getItems() as $slideshowItem) { |
|
168
|
|
|
$media = $slideshowItem->getArticleMedia(); |
|
169
|
|
|
$caption = $media->getDescription(); |
|
170
|
|
|
$url = $this->router->generate( |
|
171
|
|
|
'swp_media_get', |
|
172
|
|
|
$tenant, |
|
173
|
|
|
[ |
|
174
|
|
|
'mediaId' => $media->getImage()->getAssetId(), |
|
175
|
|
|
'extension' => $media->getImage()->getFileExtension(), |
|
176
|
|
|
] |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
$galleryItem = new GalleryItem($url, $caption); |
|
180
|
|
|
$galleryComponent->addItem($galleryItem); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$components[] = $galleryComponent; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $components; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
private function processRelatedArticles(ArticleInterface $article, TenantInterface $tenant): array |
|
191
|
|
|
{ |
|
192
|
|
|
$links = []; |
|
193
|
|
|
if ($article->getRelatedArticles()->count() > 0) { |
|
194
|
|
|
foreach ($article->getRelatedArticles() as $relatedArticle) { |
|
195
|
|
|
$relatedArticleRoute = $relatedArticle->getArticle()->getRoute(); |
|
196
|
|
|
|
|
197
|
|
|
$url = $this->router->generate( |
|
198
|
|
|
$relatedArticleRoute->getRouteName(), |
|
199
|
|
|
$tenant, |
|
200
|
|
|
[ |
|
201
|
|
|
'slug' => $relatedArticle->getArticle()->getSlug(), |
|
202
|
|
|
] |
|
203
|
|
|
); |
|
204
|
|
|
|
|
205
|
|
|
$linkedArticle = new LinkedArticle($url, LinkedArticle::RELATIONSHIP_RELATED); |
|
206
|
|
|
$links[] = $linkedArticle; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $links; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
private function configureComponentTextStyles(): ComponentTextStyles |
|
214
|
|
|
{ |
|
215
|
|
|
$linkStyle = new TextStyle('#8a0b1f'); |
|
216
|
|
|
$componentTextStyles = new ComponentTextStyles(); |
|
217
|
|
|
$componentTextStylesBody = new ComponentTextStyle(); |
|
218
|
|
|
$componentTextStylesBody->setBackgroundColor('#fff'); |
|
219
|
|
|
$componentTextStylesBody->setFontName('IowanOldStyle-Roman'); |
|
220
|
|
|
$componentTextStylesBody->setFontColor('#222222'); |
|
221
|
|
|
$componentTextStylesBody->setFontSize(16); |
|
222
|
|
|
$componentTextStylesBody->setLineHeight(22); |
|
223
|
|
|
$componentTextStylesBody->setLinkStyle($linkStyle); |
|
224
|
|
|
$componentTextStyles->setDefault($componentTextStylesBody); |
|
225
|
|
|
|
|
226
|
|
|
$componentTextStylesBody = new ComponentTextStyle(); |
|
227
|
|
|
$componentTextStylesBody->setFontName('IowanOldStyle-Roman'); |
|
228
|
|
|
$componentTextStyles->setDefaultBody($componentTextStylesBody); |
|
229
|
|
|
|
|
230
|
|
|
$componentTextStylesTitle = new ComponentTextStyle(); |
|
231
|
|
|
$componentTextStylesTitle->setFontName('BodoniSvtyTwoOSITCTT-Bold'); |
|
232
|
|
|
$componentTextStylesTitle->setFontSize(46); |
|
233
|
|
|
$componentTextStylesTitle->setLineHeight(58); |
|
234
|
|
|
$componentTextStylesTitle->setTextColor('#000000'); |
|
235
|
|
|
$componentTextStyles->setDefaultTitle($componentTextStylesTitle); |
|
236
|
|
|
|
|
237
|
|
|
$componentTextStylesIntro = new ComponentTextStyle(); |
|
238
|
|
|
$componentTextStylesIntro->setFontName('ArialMT'); |
|
239
|
|
|
$componentTextStylesIntro->setFontSize(22); |
|
240
|
|
|
$componentTextStylesIntro->setLineHeight(28); |
|
241
|
|
|
$componentTextStylesIntro->setTextColor('#A6AAA9'); |
|
242
|
|
|
$componentTextStyles->setDefaultIntro($componentTextStylesIntro); |
|
243
|
|
|
|
|
244
|
|
|
$componentTextStylesQuote = new ComponentTextStyle(); |
|
245
|
|
|
$componentTextStylesQuote->setFontName('IowanOldStyle-Italic'); |
|
246
|
|
|
$componentTextStylesQuote->setFontSize(30); |
|
247
|
|
|
$componentTextStylesQuote->setLineHeight(36); |
|
248
|
|
|
$componentTextStylesQuote->setTextColor('#A6AAA9'); |
|
249
|
|
|
$componentTextStyles->setDefaultQuote($componentTextStylesQuote); |
|
250
|
|
|
|
|
251
|
|
|
$componentTextStylesByline = new ComponentTextStyle(); |
|
252
|
|
|
$componentTextStylesByline->setFontName('DINAlternate-Bold'); |
|
253
|
|
|
$componentTextStylesByline->setFontSize(15); |
|
254
|
|
|
$componentTextStylesByline->setLineHeight(18); |
|
255
|
|
|
$componentTextStylesByline->setTextColor('#53585F'); |
|
256
|
|
|
$componentTextStyles->setDefaultByline($componentTextStylesByline); |
|
257
|
|
|
|
|
258
|
|
|
$componentTextStylesCaption = new ComponentTextStyle(); |
|
259
|
|
|
$componentTextStylesCaption->setFontName('ArialMT'); |
|
260
|
|
|
$componentTextStylesCaption->setFontSize(14); |
|
261
|
|
|
$componentTextStylesCaption->setLineHeight(16); |
|
262
|
|
|
$componentTextStylesCaption->setParagraphSpacingBefore(12); |
|
263
|
|
|
$componentTextStylesCaption->setParagraphSpacingAfter(12); |
|
264
|
|
|
$componentTextStylesCaption->setTextColor('#888888'); |
|
265
|
|
|
|
|
266
|
|
|
$componentTextStyles->setDefaultCaption($componentTextStylesCaption); |
|
267
|
|
|
|
|
268
|
|
|
return $componentTextStyles; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
private function configureComponentLayouts(): ComponentLayouts |
|
272
|
|
|
{ |
|
273
|
|
|
$componentLayouts = new ComponentLayouts(); |
|
274
|
|
|
$componentLayout = new ComponentLayout(); |
|
275
|
|
|
$componentLayout->setColumnStart(0); |
|
276
|
|
|
$componentLayout->setMargin(new Margin(12)); |
|
277
|
|
|
$componentLayouts->setHalfMarginBelowLayout($componentLayout); |
|
278
|
|
|
|
|
279
|
|
|
$componentLayout = new ComponentLayout(); |
|
280
|
|
|
$componentLayout->setColumnStart(0); |
|
281
|
|
|
$componentLayout->setMargin(new Margin(12, 12)); |
|
282
|
|
|
$componentLayouts->setMarginBetweenComponents($componentLayout); |
|
283
|
|
|
|
|
284
|
|
|
$componentLayout = new ComponentLayout(); |
|
285
|
|
|
$componentLayout->setColumnStart(0); |
|
286
|
|
|
$componentLayout->setMargin(new Margin(24)); |
|
287
|
|
|
$componentLayouts->setFullMarginBelowLayout($componentLayout); |
|
288
|
|
|
|
|
289
|
|
|
$componentLayout = new ComponentLayout(); |
|
290
|
|
|
$componentLayout->setColumnStart(0); |
|
291
|
|
|
$componentLayout->setColumnSpan(14); |
|
292
|
|
|
$componentLayout->setMargin(new Margin(12, 24)); |
|
293
|
|
|
$componentLayouts->setFullMarginAboveHalfBelowLayout($componentLayout); |
|
294
|
|
|
|
|
295
|
|
|
return $componentLayouts; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
private function createBylineComponent(ArticleInterface $article): Byline |
|
299
|
|
|
{ |
|
300
|
|
|
$routeName = $article->getRoute()->getName(); |
|
301
|
|
|
$authorNames = trim(implode(', ', $article->getAuthorsNames()), ', '); |
|
302
|
|
|
$publishedAt = $article->getPublishedAt(); |
|
303
|
|
|
$publishedAtString = $publishedAt->format('M d, Y'); |
|
304
|
|
|
|
|
305
|
|
|
$inlineTextStyle = new InlineTextStyle(); |
|
306
|
|
|
$inlineTextStyle->setRangeStart(3); |
|
307
|
|
|
$inlineTextStyle->setRangeLength(strlen($authorNames)); |
|
308
|
|
|
$inlineTextStyle->setTextStyle(new TextStyle('#b72025')); |
|
309
|
|
|
|
|
310
|
|
|
return new Byline( |
|
311
|
|
|
"By $authorNames | $routeName | $publishedAtString", |
|
312
|
|
|
'fullMarginBelowLayout', |
|
313
|
|
|
[$inlineTextStyle] |
|
314
|
|
|
); |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|