-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathBlockDocumentsBaseContentFields.php
More file actions
246 lines (224 loc) · 8.17 KB
/
BlockDocumentsBaseContentFields.php
File metadata and controls
246 lines (224 loc) · 8.17 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
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
<?php
/**
* This file is part of the eZ Platform Solr Search Engine package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformSolrSearchEngine\FieldMapper\ContentFieldMapper;
use EzSystems\EzPlatformSolrSearchEngine\FieldMapper\ContentFieldMapper;
use eZ\Publish\SPI\Persistence\Content;
use eZ\Publish\SPI\Persistence\Content\Location\Handler as LocationHandler;
use eZ\Publish\SPI\Persistence\Content\ObjectState\Handler as ObjectStateHandler;
use eZ\Publish\SPI\Persistence\Content\Section\Handler as SectionHandler;
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler;
use eZ\Publish\SPI\Search\Field;
use eZ\Publish\SPI\Search\FieldType;
/**
* Maps base Content related fields to block document (Content and Location).
*/
class BlockDocumentsBaseContentFields extends ContentFieldMapper
{
/**
* @var \eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
protected $locationHandler;
/**
* @var \eZ\Publish\SPI\Persistence\Content\Type\Handler
*/
protected $contentTypeHandler;
/**
* @var \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler
*/
protected $objectStateHandler;
/**
* @var \eZ\Publish\SPI\Persistence\Content\Section\Handler
*/
protected $sectionHandler;
/**
* @var string
*/
private $invalidCharactersPattern;
/**
* @param \eZ\Publish\SPI\Persistence\Content\Location\Handler $locationHandler
* @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler
* @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler $objectStateHandler
* @param \eZ\Publish\SPI\Persistence\Content\Section\Handler $sectionHandler
* @param string $invalidCharactersPattern
*/
public function __construct(
LocationHandler $locationHandler,
ContentTypeHandler $contentTypeHandler,
ObjectStateHandler $objectStateHandler,
SectionHandler $sectionHandler,
$invalidCharactersPattern
) {
$this->locationHandler = $locationHandler;
$this->contentTypeHandler = $contentTypeHandler;
$this->objectStateHandler = $objectStateHandler;
$this->sectionHandler = $sectionHandler;
$this->invalidCharactersPattern = $invalidCharactersPattern;
}
public function accept(Content $content)
{
return true;
}
public function mapFields(Content $content)
{
$versionInfo = $content->versionInfo;
$contentInfo = $content->versionInfo->contentInfo;
// UserGroups and Users are Content, but permissions cascade is achieved through
// Locations hierarchy. We index all ancestor Location Content ids of all
// Locations of an owner.
$ancestorLocationsContentIds = $this->getAncestorLocationsContentIds(
$contentInfo->ownerId
);
// Add owner user id as it can also be considered as user group.
$ancestorLocationsContentIds[] = $contentInfo->ownerId;
$section = $this->sectionHandler->load($contentInfo->sectionId);
return [
new Field(
'content_id',
$contentInfo->id,
new FieldType\IdentifierField()
),
new Field(
'content_type_id',
$contentInfo->contentTypeId,
new FieldType\IdentifierField()
),
new Field(
'content_version_no',
$versionInfo->versionNo,
new FieldType\IntegerField()
),
new Field(
'content_version_status',
$versionInfo->status,
new FieldType\IdentifierField()
),
new Field(
'content_name',
preg_replace($this->invalidCharactersPattern, '', $contentInfo->name),
new FieldType\StringField()
),
new Field(
'content_version_creator_user_id',
$versionInfo->creatorId,
new FieldType\IdentifierField()
),
new Field(
'content_owner_user_id',
$contentInfo->ownerId,
new FieldType\IdentifierField()
),
new Field(
'content_section_id',
$contentInfo->sectionId,
new FieldType\IdentifierField()
),
new Field(
'content_remote_id',
$contentInfo->remoteId,
new FieldType\IdentifierField()
),
new Field(
'content_modification_date',
$contentInfo->modificationDate,
new FieldType\DateField()
),
new Field(
'content_publication_date',
$contentInfo->publicationDate,
new FieldType\DateField()
),
new Field(
'content_language_codes',
array_keys($versionInfo->names),
new FieldType\MultipleStringField()
),
new Field(
'content_main_language_code',
$contentInfo->mainLanguageCode,
new FieldType\StringField()
),
new Field(
'content_always_available',
$contentInfo->alwaysAvailable,
new FieldType\BooleanField()
),
new Field(
'content_owner_user_group_ids',
$ancestorLocationsContentIds,
new FieldType\MultipleIdentifierField()
),
new Field(
'content_section_identifier',
$section->identifier,
new FieldType\IdentifierField()
),
new Field(
'content_section_name',
$section->name,
new FieldType\StringField()
),
new Field(
'content_type_group_ids',
$this->contentTypeHandler->load($contentInfo->contentTypeId)->groupIds,
new FieldType\MultipleIdentifierField()
),
new Field(
'content_object_state_ids',
$this->getObjectStateIds($contentInfo->id),
new FieldType\MultipleIdentifierField()
),
];
}
/**
* Returns an array of object state ids of a Content with given $contentId.
*
* @param int|string $contentId
*
* @return array
*/
protected function getObjectStateIds($contentId)
{
$objectStateIds = array();
foreach ($this->objectStateHandler->loadAllGroups() as $objectStateGroup) {
$objectStateIds[] = $this->objectStateHandler->getContentState(
$contentId,
$objectStateGroup->id
)->id;
}
return $objectStateIds;
}
/**
* Returns Content ids of all ancestor Locations of all Locations
* of a Content with given $contentId.
*
* Used to determine user groups of a user with $contentId.
*
* @param int|string $contentId
*
* @return array
*/
protected function getAncestorLocationsContentIds($contentId)
{
$locations = $this->locationHandler->loadLocationsByContent($contentId);
$ancestorLocationContentIds = array();
$ancestorLocationIds = array();
foreach ($locations as $location) {
$locationIds = explode('/', trim($location->pathString, '/'));
// Remove Location of Content with $contentId
array_pop($locationIds);
// Remove Root Location id (id==1 in legacy DB)
array_shift($locationIds);
$ancestorLocationIds = array_merge($ancestorLocationIds, $locationIds);
}
foreach (array_unique($ancestorLocationIds) as $locationId) {
$location = $this->locationHandler->load($locationId);
$ancestorLocationContentIds[$location->contentId] = true;
}
return array_keys($ancestorLocationContentIds);
}
}