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

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