|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Netgen\Bundle\EzFormsBundle\Form\DataMapper; |
|
6
|
|
|
|
|
7
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataMapper; |
|
8
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Symfony\Component\Form\FormInterface; |
|
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyPathInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A data mapper using property paths to read/write data. |
|
15
|
|
|
*/ |
|
16
|
|
|
final class UpdateUserMapper extends DataMapper |
|
17
|
|
|
{ |
|
18
|
|
|
protected function mapToForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var \eZ\Publish\API\Repository\Values\User\UserUpdateStruct $userUpdateStruct */ |
|
21
|
|
|
$userUpdateStruct = $data->payload; |
|
22
|
|
|
/** @var \eZ\Publish\API\Repository\Values\User\User $user */ |
|
23
|
|
|
$user = $data->target; |
|
24
|
|
|
/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */ |
|
25
|
|
|
$contentType = $data->definition; |
|
26
|
|
|
|
|
27
|
|
|
$fieldDefinitionIdentifier = (string) $propertyPath; |
|
28
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
|
29
|
|
|
|
|
30
|
|
|
if (null === $fieldDefinition) { |
|
31
|
|
|
throw new RuntimeException( |
|
32
|
|
|
"Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
|
37
|
|
|
|
|
38
|
|
|
// For user we can update only email and password. |
|
39
|
|
|
// Only email is set as it doesn't make sense to set the password to the form. |
|
40
|
|
|
if ($fieldTypeIdentifier === 'ezuser') { |
|
41
|
|
|
$form->setData( |
|
42
|
|
|
[ |
|
43
|
|
|
'email' => $user->email, |
|
44
|
|
|
] |
|
45
|
|
|
); |
|
46
|
|
|
} else { |
|
47
|
|
|
$handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
|
48
|
|
|
$form->setData( |
|
49
|
|
|
$handler->convertFieldValueToForm( |
|
50
|
|
|
$user->getFieldValue( |
|
|
|
|
|
|
51
|
|
|
$fieldDefinitionIdentifier, |
|
52
|
|
|
$userUpdateStruct->contentUpdateStruct->initialLanguageCode |
|
53
|
|
|
), |
|
54
|
|
|
$fieldDefinition |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function mapFromForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void |
|
61
|
|
|
{ |
|
62
|
|
|
/** @var \eZ\Publish\API\Repository\Values\User\UserUpdateStruct $userUpdateStruct */ |
|
63
|
|
|
$userUpdateStruct = $data->payload; |
|
64
|
|
|
/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */ |
|
65
|
|
|
$contentType = $data->definition; |
|
66
|
|
|
|
|
67
|
|
|
$fieldDefinitionIdentifier = (string) $propertyPath; |
|
68
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
|
69
|
|
|
|
|
70
|
|
|
if (null === $fieldDefinition) { |
|
71
|
|
|
throw new RuntimeException( |
|
72
|
|
|
"Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
|
77
|
|
|
|
|
78
|
|
|
$handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
|
79
|
|
|
$formData = $form->getData(); |
|
80
|
|
|
|
|
81
|
|
|
// For ezuser we need to map form data to the properties in the UserUpdateStruct |
|
82
|
|
|
if ($fieldTypeIdentifier === 'ezuser') { |
|
83
|
|
|
$convertedData = $handler->convertFieldValueFromForm($formData); |
|
84
|
|
|
|
|
85
|
|
|
$userUpdateStruct->email = $convertedData['email']; |
|
86
|
|
|
$userUpdateStruct->password = $convertedData['password']; |
|
87
|
|
|
|
|
88
|
|
|
// Creating users through Content context is not allowed, |
|
89
|
|
|
// so we map dummy data to make it non-empty |
|
90
|
|
|
// This will be ignored during actual user update |
|
91
|
|
|
// @todo this should be improved on eZ side |
|
92
|
|
|
$userUpdateStruct->contentUpdateStruct->setField( |
|
93
|
|
|
$fieldDefinitionIdentifier, |
|
94
|
|
|
[ |
|
95
|
|
|
'login' => 'dummy', |
|
96
|
|
|
] |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
// Else set field to struct, but only if it is not empty or it has not been marked |
|
100
|
|
|
// to skip update if empty |
|
101
|
|
|
elseif (!$this->shouldSkipForEmptyUpdate($form, $formData, $fieldDefinitionIdentifier)) { |
|
102
|
|
|
$convertedData = $handler->convertFieldValueFromForm($formData); |
|
103
|
|
|
$userUpdateStruct->contentUpdateStruct->setField($fieldDefinitionIdentifier, $convertedData); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: