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