Skip to content

Commit 2de29d9

Browse files
committed
Save different versions of DimensionContent
1 parent c890e28 commit 2de29d9

File tree

10 files changed

+94
-2
lines changed

10 files changed

+94
-2
lines changed

Content/Application/ContentNormalizer/Normalizer/DimensionContentNormalizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function getIgnoredAttributes(object $object): array
2828
'merged',
2929
'dimension',
3030
'resource',
31+
'version',
3132
];
3233
}
3334

@@ -40,6 +41,7 @@ public function enhance(object $object, array $normalizedData): array
4041
$normalizedData['id'] = $object->getResource()->getId();
4142
$normalizedData['locale'] = $object->getLocale();
4243
$normalizedData['stage'] = $object->getStage();
44+
$normalizedData['version'] = $object->getVersion();
4345

4446
return $normalizedData;
4547
}

Content/Application/ContentWorkflow/Subscriber/PublishTransitionSubscriber.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ShadowInterface;
2222
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
2323
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
24+
use Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface;
2425
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2526
use Symfony\Component\Workflow\Event\TransitionEvent;
2627

@@ -36,9 +37,17 @@ class PublishTransitionSubscriber implements EventSubscriberInterface
3637
*/
3738
private $contentCopier;
3839

39-
public function __construct(ContentCopierInterface $contentCopier)
40-
{
40+
/**
41+
* @var DimensionContentRepositoryInterface
42+
*/
43+
private $dimensionContentRepository;
44+
45+
public function __construct(
46+
ContentCopierInterface $contentCopier,
47+
DimensionContentRepositoryInterface $dimensionContentRepository
48+
) {
4149
$this->contentCopier = $contentCopier;
50+
$this->dimensionContentRepository = $dimensionContentRepository;
4251
}
4352

4453
public function onPublish(TransitionEvent $transitionEvent): void
@@ -77,6 +86,17 @@ public function onPublish(TransitionEvent $transitionEvent): void
7786
$targetDimensionAttributes = $dimensionAttributes;
7887
$targetDimensionAttributes['stage'] = DimensionContentInterface::STAGE_LIVE;
7988

89+
$publishLocales = $this->dimensionContentRepository->getLocales($contentRichEntity, $dimensionAttributes);
90+
91+
foreach ($publishLocales as $publishLocale) {
92+
$this->contentCopier->copy(
93+
$contentRichEntity,
94+
\array_merge($dimensionAttributes, ['locale' => $publishLocale]),
95+
$contentRichEntity,
96+
\array_merge($dimensionAttributes, ['locale' => $publishLocale, 'version' => time()])
97+
);
98+
}
99+
80100
$shadowLocale = $dimensionContent instanceof ShadowInterface
81101
? $dimensionContent->getShadowLocale()
82102
: null;

Content/Domain/Model/DimensionContentInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ interface DimensionContentInterface
2121
public const STAGE_DRAFT = 'draft';
2222
public const STAGE_LIVE = 'live';
2323

24+
public const DEFAULT_VERSION = 0;
25+
2426
public static function getResourceKey(): string;
2527

2628
public function getLocale(): ?string;
@@ -59,6 +61,10 @@ public function getStage(): string;
5961
*/
6062
public function setStage(string $stage): void;
6163

64+
public function setVersion(int $version): void;
65+
66+
public function getVersion(): int;
67+
6268
/**
6369
* @return T
6470
*/

Content/Domain/Model/DimensionContentTrait.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ trait DimensionContentTrait
3535
*/
3636
protected $stage = DimensionContentInterface::STAGE_DRAFT;
3737

38+
/**
39+
* @var int
40+
*/
41+
protected $version = DimensionContentInterface::DEFAULT_VERSION;
42+
3843
/**
3944
* @var bool
4045
*/
@@ -114,6 +119,16 @@ public function getStage(): string
114119
return $this->stage;
115120
}
116121

122+
public function setVersion(int $version): void
123+
{
124+
$this->version = $version;
125+
}
126+
127+
public function getVersion(): int
128+
{
129+
return $this->version;
130+
}
131+
117132
public function isMerged(): bool
118133
{
119134
return $this->isMerged;
@@ -132,6 +147,7 @@ public static function getDefaultDimensionAttributes(): array
132147
return [
133148
'locale' => null,
134149
'stage' => DimensionContentInterface::STAGE_DRAFT,
150+
'version' => DimensionContentInterface::DEFAULT_VERSION,
135151
];
136152
}
137153

Content/Domain/Repository/DimensionContentRepositoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,14 @@ public function load(
3131
ContentRichEntityInterface $contentRichEntity,
3232
array $dimensionAttributes
3333
): DimensionContentCollectionInterface;
34+
35+
/**
36+
* @param mixed[] $dimensionAttributes
37+
*
38+
* @return string[]
39+
*/
40+
public function getLocales(
41+
ContentRichEntityInterface $contentRichEntity,
42+
array $dimensionAttributes
43+
): array;
3444
}

Content/Infrastructure/Doctrine/DimensionContentRepository.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function load(
6666
$queryBuilder = $this->entityManager->createQueryBuilder()
6767
->from($dimensionContentClass, 'dimensionContent')
6868
->where('dimensionContent.' . $mappingProperty . ' = :id')
69+
->andWhere('dimensionContent.version = 0')
6970
->setParameter('id', $contentRichEntity->getId());
7071

7172
$this->dimensionContentQueryEnhancer->addSelects(
@@ -84,4 +85,29 @@ public function load(
8485
$dimensionContentClass
8586
);
8687
}
88+
89+
public function getLocales(
90+
ContentRichEntityInterface $contentRichEntity,
91+
array $dimensionAttributes
92+
): array {
93+
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass(\get_class($contentRichEntity));
94+
$mappingProperty = $this->contentMetadataInspector->getDimensionContentPropertyName(\get_class($contentRichEntity));
95+
96+
$queryBuilder = $this->entityManager->createQueryBuilder()
97+
->from($dimensionContentClass, 'dimensionContent')
98+
->select('dimensionContent.locale')
99+
->where('IDENTITY(dimensionContent.' . $mappingProperty . ') = :id')
100+
->andWhere('dimensionContent.locale IS NOT NULL')
101+
->setParameter('id', $contentRichEntity->getId());
102+
103+
unset($dimensionAttributes['locale']);
104+
foreach ($dimensionAttributes as $key => $value) {
105+
$queryBuilder->andWhere('dimensionContent.' . $key . ' = :' . $key)
106+
->setParameter(':' . $key, $value);
107+
}
108+
109+
return \array_map(function($row) {
110+
return $row['locale'];
111+
}, $queryBuilder->getQuery()->getArrayResult());
112+
}
87113
}

Content/Infrastructure/Doctrine/MetadataLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
5353
if ($reflection->implementsInterface(DimensionContentInterface::class)) {
5454
$this->addField($metadata, 'stage', 'string', ['length' => 16, 'nullable' => false]);
5555
$this->addField($metadata, 'locale', 'string', ['length' => 7, 'nullable' => true]);
56+
$this->addField($metadata, 'version', 'integer', ['default' => 0, 'nullable' => true]);
5657
$this->addField($metadata, 'ghostLocale', 'string', ['length' => 7, 'nullable' => true]);
5758
$this->addField($metadata, 'availableLocales', 'json', ['nullable' => true, 'options' => ['jsonb' => true]]);
5859
$this->addIndex($metadata, 'idx_dimension', ['stage', 'locale']);
5960
$this->addIndex($metadata, 'idx_locale', ['locale']);
61+
$this->addIndex($metadata, 'idx_version', ['version']);
6062
$this->addIndex($metadata, 'idx_stage', ['stage']);
6163
}
6264

Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163

164164
<service id="sulu_content.publish_transition_subscriber" class="Sulu\Bundle\ContentBundle\Content\Application\ContentWorkflow\Subscriber\PublishTransitionSubscriber">
165165
<argument type="service" id="sulu_content.content_copier"/>
166+
<argument type="service" id="sulu_content.dimension_content_repository"/>
166167

167168
<tag name="kernel.event_subscriber"/>
168169
</service>

Tests/Application/ExampleTestBundle/Controller/ExampleController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ public function cgetAction(Request $request): Response
9797
$fieldDescriptors = $this->fieldDescriptorFactory->getFieldDescriptors(Example::RESOURCE_KEY);
9898
/** @var DoctrineListBuilder $listBuilder */
9999
$listBuilder = $this->listBuilderFactory->create(Example::class);
100+
$listBuilder->where($fieldDescriptors['version'], (string) DimensionContentInterface::DEFAULT_VERSION);
100101
$listBuilder->addSelectField($fieldDescriptors['locale']);
101102
$listBuilder->addSelectField($fieldDescriptors['ghostLocale']);
103+
$listBuilder->addSelectField($fieldDescriptors['version']);
102104
$listBuilder->setParameter('locale', $request->query->get('locale'));
103105
$this->restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);
104106

Tests/Application/ExampleTestBundle/Resources/config/lists/examples.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,12 @@
6666

6767
<joins ref="ghostDimensionContent"/>
6868
</property>
69+
70+
<property name="version" translation="sulu_admin.version" visibility="never">
71+
<field-name>version</field-name>
72+
<entity-name>dimensionContent</entity-name>
73+
74+
<joins ref="dimensionContent"/>
75+
</property>
6976
</properties>
7077
</list>

0 commit comments

Comments
 (0)