|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
|
8
|
|
|
use Netgen\InformationCollection\Core\Action\EmailAction; |
|
9
|
|
|
use function array_key_exists; |
|
10
|
|
|
use eZ\Publish\API\Repository\Values\Content\Field; |
|
11
|
|
|
use eZ\Publish\Core\FieldType\BinaryFile\Value as BinaryFile; |
|
12
|
|
|
use eZ\Publish\Core\Helper\FieldHelper; |
|
13
|
|
|
use eZ\Publish\Core\Helper\TranslationHelper; |
|
14
|
|
|
use Netgen\InformationCollection\API\Value\DataTransfer\EmailContent; |
|
15
|
|
|
use Netgen\InformationCollection\API\Constants; |
|
16
|
|
|
use Netgen\InformationCollection\API\ConfigurationConstants; |
|
17
|
|
|
use Netgen\InformationCollection\API\Exception\MissingEmailBlockException; |
|
18
|
|
|
use Netgen\InformationCollection\API\Exception\MissingValueException; |
|
19
|
|
|
use Netgen\InformationCollection\API\Value\DataTransfer\TemplateContent; |
|
20
|
|
|
use Netgen\InformationCollection\API\Value\Event\InformationCollected; |
|
21
|
|
|
use function trim; |
|
22
|
|
|
use Twig\Environment; |
|
23
|
|
|
|
|
24
|
|
|
class EmailDataFactory extends BaseEmailDataFactory |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $configResolver; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \eZ\Publish\Core\Helper\TranslationHelper |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $translationHelper; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \eZ\Publish\Core\Helper\FieldHelper |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $fieldHelper; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \Twig\Environment |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $twig; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* EmailDataFactory constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $config |
|
|
|
|
|
|
50
|
|
|
* @param \eZ\Publish\Core\Helper\TranslationHelper $translationHelper |
|
51
|
|
|
* @param \eZ\Publish\Core\Helper\FieldHelper $fieldHelper |
|
52
|
|
|
* @param \Twig\Environment $twig |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
ConfigResolverInterface $configResolver, |
|
56
|
|
|
TranslationHelper $translationHelper, |
|
57
|
|
|
FieldHelper $fieldHelper, |
|
58
|
|
|
Environment $twig |
|
59
|
|
|
) { |
|
60
|
|
|
$this->configResolver = $configResolver; |
|
|
|
|
|
|
61
|
|
|
$this->config = $this->configResolver->getParameter('action_config.' . EmailAction::$defaultName, 'netgen_information_collection'); |
|
|
|
|
|
|
62
|
|
|
$this->translationHelper = $translationHelper; |
|
63
|
|
|
$this->fieldHelper = $fieldHelper; |
|
64
|
|
|
$this->twig = $twig; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Factory method. |
|
69
|
|
|
* |
|
70
|
|
|
* @param InformationCollected $value |
|
71
|
|
|
* |
|
72
|
|
|
* @return EmailContent |
|
73
|
|
|
*/ |
|
74
|
|
|
public function build(InformationCollected $value): EmailContent |
|
75
|
|
|
{ |
|
76
|
|
|
$contentType = $value->getContentType(); |
|
77
|
|
|
|
|
78
|
|
|
$template = $this->resolveTemplate($contentType->identifier); |
|
79
|
|
|
|
|
80
|
|
|
$templateWrapper = $this->twig->load($template); |
|
81
|
|
|
$data = new TemplateContent($value, $templateWrapper); |
|
82
|
|
|
|
|
83
|
|
|
$body = $this->resolveBody($data); |
|
84
|
|
|
|
|
85
|
|
|
return new EmailContent( |
|
86
|
|
|
$this->resolveEmail($data, Constants::FIELD_RECIPIENT), |
|
87
|
|
|
$this->resolveEmail($data, Constants::FIELD_SENDER), |
|
88
|
|
|
$this->resolve($data, Constants::FIELD_SUBJECT), |
|
89
|
|
|
$body, |
|
90
|
|
|
$this->resolveAttachments($contentType->identifier, $value->getInformationCollectionStruct()->getFieldsData()) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns resolved parameter. |
|
96
|
|
|
* |
|
97
|
|
|
* @param TemplateContent $data |
|
98
|
|
|
* @param string $field |
|
99
|
|
|
* @param string $property |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function resolve(TemplateContent $data, $field, $property = Constants::FIELD_TYPE_TEXT) |
|
104
|
|
|
{ |
|
105
|
|
|
$rendered = ''; |
|
106
|
|
View Code Duplication |
if ($data->getTemplateWrapper()->hasBlock($field)) { |
|
|
|
|
|
|
107
|
|
|
$rendered = $data->getTemplateWrapper()->renderBlock( |
|
108
|
|
|
$field, |
|
109
|
|
|
[ |
|
110
|
|
|
'event' => $data->getEvent(), |
|
111
|
|
|
'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(), |
|
112
|
|
|
'content' => $data->getContent(), |
|
113
|
|
|
] |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$rendered = trim($rendered); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (!empty($rendered)) { |
|
120
|
|
|
return $rendered; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$content = $data->getContent(); |
|
124
|
|
View Code Duplication |
if (array_key_exists($field, $content->fields) && |
|
|
|
|
|
|
125
|
|
|
!$this->fieldHelper->isFieldEmpty($content, $field) |
|
126
|
|
|
) { |
|
127
|
|
|
$fieldValue = $this->translationHelper->getTranslatedField($content, $field); |
|
128
|
|
|
|
|
129
|
|
|
if ($fieldValue instanceof Field) { |
|
130
|
|
|
return $fieldValue->value->{$property}; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
View Code Duplication |
if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field])) { |
|
|
|
|
|
|
135
|
|
|
return $this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field]; |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
throw new MissingValueException($field); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns resolved email parameter. |
|
143
|
|
|
* |
|
144
|
|
|
* @param TemplateContent $data |
|
145
|
|
|
* @param string $field |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function resolveEmail(TemplateContent $data, $field) |
|
150
|
|
|
{ |
|
151
|
|
|
$rendered = ''; |
|
152
|
|
View Code Duplication |
if ($data->getTemplateWrapper()->hasBlock($field)) { |
|
|
|
|
|
|
153
|
|
|
$rendered = $data->getTemplateWrapper()->renderBlock( |
|
154
|
|
|
$field, |
|
155
|
|
|
[ |
|
156
|
|
|
'event' => $data->getEvent(), |
|
157
|
|
|
'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(), |
|
158
|
|
|
'content' => $data->getContent(), |
|
159
|
|
|
] |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
$rendered = trim($rendered); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
View Code Duplication |
if (!empty($rendered)) { |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
$emails = explode(',', $rendered); |
|
168
|
|
|
|
|
169
|
|
|
$emails = array_filter($emails, function($var) { |
|
170
|
|
|
return filter_var($var, FILTER_VALIDATE_EMAIL); |
|
171
|
|
|
}); |
|
172
|
|
|
|
|
173
|
|
|
if (!empty($emails)) { |
|
174
|
|
|
return $emails; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$content = $data->getContent(); |
|
179
|
|
View Code Duplication |
if (array_key_exists($field, $content->fields) && |
|
|
|
|
|
|
180
|
|
|
!$this->fieldHelper->isFieldEmpty($content, $field) |
|
181
|
|
|
) { |
|
182
|
|
|
$fieldValue = $this->translationHelper->getTranslatedField($content, $field); |
|
183
|
|
|
|
|
184
|
|
|
if ($fieldValue instanceof Field) { |
|
185
|
|
|
return [$fieldValue->value->email]; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
View Code Duplication |
if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field])) { |
|
|
|
|
|
|
191
|
|
|
return [$this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field]]; |
|
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
throw new MissingValueException($field); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns resolved template name. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $contentTypeIdentifier |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function resolveTemplate($contentTypeIdentifier) |
|
205
|
|
|
{ |
|
206
|
|
|
if (array_key_exists($contentTypeIdentifier, $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::CONTENT_TYPES])) { |
|
|
|
|
|
|
207
|
|
|
return $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::CONTENT_TYPES][$contentTypeIdentifier]; |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::SETTINGS_DEFAULT]; |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Renders email template. |
|
215
|
|
|
* |
|
216
|
|
|
* @param TemplateContent $data |
|
217
|
|
|
* |
|
218
|
|
|
* @throws MissingEmailBlockException |
|
219
|
|
|
* |
|
220
|
|
|
* @return string |
|
221
|
|
|
*/ |
|
222
|
|
|
protected function resolveBody(TemplateContent $data) |
|
223
|
|
|
{ |
|
224
|
|
|
if ($data->getTemplateWrapper()->hasBlock(Constants::BLOCK_EMAIL)) { |
|
225
|
|
|
return $data->getTemplateWrapper() |
|
226
|
|
|
->renderBlock( |
|
227
|
|
|
Constants::BLOCK_EMAIL, |
|
228
|
|
|
[ |
|
229
|
|
|
'event' => $data->getEvent(), |
|
230
|
|
|
'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getFieldsData(), |
|
231
|
|
|
'content' => $data->getContent(), |
|
232
|
|
|
'default_variables' => !empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES]) |
|
|
|
|
|
|
233
|
|
|
? $this->config[ConfigurationConstants::DEFAULT_VARIABLES] : null, |
|
|
|
|
|
|
234
|
|
|
] |
|
235
|
|
|
); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
throw new MissingEmailBlockException( |
|
239
|
|
|
$data->getTemplateWrapper()->getSourceContext()->getName(), |
|
240
|
|
|
$data->getTemplateWrapper()->getBlockNames() |
|
241
|
|
|
); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param string $contentTypeIdentifier |
|
246
|
|
|
* @param array $collectedFields |
|
247
|
|
|
* |
|
248
|
|
|
* @return BinaryFile[] |
|
249
|
|
|
*/ |
|
250
|
|
|
protected function resolveAttachments(string $contentTypeIdentifier, array $collectedFields) |
|
251
|
|
|
{ |
|
252
|
|
|
if (empty($this->config[ConfigurationConstants::ATTACHMENTS])) { |
|
|
|
|
|
|
253
|
|
|
return []; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if (array_key_exists($contentTypeIdentifier, $this->config[ConfigurationConstants::ATTACHMENTS][ConfigurationConstants::CONTENT_TYPES])) { |
|
|
|
|
|
|
257
|
|
|
$send = $this->config[ConfigurationConstants::ATTACHMENTS][ConfigurationConstants::CONTENT_TYPES][$contentTypeIdentifier]; |
|
|
|
|
|
|
258
|
|
|
} else { |
|
259
|
|
|
$send = $this->config[ConfigurationConstants::ATTACHMENTS][ConfigurationConstants::SETTINGS_DEFAULT]; |
|
|
|
|
|
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
if (!$send) { |
|
263
|
|
|
return []; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return $this->getBinaryFileFields($collectedFields); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param array $collectedFields |
|
271
|
|
|
* |
|
272
|
|
|
* @return BinaryFile[] |
|
273
|
|
|
*/ |
|
274
|
|
|
protected function getBinaryFileFields(array $collectedFields) |
|
275
|
|
|
{ |
|
276
|
|
|
$filtered = []; |
|
277
|
|
|
foreach ($collectedFields as $identifier => $value) { |
|
278
|
|
|
if ($value instanceof BinaryFile) { |
|
279
|
|
|
$filtered[] = $value; |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return empty($filtered) ? [] : $filtered; |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.