1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aitor24\Laralang\Controllers; |
4
|
|
|
|
5
|
|
|
use Aitor24\Laralang\Facades\Laralang; |
6
|
|
|
use Aitor24\Laralang\Models\DB_Translation; |
7
|
|
|
use Aitor24\Linker\Facades\Linker; |
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use Crypt; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
|
12
|
|
|
class LaralangController extends Controller |
13
|
|
|
{ |
14
|
|
|
public function showLogin() |
15
|
|
|
{ |
16
|
|
|
if (session('laralang.password') && Crypt::decrypt(session('laralang.password')) == config('laralang.default.password')) { |
17
|
|
|
return redirect(Route('laralang::translations')); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return view('laralang::login'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function login(Request $request) |
24
|
|
|
{ |
25
|
|
|
session(['laralang.password' => Crypt::encrypt($request->input('password'))]); |
26
|
|
View Code Duplication |
if (Crypt::decrypt(session('laralang.password')) != config('laralang.default.password')) { |
|
|
|
|
27
|
|
|
return redirect(Route('laralang::login')) |
28
|
|
|
->with('status', 'Invalid password'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return redirect(Linker::route('laralang::index')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function logout(Request $request) |
35
|
|
|
{ |
36
|
|
|
$request->session()->forget('laralang.password'); |
|
|
|
|
37
|
|
|
|
38
|
|
|
return redirect(Linker::route('laralang::login')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function index() |
42
|
|
|
{ |
43
|
|
|
return view('laralang::index'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function showTranslations() |
47
|
|
|
{ |
48
|
|
|
return view('laralang::translations'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function showTranslationsFilter() |
52
|
|
|
{ |
53
|
|
|
return view('laralang::filter', ['languagesFrom' => Laralang::fromLanguages(), 'languagesTo' => Laralang::toLanguages()]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function showTranslate() |
57
|
|
|
{ |
58
|
|
|
return view('laralang::translate'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function translate(Request $request) |
62
|
|
|
{ |
63
|
|
|
Laralang::generateTranslations($request->input('is_package'), $request->input('package'), $request->input('path'), $request->input('to_langs')); |
64
|
|
|
return redirect(Linker::route('laralang::translate')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function translationsFilter(Request $request) |
68
|
|
|
{ |
69
|
|
|
return redirect(Linker::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
|
|
|
|
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.