vendor/pimcore/pimcore/models/Document/TargetingDocument.php line 121

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Pimcore
  5. *
  6. * This source file is available under two different licenses:
  7. * - GNU General Public License version 3 (GPLv3)
  8. * - Pimcore Commercial License (PCL)
  9. * Full copyright and license information is available in
  10. * LICENSE.md which is distributed with this source code.
  11. *
  12. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13. * @license http://www.pimcore.org/license GPLv3 and PCL
  14. */
  15. namespace Pimcore\Model\Document;
  16. use Pimcore\Model\Document\Targeting\TargetingDocumentInterface;
  17. /**
  18. * @method \Pimcore\Model\Document\Targeting\TargetingDocumentDaoInterface getDao()
  19. */
  20. abstract class TargetingDocument extends PageSnippet implements TargetingDocumentInterface
  21. {
  22. /**
  23. * @internal
  24. *
  25. * @var int
  26. */
  27. private $useTargetGroup;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function setUseTargetGroup(int $useTargetGroup = null)
  32. {
  33. $this->useTargetGroup = $useTargetGroup;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getUseTargetGroup()
  39. {
  40. return $this->useTargetGroup;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getTargetGroupEditablePrefix(int $targetGroupId = null): string
  46. {
  47. $prefix = '';
  48. if (!$targetGroupId) {
  49. $targetGroupId = $this->getUseTargetGroup();
  50. }
  51. if ($targetGroupId) {
  52. $prefix = self::TARGET_GROUP_EDITABLE_PREFIX . $targetGroupId . self::TARGET_GROUP_EDITABLE_SUFFIX;
  53. }
  54. return $prefix;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getTargetGroupEditableName(string $name): string
  60. {
  61. if (!$this->getUseTargetGroup()) {
  62. return $name;
  63. }
  64. $prefix = $this->getTargetGroupEditablePrefix();
  65. if (!preg_match('/^' . preg_quote($prefix, '/') . '/', $name)) {
  66. $name = $prefix . $name;
  67. }
  68. return $name;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function hasTargetGroupSpecificEditables(): bool
  74. {
  75. return $this->getDao()->hasTargetGroupSpecificEditables();
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getTargetGroupSpecificEditableNames(): array
  81. {
  82. return $this->getDao()->getTargetGroupSpecificEditableNames();
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function setEditable(Editable $editable)
  88. {
  89. if ($this->getUseTargetGroup()) {
  90. $name = $this->getTargetGroupEditableName($editable->getName());
  91. $editable->setName($name);
  92. }
  93. return parent::setEditable($editable);
  94. }
  95. /**
  96. * Get an editable with the given key/name
  97. *
  98. * @param string $name
  99. *
  100. * @return Editable|null
  101. */
  102. public function getEditable($name)
  103. {
  104. // check if a target group is requested for this page, if yes deliver a different version of the editable (prefixed)
  105. if ($this->getUseTargetGroup()) {
  106. $targetGroupEditableName = $this->getTargetGroupEditableName($name);
  107. if ($editable = parent::getEditable($targetGroupEditableName)) {
  108. return $editable;
  109. } else {
  110. // if there's no dedicated content for this target group, inherit from the "original" content (unprefixed)
  111. // and mark it as inherited so it is clear in the ui that the content is not specific to the selected target group
  112. // replace all occurrences of the target group prefix, this is needed because of block-prefixes
  113. $inheritedName = str_replace($this->getTargetGroupEditablePrefix(), '', $name);
  114. $inheritedEditable = parent::getEditable($inheritedName);
  115. if ($inheritedEditable) {
  116. $inheritedEditable = clone $inheritedEditable;
  117. $inheritedEditable->setDao(null);
  118. $inheritedEditable->setName($targetGroupEditableName);
  119. $inheritedEditable->setInherited(true);
  120. $this->setEditable($inheritedEditable);
  121. return $inheritedEditable;
  122. }
  123. }
  124. }
  125. // delegate to default
  126. return parent::getEditable($name);
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function __sleep()
  132. {
  133. $finalVars = [];
  134. $parentVars = parent::__sleep();
  135. $blockedVars = ['useTargetGroup'];
  136. foreach ($parentVars as $key) {
  137. if (!in_array($key, $blockedVars)) {
  138. $finalVars[] = $key;
  139. }
  140. }
  141. return $finalVars;
  142. }
  143. }