Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neos9 compatible #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Classes/Changes/ReloadChangedState.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
namespace Sandstorm\MxGraph\Changes;

use Neos\Neos\Ui\Domain\Model\AbstractChange;
use Sandstorm\MxGraph\Domain\Model\Diagram;

class ReloadChangedState extends AbstractChange
{

public function canApply()
public function canApply(): bool
{
return true;
}

public function apply()
public function apply(): void
{
$this->updateWorkspaceInfo();
}
Expand Down
62 changes: 42 additions & 20 deletions Classes/Controller/DiagramEditorController.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php
namespace Sandstorm\MxGraph\Controller;

use Neos\ContentRepository\Core\Feature\NodeModification\Command\SetNodeProperties;
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyValuesToWrite;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAddress;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Media\Domain\Model\Asset;
use Neos\Media\Domain\Model\Image;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Neos\Domain\Service\UserService;
use Sandstorm\MxGraph\DiagramIdentifierSearchService;
use Sandstorm\MxGraph\Domain\Model\Diagram;

class DiagramEditorController extends ActionController
{
Expand All @@ -32,6 +35,12 @@ class DiagramEditorController extends ActionController
*/
protected $diagramIdentifierSearchService;

/**
* @Flow\Inject
* @var ContentRepositoryRegistry
*/
protected $contentRepositoryRegistry;

/**
* @Flow\InjectConfiguration(path="drawioEmbedUrl")
* @var string
Expand All @@ -53,9 +62,9 @@ class DiagramEditorController extends ActionController


/**
* @param NodeInterface $diagramNode
* @param Node $diagramNode
*/
public function indexAction(NodeInterface $diagramNode)
public function indexAction(Node $diagramNode)
{
$drawioEmbedUrlWithParameters = $this->drawioEmbedUrl;
if ($drawioEmbedUrlWithParameters === self::LOCAL_DRAWIO_EMBED_URL) {
Expand All @@ -73,16 +82,12 @@ public function indexAction(NodeInterface $diagramNode)
$drawioLanguage = 'da';
} elseif ($interfaceLanguage === 'de') {
$drawioLanguage = 'de';
} elseif ($interfaceLanguage === 'en') {
// default
} elseif ($interfaceLanguage === 'es') {
$drawioLanguage = 'es';
} elseif ($interfaceLanguage === 'fi') {
$drawioLanguage = 'fi';
} elseif ($interfaceLanguage === 'fr') {
$drawioLanguage = 'fr';
} elseif ($interfaceLanguage === 'km') {
// TODO: MISSING AS IT SEEMS
} elseif ($interfaceLanguage === 'lv') {
$drawioLanguage = 'lv';
} elseif ($interfaceLanguage === 'nl') {
Expand All @@ -97,6 +102,9 @@ public function indexAction(NodeInterface $diagramNode)
$drawioLanguage = 'ru';
} elseif ($interfaceLanguage === 'zh-CN') {
$drawioLanguage = 'zh';
} elseif ($interfaceLanguage !== 'en') {
// default or míssing language setting
$this->logger->warning('Unknown interface language: ' . $interfaceLanguage);
}

if (!empty($drawioLanguage)) {
Expand All @@ -106,9 +114,9 @@ public function indexAction(NodeInterface $diagramNode)
$drawioEmbedUrlWithParameters .= '?' . http_build_query($drawioEmbedParameters);

$this->view->assign('diagram', $diagramNode->getProperty('diagramSource'));
$this->view->assign('diagramNode', $diagramNode->getContextPath());
$this->view->assign('diagramNode', NodeAddress::fromNode($diagramNode)->toJson());
$this->view->assign('drawioEmbedUrlWithParameters', $drawioEmbedUrlWithParameters);
$this->view->assign('drawioConfiguration', is_array($this->drawioConfiguration) ? $this->drawioConfiguration : []);
$this->view->assign('drawioConfiguration', (array)$this->drawioConfiguration);

}

Expand All @@ -120,30 +128,39 @@ public function offlineLocalDiagramsNetAction()


/**
* @param NodeInterface $node
* @param Node $node
* @param string $xml
* @param string $svg
* @Flow\SkipCsrfProtection
*/
public function saveAction(NodeInterface $node, $xml, $svg)
public function saveAction(Node $node, $xml, $svg)
{
$contentRepository = $this->contentRepositoryRegistry->get($node->contentRepositoryId);

if (empty($svg)) {
// XML without SVG -> autosaved - not supported right now.
$node->setProperty('diagramSourceAutosaved', $xml);
$contentRepository->handle(SetNodeProperties::create(
$node->workspaceName,
$node->aggregateId,
$node->originDimensionSpacePoint,
PropertyValuesToWrite::fromArray(['diagramSourceAutosaved' => $xml]),
));
throw new \RuntimeException("TODO - autosave not supported right now.");
}

$node->setProperty('diagramSource', $xml);
// NEW since version 3.0.0
$node->setProperty('diagramSvgText', $svg);
$propertyValuesToWrite = PropertyValuesToWrite::fromArray([
'diagramSource' => $xml,
'diagramSvgText' => $svg,
]);

$diagramIdentifier = $node->getProperty('diagramIdentifier');
if (!empty($diagramIdentifier)) {
// also update related diagrams
foreach ($this->diagramIdentifierSearchService->findRelatedDiagramsWithIdentifierExcludingOwn($diagramIdentifier, $node) as $relatedDiagramNode) {
$relatedDiagramNode->setProperty('diagramSource', $xml);
$relatedDiagramNode->setProperty('diagramSvgText', $svg);
$propertyValuesToWrite = $propertyValuesToWrite->merge(PropertyValuesToWrite::fromArray([
'diagramSource' => $xml,
'diagramSvgText' => $svg,
]));
}
}

Expand All @@ -159,8 +176,13 @@ public function saveAction(NodeInterface $node, $xml, $svg)
$image = new Image($persistentResource);
}

$node->setProperty('image', $image);
// END DEPRECATION since version 3.0.0
$propertyValuesToWrite = $propertyValuesToWrite->withValue('image', $image);
$contentRepository->handle(SetNodeProperties::create(
$node->workspaceName,
$node->aggregateId,
$node->originDimensionSpacePoint,
$propertyValuesToWrite,
));

return 'OK';
}
Expand Down
13 changes: 8 additions & 5 deletions Classes/DiagramCreationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace Sandstorm\MxGraph;

use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Neos\Ui\NodeCreationHandler\NodeCreationHandlerInterface;
use Sandstorm\MxGraph\Domain\Model\Diagram;
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyValuesToWrite;
use Neos\Neos\Ui\Domain\NodeCreation\NodeCreationCommands;
use Neos\Neos\Ui\Domain\NodeCreation\NodeCreationElements;
use Neos\Neos\Ui\Domain\NodeCreation\NodeCreationHandlerInterface;

class DiagramCreationHandler implements NodeCreationHandlerInterface
{
public function handle(NodeInterface $node, array $data)
public function handle(NodeCreationCommands $commands, NodeCreationElements $elements): NodeCreationCommands
{
$node->setProperty('diagramIdentifier', 'Diagram ' . date('Y-m-d H:i'));
return $commands->withInitialPropertyValues(PropertyValuesToWrite::fromArray([
'diagramIdentifier' => 'Diagram ' . date('Y-m-d H:i'),
]));
}
}
6 changes: 3 additions & 3 deletions Classes/DiagramIdentifierDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Sandstorm\MxGraph;

use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\Flow\Annotations as Flow;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\I18n\Translator;
use Neos\Neos\Service\DataSource\AbstractDataSource;
use Sandstorm\LazyDataSource\LazyDataSourceTrait;
Expand All @@ -27,7 +27,7 @@ class DiagramIdentifierDataSource extends AbstractDataSource
*/
protected $diagramIdentifierSearchService;

protected function getDataForIdentifiers(array $identifiers, NodeInterface $node = null, array $arguments = [])
protected function getDataForIdentifiers(array $identifiers, Node $node = null, array $arguments = [])
{
// all identifiers will be returned as is (with a label containing usage count)
$options = [];
Expand All @@ -37,7 +37,7 @@ protected function getDataForIdentifiers(array $identifiers, NodeInterface $node
return $options;
}

protected function searchData(string $searchTerm, NodeInterface $node = null, array $arguments = [])
protected function searchData(string $searchTerm, Node $node = null, array $arguments = [])
{
$options = [];
if ($node !== null) {
Expand Down
82 changes: 59 additions & 23 deletions Classes/DiagramIdentifierSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,53 @@
namespace Sandstorm\MxGraph;


use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindChildNodesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindClosestNodeFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Domain\Service\NodeSearchServiceInterface;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;

/**
* @Flow\Scope("singleton")
*/
class DiagramIdentifierSearchService
{

/**
* @Flow\Inject
* @var NodeSearchServiceInterface
* @var ContentRepositoryRegistry
*/
protected $nodeSearchService;
protected $contentRepositoryRegistry;

/**
* @param string $query
* @param NodeInterface $node
* @param string $searchTerm
* @param Node $node
* @return string[]
*/
public function findInIdentifier(string $searchTerm, NodeInterface $node): array
public function findInIdentifier(string $searchTerm, Node $node): array
{
$results = [];
$possibleResults = $this->nodeSearchService->findByProperties($searchTerm, ['Sandstorm.MxGraph:Diagram'], $node->getContext());

$contentRepository = $this->contentRepositoryRegistry->get($node->contentRepositoryId);
$subgraph = $contentRepository->getContentGraph($node->workspaceName)->getSubgraph(
$node->dimensionSpacePoint,
VisibilityConstraints::frontend()
);
$siteNode = $subgraph->findClosestNode(
$node->aggregateId,
FindClosestNodeFilter::create(nodeTypes: NodeTypeNameFactory::NAME_SITE)
);
$possibleResults = $subgraph->findChildNodes(
$siteNode->aggregateId,
FindChildNodesFilter::create(
nodeTypes: 'Sandstorm.MxGraph:Diagram',
propertyValue: $searchTerm
),
);

foreach ($possibleResults as $possibleResult) {
assert($possibleResult instanceof NodeInterface);
assert($possibleResult instanceof Node);

// we include the diagram identifier if it contains the $searchTerm (case-insensitively)
$possibleDiagramIdentifier = $possibleResult->getProperty('diagramIdentifier');
Expand All @@ -45,15 +65,32 @@ public function findInIdentifier(string $searchTerm, NodeInterface $node): array

/**
* @param string $diagramIdentifier
* @return NodeInterface[]
* @return Node[]
*/
public function findRelatedDiagramsWithIdentifierExcludingOwn(string $diagramIdentifier, NodeInterface $contextNode): array
public function findRelatedDiagramsWithIdentifierExcludingOwn(string $diagramIdentifier, Node $contextNode): array
{
$results = [];
$possibleResults = $this->nodeSearchService->findByProperties($diagramIdentifier, ['Sandstorm.MxGraph:Diagram'], $contextNode->getContext());

$contentRepository = $this->contentRepositoryRegistry->get($contextNode->contentRepositoryId);
$subgraph = $contentRepository->getContentGraph($contextNode->workspaceName)->getSubgraph(
$contextNode->dimensionSpacePoint,
VisibilityConstraints::frontend()
);
$siteNode = $subgraph->findClosestNode(
$contextNode->aggregateId,
FindClosestNodeFilter::create(nodeTypes: NodeTypeNameFactory::NAME_SITE)
);
$possibleResults = $subgraph->findChildNodes(
$siteNode->aggregateId,
FindChildNodesFilter::create(
nodeTypes: 'Sandstorm.MxGraph:Diagram',
propertyValue: $diagramIdentifier
),
);

foreach ($possibleResults as $node) {
assert($node instanceof NodeInterface);
if ($contextNode->getContextPath() === $node->getContextPath()) {
assert($node instanceof Node);
if ($contextNode->equals($node)) {
// we skip ourselves
continue;
}
Expand All @@ -70,19 +107,18 @@ public function findRelatedDiagramsWithIdentifierExcludingOwn(string $diagramIde

/**
* @param string $diagramIdentifier
* @param NodeInterface $contextNode
* @return NodeInterface|null
* @param Node $contextNode
* @return Node|null
*/
public function findMostRecentDiagramWithIdentifierExcludingOwn(string $diagramIdentifier, NodeInterface $contextNode): ?NodeInterface
public function findMostRecentDiagramWithIdentifierExcludingOwn(string $diagramIdentifier, Node $contextNode): ?Node
{
$relatedDiagramNodes = $this->findRelatedDiagramsWithIdentifierExcludingOwn($diagramIdentifier, $contextNode);

uasort($relatedDiagramNodes, function(NodeInterface $a, NodeInterface $b) {
if (method_exists($a, 'getLastModificationDateTime') && method_exists($b, 'getLastModificationDateTime')) {
return $b->getLastModificationDateTime() <=> $a->getLastModificationDateTime();
}
uasort($relatedDiagramNodes, function(Node $nodeA, Node $nodeB) {
$timestampA = $nodeA->timestamps->lastModified ?? $nodeA->timestamps->created;
$timestampB = $nodeB->timestamps->lastModified ?? $nodeA->timestamps->created;

return 0;
return $timestampB <=> $timestampA;
});

if (count($relatedDiagramNodes) > 0) {
Expand Down
Loading