Skip to content

Commit

Permalink
Add update date to settings tab
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler committed Jan 8, 2024
1 parent c890e28 commit 1858a71
Show file tree
Hide file tree
Showing 31 changed files with 160 additions and 19 deletions.
Expand Up @@ -54,6 +54,14 @@ private function setAuthorData(AuthorInterface $dimensionContent, array $data):
);
}

if (\array_key_exists('updated', $data)) {
$dimensionContent->setUpdated(
$data['updated'] && (\array_key_exists('isUpdated', $data) && $data['isUpdated'])
? new \DateTimeImmutable($data['updated'])
: null
);
}

if (\array_key_exists('authored', $data)) {
$dimensionContent->setAuthored(
$data['authored']
Expand Down
4 changes: 4 additions & 0 deletions Content/Application/ContentMerger/Merger/AuthorMerger.php
Expand Up @@ -35,6 +35,10 @@ public function merge(object $targetObject, object $sourceObject): void
$targetObject->setAuthor($author);
}

if ($updated = $sourceObject->getUpdated()) {
$targetObject->setUpdated($updated);
}

if ($authored = $sourceObject->getAuthored()) {
$targetObject->setAuthored($authored);
}
Expand Down
Expand Up @@ -24,6 +24,7 @@ public function enhance(object $object, array $normalizedData): array
}

$normalizedData['author'] = null !== $object->getAuthor() ? $object->getAuthor()->getId() : null;
$normalizedData['isUpdated'] = null !== $normalizedData['updated'];

return $normalizedData;
}
Expand Down
8 changes: 8 additions & 0 deletions Content/Domain/Model/AuthorInterface.php
Expand Up @@ -17,6 +17,14 @@

interface AuthorInterface
{
public function getIsUpdated(): ?bool;

public function setIsUpdated(bool $isUpdated): void;

public function getUpdated(): ?\DateTimeImmutable;

public function setUpdated(?\DateTimeImmutable $updated): void;

public function getAuthor(): ?ContactInterface;

public function setAuthor(?ContactInterface $author): void;
Expand Down
30 changes: 30 additions & 0 deletions Content/Domain/Model/AuthorTrait.php
Expand Up @@ -30,6 +30,36 @@ trait AuthorTrait
*/
private $authored;

/**
* @var bool
*/
private $isUpdated;

/**
* @var \DateTimeImmutable|null
*/
private $updated;

public function getIsUpdated(): ?bool
{
return $this->isUpdated;
}

public function setIsUpdated(bool $isUpdated): void
{
$this->isUpdated = $isUpdated;
}

public function getUpdated(): ?\DateTimeImmutable
{
return $this->updated;
}

public function setUpdated(?\DateTimeImmutable $updated): void
{
$this->updated = $updated;
}

public function getAuthor(): ?ContactInterface
{
return $this->author;
Expand Down
3 changes: 3 additions & 0 deletions Content/Domain/Model/TemplateTrait.php
Expand Up @@ -43,6 +43,9 @@ public function getTemplateData(): array
return $this->templateData;
}

/**
* @param mixed[] $templateData
*/
public function setTemplateData(array $templateData): void
{
$this->templateData = $templateData;
Expand Down
1 change: 1 addition & 0 deletions Content/Infrastructure/Doctrine/MetadataLoader.php
Expand Up @@ -118,6 +118,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void

if ($reflection->implementsInterface(AuthorInterface::class)) {
$this->addField($metadata, 'authored', 'datetime_immutable', ['nullable' => true]);
$this->addField($metadata, 'updated', 'datetime_immutable', ['nullable' => true]);
$this->addManyToOne($event, $metadata, 'author', ContactInterface::class, true);
}

Expand Down
6 changes: 5 additions & 1 deletion Content/Infrastructure/Sulu/Link/ContentLinkProvider.php
Expand Up @@ -89,6 +89,7 @@ public function preload(array $hrefs, $locale, $published = true): array
return null;
}

/** @var array{title?: string, name?: string} $data */
$data = $this->contentManager->normalize($resolvedDimensionContent);

return new LinkItem(
Expand All @@ -104,7 +105,10 @@ public function preload(array $hrefs, $locale, $published = true): array

/**
* @param B $dimensionContent
* @param mixed[] $data
* @param array{
* title?: string,
* name?: string
* } $data
*/
protected function getTitle(DimensionContentInterface $dimensionContent, array $data): ?string
{
Expand Down
Expand Up @@ -168,7 +168,7 @@ public function getMaxPage($scheme, $host): int
->getQuery()
->getSingleScalarResult();

return (int) \ceil($amount / $this->pageSize);
return (int) \ceil((int) $amount / $this->pageSize);
} catch (NoResultException|NonUniqueResultException $e) { // @codeCoverageIgnore
// TODO FIXME add testcase for this
return 0; // @codeCoverageIgnore
Expand Down
15 changes: 11 additions & 4 deletions Content/Infrastructure/Sulu/Teaser/ContentTeaserProvider.php
Expand Up @@ -113,6 +113,7 @@ function(ContentRichEntityInterface $contentRichEntity) use ($locale): ?Teaser {
return null;
}

/** @var array{title?: string|null, name?: string|null} $data */
$data = $this->contentManager->normalize($resolvedDimensionContent);

return $this->createTeaser($resolvedDimensionContent, $data, $locale);
Expand All @@ -125,7 +126,10 @@ function(ContentRichEntityInterface $contentRichEntity) use ($locale): ?Teaser {

/**
* @param B $dimensionContent
* @param array<string, mixed> $data
* @param array{
* title?: string|null,
* name?: string|null
* } $data
*/
protected function createTeaser(DimensionContentInterface $dimensionContent, array $data, string $locale): ?Teaser
{
Expand All @@ -139,10 +143,10 @@ protected function createTeaser(DimensionContentInterface $dimensionContent, arr
$title = $this->getTitle($dimensionContent, $data);

/** @var string $description */
$description = $this->getDescription($dimensionContent, $data); // @phpstan-ignore-line
$description = $this->getDescription($dimensionContent, $data);

/** @var string $moreText */
$moreText = $this->getMoreText($dimensionContent, $data); // @phpstan-ignore-line
$moreText = $this->getMoreText($dimensionContent, $data);

/** @var int $mediaId */
$mediaId = $this->getMediaId($dimensionContent, $data);
Expand All @@ -162,7 +166,10 @@ protected function createTeaser(DimensionContentInterface $dimensionContent, arr

/**
* @param B $dimensionContent
* @param mixed[] $data
* @param array{
* title?: string|null,
* name?: string|null
* } $data
*/
protected function getTitle(DimensionContentInterface $dimensionContent, array $data): ?string
{
Expand Down
16 changes: 16 additions & 0 deletions Resources/config/forms/content_settings_author.xml
Expand Up @@ -24,6 +24,22 @@
<title>sulu_content.author</title>
</meta>
</property>
<property name="isUpdated" type="checkbox">
<meta>
<title>sulu_content.is_updated</title>
</meta>

<params>
<param name="type" value="toggler"/>
<param name="default_value" value="false"/>
</params>
</property>

<property name="updated" type="datetime" visibleCondition="isUpdated">
<meta>
<title>sulu_content.updated_date</title>
</meta>
</property>
</properties>
</section>
</properties>
Expand Down
2 changes: 2 additions & 0 deletions Resources/translations/admin.de.json
Expand Up @@ -3,6 +3,8 @@
"sulu_content.excerpt": "Auszug & Taxonomien",
"sulu_content.content": "Inhalt",
"sulu_content.published": "Veröffentlicht am",
"sulu_content.is_updated": "Wurde aktualisiert",
"sulu_content.updated_date": "Datum der Aktualisierung",
"sulu_content.author": "Autor",
"sulu_content.authored_date": "Verfasst am",
"sulu_content.shadow_page": "Shadow Seite",
Expand Down
2 changes: 2 additions & 0 deletions Resources/translations/admin.en.json
Expand Up @@ -3,6 +3,8 @@
"sulu_content.excerpt": "Excerpt & Taxonomies",
"sulu_content.content": "Content",
"sulu_content.published": "Published on",
"sulu_content.is_updated": "Is updated",
"sulu_content.updated_date": "Updated Date",
"sulu_content.author": "Author",
"sulu_content.authored_date": "Authored Date",
"sulu_content.shadow_page": "Shadow page",
Expand Down
Expand Up @@ -185,7 +185,7 @@ public function countBy(array $filters = []): int

$queryBuilder->select('COUNT(DISTINCT example.id)');

return (int) $queryBuilder->getQuery()->getSingleScalarResult(); // @phpstan-ignore-line
return (int) $queryBuilder->getQuery()->getSingleScalarResult();
}

/**
Expand Down
6 changes: 6 additions & 0 deletions Tests/Functional/Integration/ExampleControllerTest.php
Expand Up @@ -66,6 +66,8 @@ public function testPostPublish(): int
'excerptMedia' => null,
'author' => null,
'authored' => '2020-05-08T00:00:00+00:00',
'isUpdated' => true,
'updated' => '2022-05-08T00:00:00+00:00',
'mainWebspace' => 'sulu-io',
]) ?: null);

Expand Down Expand Up @@ -136,6 +138,8 @@ public function testPost(): int
'excerptMedia' => null,
'mainWebspace' => 'sulu-io',
'authored' => '2020-05-08T00:00:00+00:00',
'isUpdated' => false,
'updated' => '2022-05-08T00:00:00+00:00', // Should be null.
]) ?: null);

$response = $this->client->getResponse();
Expand Down Expand Up @@ -226,6 +230,8 @@ public function testPut(int $id): void
'excerptMedia' => null,
'authored' => '2020-06-09T00:00:00+00:00',
'mainWebspace' => 'sulu-io2',
'isUpdated' => true,
'updated' => '2022-05-08T00:00:00+00:00',
]) ?: null);

$response = $this->client->getResponse();
Expand Down
4 changes: 3 additions & 1 deletion Tests/Functional/Integration/responses/example_get.json
Expand Up @@ -38,5 +38,7 @@
"template": "example-2",
"title": "Test Example",
"url": "/my-example",
"workflowPlace": "unpublished"
"workflowPlace": "unpublished",
"isUpdated": false,
"updated": null
}
Expand Up @@ -29,5 +29,7 @@
"stage": "draft",
"template": null,
"title": null,
"workflowPlace": null
"workflowPlace": null,
"isUpdated": false,
"updated": null
}
4 changes: 3 additions & 1 deletion Tests/Functional/Integration/responses/example_post.json
Expand Up @@ -38,5 +38,7 @@
"title": "Test Example",
"url": "/my-example",
"workflowPlace": "unpublished",
"mainWebspace": "sulu-io"
"mainWebspace": "sulu-io",
"isUpdated": false,
"updated": null
}
Expand Up @@ -38,5 +38,7 @@
"title": "Test Example",
"url": "/my-example",
"workflowPlace": "published",
"mainWebspace": "sulu-io"
"mainWebspace": "sulu-io",
"isUpdated": true,
"updated": "2022-05-08T00:00:00+00:00"
}
Expand Up @@ -40,5 +40,7 @@
"template": "example-2",
"title": "Test Example",
"url": "/my-example",
"workflowPlace": "unpublished"
"workflowPlace": "unpublished",
"isUpdated": false,
"updated": null
}
Expand Up @@ -38,5 +38,7 @@
"title": "Test Example",
"url": "\/my-example",
"workflowPlace": "unpublished",
"mainWebspace": "sulu-io"
"mainWebspace": "sulu-io",
"isUpdated": true,
"updated": "2022-05-08T00:00:00+00:00"
}
2 changes: 2 additions & 0 deletions Tests/Functional/Integration/responses/example_put.json
@@ -1,6 +1,8 @@
{
"author": null,
"authored": "2020-06-09T00:00:00+00:00",
"isUpdated": true,
"updated": "2022-05-08T00:00:00+00:00",
"article": "<p>Test Article 2</p>",
"availableLocales": [
"en",
Expand Down
Expand Up @@ -84,6 +84,8 @@ public function testMapData(): void
$data = [
'author' => 1,
'authored' => '2020-05-08T00:00:00+00:00',
'isUpdated' => true,
'updated' => '2024-05-08T00:00:00+00:00',
];

$example = new Example();
Expand All @@ -100,15 +102,19 @@ public function testMapData(): void

$this->assertSame($contact, $localizedDimensionContent->getAuthor());
$authored = $localizedDimensionContent->getAuthored();
$updated = $localizedDimensionContent->getUpdated();
$this->assertNotNull($authored);
$this->assertSame('2020-05-08T00:00:00+00:00', $authored->format('c'));
$this->assertSame('2024-05-08T00:00:00+00:00', $updated->format('c'));

Check failure on line 108 in Tests/Unit/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapperTest.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Cannot call method format() on DateTimeImmutable|null.
}

public function testMapDataNull(): void
{
$data = [
'author' => null,
'authored' => null,
'isUpdated' => false,
'updated' => '2024-05-08T00:00:00+00:00',
];

$example = new Example();
Expand All @@ -123,6 +129,7 @@ public function testMapDataNull(): void
$authorMapper = $this->createAuthorDataMapperInstance();
$authorMapper->map($unlocalizedDimensionContent, $localizedDimensionContent, $data);

$this->assertNull($localizedDimensionContent->getUpdated());
$this->assertNull($localizedDimensionContent->getAuthor());
$this->assertNull($localizedDimensionContent->getAuthored());
}
Expand Down
Expand Up @@ -90,7 +90,7 @@ public function testMapDefaultWebspace(): void

$webspace = new Webspace();
$webspace->setKey('default-webspace');
$this->webspaceCollection->setWebspaces([$webspace]);
$this->webspaceCollection->setWebspaces(['default-webspace' => $webspace]);

$authorMapper = $this->createWebspaceDataMapperInstance();
$authorMapper->map($unlocalizedDimensionContent, $localizedDimensionContent, $data);
Expand Down
Expand Up @@ -63,14 +63,18 @@ public function testMergeSet(): void

$contact = $this->prophesize(ContactInterface::class);
$authoredDate = new \DateTimeImmutable('2020-05-08T00:00:00+00:00');
$updateDate = new \DateTimeImmutable('2020-05-08T00:00:00+00:00');

$source = $this->prophesize(DimensionContentInterface::class);
$source->willImplement(AuthorInterface::class);
$source->getUpdated()->willReturn($updateDate)->shouldBeCalled();
$source->getAuthor()->willReturn($contact->reveal())->shouldBeCalled();
$source->getAuthored()->willReturn($authoredDate)->shouldBeCalled();

$target = $this->prophesize(DimensionContentInterface::class);
$target->willImplement(AuthorInterface::class);
$target->setIsUpdated(true);
$target->setUpdated($updateDate)->shouldBeCalled();
$target->setAuthor($contact->reveal())->shouldBeCalled();
$target->setAuthored($authoredDate)->shouldBeCalled();

Expand All @@ -83,11 +87,14 @@ public function testMergeNotSet(): void

$source = $this->prophesize(DimensionContentInterface::class);
$source->willImplement(AuthorInterface::class);
$source->getUpdated()->willReturn(null)->shouldBeCalled();
$source->getAuthor()->willReturn(null)->shouldBeCalled();
$source->getAuthored()->willReturn(null)->shouldBeCalled();

$target = $this->prophesize(DimensionContentInterface::class);
$target->willImplement(AuthorInterface::class);
$target->setIsUpdated(false)->shouldNotBeCalled();
$target->setUpdated(Argument::any())->shouldNotBeCalled();
$target->setAuthor(Argument::any())->shouldNotBeCalled();
$target->setAuthored(Argument::any())->shouldNotBeCalled();

Expand Down

0 comments on commit 1858a71

Please sign in to comment.