LaralangController::showTranslate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Aitor24\Laralang\Controllers;
4
5
use Aitor24\Laralang\Facades\Laralang;
6
use Aitor24\Laralang\Models\DB_Translation;
7
use App\Http\Controllers\Controller;
8
use Crypt;
9
use Illuminate\Http\Request;
10
11
class LaralangController extends Controller
12
{
13
    public function showLogin()
14
    {
15
        if (session('laralang.password') && Crypt::decrypt(session('laralang.password')) == config('laralang.default.password')) {
16
            return redirect(Route('laralang::translations'));
17
        }
18
19
        return view('laralang::login');
20
    }
21
22
    public function login(Request $request)
23
    {
24
        session(['laralang.password' => Crypt::encrypt($request->input('password'))]);
25 View Code Duplication
        if (Crypt::decrypt(session('laralang.password')) != config('laralang.default.password')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
            return redirect(Route('laralang::login'))
27
            ->with('status', 'Invalid password');
28
        }
29
30
        return redirect()->route('laralang::index');
31
    }
32
33
    public function logout(Request $request)
34
    {
35
        $request->session()->forget('laralang.password');
36
37
        return redirect()->route('laralang::login');
38
    }
39
40
    public function index()
41
    {
42
        return view('laralang::index');
43
    }
44
45
    public function showTranslations()
46
    {
47
        return view('laralang::translations');
48
    }
49
50
    public function showTranslationsFilter()
51
    {
52
        return view('laralang::filter', ['languagesFrom' => Laralang::fromLanguages(), 'languagesTo' => Laralang::toLanguages()]);
53
    }
54
55
    public function showTranslate()
56
    {
57
        return view('laralang::translate');
58
    }
59
60
    public function translate(Request $request)
61
    {
62
        Laralang::generateTranslations($request->input('is_package'), $request->input('package'), $request->input('path'), $request->input('to_langs'));
63
64
        return redirect()->route('laralang::translate');
65
    }
66
67
    public function translationsFilter(Request $request)
68
    {
69
        return redirect()->route('laralang::filterFromTo', [$request->from_lang, $request->to_lang]);
70
    }
71
72
    public function showTranslationsFiltered($from_lang, $to_lang)
73
    {
74
        return view('laralang::translations', ['from_lang' => $from_lang, 'to_lang' => $to_lang]);
75
    }
76
77
    public function api()
78
    {
79
        return DB_Translation::all();
80
    }
81
82
    public function apiTranslate(Request $request)
83
    {
84
        $translatedText = Laralang::trans($request->string)->to($request->to);
85
86
        return ['translatedText' => strval($translatedText)];
87
    }
88
89
    public function apiFilterFromTo($from_lang, $to_lang)
90
    {
91
        // filter wich translation showld send
92
        if ($to_lang == 'all' && $from_lang == 'all') {
93
            return DB_Translation::all();
94
        } elseif ($to_lang == 'all') {
95
96
            //return translation where from_lang == $from
97
            return DB_Translation::where([['from_lang', $from_lang]])->get();
98
        } elseif ($from_lang == 'all') {
99
100
            //return translation where to_lang == $to
101
            return DB_Translation::where([['to_lang', $to_lang]])->get();
102
        } else {
103
104
            //return translation where from_lang == $from and to_lang == $to
105
            return DB_Translation::where([['from_lang', $from_lang], ['to_lang', $to_lang]])->get();
106
        }
107
    }
108
109
    public function deleteTranslation(Request $request)
110
    {
111
        $trans = DB_Translation::findOrFail($request->id);
112
        $trans->delete();
113
    }
114
115
    public function deleteAllTranslations()
116
    {
117
        $trans = DB_Translation::all();
118
        $trans->delete();
119
    }
120
121
    public function editTranslation(Request $request)
122
    {
123
        $trans = DB_Translation::findOrFail($request->id);
124
        $trans->string = $request->string;
125
        $trans->to_lang = $request->to;
126
        $trans->from_lang = $request->from;
127
        $trans->translation = $request->translation;
128
        $trans->touch();
129
        $trans->save();
130
    }
131
}
132