Completed
Push — master ( b0dc4b...de1d11 )
by Alexander
02:31
created

TranslatorAdminController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 120
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 9 1
B index() 0 28 3
B postEdit() 0 26 3
C postIdentifier() 0 22 7
A test() 0 2 1
1
<?php
2
3
4
namespace Hokan22\LaravelTranslator\Controllers;
5
6
use Illuminate\Routing\Controller;
7
use Hokan22\LaravelTranslator\Models\TranslationIdentifier;
8
use Hokan22\LaravelTranslator\TranslatorFacade;
9
use Carbon\Carbon;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\DB;
12
use Illuminate\Support\Facades\Input;
13
14
/**
15
 * Class TranslatorAdminController
16
 * @package app\Libraries\Translator\resources\views
17
 */
18
class TranslatorAdminController extends Controller
19
{
20
    /**
21
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
22
     */
23
    public function index () {
24
25
        $locale = Input::get('locale', '');
26
        $search = Input::get('search', '');
27
28
        $query = TranslationIdentifier::with('translations')->orderBy('updated_at', 'DESC')->orderBy('id', 'DESC');
29
30
        if ($locale != '') {
31
            $query = TranslationIdentifier::wheredoesntHave('translations', function ($query) use ($locale) {
32
                            $query->where('locale', 'like', $locale);
33
                        });
34
        }
35
36
        if ($search != '') {
37
            $query = TranslationIdentifier::where('identifier',     'LIKE', '%'.$search.'%')
38
                                            ->orWhere('parameters', 'LIKE', '%'.$search.'%')
39
                                            ->orWhere('group',      'LIKE', '%'.$search.'%')
40
                                            ->orWhere('page_name',  'LIKE', '%'.$search.'%')
41
                                            ->orWhere('description','LIKE', '%'.$search.'%');
42
        }
43
44
        $trans_identifier = $query->orderBy('updated_at', 'DESC')->orderBy('id', 'DESC')->paginate(20)->appends(Input::except('page'));
45
46
        $available_locales = TranslatorFacade::getConfigValue('available_locales');
47
48
        return view('translator::index',[
49
            'identifier'         =>  $trans_identifier,
50
            'available_locales'  =>  $available_locales,
51
        ]);
52
    }
53
54
    /**
55
     * @param $id
56
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
57
     */
58
    public function edit ($id) {
59
60
        $identifier = TranslationIdentifier::findOrFail($id);
61
62
        $available_locales = TranslatorFacade::getConfigValue('available_locales');
63
64
        return view('translator::edit',[
65
            'identifier'         =>  $identifier,
66
            'available_locales'  =>  $available_locales,
67
        ]);
68
69
    }
70
71
    /**
72
     * @param $id
73
     * @param Request $request
74
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
75
     */
76
    public function postEdit ($id, Request $request) {
77
        
78
        TranslationIdentifier::findOrFail($id);
79
80
        foreach ($request->all() as $key => $value) {
81
82
            if ($value === null) {
83
                continue;
84
            }
85
86
            $value = str_replace("\r", "", $value);
87
            $value = str_replace("\n", "<br />", $value);
88
89
            $timestamp = Carbon::now();
90
91
            // Eloquent doesn't support composite keys, therefore a raw query is used
92
            // This query will create the translation or update the translation if it already exists in the database
93
            DB::statement("INSERT INTO `translations` (`translation_identifier_id`, `locale`, `translation`, `updated_at`, `created_at`)
94
                            VALUES ($id, '$key', '$value', '$timestamp', '$timestamp') 
95
                            ON DUPLICATE KEY 
96
                              UPDATE `translation` = '$value'");
97
98
            DB::statement("UPDATE `translation_identifiers` SET `updated_at` = '$timestamp' WHERE `id` LIKE $id");
99
        }
100
101
        return $this->edit($id);
102
103
    }
104
105
    /**
106
     * @param Request $request
107
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
108
     */
109
    public function postIdentifier (Request $request) {
110
111
        /** @var TranslationIdentifier $translation_identifiers */
112
        $translation_identifiers = TranslationIdentifier::all()->whereIn('id', array_keys($request->all()));
113
114
        foreach ($request->all() as $id => $identifier) {
115
116
            if (!is_array($identifier)) {
117
                continue;
118
            };
119
120
            $translation_identifier = $translation_identifiers->find($id);
121
122
            $translation_identifier->parameters     = isset($identifier['parameters']) ? explode($id, $identifier['parameters']) : [];
123
            $translation_identifier->group          = isset($identifier['group']) ? $identifier['group'] : 'default';
124
            $translation_identifier->page_name      = isset($identifier['page_name']) ? $identifier['page_name'] : null;
125
            $translation_identifier->description    = isset($identifier['description']) ? $identifier['description'] : null;
126
127
            $translation_identifier->save();
128
        }
129
130
        return $this->index();
131
    }
132
133
    /**
134
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
135
     */
136
    public function test () {
137
        return view('translator::test');
138
    }
139
}