vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Content/Types/PageSelection.php line 131

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\PageBundle\Content\Types;
  11. use PHPCR\NodeInterface;
  12. use PHPCR\PropertyType;
  13. use Sulu\Bundle\PageBundle\Content\PageSelectionContainer;
  14. use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface;
  15. use Sulu\Component\Content\Compat\PropertyInterface;
  16. use Sulu\Component\Content\Compat\PropertyParameter;
  17. use Sulu\Component\Content\ComplexContentType;
  18. use Sulu\Component\Content\ContentTypeExportInterface;
  19. use Sulu\Component\Content\PreResolvableContentTypeInterface;
  20. use Sulu\Component\Content\Query\ContentQueryBuilderInterface;
  21. use Sulu\Component\Content\Query\ContentQueryExecutorInterface;
  22. use Sulu\Component\Security\Authorization\PermissionTypes;
  23. use Sulu\Component\Util\ArrayableInterface;
  24. /**
  25. * content type for internal links selection.
  26. */
  27. class PageSelection extends ComplexContentType implements ContentTypeExportInterface, PreResolvableContentTypeInterface
  28. {
  29. /**
  30. * @param bool $showDrafts
  31. * @param mixed[] $permissions
  32. */
  33. public function __construct(
  34. private ContentQueryExecutorInterface $contentQueryExecutor,
  35. private ContentQueryBuilderInterface $contentQueryBuilder,
  36. private ReferenceStoreInterface $referenceStore,
  37. private $showDrafts,
  38. private $permissions = null,
  39. private array $enabledTwigAttributes = [
  40. 'path' => true,
  41. ]
  42. ) {
  43. if ($this->enabledTwigAttributes['path'] ?? true) {
  44. @trigger_deprecation('sulu/sulu', '2.3', 'Enabling the "path" parameter is deprecated.');
  45. }
  46. }
  47. public function read(
  48. NodeInterface $node,
  49. PropertyInterface $property,
  50. $webspaceKey,
  51. $languageCode,
  52. $segmentKey
  53. ) {
  54. $data = [];
  55. if ($node->hasProperty($property->getName())) {
  56. $data = $node->getProperty($property->getName())->getString();
  57. }
  58. $refs = $data ?? [];
  59. $property->setValue($refs);
  60. }
  61. public function getDefaultParams(?PropertyInterface $property = null)
  62. {
  63. return ['properties' => new PropertyParameter('properties', [], 'collection')];
  64. }
  65. public function write(
  66. NodeInterface $node,
  67. PropertyInterface $property,
  68. $userId,
  69. $webspaceKey,
  70. $languageCode,
  71. $segmentKey
  72. ) {
  73. $value = $property->getValue();
  74. if ($value instanceof ArrayableInterface) {
  75. $value = $value->toArray();
  76. }
  77. if (isset($value)) {
  78. // remove not existing ids
  79. $session = $node->getSession();
  80. $selectedNodes = $session->getNodesByIdentifier($value);
  81. $ids = [];
  82. foreach ($selectedNodes as $selectedNode) {
  83. if ($selectedNode->getIdentifier() === $node->getIdentifier()) {
  84. throw new \InvalidArgumentException('You are not allowed to link a page to itself!');
  85. }
  86. $ids[] = $selectedNode->getIdentifier();
  87. }
  88. $value = $ids;
  89. }
  90. // set value to node
  91. $node->setProperty($property->getName(), $value, PropertyType::REFERENCE);
  92. }
  93. public function remove(
  94. NodeInterface $node,
  95. PropertyInterface $property,
  96. $webspaceKey,
  97. $languageCode,
  98. $segmentKey
  99. ) {
  100. if ($node->hasProperty($property->getName())) {
  101. $node->getProperty($property->getName())->remove();
  102. }
  103. }
  104. public function getContentData(PropertyInterface $property)
  105. {
  106. $data = $property->getValue();
  107. $container = new PageSelectionContainer(
  108. isset($data) ? $data : [],
  109. $this->contentQueryExecutor,
  110. $this->contentQueryBuilder,
  111. \array_merge($this->getDefaultParams(), $property->getParams()),
  112. $property->getStructure()->getWebspaceKey(),
  113. $property->getStructure()->getLanguageCode(),
  114. $this->showDrafts,
  115. $this->permissions[PermissionTypes::VIEW],
  116. $this->enabledTwigAttributes
  117. );
  118. return $container->getData();
  119. }
  120. public function exportData($propertyValue)
  121. {
  122. if (!\is_array($propertyValue) || empty($propertyValue)) {
  123. return '';
  124. }
  125. return \json_encode($propertyValue);
  126. }
  127. public function importData(
  128. NodeInterface $node,
  129. PropertyInterface $property,
  130. $value,
  131. $userId,
  132. $webspaceKey,
  133. $languageCode,
  134. $segmentKey = null
  135. ) {
  136. $property->setValue(\json_decode($value));
  137. $this->write($node, $property, $userId, $webspaceKey, $languageCode, $segmentKey);
  138. }
  139. public function preResolve(PropertyInterface $property)
  140. {
  141. $uuids = $property->getValue();
  142. if (!\is_array($uuids)) {
  143. return;
  144. }
  145. foreach ($uuids as $uuid) {
  146. $this->referenceStore->add($uuid);
  147. }
  148. }
  149. }