1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Transfer. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE file located |
7
|
|
|
* in the root directory. |
8
|
|
|
*/ |
9
|
|
|
namespace Transfer\EzPlatform\Repository\Manager\Sub; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\ContentTypeService; |
12
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
13
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentType; |
14
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup; |
15
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroupCreateStruct; |
16
|
|
|
use Psr\Log\LoggerAwareInterface; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Transfer\EzPlatform\Repository\Values\ContentTypeObject; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Content type manager. |
22
|
|
|
* |
23
|
|
|
* @internal |
24
|
|
|
* |
25
|
|
|
* @author Harald Tollefsen <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ContentTypeGroupSubManager implements LoggerAwareInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ContentTypeService Content type service |
31
|
|
|
*/ |
32
|
|
|
private $contentTypeService; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var LoggerInterface |
36
|
|
|
*/ |
37
|
|
|
private $logger; |
38
|
|
|
/** |
39
|
|
|
* FieldDefinitionSubManager constructor. |
40
|
|
|
* |
41
|
|
|
* @param ContentTypeService $contentTypeService |
42
|
|
|
*/ |
43
|
5 |
|
public function __construct(ContentTypeService $contentTypeService) |
44
|
|
|
{ |
45
|
5 |
|
$this->contentTypeService = $contentTypeService; |
46
|
5 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param LoggerInterface $logger |
50
|
|
|
*/ |
51
|
5 |
|
public function setLogger(LoggerInterface $logger) |
52
|
|
|
{ |
53
|
5 |
|
$this->logger = $logger; |
54
|
5 |
|
} |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* @param array $identifiers |
58
|
|
|
* |
59
|
|
|
* @return ContentTypeGroup[] |
60
|
|
|
*/ |
61
|
37 |
|
public function loadContentTypeGroupsByIdentifiers(array $identifiers) |
62
|
|
|
{ |
63
|
37 |
|
$contentTypeGroups = array_map( |
64
|
|
|
function ($identifier) { |
65
|
|
|
try { |
66
|
6 |
|
return $this->contentTypeService->loadContentTypeGroupByIdentifier($identifier); |
67
|
1 |
|
} catch (NotFoundException $notFoundException) { |
68
|
1 |
|
return $this->createContentTypeGroupByIdentifier($identifier); |
69
|
|
|
} |
70
|
37 |
|
}, |
71
|
|
|
$identifiers |
72
|
37 |
|
); |
73
|
|
|
|
74
|
37 |
|
return $contentTypeGroups; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $contentTypeGroupIdentifier |
79
|
|
|
* |
80
|
|
|
* @return ContentTypeGroup |
81
|
|
|
*/ |
82
|
1 |
|
private function createContentTypeGroupByIdentifier($contentTypeGroupIdentifier) |
83
|
|
|
{ |
84
|
1 |
|
$contentTypeGroupCreateStruct = new ContentTypeGroupCreateStruct(); |
85
|
1 |
|
$contentTypeGroupCreateStruct->identifier = $contentTypeGroupIdentifier; |
86
|
|
|
|
87
|
1 |
|
return $this->contentTypeService->createContentTypeGroup($contentTypeGroupCreateStruct); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ContentTypeObject $object |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
* |
95
|
|
|
* @throws NotFoundException |
96
|
|
|
* @throws \Exception |
97
|
|
|
*/ |
98
|
37 |
|
public function updateContentTypeGroupsAssignment(ContentTypeObject $object) |
99
|
|
|
{ |
100
|
|
|
// Load contenttype |
101
|
37 |
|
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($object->data['identifier']); |
102
|
|
|
|
103
|
|
|
// Get identifiers of current contenttypegroups |
104
|
37 |
|
$currentContentTypeGroupIdentifiers = array_map( |
105
|
37 |
|
function (ContentTypeGroup $contentTypeGroup) { |
106
|
37 |
|
return $contentTypeGroup->identifier; |
107
|
37 |
|
}, |
108
|
37 |
|
$contentType->getContentTypeGroups() |
109
|
37 |
|
); |
110
|
|
|
|
111
|
|
|
// Get new contenttypegroup identifiers |
112
|
37 |
|
$newContentTypeGroupIdentifiers = $object->data['contenttype_groups']; |
113
|
|
|
|
114
|
|
|
// Compare identifiers to identify which once to add/remove/keep |
115
|
37 |
|
$remove = array_diff($currentContentTypeGroupIdentifiers, $newContentTypeGroupIdentifiers); |
116
|
37 |
|
$add = array_diff($newContentTypeGroupIdentifiers, $currentContentTypeGroupIdentifiers); |
117
|
|
|
|
118
|
37 |
|
$this->attachContentTypeGroupsByIdentifiers($contentType, $add); |
119
|
37 |
|
$this->detachContentTypeGroupsByIdentifiers($contentType, $remove); |
120
|
|
|
|
121
|
37 |
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Load (and create if not exists) new contenttype groups, and assign them to a contenttype. |
126
|
|
|
* |
127
|
|
|
* @param ContentType $contentType |
128
|
|
|
* @param array $contentTypeGroupsIdentifiers |
129
|
|
|
* |
130
|
|
|
* @throws NotFoundException |
131
|
|
|
*/ |
132
|
37 |
|
private function attachContentTypeGroupsByIdentifiers(ContentType $contentType, array $contentTypeGroupsIdentifiers) |
133
|
|
|
{ |
134
|
37 |
|
$contentTypeGroups = $this->loadContentTypeGroupsByIdentifiers($contentTypeGroupsIdentifiers); |
135
|
37 |
|
foreach ($contentTypeGroups as $contentTypeGroup) { |
136
|
1 |
|
$this->contentTypeService->assignContentTypeGroup($contentType, $contentTypeGroup); |
137
|
37 |
|
} |
138
|
37 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Load contenttype groups, and unassign them from a contenttype. |
142
|
|
|
* |
143
|
|
|
* @param ContentType $contentType |
144
|
|
|
* @param array $contentTypeGroupsIdentifiers |
145
|
|
|
* |
146
|
|
|
* @throws NotFoundException |
147
|
|
|
*/ |
148
|
37 |
|
private function detachContentTypeGroupsByIdentifiers(ContentType $contentType, array $contentTypeGroupsIdentifiers) |
149
|
|
|
{ |
150
|
37 |
|
$contentTypeGroups = $this->loadContentTypeGroupsByIdentifiers($contentTypeGroupsIdentifiers); |
151
|
37 |
|
foreach ($contentTypeGroups as $contentTypeGroup) { |
152
|
1 |
|
$this->contentTypeService->unassignContentTypeGroup($contentType, $contentTypeGroup); |
153
|
37 |
|
} |
154
|
37 |
|
} |
155
|
|
|
} |
156
|
|
|
|