vendor/sulu/sulu/src/Sulu/Bundle/SnippetBundle/Content/SnippetContent.php line 119

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\SnippetBundle\Content;
  11. use PHPCR\NodeInterface;
  12. use PHPCR\PropertyType;
  13. use PHPCR\Util\UUIDHelper;
  14. use Sulu\Bundle\SnippetBundle\Snippet\DefaultSnippetManagerInterface;
  15. use Sulu\Bundle\SnippetBundle\Snippet\SnippetResolverInterface;
  16. use Sulu\Bundle\SnippetBundle\Snippet\WrongSnippetTypeException;
  17. use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface;
  18. use Sulu\Component\Content\Compat\PropertyInterface;
  19. use Sulu\Component\Content\Compat\PropertyParameter;
  20. use Sulu\Component\Content\Compat\Structure\PageBridge;
  21. use Sulu\Component\Content\Compat\Structure\SnippetBridge;
  22. use Sulu\Component\Content\ComplexContentType;
  23. use Sulu\Component\Content\ContentTypeExportInterface;
  24. use Sulu\Component\Content\PreResolvableContentTypeInterface;
  25. class SnippetContent extends ComplexContentType implements ContentTypeExportInterfacePreResolvableContentTypeInterface
  26. {
  27.     /**
  28.      * @var SnippetResolverInterface
  29.      */
  30.     private $snippetResolver;
  31.     /**
  32.      * @var DefaultSnippetManagerInterface
  33.      */
  34.     private $defaultSnippetManager;
  35.     /**
  36.      * @var ReferenceStoreInterface
  37.      */
  38.     private $referenceStore;
  39.     /**
  40.      * @var bool
  41.      */
  42.     protected $defaultEnabled;
  43.     /**
  44.      * @param true $defaultEnabled
  45.      */
  46.     public function __construct(
  47.         DefaultSnippetManagerInterface $defaultSnippetManager,
  48.         SnippetResolverInterface $snippetResolver,
  49.         ReferenceStoreInterface $referenceStore,
  50.         $defaultEnabled
  51.     ) {
  52.         $this->snippetResolver $snippetResolver;
  53.         $this->defaultSnippetManager $defaultSnippetManager;
  54.         $this->referenceStore $referenceStore;
  55.         $this->defaultEnabled $defaultEnabled;
  56.     }
  57.     public function read(NodeInterface $nodePropertyInterface $property$webspaceKey$languageCode$segmentKey)
  58.     {
  59.         $refs = [];
  60.         if ($node->hasProperty($property->getName())) {
  61.             $refs $node->getProperty($property->getName())->getString();
  62.         }
  63.         $property->setValue($refs);
  64.     }
  65.     public function write(
  66.         NodeInterface $node,
  67.         PropertyInterface $property,
  68.         $userId,
  69.         $webspaceKey,
  70.         $languageCode,
  71.         $segmentKey
  72.     ) {
  73.         $values $property->getValue();
  74.         $snippetReferences = [];
  75.         $values \is_array($values) ? $values : [];
  76.         foreach ($values as $value) {
  77.             if ($value instanceof SnippetBridge) {
  78.                 $snippetReferences[] = $value->getUuid();
  79.             } elseif (\is_array($value) && \array_key_exists('uuid'$value) && UUIDHelper::isUUID($value['uuid'])) {
  80.                 $snippetReferences[] = $value['uuid'];
  81.             } elseif (UUIDHelper::isUUID($value)) {
  82.                 $snippetReferences[] = $value;
  83.             } else {
  84.                 throw new \InvalidArgumentException(
  85.                     \sprintf(
  86.                         'Property value must either be a UUID or a Snippet, "%s" given.',
  87.                         \gettype($value)
  88.                     )
  89.                 );
  90.             }
  91.         }
  92.         $node->setProperty($property->getName(), $snippetReferencesPropertyType::REFERENCE);
  93.     }
  94.     public function remove(NodeInterface $nodePropertyInterface $property$webspaceKey$languageCode$segmentKey)
  95.     {
  96.         if ($node->hasProperty($property->getName())) {
  97.             $node->getProperty($property->getName())->remove();
  98.         }
  99.     }
  100.     public function getViewData(PropertyInterface $property)
  101.     {
  102.         $viewData = [];
  103.         foreach ($this->getSnippets($property) as $snippet) {
  104.             $viewData[] = $snippet['view'];
  105.         }
  106.         return $viewData;
  107.     }
  108.     public function getContentData(PropertyInterface $property)
  109.     {
  110.         $contentData = [];
  111.         foreach ($this->getSnippets($property) as $snippet) {
  112.             $contentData[] = $snippet['content'];
  113.         }
  114.         return $contentData;
  115.     }
  116.     /**
  117.      * Returns snippets with given property value.
  118.      */
  119.     private function getSnippets(PropertyInterface $property)
  120.     {
  121.         /** @var PageBridge $page */
  122.         $page $property->getStructure();
  123.         $webspaceKey $page->getWebspaceKey();
  124.         $locale $page->getLanguageCode();
  125.         $shadowLocale null;
  126.         if ($page->getIsShadow()) {
  127.             $shadowLocale $page->getShadowBaseLanguage();
  128.         }
  129.         $refs $property->getValue();
  130.         $ids $this->getUuids($refs);
  131.         $snippetType $this->getParameterValue($property->getParams(), 'snippetType');
  132.         $default $this->getParameterValue($property->getParams(), 'default'false);
  133.         $snippetArea $default;
  134.         if (true === $snippetArea || 'true' === $snippetArea) {
  135.             $snippetArea $snippetType;
  136.         }
  137.         if (empty($ids) && $snippetArea && $this->defaultEnabled) {
  138.             $ids $this->loadSnippetAreaIds($webspaceKey$snippetArea$locale);
  139.         }
  140.         $params $property->getParams();
  141.         $loadExcerpt = isset($params['loadExcerpt']) ? $params['loadExcerpt']->getValue() : false;
  142.         return $this->snippetResolver->resolve($ids$webspaceKey$locale$shadowLocale$loadExcerpt);
  143.     }
  144.     private function loadSnippetAreaIds($webspaceKey$snippetArea$locale)
  145.     {
  146.         try {
  147.             $snippet $this->defaultSnippetManager->load($webspaceKey$snippetArea$locale);
  148.         } catch (WrongSnippetTypeException $exception) {
  149.             return [];
  150.         }
  151.         if (!$snippet) {
  152.             return [];
  153.         }
  154.         return [$snippet->getUuid()];
  155.     }
  156.     /**
  157.      * The data is not always normalized, so we normalize the data here.
  158.      */
  159.     private function getUuids($data)
  160.     {
  161.         return \is_array($data) ? $data : [];
  162.     }
  163.     /**
  164.      * Returns value of parameter.
  165.      * If parameter not exists the default will be returned.
  166.      *
  167.      * @param PropertyParameter[] $parameter
  168.      * @param string $name
  169.      */
  170.     private function getParameterValue(array $parameter$name$default null)
  171.     {
  172.         if (!\array_key_exists($name$parameter)) {
  173.             return $default;
  174.         }
  175.         return $parameter[$name]->getValue();
  176.     }
  177.     public function exportData($propertyValue)
  178.     {
  179.         $uuids $this->getUuids($propertyValue);
  180.         if (empty($uuids)) {
  181.             return '';
  182.         }
  183.         return \json_encode($this->getUuids($propertyValue));
  184.     }
  185.     public function importData(
  186.         NodeInterface $node,
  187.         PropertyInterface $property,
  188.         $value,
  189.         $userId,
  190.         $webspaceKey,
  191.         $languageCode,
  192.         $segmentKey null
  193.     ) {
  194.         $property->setValue(\json_decode($value));
  195.         $this->write($node$property$userId$webspaceKey$languageCode$segmentKey);
  196.     }
  197.     public function preResolve(PropertyInterface $property)
  198.     {
  199.         foreach ($this->getUuids($property->getValue()) as $uuid) {
  200.             $this->referenceStore->add($uuid);
  201.         }
  202.     }
  203. }