|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* (c) shopware AG <[email protected]> |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace ShopwarePlugins\Connect\Components\Translations; |
|
9
|
|
|
|
|
10
|
|
|
use ShopwarePlugins\Connect\Components\Config; |
|
11
|
|
|
use ShopwarePlugins\Connect\Components\Gateway\ProductTranslationsGateway; |
|
12
|
|
|
use Shopware\Components\Model\ModelManager; |
|
13
|
|
|
use Shopware\Connect\Struct\Translation; |
|
14
|
|
|
|
|
15
|
|
|
class ProductTranslator implements ProductTranslatorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \ShopwarePlugins\Connect\Components\Config |
|
19
|
|
|
*/ |
|
20
|
|
|
private $config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \ShopwarePlugins\Connect\Components\Gateway\ProductTranslationsGateway |
|
24
|
|
|
*/ |
|
25
|
|
|
private $productTranslationsGateway; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Shopware\Components\Model\ModelManager |
|
29
|
|
|
*/ |
|
30
|
|
|
private $manager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $baseProductUrl; |
|
36
|
|
|
|
|
37
|
|
|
private $shopRepository; |
|
38
|
|
|
|
|
39
|
|
|
private $localeRepository; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
public function __construct( |
|
42
|
|
|
Config $config, |
|
43
|
|
|
ProductTranslationsGateway $productTranslationsGateway, |
|
44
|
|
|
ModelManager $manager, |
|
45
|
|
|
$baseProductUrl |
|
46
|
|
|
) { |
|
47
|
|
|
$this->config = $config; |
|
48
|
|
|
$this->productTranslationsGateway = $productTranslationsGateway; |
|
49
|
|
|
$this->manager = $manager; |
|
50
|
|
|
$this->baseProductUrl = $baseProductUrl; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function translate($productId, $sourceId) |
|
57
|
|
|
{ |
|
58
|
|
|
$exportLanguages = $this->config->getConfig('exportLanguages'); |
|
|
|
|
|
|
59
|
|
|
$exportLanguages = $exportLanguages ?: []; |
|
60
|
|
|
|
|
61
|
|
|
$translations = $this->productTranslationsGateway->getTranslations($productId, $exportLanguages); |
|
62
|
|
|
|
|
63
|
|
|
$result = []; |
|
64
|
|
|
foreach ($translations as $shopId => $translation) { |
|
65
|
|
|
/** @var \Shopware\Models\Shop\Shop $shop */ |
|
66
|
|
|
$shop = $this->getShopRepository()->find($shopId); |
|
67
|
|
|
if (!$shop) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** @var \Shopware\Models\Shop\Locale $locale */ |
|
72
|
|
|
$locale = $shop->getLocale(); |
|
73
|
|
|
if (strlen($locale->getLocale()) === 0) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$localeCode = explode('_', $locale->getLocale()); |
|
78
|
|
|
|
|
79
|
|
|
if (count($localeCode) === 0) { |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$result[$localeCode[0]] = new Translation( |
|
84
|
|
|
[ |
|
85
|
|
|
'title' => isset($translation['title']) ? $translation['title'] : '', |
|
86
|
|
|
'shortDescription' => isset($translation['shortDescription']) ? $translation['shortDescription'] : '', |
|
87
|
|
|
'longDescription' => isset($translation['longDescription']) ? $translation['longDescription'] : '', |
|
88
|
|
|
'additionalDescription' => isset($translation['additionalDescription']) ? $translation['additionalDescription'] : '', |
|
89
|
|
|
'url' => $this->getUrlForProduct($sourceId, $shop->getId()), |
|
90
|
|
|
] |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $result; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
public function translateConfiguratorGroup($groupId, $groupName, $translations) |
|
101
|
|
|
{ |
|
102
|
|
|
// exportLanguages actually is shop ids |
|
103
|
|
|
$exportLanguages = $this->config->getConfig('exportLanguages'); |
|
|
|
|
|
|
104
|
|
|
$exportLanguages = $exportLanguages ?: []; |
|
105
|
|
|
|
|
106
|
|
|
$groupTranslations = $this->productTranslationsGateway->getConfiguratorGroupTranslations($groupId, $exportLanguages); |
|
107
|
|
|
foreach ($exportLanguages as $shopId) { |
|
108
|
|
|
if ($shopId === 1) { |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
/** @var \Shopware\Models\Shop\Shop $shop */ |
|
112
|
|
|
$shop = $this->getShopRepository()->find($shopId); |
|
113
|
|
|
if (!$shop) { |
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** @var \Shopware\Models\Shop\Locale $locale */ |
|
118
|
|
|
$locale = $shop->getLocale(); |
|
119
|
|
|
if (!$locale) { |
|
120
|
|
|
continue; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$localeCode = explode('_', $locale->getLocale()); |
|
124
|
|
|
if (count($localeCode) === 0) { |
|
125
|
|
|
continue; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$groupTranslation = isset($groupTranslations[$shopId]) ? $groupTranslations[$shopId] : ''; |
|
129
|
|
|
if (!array_key_exists($localeCode[0], $translations)) { |
|
130
|
|
|
continue; |
|
131
|
|
|
} |
|
132
|
|
|
$translationStruct = $translations[$localeCode[0]]; |
|
133
|
|
|
if (!$translationStruct instanceof Translation) { |
|
134
|
|
|
continue; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$translationStruct->variantLabels[$groupName] = $groupTranslation; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $translations; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
|
|
public function translateConfiguratorOption($optionId, $optionName, $translations) |
|
147
|
|
|
{ |
|
148
|
|
|
$exportLanguages = $this->config->getConfig('exportLanguages'); |
|
|
|
|
|
|
149
|
|
|
$exportLanguages = $exportLanguages ?: []; |
|
150
|
|
|
|
|
151
|
|
|
$optionTranslations = $this->productTranslationsGateway->getConfiguratorOptionTranslations($optionId, $exportLanguages); |
|
152
|
|
|
foreach ($exportLanguages as $shopId) { |
|
153
|
|
|
if ($shopId === 1) { |
|
154
|
|
|
continue; |
|
155
|
|
|
} |
|
156
|
|
|
/** @var \Shopware\Models\Shop\Shop $shop */ |
|
157
|
|
|
$shop = $this->getShopRepository()->find($shopId); |
|
158
|
|
|
/** @var \Shopware\Models\Shop\Locale $locale */ |
|
159
|
|
|
$locale = $shop->getLocale(); |
|
160
|
|
|
if (!$locale) { |
|
161
|
|
|
continue; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$localeCode = explode('_', $locale->getLocale()); |
|
165
|
|
|
if (count($localeCode) === 0) { |
|
166
|
|
|
continue; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$optionTranslation = isset($optionTranslations[$shopId]) ? $optionTranslations[$shopId] : ''; |
|
170
|
|
|
$translationStruct = $translations[$localeCode[0]]; |
|
171
|
|
|
if (!$translationStruct instanceof Translation) { |
|
172
|
|
|
continue; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$translationStruct->variantValues[$optionName] = $optionTranslation; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $translations; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritdoc} |
|
183
|
|
|
*/ |
|
184
|
|
|
public function validate(Translation $translation, $optionsCount) |
|
185
|
|
|
{ |
|
186
|
|
|
// currently we dont want to stop the other translations like description |
|
187
|
|
|
// even we dont have translated title |
|
|
|
|
|
|
188
|
|
|
// if (strlen($translation->title) === 0) { |
|
189
|
|
|
// throw new \Exception('Translation title cannot be empty string.'); |
|
190
|
|
|
// } |
|
191
|
|
|
|
|
192
|
|
|
if (strlen($translation->url) === 0) { |
|
193
|
|
|
throw new \Exception('Translation url cannot be empty string.'); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if (count($translation->variantLabels) != $optionsCount) { |
|
197
|
|
|
throw new \Exception('variantLabels property has not correct items count.'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if (count($translation->variantValues) != $optionsCount) { |
|
201
|
|
|
throw new \Exception('variantValues property has not correct items count.'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return true; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
View Code Duplication |
public function getUrlForProduct($productId, $shopId = null) |
|
|
|
|
|
|
208
|
|
|
{ |
|
209
|
|
|
$shopId = (int) $shopId; |
|
210
|
|
|
$url = $this->baseProductUrl . $productId; |
|
211
|
|
|
if ($shopId > 0) { |
|
212
|
|
|
$url = $url . '/shId/' . $shopId; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return $url; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
private function getShopRepository() |
|
219
|
|
|
{ |
|
220
|
|
|
if (!$this->shopRepository) { |
|
221
|
|
|
$this->shopRepository = $this->manager->getRepository('Shopware\Models\Shop\Shop'); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return $this->shopRepository; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.