1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Laravel Controller for handling request to the translator admin interface |
5
|
|
|
*/ |
|
|
|
|
6
|
|
|
namespace Hokan22\LaravelTranslator\Controllers; |
7
|
|
|
|
8
|
|
|
use Illuminate\Routing\Controller; |
9
|
|
|
use Hokan22\LaravelTranslator\Models\TranslationIdentifier; |
10
|
|
|
use Hokan22\LaravelTranslator\TranslatorFacade; |
11
|
|
|
use Carbon\Carbon; |
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use Illuminate\Support\Facades\DB; |
14
|
|
|
use Illuminate\Support\Facades\Input; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class TranslatorAdminController |
18
|
|
|
* |
19
|
|
|
* @category LaravelController |
20
|
|
|
* @package Hokan22\LaravelTranslator\Controllers |
21
|
|
|
* @author Alexander Viertel <[email protected]> |
22
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
23
|
|
|
* @link https://github.com/Hokan22/laravel-translator |
24
|
|
|
*/ |
25
|
|
|
class TranslatorAdminController extends Controller |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Return an overview of translations |
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
31
|
|
|
*/ |
32
|
|
|
public function index() |
33
|
|
|
{ |
34
|
|
|
$search = Input::get('search', ''); |
35
|
|
|
$locale = Input::get('locale', ''); |
36
|
|
|
|
37
|
|
|
$query = TranslationIdentifier::with('translations'); |
38
|
|
|
|
39
|
|
|
if ($locale != '') { |
40
|
|
|
$query = $query->whereDoesntHave('translations', function ($query) use ($locale) |
|
|
|
|
41
|
|
|
{ |
|
|
|
|
42
|
|
|
$query->where('translations.locale', 'like', $locale); |
43
|
|
|
} |
|
|
|
|
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($search != '') { |
48
|
|
|
$query = $query->where(function ($query) use ($search) { |
|
|
|
|
49
|
|
|
$query ->where('identifier', 'LIKE', '%'.$search.'%') |
50
|
|
|
->orWhere('parameters', 'LIKE', '%'.$search.'%') |
51
|
|
|
->orWhere('group', 'LIKE', '%'.$search.'%') |
52
|
|
|
->orWhere('page_name', 'LIKE', '%'.$search.'%') |
53
|
|
|
->orWhere('description','LIKE', '%'.$search.'%'); |
54
|
|
|
}); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$trans_identifier = $query->orderBy('id')->paginate(20)->appends(Input::except('page')); |
58
|
|
|
|
59
|
|
|
$available_locales = TranslatorFacade::getConfigValue('available_locales'); |
60
|
|
|
|
61
|
|
|
return view('translator::index', |
|
|
|
|
62
|
|
|
[ |
63
|
|
|
'identifier' => $trans_identifier, |
64
|
|
|
'available_locales' => $available_locales, |
65
|
|
|
'page' => Input::get('page'), |
66
|
|
|
'query_locale' => Input::get('locale'), |
67
|
|
|
'search' => Input::get('search'), |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the edit view for a translation with $id |
74
|
|
|
* |
75
|
|
|
* @param integer $id ID of the translation Identifier to edit |
|
|
|
|
76
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
|
|
|
77
|
|
|
*/ |
78
|
|
|
public function edit($id) |
79
|
|
|
{ |
80
|
|
|
$identifier = TranslationIdentifier::findOrFail($id); |
81
|
|
|
|
82
|
|
|
$available_locales = TranslatorFacade::getConfigValue('available_locales'); |
83
|
|
|
|
84
|
|
|
return view('translator::edit', |
|
|
|
|
85
|
|
|
[ |
86
|
|
|
'identifier' => $identifier, |
87
|
|
|
'available_locales' => $available_locales, |
88
|
|
|
'page' => Input::get('page'), |
89
|
|
|
'locale' => Input::get('locale'), |
90
|
|
|
'search' => Input::get('search'), |
91
|
|
|
] |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Update |
98
|
|
|
* |
99
|
|
|
* @param integer $id ID of the identifier to edit |
|
|
|
|
100
|
|
|
* @param Request $request Object with the values of the identifier |
|
|
|
|
101
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
|
|
|
102
|
|
|
*/ |
103
|
|
|
public function postEdit($id, Request $request) |
104
|
|
|
{ |
105
|
|
|
TranslationIdentifier::findOrFail($id); |
106
|
|
|
|
107
|
|
|
foreach ($request->all() as $key => $value) { |
108
|
|
|
|
109
|
|
|
if ($value === null || in_array($key, ['_token', 'page', 'locale', 'search'])) { |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$value = str_replace("\r", "", $value); |
114
|
|
|
$value = str_replace("\n", "<br />", $value); |
115
|
|
|
|
116
|
|
|
$timestamp = Carbon::now(); |
117
|
|
|
|
118
|
|
|
// Eloquent doesn't support composite keys, therefore a raw query is used |
119
|
|
|
// This query will create the translation or update the translation if it already exists in the database |
120
|
|
|
DB::statement("INSERT INTO `translations` (`translation_identifier_id`, `locale`, `translation`, `updated_at`, `created_at`) |
|
|
|
|
121
|
|
|
VALUES ($id, '$key', '$value', '$timestamp', '$timestamp') |
122
|
|
|
ON DUPLICATE KEY |
123
|
|
|
UPDATE `translation` = '$value'"); |
|
|
|
|
124
|
|
|
|
125
|
|
|
DB::statement("UPDATE `translation_identifiers` SET `updated_at` = '$timestamp' WHERE `id` LIKE $id"); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->edit($id); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Post edit of multiple Identifiers from the index view |
133
|
|
|
* |
134
|
|
|
* @param Request $request Request with multiple values of identifiers to update |
|
|
|
|
135
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
|
|
|
136
|
|
|
*/ |
137
|
|
|
public function postIdentifier(Request $request) |
138
|
|
|
{ |
139
|
|
|
/** @var TranslationIdentifier $translation_identifiers */ |
|
|
|
|
140
|
|
|
$translation_identifiers = TranslationIdentifier::all()->whereIn('id', array_keys($request->all())); |
141
|
|
|
|
142
|
|
|
foreach ($request->all() as $id => $identifier) { |
143
|
|
|
|
144
|
|
|
if (!is_array($identifier)) { |
145
|
|
|
continue; |
146
|
|
|
}; |
147
|
|
|
|
148
|
|
|
$translation_identifier = $translation_identifiers->find($id); |
149
|
|
|
|
150
|
|
|
$translation_identifier->parameters = isset($identifier['parameters']) ? explode($id, $identifier['parameters']) : []; |
151
|
|
|
$translation_identifier->group = isset($identifier['group']) ? $identifier['group'] : 'default'; |
152
|
|
|
$translation_identifier->page_name = isset($identifier['page_name']) ? $identifier['page_name'] : null; |
153
|
|
|
$translation_identifier->description = isset($identifier['description']) ? $identifier['description'] : null; |
154
|
|
|
|
155
|
|
|
$translation_identifier->save(); |
156
|
|
|
} |
157
|
|
|
return $this->index(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Test view with some test translations |
162
|
|
|
* |
163
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
164
|
|
|
*/ |
165
|
|
|
public function test() |
166
|
|
|
{ |
167
|
|
|
return view('translator::test'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
|
|
|
|
171
|
|
|
* @param $state string 'enabled|disabled' |
|
|
|
|
172
|
|
|
* @return \Illuminate\Http\RedirectResponse |
173
|
|
|
*/ |
174
|
|
|
public function changeLiveMode ($state) { |
|
|
|
|
175
|
|
|
|
176
|
|
|
if ($state == 'enable') { |
177
|
|
|
session(['translation_live_mode' => true]); |
178
|
|
|
} else { |
179
|
|
|
session(['translation_live_mode' => false]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return redirect()->back(); |
183
|
|
|
} |
184
|
|
|
} |