Skip to content

Commit

Permalink
Save different versions of DimensionContent
Browse files Browse the repository at this point in the history
  • Loading branch information
redjanym committed Feb 26, 2024
1 parent c890e28 commit 2de29d9
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getIgnoredAttributes(object $object): array
'merged',
'dimension',
'resource',
'version',
];
}

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

return $normalizedData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ShadowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\TransitionEvent;

Expand All @@ -36,9 +37,17 @@ class PublishTransitionSubscriber implements EventSubscriberInterface
*/
private $contentCopier;

public function __construct(ContentCopierInterface $contentCopier)
{
/**
* @var DimensionContentRepositoryInterface
*/
private $dimensionContentRepository;

public function __construct(
ContentCopierInterface $contentCopier,
DimensionContentRepositoryInterface $dimensionContentRepository
) {
$this->contentCopier = $contentCopier;
$this->dimensionContentRepository = $dimensionContentRepository;
}

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

$publishLocales = $this->dimensionContentRepository->getLocales($contentRichEntity, $dimensionAttributes);

foreach ($publishLocales as $publishLocale) {
$this->contentCopier->copy(
$contentRichEntity,
\array_merge($dimensionAttributes, ['locale' => $publishLocale]),
$contentRichEntity,
\array_merge($dimensionAttributes, ['locale' => $publishLocale, 'version' => time()])
);
}

$shadowLocale = $dimensionContent instanceof ShadowInterface
? $dimensionContent->getShadowLocale()
: null;
Expand Down
6 changes: 6 additions & 0 deletions Content/Domain/Model/DimensionContentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface DimensionContentInterface
public const STAGE_DRAFT = 'draft';
public const STAGE_LIVE = 'live';

public const DEFAULT_VERSION = 0;

public static function getResourceKey(): string;

public function getLocale(): ?string;
Expand Down Expand Up @@ -59,6 +61,10 @@ public function getStage(): string;
*/
public function setStage(string $stage): void;

public function setVersion(int $version): void;

public function getVersion(): int;

/**
* @return T
*/
Expand Down
16 changes: 16 additions & 0 deletions Content/Domain/Model/DimensionContentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ trait DimensionContentTrait
*/
protected $stage = DimensionContentInterface::STAGE_DRAFT;

/**
* @var int
*/
protected $version = DimensionContentInterface::DEFAULT_VERSION;

/**
* @var bool
*/
Expand Down Expand Up @@ -114,6 +119,16 @@ public function getStage(): string
return $this->stage;
}

public function setVersion(int $version): void
{
$this->version = $version;
}

public function getVersion(): int
{
return $this->version;
}

public function isMerged(): bool
{
return $this->isMerged;
Expand All @@ -132,6 +147,7 @@ public static function getDefaultDimensionAttributes(): array
return [
'locale' => null,
'stage' => DimensionContentInterface::STAGE_DRAFT,
'version' => DimensionContentInterface::DEFAULT_VERSION,
];
}

Expand Down
10 changes: 10 additions & 0 deletions Content/Domain/Repository/DimensionContentRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ public function load(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): DimensionContentCollectionInterface;

/**
* @param mixed[] $dimensionAttributes
*
* @return string[]
*/
public function getLocales(

Check failure on line 40 in Content/Domain/Repository/DimensionContentRepositoryInterface.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Method Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface::getLocales() has parameter $contentRichEntity with generic interface Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface but does not specify its types: T

Check failure on line 40 in Content/Domain/Repository/DimensionContentRepositoryInterface.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Method Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface::getLocales() has parameter $contentRichEntity with generic interface Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface but does not specify its types: T
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): array;
}
26 changes: 26 additions & 0 deletions Content/Infrastructure/Doctrine/DimensionContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function load(
$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->where('dimensionContent.' . $mappingProperty . ' = :id')
->andWhere('dimensionContent.version = 0')
->setParameter('id', $contentRichEntity->getId());

$this->dimensionContentQueryEnhancer->addSelects(
Expand All @@ -84,4 +85,29 @@ public function load(
$dimensionContentClass
);
}

public function getLocales(

Check failure on line 89 in Content/Infrastructure/Doctrine/DimensionContentRepository.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Method Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\DimensionContentRepository::getLocales() has parameter $contentRichEntity with generic interface Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface but does not specify its types: T

Check failure on line 89 in Content/Infrastructure/Doctrine/DimensionContentRepository.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Method Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\DimensionContentRepository::getLocales() has parameter $contentRichEntity with generic interface Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface but does not specify its types: T
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): array {
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass(\get_class($contentRichEntity));
$mappingProperty = $this->contentMetadataInspector->getDimensionContentPropertyName(\get_class($contentRichEntity));

$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->select('dimensionContent.locale')
->where('IDENTITY(dimensionContent.' . $mappingProperty . ') = :id')
->andWhere('dimensionContent.locale IS NOT NULL')
->setParameter('id', $contentRichEntity->getId());

unset($dimensionAttributes['locale']);
foreach ($dimensionAttributes as $key => $value) {
$queryBuilder->andWhere('dimensionContent.' . $key . ' = :' . $key)
->setParameter(':' . $key, $value);
}

return \array_map(function($row) {

Check failure on line 109 in Content/Infrastructure/Doctrine/DimensionContentRepository.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Method Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\DimensionContentRepository::getLocales() should return array<string> but returns array.

Check failure on line 109 in Content/Infrastructure/Doctrine/DimensionContentRepository.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Method Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\DimensionContentRepository::getLocales() should return array<string> but returns array.
return $row['locale'];

Check failure on line 110 in Content/Infrastructure/Doctrine/DimensionContentRepository.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Cannot access offset 'locale' on mixed.

Check failure on line 110 in Content/Infrastructure/Doctrine/DimensionContentRepository.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Cannot access offset 'locale' on mixed.
}, $queryBuilder->getQuery()->getArrayResult());
}
}
2 changes: 2 additions & 0 deletions Content/Infrastructure/Doctrine/MetadataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
if ($reflection->implementsInterface(DimensionContentInterface::class)) {
$this->addField($metadata, 'stage', 'string', ['length' => 16, 'nullable' => false]);
$this->addField($metadata, 'locale', 'string', ['length' => 7, 'nullable' => true]);
$this->addField($metadata, 'version', 'integer', ['default' => 0, 'nullable' => true]);
$this->addField($metadata, 'ghostLocale', 'string', ['length' => 7, 'nullable' => true]);
$this->addField($metadata, 'availableLocales', 'json', ['nullable' => true, 'options' => ['jsonb' => true]]);
$this->addIndex($metadata, 'idx_dimension', ['stage', 'locale']);
$this->addIndex($metadata, 'idx_locale', ['locale']);
$this->addIndex($metadata, 'idx_version', ['version']);
$this->addIndex($metadata, 'idx_stage', ['stage']);
}

Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@

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

<tag name="kernel.event_subscriber"/>
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ public function cgetAction(Request $request): Response
$fieldDescriptors = $this->fieldDescriptorFactory->getFieldDescriptors(Example::RESOURCE_KEY);
/** @var DoctrineListBuilder $listBuilder */
$listBuilder = $this->listBuilderFactory->create(Example::class);
$listBuilder->where($fieldDescriptors['version'], (string) DimensionContentInterface::DEFAULT_VERSION);
$listBuilder->addSelectField($fieldDescriptors['locale']);
$listBuilder->addSelectField($fieldDescriptors['ghostLocale']);
$listBuilder->addSelectField($fieldDescriptors['version']);
$listBuilder->setParameter('locale', $request->query->get('locale'));
$this->restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,12 @@

<joins ref="ghostDimensionContent"/>
</property>

<property name="version" translation="sulu_admin.version" visibility="never">
<field-name>version</field-name>
<entity-name>dimensionContent</entity-name>

<joins ref="dimensionContent"/>
</property>
</properties>
</list>

0 comments on commit 2de29d9

Please sign in to comment.