Passed
Push — master ( 4247c7...df0636 )
by Alexander
02:20
created

TranslatorAdminController::postEdit()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
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');
0 ignored issues
show
Bug introduced by
The method getConfigValue() does not exist on Hokan22\LaravelTranslator\TranslatorFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        /** @scrutinizer ignore-call */ 
47
        $available_locales = TranslatorFacade::getConfigValue('available_locales');
Loading history...
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
        $translation_identifiers = TranslationIdentifier::all()->whereIn('id', array_keys($request->all()));
112
113
        foreach ($request->all() as $id => $identifier) {
114
115
            if (!is_array($identifier)) {
116
                continue;
117
            };
118
119
            $translation_identifier = $translation_identifiers->find($id);
120
121
            $translation_identifier->parameters     = isset($identifier['parameters']) ? explode($id, $identifier['parameters']) : [];
0 ignored issues
show
Bug introduced by
The property parameters does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
122
            $translation_identifier->group          = isset($identifier['group']) ? $identifier['group'] : 'default';
0 ignored issues
show
Bug introduced by
The property group does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
123
            $translation_identifier->page_name      = isset($identifier['page_name']) ? $identifier['page_name'] : null;
0 ignored issues
show
Bug introduced by
The property page_name does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
124
            $translation_identifier->description    = isset($identifier['description']) ? $identifier['description'] : null;
0 ignored issues
show
Bug introduced by
The property description does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
125
126
            $translation_identifier->save();
127
        }
128
129
        return $this->index();
130
    }
131
132
    /**
133
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
134
     */
135
    public function test () {
136
        return view('translator::test');
137
    }
138
}