Skip to content

Commit 242c4fe

Browse files
authored
allow creation of multiple entries in 1 request (#993)
1 parent f741163 commit 242c4fe

5 files changed

Lines changed: 97 additions & 38 deletions

File tree

src/Translation/Repository/TranslationRepository.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
2121
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidLocaleException;
2222
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
23+
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\CreateTranslationData;
2324
use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\TranslationData;
2425
use Pimcore\Bundle\StudioBackendBundle\Translation\Service\TranslatorServiceInterface;
2526
use Pimcore\Model\Translation;
@@ -55,25 +56,20 @@ public function getAllTranslations(string $locale): array
5556
/**
5657
* {@inheritdoc}
5758
*/
58-
public function createTranslations(string $key, string $type): void
59+
public function createTranslations(array $translationData): void
5960
{
6061
$languages = $this->adminResolver->getLanguages();
6162

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();
63+
/** @var CreateTranslationData $translation */
64+
foreach ($translationData as $translation) {
65+
if ($this->getTranslationByKey($translation->getKey()) !== null) {
66+
throw new ElementExistsException(
67+
sprintf("Translation with key '%s' already exists", $translation->getKey()),
68+
);
69+
}
7670

71+
$this->createTranslationEntry($translation->getKey(), $translation->getType(), $languages);
72+
}
7773
}
7874

7975
/**
@@ -86,18 +82,15 @@ public function updateTranslations(array $translationData, string $locale): void
8682

8783
/** @var TranslationData $translation */
8884
foreach ($translationData as $translation) {
89-
if ($this->getTranslationByKey($translation->getKey()) === null) {
85+
$entry = $this->getTranslationByKey($translation->getKey());
86+
if ($entry === null) {
9087
throw new NotFoundException('translation', $translation->getKey(), 'key');
9188
}
9289

93-
$t = new Translation();
94-
$t->setDomain(TranslatorServiceInterface::DOMAIN);
95-
$t->setKey($translation->getKey());
96-
$t->setType($translation->getType());
97-
$t->addTranslation($locale, $translation->getTranslation());
98-
$t->setCreationDate(time());
99-
$t->setModificationDate(time());
100-
$t->save();
90+
$entry->addTranslation($locale, $translation->getTranslation());
91+
$entry->setType($translation->getType());
92+
$entry->setModificationDate(time());
93+
$entry->save();
10194
}
10295
}
10396

@@ -111,6 +104,18 @@ public function deleteTranslation(string $key): void
111104
$translation->delete();
112105
}
113106

107+
private function createTranslationEntry(string $key, string $type, array $languages): void
108+
{
109+
$t = new Translation();
110+
$t->setDomain(TranslatorServiceInterface::DOMAIN);
111+
$t->setKey($key);
112+
$t->setType($type);
113+
$t->setCreationDate(time());
114+
$t->setModificationDate(time());
115+
$this->setNewValues($t, $languages);
116+
$t->save();
117+
}
118+
114119
private function getTranslationList(): Listing
115120
{
116121
$list = new Translation\Listing();

src/Translation/Repository/TranslationRepositoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getAllTranslations(string $locale): array;
3636
/**
3737
* @throws ElementExistsException
3838
*/
39-
public function createTranslations(string $key, string $type): void;
39+
public function createTranslations(array $translationData): void;
4040

4141
/**
4242
* @param array<TranslationData> $translationData

src/Translation/Schema/CreateTranslation.php

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

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

19+
use OpenApi\Attributes\Items;
1920
use OpenApi\Attributes\Property;
2021
use OpenApi\Attributes\Schema;
2122

@@ -26,26 +27,22 @@
2627
schema: 'CreateTranslation',
2728
title: 'Translation Create',
2829
description: 'Translation Crete Scheme for API',
29-
required: ['key', 'type'],
30+
required: ['translationData'],
3031
type: 'object'
3132
)]
3233
final readonly class CreateTranslation
3334
{
3435
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',
36+
#[Property(description: 'Translation Data', type: 'array', items: new Items(ref: CreateTranslationData::class))]
37+
private array $translationData = []
3938
) {
4039
}
4140

42-
public function getKey(): string
41+
/**
42+
* @return array<CreateTranslationData>
43+
*/
44+
public function getTranslationData(): array
4345
{
44-
return $this->key;
45-
}
46-
47-
public function getType(): string
48-
{
49-
return $this->type;
46+
return $this->translationData;
5047
}
5148
}
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: 'CreateTranslationData',
27+
title: 'Translation Data for create',
28+
description: 'Translation Data Scheme for create endpoint of the API',
29+
required: ['key', 'type'],
30+
type: 'object'
31+
)]
32+
final readonly class CreateTranslationData
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
@@ -42,11 +42,17 @@ public function __construct(
4242
$this->translatorBag = $this->getTranslatorBag();
4343
}
4444

45+
/**
46+
* {@inheritdoc}
47+
*/
4548
public function createTranslations(CreateTranslation $translation): void
4649
{
47-
$this->translationRepository->createTranslations($translation->getKey(), $translation->getType());
50+
$this->translationRepository->createTranslations($translation->getTranslationData());
4851
}
4952

53+
/**
54+
* {@inheritdoc}
55+
*/
5056
public function updateTranslations(UpdateTranslation $translation): void
5157
{
5258
$this->translationRepository->updateTranslations($translation->getTranslationData(), $translation->getLocale());

0 commit comments

Comments
 (0)