Completed
Push — master ( 9227fb...a71857 )
by
unknown
06:03
created

TranslationMatrixController::exportLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 1
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();
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

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.

Loading history...
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::beginTransaction();
41
42
        Label::query()->truncate();
43
        Translation::query()->where('translatable_type', '=', Label::class)->delete();
44
45
        $labels = $request->input('labels', []);
46
        foreach ($labels as $label) {
47
            $this->createLabel($label);
48
        }
49
50
        DB::commit();
51
52
        return response()->json([
53
            'labels' => $labels,
54
        ], 200);
55
    }
56
57
    /**
58
     * Download labels in JSON key-value format for given locale.
59
     *
60
     * @param  \Illuminate\Http\Request  $request
61
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
62
     */
63
    public function exportLocale(Request $request)
64
    {
65
        $locale = $request->input('locale', app()->getLocale());
66
67
        $json = Label::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

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.

Loading history...
68
            ->select('key', 'value')
69
            ->locale($locale)
70
            ->orderBy('key', 'asc')
71
            ->get()
72
            ->pluck('value', 'key')
73
            ->toArray();
74
75
        $path = storage_path('app/labels_'.$locale.'.json');
76
        file_put_contents($path, json_encode($json, JSON_PRETTY_PRINT));
77
78
        return response()->download($path, $locale.'.json');
79
    }
80
81
    /**
82
     * Return all non-scoped labels.
83
     *
84
     * @return \Illuminate\Database\Eloquent\Collection
85
     */
86
    protected function labels()
87
    {
88
        return Label::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

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.

Loading history...
89
            ->select('translations.locale_id', 'labels.type', 'labels.key', 'labels.value')
90
            ->join('translations', 'labels.id', '=', 'translations.translatable_id')
91
            ->where('translations.translatable_type', '=', Label::class)
92
            ->get();
93
    }
94
95
    /**
96
     * Create label and associated translation.
97
     *
98
     * @param  array  $data
99
     * @return void
100
     */
101
    protected function createLabel(array $data)
102
    {
103
        /** @var \BBSLab\NovaTranslation\Models\Translation $keyTranslation */
104
        $keyTranslation = Translation::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

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.

Loading history...
105
            ->select('translations.translation_id')
106
            ->join('labels', 'translations.translatable_id', '=', 'labels.id')
107
            ->where('translations.translatable_type', '=', Label::class)
108
            ->where('labels.key', '=', $data['key'])
109
            ->first();
110
111
        $translationId = ! empty($keyTranslation) ? $keyTranslation->translation_id : (new Label)->freshTranslationId();
112
113
        /** @var \BBSLab\NovaTranslation\Models\Label $label */
114
        $label = Label::withoutEvents(function () use ($data) {
115
            return Label::query()->create([
116
                'type' => $data['type'],
117
                'key' => $data['key'],
118
                'value' => ! empty($data['value']) ? $data['value'] : '',
119
            ]);
120
        });
121
122
        $label->upsertTranslationEntry($data['locale_id'], $translationId);
123
    }
124
}
125