This repository has been archived by the owner on Jan 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRelationProcessor.php
265 lines (250 loc) · 10.4 KB
/
RelationProcessor.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
<?php
/**
* File containing the RelationProcessor class
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/
namespace eZ\Publish\Core\Repository;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\SPI\Persistence\Handler;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\Core\Repository\Values\Content\Relation;
use eZ\Publish\Core\FieldType\Value as BaseValue;
use eZ\Publish\SPI\FieldType\FieldType as SPIFieldType;
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as SPIRelationCreateStruct;
/**
* RelationProcessor is an internal service used for handling field relations upon Content creation or update.
*
* @internal
*/
class RelationProcessor
{
/**
* @var \eZ\Publish\API\Repository\Repository
*/
protected $repository;
/**
* @var \eZ\Publish\SPI\Persistence\Handler
*/
protected $persistenceHandler;
/**
* Setups service with reference to repository object that created it & corresponding handler
*
* @param \eZ\Publish\API\Repository\Repository $repository
* @param \eZ\Publish\SPI\Persistence\Handler $handler
*/
public function __construct( RepositoryInterface $repository, Handler $handler )
{
$this->repository = $repository;
$this->persistenceHandler = $handler;
}
/**
* Returns field relations data for the current version of the given $contentInfo.
*
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
*
* @return mixed
*/
public function getFieldRelations( ContentInfo $contentInfo )
{
$relations = array();
$locationIdToContentIdMapping = array();
$content = $this->repository->getContentService()->loadContentByContentInfo( $contentInfo );
foreach ( $content->getFields() as $field )
{
$fieldDefinition = $content->contentType->getFieldDefinition( $field->fieldDefIdentifier );
$fieldType = $this->repository->getFieldTypeService()->buildFieldType( $fieldDefinition->fieldTypeIdentifier );
$this->appendFieldRelations(
$relations,
$locationIdToContentIdMapping,
$fieldType,
$fieldType->acceptValue( $field->value ),
$fieldDefinition->id
);
}
return $relations;
}
/**
* Appends destination Content ids of given $fieldValue to the $relation array.
*
* If $fieldValue contains Location ids, the will be converted to the Content id that Location encapsulates.
*
* @param array $relations
* @param array $locationIdToContentIdMapping An array with Location Ids as keys and corresponding Content Id as values
* @param \eZ\Publish\SPI\FieldType\FieldType $fieldType
* @param \eZ\Publish\Core\FieldType\Value $fieldValue Accepted field value.
* @param string $fieldDefinitionId
*
* @return void
*/
public function appendFieldRelations(
array &$relations,
array &$locationIdToContentIdMapping,
SPIFieldType $fieldType,
BaseValue $fieldValue,
$fieldDefinitionId
)
{
foreach ( $fieldType->getRelations( $fieldValue ) as $relationType => $destinationIds )
{
if ( $relationType === Relation::FIELD )
{
if ( !isset( $relations[$relationType][$fieldDefinitionId] ) )
{
$relations[$relationType][$fieldDefinitionId] = array();
}
$relations[$relationType][$fieldDefinitionId] += array_flip( $destinationIds );
}
// Using bitwise operators as Legacy Stack stores COMMON, LINK and EMBED relation types
// in the same entry using bitmask
else if ( $relationType & ( Relation::LINK | Relation::EMBED ) )
{
if ( !isset( $relations[$relationType] ) )
{
$relations[$relationType] = array();
}
if ( isset( $destinationIds["locationIds"] ) )
{
foreach ( $destinationIds["locationIds"] as $locationId )
{
if ( !isset( $locationIdToContentIdMapping[$locationId] ) )
{
$location = $this->repository->getLocationService()->loadLocation( $locationId );
$locationIdToContentIdMapping[$locationId] = $location->contentId;
}
$relations[$relationType][$locationIdToContentIdMapping[$locationId]] = true;
}
}
if ( isset( $destinationIds["contentIds"] ) )
{
$relations[$relationType] += array_flip( $destinationIds["contentIds"] );
}
}
}
}
/**
* Persists relation data for a content version.
*
* This method creates new relations and deletes removed relations.
*
* @param array $inputRelations
* @param mixed $sourceContentId
* @param mixed $sourceContentVersionNo
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
* @param \eZ\Publish\API\Repository\Values\Content\Relation[] $existingRelations An array of existing relations for Content version (empty when creating new content)
*
* @return void
*/
public function processFieldRelations(
array $inputRelations,
$sourceContentId,
$sourceContentVersionNo,
ContentType $contentType,
array $existingRelations = array()
)
{
// Map existing relations for easier handling
$mappedRelations = array();
foreach ( $existingRelations as $relation )
{
if ( $relation->type === Relation::FIELD )
{
$fieldDefinitionId = $contentType->getFieldDefinition( $relation->sourceFieldDefinitionIdentifier )->id;
$mappedRelations[$relation->type][$fieldDefinitionId][$relation->destinationContentInfo->id] = $relation;
}
// Using bitwise AND as Legacy Stack stores COMMON, LINK and EMBED relation types
// in the same entry using bitmask
if ( $relation->type & Relation::LINK )
{
$mappedRelations[Relation::LINK][$relation->destinationContentInfo->id] = $relation;
}
if ( $relation->type & Relation::EMBED )
{
$mappedRelations[Relation::EMBED][$relation->destinationContentInfo->id] = $relation;
}
}
// Add new relations
foreach ( $inputRelations as $relationType => $relationData )
{
if ( $relationType === Relation::FIELD )
{
foreach ( $relationData as $fieldDefinitionId => $contentIds )
{
foreach ( array_keys( $contentIds ) as $destinationContentId )
{
if ( isset( $mappedRelations[$relationType][$fieldDefinitionId][$destinationContentId] ) )
{
unset( $mappedRelations[$relationType][$fieldDefinitionId][$destinationContentId] );
}
else
{
$this->persistenceHandler->contentHandler()->addRelation(
new SPIRelationCreateStruct(
array(
"sourceContentId" => $sourceContentId,
"sourceContentVersionNo" => $sourceContentVersionNo,
"sourceFieldDefinitionId" => $fieldDefinitionId,
"destinationContentId" => $destinationContentId,
"type" => $relationType
)
)
);
}
}
}
}
else if ( $relationType === Relation::LINK || $relationType === Relation::EMBED )
{
foreach ( array_keys( $relationData ) as $destinationContentId )
{
if ( isset( $mappedRelations[$relationType][$destinationContentId] ) )
{
unset( $mappedRelations[$relationType][$destinationContentId] );
}
else
{
$this->persistenceHandler->contentHandler()->addRelation(
new SPIRelationCreateStruct(
array(
"sourceContentId" => $sourceContentId,
"sourceContentVersionNo" => $sourceContentVersionNo,
"sourceFieldDefinitionId" => null,
"destinationContentId" => $destinationContentId,
"type" => $relationType
)
)
);
}
}
}
}
// Remove relations not present in input set
foreach ( $mappedRelations as $relationType => $relationData )
{
foreach ( $relationData as $relationEntry )
{
switch ( $relationType )
{
case Relation::FIELD:
foreach ( $relationEntry as $relation )
{
$this->persistenceHandler->contentHandler()->removeRelation(
$relation->id,
$relationType
);
}
break;
case Relation::LINK:
case Relation::EMBED:
$this->persistenceHandler->contentHandler()->removeRelation(
$relationEntry->id,
$relationType
);
}
}
}
}
}