-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Property.php
296 lines (266 loc) · 12.2 KB
/
Property.php
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
declare(strict_types=1);
namespace Neos\Neos\Ui\Domain\Model\Changes;
/*
* This file is part of the Neos.Neos.Ui package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\ContentRepository\Core\DimensionSpace\Exception\DimensionSpacePointNotFound;
use Neos\ContentRepository\Core\DimensionSpace\OriginDimensionSpacePoint;
use Neos\ContentRepository\Core\Feature\NodeDisabling\Command\DisableNodeAggregate;
use Neos\ContentRepository\Core\Feature\NodeDisabling\Command\EnableNodeAggregate;
use Neos\ContentRepository\Core\Feature\NodeModification\Command\SetNodeProperties;
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyValuesToWrite;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Command\SetNodeReferences;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Dto\NodeReferencesToWrite;
use Neos\ContentRepository\Core\Feature\NodeTypeChange\Command\ChangeNodeAggregateType;
use Neos\ContentRepository\Core\Feature\NodeTypeChange\Dto\NodeAggregateTypeChangeChildConstraintConflictResolutionStrategy;
use Neos\ContentRepository\Core\Feature\NodeVariation\Command\CreateNodeVariant;
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamDoesNotExistYet;
use Neos\ContentRepository\Core\SharedModel\Exception\NodeAggregatesTypeIsAmbiguous;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds;
use Neos\ContentRepository\Core\SharedModel\Node\NodeVariantSelectionStrategy;
use Neos\ContentRepository\Core\SharedModel\Node\ReferenceName;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Ui\Domain\Model\AbstractChange;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\ReloadContentOutOfBand;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\UpdateNodeInfo;
use Neos\Neos\Ui\Domain\Model\RenderedNodeDomAddress;
use Neos\Neos\Ui\Domain\Service\NodePropertyConversionService;
/** @codingStandardsIgnoreStart */
/** @codingStandardsIgnoreEnd */
/**
* Changes a property on a node
*/
class Property extends AbstractChange
{
/**
* @Flow\Inject
* @var NodePropertyConversionService
*/
protected $nodePropertyConversionService;
/**
* The node dom address
*/
protected ?RenderedNodeDomAddress $nodeDomAddress = null;
/**
* The name of the property to be changed
*/
protected ?string $propertyName = null;
/**
* The value, the property will be set to
* @var string|array<int|string,mixed>|null
*/
protected string|array|null $value = null;
/**
* The change has been initiated from the inline editing
*/
protected bool $isInline = false;
public function setPropertyName(string $propertyName): void
{
$this->propertyName = $propertyName;
}
public function getPropertyName(): ?string
{
return $this->propertyName;
}
public function setNodeDomAddress(RenderedNodeDomAddress $nodeDomAddress = null): void
{
$this->nodeDomAddress = $nodeDomAddress;
}
public function getNodeDomAddress(): ?RenderedNodeDomAddress
{
return $this->nodeDomAddress;
}
/**
* @param string|array<int|string,mixed>|null $value
*/
public function setValue(string|array|null $value): void
{
$this->value = $value;
}
/**
* @return string|array<int|string,mixed>|null
*/
public function getValue(): string|array|null
{
return $this->value;
}
public function setIsInline(bool $isInline): void
{
$this->isInline = $isInline;
}
public function getIsInline(): bool
{
return $this->isInline;
}
/**
* Checks whether this change can be applied to the subject
*/
public function canApply(): bool
{
if (is_null($this->subject)) {
return false;
}
$nodeType = $this->subject->nodeType;
$propertyName = $this->getPropertyName();
$nodeTypeProperties = $nodeType->getProperties();
return isset($nodeTypeProperties[$propertyName]);
}
/**
* Applies this change
*
* @throws \Neos\ContentRepository\Exception\NodeException
* @throws ContentStreamDoesNotExistYet
* @throws NodeAggregatesTypeIsAmbiguous
* @throws DimensionSpacePointNotFound
*/
public function apply(): void
{
$subject = $this->subject;
$propertyName = $this->getPropertyName();
if ($this->canApply() && !is_null($subject) && !is_null($propertyName)) {
$contentRepository = $this->contentRepositoryRegistry->get($subject->subgraphIdentity->contentRepositoryId);
$propertyType = $subject->nodeType->getPropertyType($propertyName);
// Use extra commands for reference handling
if ($propertyType === 'reference' || $propertyType === 'references') {
$value = $this->getValue();
$destinationNodeAggregateIds = [];
if ($propertyType === 'reference') {
if (is_string($value) && !empty($value)) {
$destinationNodeAggregateIds[] = $value;
}
}
if ($propertyType === 'references') {
/** @var array<int,string> $values */
$values = $value;
if (is_array($values)) {
foreach ($values as $singleNodeAggregateId) {
$destinationNodeAggregateIds[] = $singleNodeAggregateId;
}
}
}
$commandResult = $contentRepository->handle(
new SetNodeReferences(
$subject->subgraphIdentity->contentStreamId,
$subject->nodeAggregateId,
$subject->originDimensionSpacePoint,
ReferenceName::fromString($propertyName),
NodeReferencesToWrite::fromNodeAggregateIds(NodeAggregateIds::fromArray($destinationNodeAggregateIds))
)
);
} else {
$value = $this->nodePropertyConversionService->convert(
$subject->nodeType,
$propertyName,
$this->getValue()
);
if ($propertyName[0] !== '_' || $propertyName === '_hiddenInIndex') {
$originDimensionSpacePoint = $subject->originDimensionSpacePoint;
if (!$subject->subgraphIdentity->dimensionSpacePoint->equals($originDimensionSpacePoint)) {
$originDimensionSpacePoint = OriginDimensionSpacePoint::fromDimensionSpacePoint($subject->subgraphIdentity->dimensionSpacePoint);
// if origin dimension space point != current DSP -> translate transparently (matching old behavior)
$contentRepository->handle(
new CreateNodeVariant(
$subject->subgraphIdentity->contentStreamId,
$subject->nodeAggregateId,
$subject->originDimensionSpacePoint,
$originDimensionSpacePoint
)
)->block();
}
$commandResult = $contentRepository->handle(
new SetNodeProperties(
$subject->subgraphIdentity->contentStreamId,
$subject->nodeAggregateId,
$originDimensionSpacePoint,
PropertyValuesToWrite::fromArray(
[
$propertyName => $value
]
)
)
);
} else {
// property starts with "_"
if ($propertyName === '_nodeType') {
$commandResult = $contentRepository->handle(
new ChangeNodeAggregateType(
$subject->subgraphIdentity->contentStreamId,
$subject->nodeAggregateId,
NodeTypeName::fromString($value),
NodeAggregateTypeChangeChildConstraintConflictResolutionStrategy::STRATEGY_DELETE
)
);
} elseif ($propertyName === '_hidden') {
if ($value === true) {
$commandResult = $contentRepository->handle(
new DisableNodeAggregate(
$subject->subgraphIdentity->contentStreamId,
$subject->nodeAggregateId,
$subject->originDimensionSpacePoint->toDimensionSpacePoint(),
NodeVariantSelectionStrategy::STRATEGY_ALL_SPECIALIZATIONS
)
);
} else {
// unhide
$commandResult = $contentRepository->handle(
new EnableNodeAggregate(
$subject->subgraphIdentity->contentStreamId,
$subject->nodeAggregateId,
$subject->originDimensionSpacePoint->toDimensionSpacePoint(),
NodeVariantSelectionStrategy::STRATEGY_ALL_SPECIALIZATIONS
)
);
}
} else {
throw new \Exception("TODO FIX");
}
}
}
$commandResult->block();
// !!! REMEMBER: we are not allowed to use $node anymore,
// because it may have been modified by the commands above.
// Thus, we need to re-fetch it (as a workaround; until we do not need this anymore)
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($subject);
$originalNodeAggregateId = $subject->nodeAggregateId;
$node = $subgraph->findNodeById($originalNodeAggregateId);
if (is_null($node)) {
throw new \InvalidArgumentException(
'Cannot apply Property on missing node ' . $originalNodeAggregateId->value,
1645560836
);
}
$this->updateWorkspaceInfo();
$parentNode = $subgraph->findParentNode($node->nodeAggregateId);
$reloadIfChangedConfigurationPath = sprintf('properties.%s.ui.reloadIfChanged', $propertyName);
if (!$this->getIsInline() && $node->nodeType->getConfiguration($reloadIfChangedConfigurationPath)) {
if ($this->getNodeDomAddress() && $this->getNodeDomAddress()->getFusionPath()
&& $parentNode
&& $parentNode->nodeType->isOfType('Neos.Neos:ContentCollection')) {
$reloadContentOutOfBand = new ReloadContentOutOfBand();
$reloadContentOutOfBand->setNode($node);
$reloadContentOutOfBand->setNodeDomAddress($this->getNodeDomAddress());
$this->feedbackCollection->add($reloadContentOutOfBand);
} else {
$this->reloadDocument($node);
}
}
$reloadPageIfChangedConfigurationPath = sprintf('properties.%s.ui.reloadPageIfChanged', $propertyName);
if (!$this->getIsInline()
&& $node->nodeType->getConfiguration($reloadPageIfChangedConfigurationPath)) {
$this->reloadDocument($node);
}
// This might be needed to update node label and other things that we can calculate only on the server
$updateNodeInfo = new UpdateNodeInfo();
$updateNodeInfo->setNode($node);
$this->feedbackCollection->add($updateNodeInfo);
}
}
}