|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use BBSLab\NovaTranslation\Models\Label; |
|
6
|
|
|
use BBSLab\NovaTranslation\Models\Locale; |
|
7
|
|
|
use BBSLab\NovaTranslation\Models\Translation; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Support\Facades\DB; |
|
10
|
|
|
|
|
11
|
|
|
class TranslationMatrixController |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Setup labels matrix endpoint. |
|
15
|
|
|
* |
|
16
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
17
|
|
|
*/ |
|
18
|
|
|
public function index() |
|
19
|
|
|
{ |
|
20
|
|
|
$labels = $this->labels(); |
|
21
|
|
|
|
|
22
|
|
|
$locales = Locale::query()->select('id', 'label')->get(); |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
return response()->json(compact('labels', 'locales'), 200); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Save all labels provided in payload. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Illuminate\Http\Request $request |
|
31
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
32
|
|
|
*/ |
|
33
|
|
|
public function save(Request $request) |
|
34
|
|
|
{ |
|
35
|
|
|
// @TODO... |
|
36
|
|
|
DB::beginTransaction(); |
|
37
|
|
|
|
|
38
|
|
|
Label::query()->truncate(); |
|
39
|
|
|
Translation::query()->where('translatable_type', '=', Label::class)->delete(); |
|
40
|
|
|
|
|
41
|
|
|
$labels = $request->input('labels', []); |
|
42
|
|
|
foreach ($labels as $label) { |
|
43
|
|
|
$this->createLabel($label); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
DB::commit(); |
|
47
|
|
|
|
|
48
|
|
|
return response()->json([ |
|
49
|
|
|
'labels' => $labels, |
|
50
|
|
|
], 200); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Return all non-scoped labels. |
|
55
|
|
|
* |
|
56
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function labels() |
|
59
|
|
|
{ |
|
60
|
|
|
return Label::query() |
|
|
|
|
|
|
61
|
|
|
->select('translations.locale_id', 'labels.type', 'labels.key', 'labels.value') |
|
62
|
|
|
->join('translations', 'labels.id', '=', 'translations.translatable_id') |
|
63
|
|
|
->where('translations.translatable_type', '=', Label::class) |
|
64
|
|
|
->get(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Create label and associated translation. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $data |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function createLabel(array $data) |
|
74
|
|
|
{ |
|
75
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Translation $keyTranslation */ |
|
76
|
|
|
$keyTranslation = Translation::query() |
|
|
|
|
|
|
77
|
|
|
->select('translations.translation_id') |
|
78
|
|
|
->join('labels', 'translations.translatable_id', '=', 'labels.id') |
|
79
|
|
|
->where('translations.translatable_type', '=', Label::class) |
|
80
|
|
|
->where('labels.key', '=', $data['key']) |
|
81
|
|
|
->first(); |
|
82
|
|
|
|
|
83
|
|
|
$translationId = ! empty($keyTranslation) ? $keyTranslation->translation_id : (new Label)->freshTranslationId(); |
|
84
|
|
|
|
|
85
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Label $label */ |
|
86
|
|
|
$label = Label::query()->create([ |
|
87
|
|
|
'type' => Label::TYPE_TEXT, |
|
88
|
|
|
'key' => $data['key'], |
|
89
|
|
|
'value' => ! empty($data['value']) ? $data['value'] : '', |
|
90
|
|
|
]); |
|
91
|
|
|
|
|
92
|
|
|
$label->upsertTranslationEntry($data['locale_id'], $translationId); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.