vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Content/PageSelectionContainer.php line 130

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;
  11. use JMS\Serializer\Annotation\Exclude;
  12. use Sulu\Component\Content\Compat\StructureInterface;
  13. use Sulu\Component\Content\Query\ContentQueryBuilderInterface;
  14. use Sulu\Component\Content\Query\ContentQueryExecutorInterface;
  15. use Sulu\Component\Util\ArrayableInterface;
  16. /**
  17.  * Container for PageSelection, holds the config for a internal links, and lazy loads the structures.
  18.  */
  19. class PageSelectionContainer implements ArrayableInterface
  20. {
  21.     /**
  22.      * The content mapper, which is needed for lazy loading.
  23.      *
  24.      * @Exclude
  25.      *
  26.      * @var ContentQueryExecutorInterface
  27.      */
  28.     private $contentQueryExecutor;
  29.     /**
  30.      * The content mapper, which is needed for lazy loading.
  31.      *
  32.      * @Exclude
  33.      *
  34.      * @var ContentQueryBuilderInterface
  35.      */
  36.     private $contentQueryBuilder;
  37.     /**
  38.      * The params to load.
  39.      *
  40.      * @Exclude
  41.      *
  42.      * @var array
  43.      */
  44.     private $params;
  45.     /**
  46.      * The key of the webspace.
  47.      *
  48.      * @Exclude
  49.      *
  50.      * @var string
  51.      */
  52.     private $webspaceKey;
  53.     /**
  54.      * The code of the language.
  55.      *
  56.      * @Exclude
  57.      *
  58.      * @var string
  59.      */
  60.     private $languageCode;
  61.     /**
  62.      * @var string[]
  63.      */
  64.     private $ids;
  65.     /**
  66.      * @Exclude
  67.      *
  68.      * @var StructureInterface[]
  69.      */
  70.     private $data;
  71.     /**
  72.      * @var bool
  73.      */
  74.     private $showDrafts;
  75.     /**
  76.      * @var array
  77.      */
  78.     private $permission;
  79.     /**
  80.      * @var array
  81.      */
  82.     private $enabledTwigAttributes = [];
  83.     public function __construct(
  84.         $ids,
  85.         ContentQueryExecutorInterface $contentQueryExecutor,
  86.         ContentQueryBuilderInterface $contentQueryBuilder,
  87.         $params,
  88.         $webspaceKey,
  89.         $languageCode,
  90.         $showDrafts,
  91.         $permission null,
  92.         array $enabledTwigAttributes = [
  93.             'path' => true,
  94.         ]
  95.     ) {
  96.         $this->ids $ids;
  97.         $this->contentQueryExecutor $contentQueryExecutor;
  98.         $this->contentQueryBuilder $contentQueryBuilder;
  99.         $this->webspaceKey $webspaceKey;
  100.         $this->languageCode $languageCode;
  101.         $this->params $params;
  102.         $this->showDrafts $showDrafts;
  103.         $this->permission $permission;
  104.         $this->enabledTwigAttributes $enabledTwigAttributes;
  105.         if ($enabledTwigAttributes['path'] ?? true) {
  106.             @\trigger_error('Enabling the "path" parameter is deprecated since sulu/sulu 2.3.'\E_USER_DEPRECATED);
  107.         }
  108.     }
  109.     /**
  110.      * Lazy loads the data based on the filter criteria from the config.
  111.      *
  112.      * @return StructureInterface[]
  113.      */
  114.     public function getData()
  115.     {
  116.         if (null === $this->data) {
  117.             $this->data $this->loadData();
  118.         }
  119.         return $this->data;
  120.     }
  121.     /**
  122.      * lazy load data.
  123.      */
  124.     private function loadData()
  125.     {
  126.         $result = [];
  127.         if (null !== $this->ids && \count($this->ids) > 0) {
  128.             $this->contentQueryBuilder->init(
  129.                 [
  130.                     'ids' => $this->ids,
  131.                     'properties' => (isset($this->params['properties']) ? $this->params['properties']->getValue() : []),
  132.                     'published' => !$this->showDrafts,
  133.                 ]
  134.             );
  135.             $pages $this->contentQueryExecutor->execute(
  136.                 $this->webspaceKey,
  137.                 [$this->languageCode],
  138.                 $this->contentQueryBuilder,
  139.                 true,
  140.                 -1,
  141.                 null,
  142.                 null,
  143.                 false,
  144.                 $this->permission
  145.             );
  146.             // init vars
  147.             $map = [];
  148.             // map pages
  149.             foreach ($pages as $page) {
  150.                 if (!($this->enabledTwigAttributes['path'] ?? true)) {
  151.                     unset($page['path']);
  152.                 }
  153.                 $map[$page['id']] = $page;
  154.             }
  155.             foreach ($this->ids as $id) {
  156.                 if (isset($map[$id])) {
  157.                     $result[] = $map[$id];
  158.                 }
  159.             }
  160.         }
  161.         return $result;
  162.     }
  163.     /**
  164.      * magic getter.
  165.      */
  166.     public function __get($name)
  167.     {
  168.         switch ($name) {
  169.             case 'data':
  170.                 return $this->getData();
  171.         }
  172.         return;
  173.     }
  174.     /**
  175.      * magic isset.
  176.      */
  177.     public function __isset($name)
  178.     {
  179.         return 'data' == $name;
  180.     }
  181.     public function toArray($depth null)
  182.     {
  183.         return ['ids' => $this->ids];
  184.     }
  185. }