-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeService.php
257 lines (219 loc) · 8.29 KB
/
NodeService.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
<?php
declare(strict_types=1);
namespace Garagist\Mautic\Service;
use Neos\ContentRepository\Domain\Factory\NodeFactory;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Repository\NodeDataRepository;
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
use Neos\ContentRepository\Domain\Service\Context;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\ContentRepository\Domain\Utility\NodePaths;
use Neos\ContentRepository\Security\Authorization\Privilege\Node\NodePrivilegeSubject;
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Neos\Domain\Repository\DomainRepository;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\Domain\Service\ContentDimensionPresetSourceInterface;
use Neos\Neos\Domain\Service\SiteService;
use Neos\Neos\Security\Authorization\Privilege\NodeTreePrivilege;
use Neos\ContentRepository\Domain\Projection\Content\TraversableNodeInterface;
/**
* A service for retrieving nodes from NeosCR
* @api
*/
#[Flow\Scope('singleton')]
class NodeService
{
#[Flow\Inject]
protected ContentDimensionPresetSourceInterface $contentDimensionPresetSource;
#[Flow\Inject]
protected DomainRepository $domainRepository;
#[Flow\Inject]
protected SiteRepository $siteRepository;
#[Flow\Inject]
protected ContextFactoryInterface $contextFactory;
#[Flow\Inject]
protected PrivilegeManagerInterface $privilegeManager;
#[Flow\Inject]
protected WorkspaceRepository $workspaceRepository;
#[Flow\Inject]
protected NodeDataRepository $nodeDataRepository;
#[Flow\Inject]
protected NodeFactory $nodeFactory;
/**
* @var array
*/
protected array $options = [];
/**
* Get the site node from a node
*
* @param NodeInterface $node
* @return NodeInterface
*/
public function getSiteNodeFromNode(NodeInterface $node): NodeInterface
{
if ($siteNode = $node->getContext()->getCurrentSiteNode()) {
return $siteNode;
}
$flowQuery = new FlowQuery([$node]);
$nodes = $flowQuery->parents('[instanceof Neos.Neos:Document]')->get();
return end($nodes);
}
/**
* Get site name based on node
*
* @param NodeInterface $node
* @return string|null
*/
public function getSiteNameBasedOnNode(NodeInterface $node): ?string
{
if ($site = $node->getContext()->getCurrentSite()) {
return $site->getName();
}
$name = null;
try {
$nodePath = $node->findNodePath();
$nodePathSegments = $nodePath->getParts();
// Seems hacky, but we need to get the site by the node name here
// and extract the site name by the node's path
if (count($nodePathSegments) >= 3) {
$siteName = $nodePathSegments[2];
$site = $this->siteRepository->findOneByNodeName($siteName->__toString());
if ($site && $site->isOnline()) {
$name = $site->getName();
}
}
} catch (\Exception $th) {
}
return $name;
}
/**
* Get all sites nodes
*
* @return NodeInterface[]
*/
public function getSiteNodes() {
$siteNodes = [];
$liveWorkspace = $this->workspaceRepository->findByIdentifier('live');
foreach ($this->siteRepository->findOnline() as $site) {
$siteNodePath = NodePaths::addNodePathSegment(SiteService::SITES_ROOT_PATH, $site->getNodeName());
$siteNodesInAllDimensions = $this->nodeDataRepository->findByPathWithoutReduce($siteNodePath, $liveWorkspace);
foreach ($siteNodesInAllDimensions as $siteNodeData) {
$siteNodeContext = $this->nodeFactory->createContextMatchingNodeData($siteNodeData);
$siteNode = $this->nodeFactory->createFromNodeData($siteNodeData, $siteNodeContext);
// if the node exists, check if the user is granted access to this node
if ($this->privilegeManager->isGranted(NodeTreePrivilege::class, new NodePrivilegeSubject($siteNode))) {
$siteNodes[] = $siteNode;
break;
}
}
}
return $siteNodes;
}
/**
* Get node by id
*
* @param string $identifier
* @return NodeInterface
*/
public function getNodeById(string $identifier): NodeInterface
{
$context = $this->getContentContextDimension();
return $context->getNodeByIdentifier($identifier);
}
/**
* Get nodes by type
*
* @param string $nodeTypeName The name of the node type
* @param Context|null $context
*
* @return array
* @throws Exception
*/
public function getNodesByType(string $nodeTypeName, ?Context $context = null): array
{
if (!isset($context)) {
$context = $this->getContentContextDimension();
}
$flowQuery = new FlowQuery(array($context->getCurrentSiteNode()));
return $flowQuery->context([
'invisibleContentShown' => false
])->find('[instanceof ' . $nodeTypeName . ']')->get();
}
/**
* Get parent from a node with a given nodeTypeName
*
* @param NodeInterface $node
* @param string $nodeTypeName
*
* @return NodeInterface|null
*/
public function getParentByType(NodeInterface $node, string $nodeTypeName): ?NodeInterface
{
$flowQuery = new FlowQuery(array($node));
return $flowQuery->parent()->closest('[instanceof ' . $nodeTypeName . ']')->get(0);
}
/**
* Create Dimension Context
*
* @return Context
*/
public function getContentContextDimension(): Context
{
$dimensionName = $this->options['dimensionName'] ?? 'language';
$workspaceName = $this->options['workspaceName'] ?? 'live';
if (!isset($this->options['dimensionValues']) || empty($this->options['dimensionValues'])) {
$defaultPreset = $this->contentDimensionPresetSource->getDefaultPreset($dimensionName);
if (!isset($defaultPreset['values'])) {
return $this->createContentContext([], $workspaceName);
}
$dimensionValues = $defaultPreset['values'];
}
$dimensionArray[$dimensionName] = $dimensionValues;
return $this->createContentContext($dimensionArray, $workspaceName);
}
/**
* Create a ContentContext based on the given workspace name
*
* @param array $dimensions Optional list of dimensions and their values which should be set
* @param string $workspaceName Optional Name of the workspace to set for the context
*
* @return Context
*/
protected function createContentContext(array $dimensions = [], string $workspaceName = 'live'): Context
{
$contextProperties = array(
'workspaceName' => $workspaceName,
'invisibleContentShown' => true,
'inaccessibleContentShown' => true
);
if ($dimensions !== array()) {
$contextProperties['dimensions'] = $dimensions;
$contextProperties['targetDimensions'] = array_map(function ($dimensionValues) {
return array_shift($dimensionValues);
}, $dimensions);
}
$currentDomain = $this->domainRepository->findOneByActiveRequest();
if ($currentDomain !== null) {
$contextProperties['currentSite'] = $currentDomain->getSite();
$contextProperties['currentDomain'] = $currentDomain;
} else {
$contextProperties['currentSite'] = $this->siteRepository->findFirstOnline();
}
return $this->contextFactory->create($contextProperties);
}
/**
* Set options
*
* @param array $options Contains options
* like string $dimensionName Optional Dimension name,
* string $dimensionValues Optional The preferred dimension values, including the fallback dimension like array('en', 'de'); ,
* string $workspaceName Optional Name of the workspace to set for the context
* @return void
*/
public function setOptions(array $options): void
{
$this->options = $options;
}
}