-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArticleUrlParserGenerator.php
More file actions
172 lines (143 loc) · 5.66 KB
/
ArticleUrlParserGenerator.php
File metadata and controls
172 lines (143 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
namespace Remp\MailerModule\Models\Generators;
use Nette\Application\UI\Form;
use Nette\Http\Url;
use Nette\Utils\ArrayHash;
use Remp\Mailer\Components\GeneratorWidgets\Widgets\ArticleUrlParserWidget\ArticleUrlParserWidget;
use Remp\MailerModule\Models\ContentGenerator\Engine\EngineFactory;
use Remp\MailerModule\Models\PageMeta\Content\ContentInterface;
use Remp\MailerModule\Models\PageMeta\Content\InvalidUrlException;
use Remp\MailerModule\Repositories\SourceTemplatesRepository;
use Tomaj\NetteApi\Params\PostInputParam;
class ArticleUrlParserGenerator implements IGenerator
{
public $onSubmit;
private array $contentProcessors = [];
public function __construct(
protected SourceTemplatesRepository $sourceTemplatesRepository,
protected ContentInterface $content,
protected readonly EngineFactory $engineFactory,
) {
}
public function setContentProcessor(string $domain, $contentProcessor)
{
$this->contentProcessors[$domain] = $contentProcessor;
}
public function generateForm(Form $form): void
{
$form->addTextArea('intro', 'Intro text')
->setHtmlAttribute('rows', 4)
->setHtmlAttribute('class', 'form-control wysiwyg-editor')
->getControlPrototype();
$form->addTextArea('articles', 'Articles')
->setHtmlAttribute('rows', 7)
->setHtmlAttribute('class', 'form-control')
->setOption('description', 'Paste article URLs. Each on separate line.')
->setRequired(true)
->getControlPrototype();
$form->addTextArea('external_intro', 'External intro text')
->setHtmlAttribute('rows', 4)
->setHtmlAttribute('class', 'form-control wysiwyg-editor')
->getControlPrototype();
$form->addTextArea('external_articles', 'External articles')
->setHtmlAttribute('rows', 7)
->setHtmlAttribute('class', 'form-control')
->setOption('description', 'Paste article URLs from external website supported by the generator. Each on separate line.')
->getControlPrototype();
$form->addTextArea('footer', 'Footer text')
->setHtmlAttribute('rows', 6)
->setHtmlAttribute('class', 'form-control wysiwyg-editor')
->getControlPrototype();
$form->onSuccess[] = [$this, 'formSucceeded'];
}
public function onSubmit(callable $onSubmit): void
{
$this->onSubmit = $onSubmit;
}
public function formSucceeded(Form $form, ArrayHash $values): void
{
try {
$output = $this->process((array)$values);
$addonParams = [
'render' => true,
'errors' => $output['errors'],
];
$this->onSubmit->__invoke($output['htmlContent'], $output['textContent'], $addonParams);
} catch (InvalidUrlException $e) {
$form->addError($e->getMessage());
}
}
public function apiParams(): array
{
return [
(new PostInputParam('articles'))->setRequired(),
(new PostInputParam('footer'))->setRequired(),
(new PostInputParam('utm_campaign'))->setRequired(),
(new PostInputParam('intro'))->setRequired(),
(new PostInputParam('external_intro')),
(new PostInputParam('external_articles')),
];
}
public function process(array $values): array
{
$sourceTemplate = $this->sourceTemplatesRepository->find($values['source_template_id']);
if (!$sourceTemplate) {
throw new \RuntimeException("Unable to find source template with ID [{$values['source_template_id']}]");
}
$errors = [];
$urls = explode("\n", trim($values['articles']));
$items = $this->readArticlesMetadata($urls, $errors);
$externalUrls = explode("\n", trim($values['external_articles']));
$externalItems = $this->readArticlesMetadata($externalUrls, $errors);
$params = [
'intro' => $values['intro'],
'external_intro' => $values['external_intro'],
'footer' => $values['footer'],
'items' => $items,
'external_items' => $externalItems,
];
$engine = $this->engineFactory->engine();
return [
'htmlContent' => $engine->render($sourceTemplate->content_html, $params),
'textContent' => strip_tags($engine->render($sourceTemplate->content_text, $params)),
'errors' => $errors,
];
}
public function getWidgets(): array
{
return [ArticleUrlParserWidget::class];
}
public function preprocessParameters($data): ?ArrayHash
{
return null;
}
private function readArticlesMetadata(array $urls, &$errors): array
{
$items = [];
foreach ($urls as $url) {
$url = trim($url);
if (empty($url)) {
// people sometimes enter blank lines
continue;
}
try {
$parsedUrl = new Url($url);
if (isset($this->contentProcessors[$parsedUrl->getDomain()])) {
$processor = $this->contentProcessors[$parsedUrl->getDomain()];
$meta = $processor->fetchUrlMeta($url);
} else {
$meta = $this->content->fetchUrlMeta($url);
}
if ($meta) {
$items[$url] = $meta;
} else {
$errors[] = $url;
}
} catch (InvalidUrlException $e) {
$errors[] = $url;
}
}
return $items;
}
}