Skip to content

Commit f741163

Browse files
authored
[Translations] Add seperate CREATE endpoint (#991)
* feat: add translation create endpoint * Apply php-cs-fixer changes
1 parent 763e051 commit f741163

7 files changed

Lines changed: 193 additions & 13 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Pimcore
6+
*
7+
* This source file is available under two different licenses:
8+
* - GNU General Public License version 3 (GPLv3)
9+
* - Pimcore Commercial License (PCL)
10+
* Full copyright and license information is available in
11+
* LICENSE.md which is distributed with this source code.
12+
*
13+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
14+
* @license http://www.pimcore.org/license GPLv3 and PCL
15+
*/
16+
17+
namespace Pimcore\Bundle\StudioBackendBundle\Translation\Controller;
18+
19+
use OpenApi\Attributes\Post;
20+
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
21+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
22+
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
23+
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse;
24+
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
25+
use Pimcore\Bundle\StudioBackendBundle\Translation\Attribute\Request\TranslationRequestBody;
26+
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\CreateTranslation;
27+
use Pimcore\Bundle\StudioBackendBundle\Translation\Service\TranslatorServiceInterface;
28+
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
29+
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions;
30+
use Symfony\Component\HttpFoundation\Response;
31+
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
32+
use Symfony\Component\Routing\Attribute\Route;
33+
use Symfony\Component\Security\Http\Attribute\IsGranted;
34+
use Symfony\Component\Serializer\SerializerInterface;
35+
36+
/**
37+
* @internal
38+
*/
39+
final class CreateController extends AbstractApiController
40+
{
41+
private const string ROUTE = '/translations/create';
42+
43+
public function __construct(
44+
SerializerInterface $serializer,
45+
private readonly TranslatorServiceInterface $translatorService
46+
) {
47+
parent::__construct($serializer);
48+
}
49+
50+
/**
51+
* @throws ElementExistsException
52+
*/
53+
#[Route(self::ROUTE, name: 'pimcore_studio_api_translations_create', methods: ['POST'])]
54+
#[IsGranted(UserPermissions::TRANSLATIONS->value)]
55+
#[Post(
56+
path: self::PREFIX . self::ROUTE,
57+
operationId: 'translation_create',
58+
description: 'translation_create_description',
59+
summary: 'translation_create_summary',
60+
tags: [Tags::Translation->name]
61+
)]
62+
#[TranslationRequestBody(CreateTranslation::class)]
63+
#[SuccessResponse(
64+
description: 'translation_create_success_response'
65+
)]
66+
#[DefaultResponses([
67+
HttpResponseCodes::CONFLICT,
68+
HttpResponseCodes::NOT_FOUND,
69+
HttpResponseCodes::UNAUTHORIZED,
70+
])]
71+
public function createTranslations(
72+
#[MapRequestPayload] CreateTranslation $translation
73+
): Response {
74+
$this->translatorService->createTranslations($translation);
75+
76+
return new Response();
77+
}
78+
}

src/Translation/Repository/TranslationRepository.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
namespace Pimcore\Bundle\StudioBackendBundle\Translation\Repository;
1818

1919
use Pimcore\Bundle\StaticResolverBundle\Lib\Tools\AdminResolverInterface;
20+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
2021
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidLocaleException;
2122
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
2223
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\TranslationData;
2324
use Pimcore\Bundle\StudioBackendBundle\Translation\Service\TranslatorServiceInterface;
2425
use Pimcore\Model\Translation;
2526
use Pimcore\Model\Translation\Listing;
2627
use function in_array;
28+
use function sprintf;
2729

2830
/**
2931
* @internal
@@ -53,23 +55,48 @@ public function getAllTranslations(string $locale): array
5355
/**
5456
* {@inheritdoc}
5557
*/
56-
public function createTranslations(array $translationData, string $locale): void
58+
public function createTranslations(string $key, string $type): void
59+
{
60+
$languages = $this->adminResolver->getLanguages();
61+
62+
if ($this->getTranslationByKey($key) !== null) {
63+
throw new ElementExistsException(
64+
sprintf("Translation with key '%s' already exists", $key),
65+
);
66+
}
67+
68+
$t = new Translation();
69+
$t->setDomain(TranslatorServiceInterface::DOMAIN);
70+
$t->setKey($key);
71+
$t->setType($type);
72+
$t->setCreationDate(time());
73+
$t->setModificationDate(time());
74+
$this->setNewValues($t, $languages);
75+
$t->save();
76+
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public function updateTranslations(array $translationData, string $locale): void
5783
{
5884
$languages = $this->adminResolver->getLanguages();
5985
$this->validateLocale($locale, $languages);
6086

6187
/** @var TranslationData $translation */
6288
foreach ($translationData as $translation) {
89+
if ($this->getTranslationByKey($translation->getKey()) === null) {
90+
throw new NotFoundException('translation', $translation->getKey(), 'key');
91+
}
92+
6393
$t = new Translation();
6494
$t->setDomain(TranslatorServiceInterface::DOMAIN);
6595
$t->setKey($translation->getKey());
6696
$t->setType($translation->getType());
6797
$t->addTranslation($locale, $translation->getTranslation());
6898
$t->setCreationDate(time());
6999
$t->setModificationDate(time());
70-
if ($this->getTranslationByKey($translation->getKey()) === null) {
71-
$this->setNewValues($t, $languages, $locale);
72-
}
73100
$t->save();
74101
}
75102
}
@@ -94,14 +121,10 @@ private function getTranslationList(): Listing
94121
return $list;
95122
}
96123

97-
private function setNewValues(Translation $translation, array $languages, string $locale): void
124+
private function setNewValues(Translation $translation, array $languages): void
98125
{
99-
$translation->setCreationDate(time());
100-
101126
foreach ($languages as $language) {
102-
if ($language !== $locale) {
103-
$translation->addTranslation($language, '');
104-
}
127+
$translation->addTranslation($language, '');
105128
}
106129
}
107130

src/Translation/Repository/TranslationRepositoryInterface.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace Pimcore\Bundle\StudioBackendBundle\Translation\Repository;
1818

19+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
1920
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidLocaleException;
2021
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\TranslationData;
2122
use Pimcore\Model\Translation;
@@ -32,12 +33,17 @@ interface TranslationRepositoryInterface
3233
*/
3334
public function getAllTranslations(string $locale): array;
3435

36+
/**
37+
* @throws ElementExistsException
38+
*/
39+
public function createTranslations(string $key, string $type): void;
40+
3541
/**
3642
* @param array<TranslationData> $translationData
3743
*
3844
* @throws InvalidLocaleException
3945
*/
40-
public function createTranslations(array $translationData, string $locale): void;
46+
public function updateTranslations(array $translationData, string $locale): void;
4147

4248
public function deleteTranslation(string $key): void;
4349
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Pimcore
6+
*
7+
* This source file is available under two different licenses:
8+
* - GNU General Public License version 3 (GPLv3)
9+
* - Pimcore Commercial License (PCL)
10+
* Full copyright and license information is available in
11+
* LICENSE.md which is distributed with this source code.
12+
*
13+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
14+
* @license http://www.pimcore.org/license GPLv3 and PCL
15+
*/
16+
17+
namespace Pimcore\Bundle\StudioBackendBundle\Translation\Schema;
18+
19+
use OpenApi\Attributes\Property;
20+
use OpenApi\Attributes\Schema;
21+
22+
/**
23+
* @internal
24+
*/
25+
#[Schema(
26+
schema: 'CreateTranslation',
27+
title: 'Translation Create',
28+
description: 'Translation Crete Scheme for API',
29+
required: ['key', 'type'],
30+
type: 'object'
31+
)]
32+
final readonly class CreateTranslation
33+
{
34+
public function __construct(
35+
#[Property(description: 'Key', type: 'string', example: 'my_translation_key')]
36+
private string $key,
37+
#[Property(description: 'Type', type: 'string', example: 'simple')]
38+
private string $type = 'simple',
39+
) {
40+
}
41+
42+
public function getKey(): string
43+
{
44+
return $this->key;
45+
}
46+
47+
public function getType(): string
48+
{
49+
return $this->type;
50+
}
51+
}

src/Translation/Service/TranslatorService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use InvalidArgumentException;
2020
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidLocaleException;
2121
use Pimcore\Bundle\StudioBackendBundle\Translation\Repository\TranslationRepositoryInterface;
22+
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\CreateTranslation;
2223
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\Translation;
2324
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\UpdateTranslation;
2425
use Symfony\Component\Translation\TranslatorBagInterface;
@@ -41,9 +42,14 @@ public function __construct(
4142
$this->translatorBag = $this->getTranslatorBag();
4243
}
4344

45+
public function createTranslations(CreateTranslation $translation): void
46+
{
47+
$this->translationRepository->createTranslations($translation->getKey(), $translation->getType());
48+
}
49+
4450
public function updateTranslations(UpdateTranslation $translation): void
4551
{
46-
$this->translationRepository->createTranslations($translation->getTranslationData(), $translation->getLocale());
52+
$this->translationRepository->updateTranslations($translation->getTranslationData(), $translation->getLocale());
4753
}
4854

4955
/**

src/Translation/Service/TranslatorServiceInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
namespace Pimcore\Bundle\StudioBackendBundle\Translation\Service;
1818

19+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
1920
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidLocaleException;
21+
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\CreateTranslation;
2022
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\Translation;
2123
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\UpdateTranslation;
2224

@@ -27,6 +29,14 @@ interface TranslatorServiceInterface
2729
{
2830
public const string DOMAIN = 'studio';
2931

32+
/**
33+
* @throws ElementExistsException
34+
*/
35+
public function createTranslations(CreateTranslation $translation): void;
36+
37+
/**
38+
* @throws InvalidLocaleException
39+
*/
3040
public function updateTranslations(UpdateTranslation $translation): void;
3141

3242
/**

translations/studio_api_docs.en.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,13 +618,19 @@ thumbnail_video_get_collection_description: |
618618
Get collection of all thumbnails for videos.
619619
thumbnail_video_get_collection_success_response: All video thumbnails
620620
thumbnail_video_get_collection_summary: Get collection of thumbnails for videos
621+
translation_create_description: |
622+
Add new translation entries for given translation key
623+
translation_create_success_response: Successfully created translations
624+
translation_create_summary: Create translations
621625
translation_get_collection_description: Get translations for given keys and locale
622626
translation_get_collection_success_response: Key value pairs for given keys and locale
623627
translation_get_collection_summary: Get translations
624628
translation_delete_by_key_description: Delete translations for given translation key
625629
translation_delete_by_key_success_response: Successfully deleted translations
626630
translation_delete_by_key_summary: Delete translations
627-
translation_update_description: Update translations for given translation data and locale
631+
translation_update_description: |
632+
Update translations for given translation data and locale.The <strong>{key}</strong> must be an existing translation key. <br>
633+
See the full description of updatable translation data fields with the schema <strong>TranslationData</strong>
628634
translation_update_success_response: Successfully updated translations
629635
translation_update_summary: Update translations
630636
user_clone_by_id_success_response: Node of the cloned user.

0 commit comments

Comments
 (0)