From dff3c9e5c5eee0ac5aa00a9a326152aa54ef0402 Mon Sep 17 00:00:00 2001 From: Toni Bessel Date: Wed, 16 Feb 2022 17:25:47 +0100 Subject: [PATCH] TASK: Add 1.0 version of the NodeTypeFinder. Related: NEOS-69 #time 6h --- .../NodeTypeFinderCommandController.php | 62 ++++++++++++++ .../Controller/NodeTypeFinderController.php | 62 ++++++++++++++ Classes/Service/NodeTypeFinderService.php | 84 +++++++++++++++++++ Configuration/Policy.yaml | 12 +++ Configuration/Settings.yaml | 10 +++ .../Fusion/Backend/NodeTypeFinder.fusion | 44 ++++++++++ Resources/Private/Fusion/Backend/Root.fusion | 1 + composer.json | 21 +++++ 8 files changed, 296 insertions(+) create mode 100644 Classes/Command/NodeTypeFinderCommandController.php create mode 100644 Classes/Controller/NodeTypeFinderController.php create mode 100644 Classes/Service/NodeTypeFinderService.php create mode 100644 Configuration/Policy.yaml create mode 100644 Configuration/Settings.yaml create mode 100644 Resources/Private/Fusion/Backend/NodeTypeFinder.fusion create mode 100644 Resources/Private/Fusion/Backend/Root.fusion create mode 100644 composer.json diff --git a/Classes/Command/NodeTypeFinderCommandController.php b/Classes/Command/NodeTypeFinderCommandController.php new file mode 100644 index 0000000..712841b --- /dev/null +++ b/Classes/Command/NodeTypeFinderCommandController.php @@ -0,0 +1,62 @@ +output->outputTable($this->nodeTypeFinderService->findNodeTypeOccurrences( + $nodeTypeName, + self::buildControllerContext() + ), ['Occurrence on page:']); + } + + private static function buildControllerContext(): ControllerContext + { + $_SERVER['FLOW_REWRITEURLS'] = '1'; + $httpRequest = ServerRequest::fromGlobals(); + $request = ActionRequest::fromHttpRequest($httpRequest); + $uriBuilder = new UriBuilder(); + $uriBuilder->setRequest($request); + + return new ControllerContext( + $request, + new ActionResponse(), + new Arguments([]), + $uriBuilder + ); + } +} diff --git a/Classes/Controller/NodeTypeFinderController.php b/Classes/Controller/NodeTypeFinderController.php new file mode 100644 index 0000000..7af62b1 --- /dev/null +++ b/Classes/Controller/NodeTypeFinderController.php @@ -0,0 +1,62 @@ +setFusionPathPattern('resource://Netlogix.NodeTypeFinder/Private/Fusion/Backend'); + } + + /** + * @param string $searchTerm + * + * @throws \Neos\Eel\Exception + * @throws \Neos\Flow\Http\Exception + * @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException + * @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException + * @throws \Neos\Flow\Property\Exception + * @throws \Neos\Flow\Security\Exception + * @throws \Neos\Neos\Exception + */ + public function indexAction(?string $searchTerm = null): void + { + $this->view->assign('searchTerm', $searchTerm); + + if (!empty($searchTerm)) { + $this->view->assign('occurrences', iterator_to_array($this->search($searchTerm))); + } + } + + private function search(string $searchTerm): \Generator + { + $occurrences = $this->nodeTypeFinderService->findNodeTypeOccurrences( + $searchTerm, + $this->controllerContext + ); + + foreach ($occurrences as $occurrence) { + yield ['url' => $occurrence['url'], 'label' => $occurrence['label']]; + } + } +} diff --git a/Classes/Service/NodeTypeFinderService.php b/Classes/Service/NodeTypeFinderService.php new file mode 100644 index 0000000..e01f6d8 --- /dev/null +++ b/Classes/Service/NodeTypeFinderService.php @@ -0,0 +1,84 @@ +contextFactory->create(['workspaceName' => 'live']); + + $nodes = (new FlowQuery([$context->getCurrentSiteNode()])) + ->find('/') + ->find('[instanceof '.$nodeTypeName.']') + ->get(); + + foreach ($nodes as $node) { + if (!$node instanceof NodeInterface) { + continue; + } + + $documentQuery = new FlowQuery([$node]); + $documentNode = $documentQuery->closest('[instanceof Neos.Neos:Document]')->get(0); + + if (!$documentNode instanceof NodeInterface) { + continue; + } + + $uri = $this->linkingService->createNodeUri( + $controllerContext, + $documentNode, + null, + null, + true + ); + + if (!array_key_exists($uri, $occurrences)) { + $occurrences[$uri] = [ + 'url' => str_replace('./', '', $uri), + 'label' => $documentNode->getLabel() + ]; + } + } + + return array_values($occurrences); + } +} diff --git a/Configuration/Policy.yaml b/Configuration/Policy.yaml new file mode 100644 index 0000000..85be963 --- /dev/null +++ b/Configuration/Policy.yaml @@ -0,0 +1,12 @@ +privilegeTargets: + 'Neos\Neos\Security\Authorization\Privilege\ModulePrivilege': + 'Netlogix.NodeTypeFinder:BackendModule': + matcher: 'administration/nodeTypeFinderModule' + +roles: + 'Neos.Neos:Administrator': + privileges: + - + privilegeTarget: 'Netlogix.NodeTypeFinder:BackendModule' + permission: GRANT + diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml new file mode 100644 index 0000000..c4f81c3 --- /dev/null +++ b/Configuration/Settings.yaml @@ -0,0 +1,10 @@ +Neos: + Neos: + modules: + administration: + submodules: + nodeTypeFinderModule: + label: 'NodeType Finder' + controller: 'Netlogix\NodeTypeFinder\Controller\NodeTypeFinderController' + description: 'Auflistung der Seiten URLs auf denen der gegebene NodeType vorkommt.' + icon: 'icon-search' diff --git a/Resources/Private/Fusion/Backend/NodeTypeFinder.fusion b/Resources/Private/Fusion/Backend/NodeTypeFinder.fusion new file mode 100644 index 0000000..e36b79b --- /dev/null +++ b/Resources/Private/Fusion/Backend/NodeTypeFinder.fusion @@ -0,0 +1,44 @@ +include: resource://Neos.Fusion/Private/Fusion/Root.fusion +include: resource://Neos.Fusion.Form/Private/Fusion/Root.fusion + +Netlogix.NodeTypeFinder.NodeTypeFinderController.index = Netlogix.NodeTypeFinder:BackendModule + +prototype(Netlogix.NodeTypeFinder:BackendModule) < prototype(Neos.Fusion:Component) { + + renderer = afx` + +
+ + + + + Search + +
+
+
+ Occurrences of NodeType +
+ + + + + + +
+ + {occurrence.label} + +
+
+
+ No occurrences of NodeType {searchTerm}. +
+
+
+
+ Search for a NodeType to see a list of its occurrences. +
+
+ ` +} diff --git a/Resources/Private/Fusion/Backend/Root.fusion b/Resources/Private/Fusion/Backend/Root.fusion new file mode 100644 index 0000000..dd5c864 --- /dev/null +++ b/Resources/Private/Fusion/Backend/Root.fusion @@ -0,0 +1 @@ +include: NodeTypeFinder.fusion diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6a6c6a9 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "netlogix/nodetypefinder", + "description": "Neos Backend Module to search for occurrences of NodeTypes in the page tree.", + "type": "neos-plugin", + "license": "MIT", + "require": { + "php": "^7.4 || ^8.0", + "neos/neos": "^5.3 || ^7.0", + "guzzlehttp/psr7": "^1.8 || ^2.0" + }, + "autoload": { + "psr-4": { + "Netlogix\\NodeTypeFinder\\": "Classes/" + } + }, + "extra": { + "neos": { + "package-key": "Netlogix.NodeTypeFinder" + } + } +}