vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Teaser/TeaserContentType.php line 92

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\Teaser;
  11. use PHPCR\NodeInterface;
  12. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\AnyOfsMetadata;
  13. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\ArrayMetadata;
  14. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\EmptyArrayMetadata;
  15. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\NullMetadata;
  16. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\NumberMetadata;
  17. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\ObjectMetadata;
  18. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\PropertyMetadata;
  19. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\PropertyMetadataMapperInterface;
  20. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\PropertyMetadataMinMaxValueResolver;
  21. use Sulu\Bundle\AdminBundle\Metadata\SchemaMetadata\StringMetadata;
  22. use Sulu\Bundle\PageBundle\Teaser\Provider\TeaserProviderPoolInterface;
  23. use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface;
  24. use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreNotExistsException;
  25. use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStorePoolInterface;
  26. use Sulu\Component\Content\Compat\PropertyInterface;
  27. use Sulu\Component\Content\Compat\PropertyParameter;
  28. use Sulu\Component\Content\Metadata\PropertyMetadata as ContentPropertyMetadata;
  29. use Sulu\Component\Content\PreResolvableContentTypeInterface;
  30. use Sulu\Component\Content\SimpleContentType;
  31. /**
  32.  * Provides content-type for selecting teasers.
  33.  */
  34. class TeaserContentType extends SimpleContentType implements PreResolvableContentTypeInterfacePropertyMetadataMapperInterface
  35. {
  36.     /**
  37.      * @var TeaserProviderPoolInterface
  38.      */
  39.     private $teaserProviderPool;
  40.     /**
  41.      * @var TeaserManagerInterface
  42.      */
  43.     private $teaserManager;
  44.     /**
  45.      * @var ReferenceStorePoolInterface
  46.      */
  47.     private $referenceStorePool;
  48.     /**
  49.      * @var PropertyMetadataMinMaxValueResolver|null
  50.      */
  51.     private $propertyMetadataMinMaxValueResolver;
  52.     public function __construct(
  53.         TeaserProviderPoolInterface $providerPool,
  54.         TeaserManagerInterface $teaserManager,
  55.         ReferenceStorePoolInterface $referenceStorePool,
  56.         ?PropertyMetadataMinMaxValueResolver $propertyMetadataMinMaxValueResolver null
  57.     ) {
  58.         parent::__construct('teaser_selection');
  59.         $this->teaserProviderPool $providerPool;
  60.         $this->teaserManager $teaserManager;
  61.         $this->referenceStorePool $referenceStorePool;
  62.         $this->propertyMetadataMinMaxValueResolver $propertyMetadataMinMaxValueResolver;
  63.     }
  64.     public function getDefaultParams(PropertyInterface $property null)
  65.     {
  66.         return [
  67.             'providerConfiguration' => $this->teaserProviderPool->getConfiguration(),
  68.             'present_as' => new PropertyParameter('present_as', [], 'collection'),
  69.         ];
  70.     }
  71.     protected function decodeValue($value)
  72.     {
  73.         return \json_decode($valuetrue);
  74.     }
  75.     protected function encodeValue($value)
  76.     {
  77.         return \json_encode($value);
  78.     }
  79.     public function getContentData(PropertyInterface $property)
  80.     {
  81.         $items $this->getItems($property);
  82.         if (=== \count($items)) {
  83.             return [];
  84.         }
  85.         $result $this->teaserManager->find($items$property->getStructure()->getLanguageCode());
  86.         $mediaReferenceStore $this->getMediaReferenceStore();
  87.         if (!$mediaReferenceStore) {
  88.             return $result;
  89.         }
  90.         foreach ($result as $item) {
  91.             if ($item->getMediaId()) {
  92.                 $mediaReferenceStore->add($item->getMediaId());
  93.             }
  94.         }
  95.         return $result;
  96.     }
  97.     /**
  98.      * @return ReferenceStoreInterface|null
  99.      */
  100.     private function getMediaReferenceStore()
  101.     {
  102.         try {
  103.             return $this->referenceStorePool->getStore('media');
  104.         } catch (ReferenceStoreNotExistsException $exception) {
  105.             return null;
  106.         }
  107.     }
  108.     public function getViewData(PropertyInterface $property)
  109.     {
  110.         return $this->getValue($property);
  111.     }
  112.     public function importData(
  113.         NodeInterface $node,
  114.         PropertyInterface $property,
  115.         $value,
  116.         $userId,
  117.         $webspaceKey,
  118.         $languageCode,
  119.         $segmentKey null
  120.     ) {
  121.         if (!empty($value)) {
  122.             $value \json_decode($value);
  123.         }
  124.         parent::importData($node$property$value$userId$webspaceKey$languageCode$segmentKey);
  125.     }
  126.     public function preResolve(PropertyInterface $property)
  127.     {
  128.         foreach ($this->getItems($property) as $item) {
  129.             try {
  130.                 $this->referenceStorePool->getStore($item['type'])->add($item['id']);
  131.             } catch (ReferenceStoreNotExistsException $exception) {
  132.                 // ignore not existing stores
  133.             }
  134.         }
  135.     }
  136.     /**
  137.      * Returns items.
  138.      *
  139.      * @return array
  140.      */
  141.     private function getItems(PropertyInterface $property)
  142.     {
  143.         $value $this->getValue($property);
  144.         if (!\is_array($value['items']) || === \count($value['items'])) {
  145.             return [];
  146.         }
  147.         return $value['items'];
  148.     }
  149.     /**
  150.      * Returns property-value merged with defaults.
  151.      *
  152.      * @return array
  153.      */
  154.     private function getValue(PropertyInterface $property)
  155.     {
  156.         $default = ['presentAs' => null'items' => []];
  157.         if (!\is_array($property->getValue())) {
  158.             return $default;
  159.         }
  160.         return \array_merge($default$property->getValue());
  161.     }
  162.     public function mapPropertyMetadata(ContentPropertyMetadata $propertyMetadata): PropertyMetadata
  163.     {
  164.         $mandatory $propertyMetadata->isRequired();
  165.         $minMaxValue = (object) [
  166.             'min' => null,
  167.             'max' => null,
  168.         ];
  169.         if (null !== $this->propertyMetadataMinMaxValueResolver) {
  170.             $minMaxValue $this->propertyMetadataMinMaxValueResolver->resolveMinMaxValue($propertyMetadata);
  171.         }
  172.         $itemsMetadata = new ArrayMetadata(
  173.             new ObjectMetadata([
  174.                 new PropertyMetadata('id'true, new AnyOfsMetadata([
  175.                     new StringMetadata(),
  176.                     new NumberMetadata(),
  177.                 ])),
  178.                 new PropertyMetadata('type'true, new StringMetadata()),
  179.                 new PropertyMetadata('title'false, new StringMetadata()),
  180.                 new PropertyMetadata('description'false, new StringMetadata()),
  181.                 new PropertyMetadata('mediaId'false, new NumberMetadata()),
  182.             ]),
  183.             $minMaxValue->min,
  184.             $minMaxValue->max,
  185.             true
  186.         );
  187.         if (!$mandatory) {
  188.             $itemsMetadata = new AnyOfsMetadata([
  189.                 new EmptyArrayMetadata(),
  190.                 $itemsMetadata,
  191.             ]);
  192.         }
  193.         $teaserSelectionMetadata = new ObjectMetadata([
  194.             new PropertyMetadata('items'$mandatory$itemsMetadata),
  195.             new PropertyMetadata('presentAs'false, new StringMetadata()),
  196.         ]);
  197.         if (!$mandatory) {
  198.             $teaserSelectionMetadata = new AnyOfsMetadata([
  199.                 new NullMetadata(),
  200.                 $teaserSelectionMetadata,
  201.             ]);
  202.         }
  203.         return new PropertyMetadata($propertyMetadata->getName(), $mandatory$teaserSelectionMetadata);
  204.     }
  205. }