|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WeAreNeopix\LaravelModelTranslation\Drivers\MySQL; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
9
|
|
|
use WeAreNeopix\LaravelModelTranslation\Contracts\TranslationDriver; |
|
10
|
|
|
|
|
11
|
|
|
class MySQLTranslationDriver implements TranslationDriver |
|
12
|
|
|
{ |
|
13
|
|
|
private function queryForModel(Model $model, string $language = null): Builder |
|
14
|
|
|
{ |
|
15
|
|
|
$query = Translation::where('translatable_type', $model->getModelIdentifier()) |
|
16
|
|
|
->where('translatable_id', $model->getInstanceIdentifier()); |
|
17
|
|
|
|
|
18
|
|
|
if ($language !== null) { |
|
19
|
|
|
$query->where('language', $language); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
return $query; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function storeTranslationsForModel(Model $model, string $language, array $translations): bool |
|
26
|
|
|
{ |
|
27
|
|
|
$translations = collect($translations)->map(function ($translation, $attribute) use ($language, $model) { |
|
28
|
|
|
return [ |
|
29
|
|
|
'translatable_type' => $model->getModelIdentifier(), |
|
30
|
|
|
'translatable_id' => $model->getInstanceIdentifier(), |
|
31
|
|
|
'language' => $language, |
|
32
|
|
|
'name' => $attribute, |
|
33
|
|
|
'value' => $translation, |
|
34
|
|
|
]; |
|
35
|
|
|
}); |
|
36
|
|
|
|
|
37
|
|
|
return Translation::query()->insert($translations->toArray()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getTranslationsForModel(Model $model, string $language): array |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->queryForModel($model, $language) |
|
43
|
|
|
->get() |
|
44
|
|
|
->mapWithKeys(function (Translation $translation) { |
|
45
|
|
|
return [ |
|
46
|
|
|
$translation->name => $translation->value, |
|
47
|
|
|
]; |
|
48
|
|
|
}) |
|
49
|
|
|
->toArray(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getTranslationsForModels(Collection $models, string $language): Collection |
|
53
|
|
|
{ |
|
54
|
|
|
$modelIdentifier = $models->first()->getModelIdentifier(); |
|
55
|
|
|
$instanceIdentifiers = $models->map(function (Model $model) { |
|
56
|
|
|
return $model->getInstanceIdentifier(); |
|
57
|
|
|
}); |
|
58
|
|
|
|
|
59
|
|
|
$translations = Translation::where('translatable_type', $modelIdentifier) |
|
60
|
|
|
->whereIn('translatable_id', $instanceIdentifiers) |
|
61
|
|
|
->where('language', $language) |
|
62
|
|
|
->get() |
|
63
|
|
|
->groupBy('translatable_id'); |
|
64
|
|
|
|
|
65
|
|
|
/* |
|
66
|
|
|
* First we map the translations to their Models, setting an empty collection as default |
|
67
|
|
|
* We need to do this because the method needs to return an empty array for each model that has no translations. |
|
68
|
|
|
*/ |
|
69
|
|
|
$translationsPerModel = $models->mapWithKeys(function (Model $model) use ($translations) { |
|
70
|
|
|
return [ |
|
71
|
|
|
$model->getInstanceIdentifier() => $translations->get($model->getInstanceIdentifier(), collect([])), |
|
72
|
|
|
]; |
|
73
|
|
|
}); |
|
74
|
|
|
|
|
75
|
|
|
// Then we transform the translations to match the expected output format |
|
76
|
|
|
return $translationsPerModel->map(function (Collection $instanceTranslations) { |
|
77
|
|
|
return $instanceTranslations->mapWithKeys(function (Translation $translation) { |
|
78
|
|
|
return [ |
|
79
|
|
|
$translation->name => $translation->value, |
|
80
|
|
|
]; |
|
81
|
|
|
})->toArray(); |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getAvailableLanguagesForModel(Model $model): array |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->queryForModel($model)->groupBy('language')->pluck('language')->toArray(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getModelsAvailableInLanguage(string $modelIdentifier, string $language): array |
|
91
|
|
|
{ |
|
92
|
|
|
return Translation::where('translatable_type', $modelIdentifier) |
|
93
|
|
|
->where('language', $language) |
|
94
|
|
|
->groupBy('translatable_id') |
|
95
|
|
|
->pluck('translatable_id') |
|
96
|
|
|
->toArray(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function putTranslationsForModel(Model $model, string $language, array $translations): bool |
|
100
|
|
|
{ |
|
101
|
|
|
$this->queryForModel($model, $language) |
|
102
|
|
|
->delete(); |
|
103
|
|
|
|
|
104
|
|
|
return $this->storeTranslationsForModel($model, $language, $translations); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function mysqlCaseUpdate(array $translations) |
|
108
|
|
|
{ |
|
109
|
|
|
$sql = 'CASE '; |
|
110
|
|
|
foreach ($translations as $attribute => $translation) { |
|
111
|
|
|
$sql .= "WHEN name = '{$attribute}' THEN '{$translation}' "; |
|
112
|
|
|
} |
|
113
|
|
|
$sql .= 'END'; |
|
114
|
|
|
|
|
115
|
|
|
return $sql; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function patchTranslationsForModel(Model $model, string $language, array $translations): bool |
|
119
|
|
|
{ |
|
120
|
|
|
list($existing, $new) = $this->separateExistingFromNewTranslations($model, $language, $translations); |
|
121
|
|
|
|
|
122
|
|
|
if (!$this->storeTranslationsForModel($model, $language, $new)) { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$sql = $this->mysqlCaseUpdate($existing); |
|
127
|
|
|
$translationNames = array_keys($existing); |
|
128
|
|
|
|
|
129
|
|
|
return $this->queryForModel($model, $language) |
|
130
|
|
|
->whereIn('name', $translationNames) |
|
131
|
|
|
->update([ |
|
132
|
|
|
'value' => DB::raw($sql), |
|
133
|
|
|
]); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
private function separateExistingFromNewTranslations(Model $model, string $language, array $translations) |
|
137
|
|
|
{ |
|
138
|
|
|
$existingTranslations = array_keys($this->getTranslationsForModel($model, $language)); |
|
139
|
|
|
$translations = collect($translations); |
|
140
|
|
|
|
|
141
|
|
|
return [ |
|
142
|
|
|
$translations->only($existingTranslations)->toArray(), |
|
143
|
|
|
$translations->except($existingTranslations)->toArray(), |
|
144
|
|
|
]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function deleteAllTranslationsForModel(Model $model): bool |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->deleteTranslationsForModel($model); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function deleteLanguagesForModel(Model $model, array $languages): bool |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->queryForModel($model) |
|
155
|
|
|
->whereIn('language', $languages) |
|
156
|
|
|
->delete(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function deleteAttributesForModel(Model $model, array $attributes, string $language = null): bool |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->deleteTranslationsForModel($model, $language, $attributes); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function deleteTranslationsForModel(Model $model, string $language = null, array $attributes = null): bool |
|
165
|
|
|
{ |
|
166
|
|
|
$query = $this->queryForModel($model, $language); |
|
167
|
|
|
|
|
168
|
|
|
if ($attributes !== null) { |
|
169
|
|
|
$query->whereIn('name', $attributes); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $query->forceDelete(); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|