1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use BBSLab\CloudinaryField\HasCloudinaryField; |
6
|
|
|
use BBSLab\NovaTranslation\Models\Label; |
7
|
|
|
use BBSLab\NovaTranslation\Models\Locale; |
8
|
|
|
use BBSLab\NovaTranslation\Models\Translation; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Support\Facades\DB; |
11
|
|
|
|
12
|
|
|
class TranslationMatrixController |
13
|
|
|
{ |
14
|
|
|
use HasCloudinaryField; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Setup labels matrix endpoint. |
18
|
|
|
* |
19
|
|
|
* @return \Illuminate\Http\JsonResponse |
20
|
|
|
*/ |
21
|
|
|
public function index() |
22
|
|
|
{ |
23
|
|
|
$labels = $this->labels(); |
24
|
|
|
|
25
|
|
|
$locales = Locale::query()->select('id', 'iso', 'label')->get(); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$cloudinary = $this->cloudinaryMeta(); |
28
|
|
|
|
29
|
|
|
return response()->json(compact('labels', 'locales', 'cloudinary'), 200); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Save all labels provided in payload. |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Http\Request $request |
36
|
|
|
* @return \Illuminate\Http\JsonResponse |
37
|
|
|
*/ |
38
|
|
|
public function save(Request $request) |
39
|
|
|
{ |
40
|
|
|
DB::connection()->getPdo()->setAttribute(\PDO::ATTR_AUTOCOMMIT, 0); |
41
|
|
|
DB::beginTransaction(); |
42
|
|
|
|
43
|
|
|
Label::query()->truncate(); |
44
|
|
|
Translation::query()->where('translatable_type', '=', Label::class)->delete(); |
45
|
|
|
|
46
|
|
|
$labels = $request->input('labels', []); |
47
|
|
|
foreach ($labels as $label) { |
48
|
|
|
$this->createLabel($label); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
DB::commit(); |
52
|
|
|
|
53
|
|
|
return response()->json([ |
54
|
|
|
'labels' => $labels, |
55
|
|
|
], 200); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Download labels in JSON key-value format for given locale. |
60
|
|
|
* |
61
|
|
|
* @param \Illuminate\Http\Request $request |
62
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
63
|
|
|
*/ |
64
|
|
|
public function exportLocale(Request $request) |
65
|
|
|
{ |
66
|
|
|
$locale = $request->input('locale', app()->getLocale()); |
67
|
|
|
|
68
|
|
|
$json = Label::query() |
|
|
|
|
69
|
|
|
->select('key', 'value') |
70
|
|
|
->locale($locale) |
71
|
|
|
->orderBy('key', 'asc') |
72
|
|
|
->get() |
73
|
|
|
->pluck('value', 'key') |
74
|
|
|
->toArray(); |
75
|
|
|
|
76
|
|
|
$path = storage_path('app/labels_'.$locale.'.json'); |
77
|
|
|
file_put_contents($path, json_encode($json, JSON_PRETTY_PRINT)); |
78
|
|
|
|
79
|
|
|
return response()->download($path, $locale.'.json'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return all non-scoped labels. |
84
|
|
|
* |
85
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
86
|
|
|
*/ |
87
|
|
|
protected function labels() |
88
|
|
|
{ |
89
|
|
|
return Label::query() |
|
|
|
|
90
|
|
|
->select('translations.locale_id', 'labels.type', 'labels.key', 'labels.value') |
91
|
|
|
->join('translations', 'labels.id', '=', 'translations.translatable_id') |
92
|
|
|
->where('translations.translatable_type', '=', Label::class) |
93
|
|
|
->get(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create label and associated translation. |
98
|
|
|
* |
99
|
|
|
* @param array $data |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
protected function createLabel(array $data) |
103
|
|
|
{ |
104
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Translation $keyTranslation */ |
105
|
|
|
$keyTranslation = Translation::query() |
|
|
|
|
106
|
|
|
->select('translations.translation_id') |
107
|
|
|
->join('labels', 'translations.translatable_id', '=', 'labels.id') |
108
|
|
|
->where('translations.translatable_type', '=', Label::class) |
109
|
|
|
->where('labels.key', '=', $data['key']) |
110
|
|
|
->first(); |
111
|
|
|
|
112
|
|
|
$translationId = ! empty($keyTranslation) ? $keyTranslation->translation_id : (new Label)->freshTranslationId(); |
113
|
|
|
|
114
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Label $label */ |
115
|
|
|
$label = Label::withoutEvents(function () use ($data) { |
116
|
|
|
return Label::query()->create([ |
117
|
|
|
'type' => $data['type'], |
118
|
|
|
'key' => $data['key'], |
119
|
|
|
'value' => ! empty($data['value']) ? $data['value'] : '', |
120
|
|
|
]); |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
$label->upsertTranslationEntry($data['locale_id'], $translationId); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.