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

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 ContentTypeExportInterfacePreResolvableContentTypeInterface
  28. {
  29.     /**
  30.      * @var ContentQueryExecutorInterface
  31.      */
  32.     private $contentQueryExecutor;
  33.     /**
  34.      * @var ContentQueryBuilderInterface
  35.      */
  36.     private $contentQueryBuilder;
  37.     /**
  38.      * @var ReferenceStoreInterface
  39.      */
  40.     private $referenceStore;
  41.     /**
  42.      * @var bool
  43.      */
  44.     private $showDrafts;
  45.     /**
  46.      * @var array
  47.      */
  48.     private $permissions;
  49.     /**
  50.      * @var array
  51.      */
  52.     private $enabledTwigAttributes = [];
  53.     public function __construct(
  54.         ContentQueryExecutorInterface $contentQueryExecutor,
  55.         ContentQueryBuilderInterface $contentQueryBuilder,
  56.         ReferenceStoreInterface $referenceStore,
  57.         $showDrafts,
  58.         $permissions null,
  59.         array $enabledTwigAttributes = [
  60.             'path' => true,
  61.         ]
  62.     ) {
  63.         $this->contentQueryExecutor $contentQueryExecutor;
  64.         $this->contentQueryBuilder $contentQueryBuilder;
  65.         $this->referenceStore $referenceStore;
  66.         $this->showDrafts $showDrafts;
  67.         $this->permissions $permissions;
  68.         $this->enabledTwigAttributes $enabledTwigAttributes;
  69.         if ($enabledTwigAttributes['path'] ?? true) {
  70.             @\trigger_error('Enabling the "path" parameter is deprecated since sulu/sulu 2.3.'\E_USER_DEPRECATED);
  71.         }
  72.     }
  73.     public function read(
  74.         NodeInterface $node,
  75.         PropertyInterface $property,
  76.         $webspaceKey,
  77.         $languageCode,
  78.         $segmentKey
  79.     ) {
  80.         $data = [];
  81.         if ($node->hasProperty($property->getName())) {
  82.             $data $node->getProperty($property->getName())->getString();
  83.         }
  84.         $refs $data ?? [];
  85.         $property->setValue($refs);
  86.     }
  87.     public function getDefaultParams(PropertyInterface $property null)
  88.     {
  89.         return ['properties' => new PropertyParameter('properties', [], 'collection')];
  90.     }
  91.     public function write(
  92.         NodeInterface $node,
  93.         PropertyInterface $property,
  94.         $userId,
  95.         $webspaceKey,
  96.         $languageCode,
  97.         $segmentKey
  98.     ) {
  99.         $value $property->getValue();
  100.         if ($value instanceof ArrayableInterface) {
  101.             $value $value->toArray();
  102.         }
  103.         if (isset($value)) {
  104.             // remove not existing ids
  105.             $session $node->getSession();
  106.             $selectedNodes $session->getNodesByIdentifier($value);
  107.             $ids = [];
  108.             foreach ($selectedNodes as $selectedNode) {
  109.                 if ($selectedNode->getIdentifier() === $node->getIdentifier()) {
  110.                     throw new \InvalidArgumentException('You are not allowed to link a page to itself!');
  111.                 }
  112.                 $ids[] = $selectedNode->getIdentifier();
  113.             }
  114.             $value $ids;
  115.         }
  116.         // set value to node
  117.         $node->setProperty($property->getName(), $valuePropertyType::REFERENCE);
  118.     }
  119.     public function remove(
  120.         NodeInterface $node,
  121.         PropertyInterface $property,
  122.         $webspaceKey,
  123.         $languageCode,
  124.         $segmentKey
  125.     ) {
  126.         if ($node->hasProperty($property->getName())) {
  127.             $node->getProperty($property->getName())->remove();
  128.         }
  129.     }
  130.     public function getContentData(PropertyInterface $property)
  131.     {
  132.         $data $property->getValue();
  133.         $container = new PageSelectionContainer(
  134.             isset($data) ? $data : [],
  135.             $this->contentQueryExecutor,
  136.             $this->contentQueryBuilder,
  137.             \array_merge($this->getDefaultParams(), $property->getParams()),
  138.             $property->getStructure()->getWebspaceKey(),
  139.             $property->getStructure()->getLanguageCode(),
  140.             $this->showDrafts,
  141.             $this->permissions[PermissionTypes::VIEW],
  142.             $this->enabledTwigAttributes
  143.         );
  144.         return $container->getData();
  145.     }
  146.     public function exportData($propertyValue)
  147.     {
  148.         if (!\is_array($propertyValue) || empty($propertyValue)) {
  149.             return '';
  150.         }
  151.         return \json_encode($propertyValue);
  152.     }
  153.     public function importData(
  154.         NodeInterface $node,
  155.         PropertyInterface $property,
  156.         $value,
  157.         $userId,
  158.         $webspaceKey,
  159.         $languageCode,
  160.         $segmentKey null
  161.     ) {
  162.         $property->setValue(\json_decode($value));
  163.         $this->write($node$property$userId$webspaceKey$languageCode$segmentKey);
  164.     }
  165.     public function preResolve(PropertyInterface $property)
  166.     {
  167.         $uuids $property->getValue();
  168.         if (!\is_array($uuids)) {
  169.             return;
  170.         }
  171.         foreach ($uuids as $uuid) {
  172.             $this->referenceStore->add($uuid);
  173.         }
  174.     }
  175. }