|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBS\Nova\Translation\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use BBS\Nova\Translation\Models\Label; |
|
6
|
|
|
use BBS\Nova\Translation\Models\Locale; |
|
7
|
|
|
use BBS\Nova\Translation\Models\Scopes\TranslatableScope; |
|
8
|
|
|
use BBS\Nova\Translation\Models\Translation; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Support\Facades\DB; |
|
11
|
|
|
|
|
12
|
|
|
class LabelsController |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Setup labels matrix endpoint. |
|
16
|
|
|
* |
|
17
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
18
|
|
|
*/ |
|
19
|
|
|
public function index() |
|
20
|
|
|
{ |
|
21
|
|
|
$labels = $this->labels(); |
|
22
|
|
|
|
|
23
|
|
|
$locales = Locale::query()->select('id', 'label')->get(); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
return response()->json(compact('labels', 'locales'), 200); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Save all labels provided in payload. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Illuminate\Http\Request $request |
|
32
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
33
|
|
|
*/ |
|
34
|
|
|
public function save(Request $request) |
|
35
|
|
|
{ |
|
36
|
|
|
DB::beginTransaction(); |
|
37
|
|
|
|
|
38
|
|
|
Label::query()->withoutGlobalScope(TranslatableScope::class)->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.key', 'labels.value') |
|
62
|
|
|
->join('translations', 'labels.id', '=', 'translations.translatable_id') |
|
63
|
|
|
->where('translations.translatable_type', '=', Label::class) |
|
64
|
|
|
->withoutGlobalScope(TranslatableScope::class) |
|
65
|
|
|
->get(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Create label and associated translation. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $data |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function createLabel(array $data) |
|
75
|
|
|
{ |
|
76
|
|
|
$translatedLabel = Label::query() |
|
|
|
|
|
|
77
|
|
|
->select('labels.translation_id') |
|
78
|
|
|
->join('translations', function ($join) { |
|
79
|
|
|
$join |
|
80
|
|
|
->on('translations.translatable_id', '=', 'labels.id') |
|
81
|
|
|
->where('translations.translatable_type', '=', Label::class); |
|
82
|
|
|
}) |
|
83
|
|
|
->where('labels.key', '=', $data['key']) |
|
84
|
|
|
->withoutGlobalScope(TranslatableScope::class) |
|
85
|
|
|
->first(); |
|
86
|
|
|
|
|
87
|
|
|
$translationId = ! empty($translatedLabel) ? $translatedLabel->translation_id : Label::freshTranslationId(); |
|
88
|
|
|
|
|
89
|
|
|
$label = Label::query()->create([ |
|
90
|
|
|
'translation_id' => $translationId, |
|
91
|
|
|
'key' => $data['key'], |
|
92
|
|
|
'value' => ! empty($data['value']) ? $data['value'] : '', |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
Translation::query()->create([ |
|
96
|
|
|
'locale_id' => $data['locale_id'], |
|
97
|
|
|
'translation_id' => $translationId, |
|
98
|
|
|
'translatable_id' => $label->id, |
|
99
|
|
|
'translatable_type' => Label::class, |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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.