vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Content/Structure/ExcerptStructureExtension.php line 205

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\Structure;
  11. use PHPCR\NodeInterface;
  12. use Sulu\Bundle\SearchBundle\Search\Factory;
  13. use Sulu\Component\Content\Compat\PropertyInterface;
  14. use Sulu\Component\Content\Compat\StructureInterface;
  15. use Sulu\Component\Content\Compat\StructureManagerInterface;
  16. use Sulu\Component\Content\ContentTypeExportInterface;
  17. use Sulu\Component\Content\ContentTypeManagerInterface;
  18. use Sulu\Component\Content\Extension\AbstractExtension;
  19. use Sulu\Component\Content\Extension\ExportExtensionInterface;
  20. use Sulu\Component\Content\Mapper\Translation\TranslatedProperty;
  21. use Sulu\Component\Export\Manager\ExportManagerInterface;
  22. use Sulu\Component\Import\Manager\ImportManagerInterface;
  23. /**
  24. * extends structure with seo content.
  25. */
  26. class ExcerptStructureExtension extends AbstractExtension implements ExportExtensionInterface
  27. {
  28. /**
  29. * name of structure extension.
  30. */
  31. public const EXCERPT_EXTENSION_NAME = 'excerpt';
  32. /**
  33. * will be filled with data in constructor
  34. * {@inheritdoc}
  35. */
  36. protected $properties = [];
  37. protected $name = self::EXCERPT_EXTENSION_NAME;
  38. protected $additionalPrefix = self::EXCERPT_EXTENSION_NAME;
  39. /**
  40. * @var ContentTypeManagerInterface
  41. */
  42. protected $contentTypeManager;
  43. /**
  44. * @var StructureManagerInterface
  45. */
  46. protected $structureManager;
  47. /**
  48. * @var ExportManagerInterface
  49. */
  50. protected $exportManager;
  51. /**
  52. * @var ImportManagerInterface
  53. */
  54. protected $importManager;
  55. /**
  56. * @var string
  57. */
  58. private $languageNamespace;
  59. /**
  60. * @var string
  61. */
  62. private $languageCode;
  63. /**
  64. * @var Factory
  65. */
  66. private $factory;
  67. public function __construct(
  68. StructureManagerInterface $structureManager,
  69. ContentTypeManagerInterface $contentTypeManager,
  70. ExportManagerInterface $exportManager,
  71. ImportManagerInterface $importManager,
  72. Factory $factory
  73. ) {
  74. $this->contentTypeManager = $contentTypeManager;
  75. $this->structureManager = $structureManager;
  76. $this->exportManager = $exportManager;
  77. $this->importManager = $importManager;
  78. $this->factory = $factory;
  79. }
  80. public function save(NodeInterface $node, $data, $webspaceKey, $languageCode)
  81. {
  82. $this->setLanguageCode($languageCode, 'i18n', null);
  83. $excerptStructure = $this->getExcerptStructure($languageCode);
  84. foreach ($excerptStructure->getProperties() as $property) {
  85. $contentType = $this->contentTypeManager->get($property->getContentTypeName());
  86. if (\array_key_exists($property->getName(), $data)) {
  87. $property->setValue($data[$property->getName()]);
  88. $contentType->write(
  89. $node,
  90. new TranslatedProperty(
  91. $property,
  92. $languageCode,
  93. $this->languageNamespace,
  94. $this->additionalPrefix
  95. ),
  96. null, // userid
  97. $webspaceKey,
  98. $languageCode,
  99. null // segmentkey
  100. );
  101. }
  102. }
  103. }
  104. public function load(NodeInterface $node, $webspaceKey, $languageCode)
  105. {
  106. $excerptStructure = $this->getExcerptStructure($languageCode);
  107. $data = [];
  108. foreach ($excerptStructure->getProperties() as $property) {
  109. $contentType = $this->contentTypeManager->get($property->getContentTypeName());
  110. $contentType->read(
  111. $node,
  112. new TranslatedProperty(
  113. $property,
  114. $languageCode,
  115. $this->languageNamespace,
  116. $this->additionalPrefix
  117. ),
  118. $webspaceKey,
  119. $languageCode,
  120. null // segmentkey
  121. );
  122. $data[$property->getName()] = $property->getValue();
  123. }
  124. return $data;
  125. }
  126. public function setLanguageCode($languageCode, $languageNamespace, $namespace)
  127. {
  128. // lazy load excerpt structure to avoid redeclaration of classes
  129. // should be done before parent::setLanguageCode because it uses the $thi<->properties
  130. // which will be set in initExcerptStructure
  131. $this->initProperties($languageCode);
  132. $this->languageCode = $languageCode;
  133. $this->languageNamespace = $languageNamespace;
  134. parent::setLanguageCode($languageCode, $languageNamespace, $namespace);
  135. }
  136. public function getContentData($container)
  137. {
  138. $container = new ExcerptValueContainer($container);
  139. $data = [];
  140. foreach ($this->getExcerptStructure()->getProperties() as $property) {
  141. if ($container->__isset($property->getName())) {
  142. $property->setValue($container->__get($property->getName()));
  143. $contentType = $this->contentTypeManager->get($property->getContentTypeName());
  144. $data[$property->getName()] = $contentType->getContentData($property);
  145. }
  146. }
  147. return $data;
  148. }
  149. public function getFieldMapping()
  150. {
  151. $mappings = parent::getFieldMapping();
  152. foreach ($this->getExcerptStructure()->getPropertiesByTagName('sulu.search.field') as $property) {
  153. $tag = $property->getTag('sulu.search.field');
  154. $tagAttributes = $tag->getAttributes();
  155. $mappings['excerpt' . \ucfirst($property->getName())] = [
  156. 'type' => isset($tagAttributes['type']) ? $tagAttributes['type'] : 'string',
  157. 'field' => $this->factory->createMetadataExpression(
  158. \sprintf('object.getExtensionsData()["excerpt"]["%s"]', $property->getName())
  159. ),
  160. ];
  161. }
  162. return $mappings;
  163. }
  164. /**
  165. * Returns and caches excerpt-structure.
  166. *
  167. * @param string $locale
  168. *
  169. * @return StructureInterface
  170. */
  171. private function getExcerptStructure($locale = null)
  172. {
  173. if (null === $locale) {
  174. $locale = $this->languageCode;
  175. }
  176. $excerptStructure = $this->structureManager->getStructure(self::EXCERPT_EXTENSION_NAME);
  177. $excerptStructure->setLanguageCode($locale);
  178. return $excerptStructure;
  179. }
  180. /**
  181. * Initiates structure and properties.
  182. *
  183. * @param string $locale
  184. */
  185. private function initProperties($locale)
  186. {
  187. // Reset the properties before new initialization.
  188. $this->properties = [];
  189. /** @var PropertyInterface $property */
  190. foreach ($this->getExcerptStructure($locale)->getProperties() as $property) {
  191. $this->properties[] = $property->getName();
  192. }
  193. }
  194. public function export($properties, $format = null)
  195. {
  196. $container = new ExcerptValueContainer($properties);
  197. $data = [];
  198. foreach ($this->getExcerptStructure()->getProperties() as $property) {
  199. if ($container->__isset($property->getName())) {
  200. $property->setValue($container->__get($property->getName()));
  201. $contentType = $this->contentTypeManager->get($property->getContentTypeName());
  202. if ($this->exportManager->hasExport($property->getContentTypeName(), $format)) {
  203. $options = $this->exportManager->getOptions($property->getContentTypeName(), $format);
  204. $data[$property->getName()] = [
  205. 'name' => $property->getName(),
  206. 'value' => $contentType->exportData($property->getValue()),
  207. 'type' => $property->getContentTypeName(),
  208. 'options' => $options,
  209. ];
  210. }
  211. }
  212. }
  213. return $data;
  214. }
  215. public function getImportPropertyNames()
  216. {
  217. $propertyNames = [];
  218. foreach ($this->getExcerptStructure()->getProperties() as $property) {
  219. $propertyNames[] = $property->getName();
  220. }
  221. return $propertyNames;
  222. }
  223. public function import(NodeInterface $node, $data, $webspaceKey, $languageCode, $format)
  224. {
  225. $this->setLanguageCode($languageCode, 'i18n', null);
  226. foreach ($this->getExcerptStructure()->getProperties() as $property) {
  227. $contentType = $this->contentTypeManager->get($property->getContentTypeName());
  228. if (isset($data[$property->getName()])
  229. && $this->importManager->hasImport($property->getContentTypeName(), $format)
  230. ) {
  231. /* @var ContentTypeExportInterface $contentType */
  232. $contentType->importData(
  233. $node,
  234. new TranslatedProperty(
  235. $property,
  236. $languageCode,
  237. $this->languageNamespace,
  238. $this->additionalPrefix
  239. ),
  240. $data[$property->getName()],
  241. null, // userid
  242. $webspaceKey,
  243. $languageCode,
  244. null // segmentkey
  245. );
  246. }
  247. }
  248. }
  249. }