1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\LangFileManager\app\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests; |
6
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
7
|
|
|
use Backpack\LangFileManager\app\Services\LangFiles; |
8
|
|
|
use Backpack\LangFileManager\app\Models\Language; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
// VALIDATION: change the requests to match your own file names if you need form validation |
11
|
|
|
use Backpack\LangFileManager\app\Http\Requests\LanguageRequest as StoreRequest; |
12
|
|
|
use Backpack\LangFileManager\app\Http\Requests\LanguageRequest as UpdateRequest; |
13
|
|
|
|
14
|
|
|
class LanguageCrudController extends CrudController |
15
|
|
|
{ |
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
parent::__construct(); |
19
|
|
|
|
20
|
|
|
$this->crud->setModel("Backpack\LangFileManager\app\Models\Language"); |
21
|
|
|
$this->crud->setRoute('admin/language'); |
22
|
|
|
$this->crud->setEntityNameStrings(trans('backpack::langfilemanager.language'), trans('backpack::langfilemanager.languages')); |
|
|
|
|
23
|
|
|
|
24
|
|
|
$this->crud->setColumns([ |
25
|
|
|
[ |
26
|
|
|
'name' => 'name', |
27
|
|
|
'label' => trans('backpack::langfilemanager.language_name'), |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
'name' => 'active', |
31
|
|
|
'label' => trans('backpack::langfilemanager.active'), |
32
|
|
|
'type' => 'boolean', |
33
|
|
|
], |
34
|
|
|
[ |
35
|
|
|
'name' => 'default', |
36
|
|
|
'label' => trans('backpack::langfilemanager.default'), |
37
|
|
|
'type' => 'boolean', |
38
|
|
|
], |
39
|
|
|
]); |
40
|
|
|
$this->crud->addField([ |
41
|
|
|
'name' => 'name', |
42
|
|
|
'label' => trans('backpack::langfilemanager.language_name'), |
43
|
|
|
'type' => 'text', |
44
|
|
|
]); |
45
|
|
|
$this->crud->addField([ |
46
|
|
|
'name' => 'native', |
47
|
|
|
'label' => trans('backpack::langfilemanager.native_name'), |
48
|
|
|
'type' => 'text', |
49
|
|
|
]); |
50
|
|
|
$this->crud->addField([ |
51
|
|
|
'name' => 'abbr', |
52
|
|
|
'label' => trans('backpack::langfilemanager.code_iso639-1'), |
53
|
|
|
'type' => 'text', |
54
|
|
|
]); |
55
|
|
|
$this->crud->addField([ |
56
|
|
|
'name' => 'flag', |
57
|
|
|
'label' => trans('backpack::langfilemanager.flag_image'), |
58
|
|
|
'type' => 'browse', |
59
|
|
|
]); |
60
|
|
|
$this->crud->addField([ |
61
|
|
|
'name' => 'active', |
62
|
|
|
'label' => trans('backpack::langfilemanager.active'), |
63
|
|
|
'type' => 'checkbox', |
64
|
|
|
]); |
65
|
|
|
$this->crud->addField([ |
66
|
|
|
'name' => 'default', |
67
|
|
|
'label' => trans('backpack::langfilemanager.default'), |
68
|
|
|
'type' => 'checkbox', |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function store(StoreRequest $request) |
73
|
|
|
{ |
74
|
|
|
$defaultLang = Language::where('default', 1)->first(); |
75
|
|
|
|
76
|
|
|
// Copy the default language folder to the new language folder |
77
|
|
|
\File::copyDirectory(resource_path('lang/'.$defaultLang->abbr), resource_path('lang/'.$request->input('abbr'))); |
78
|
|
|
|
79
|
|
|
return parent::storeCrud(); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function update(UpdateRequest $request) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
return parent::updateCrud(); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* After delete remove also the language folder. |
89
|
|
|
* |
90
|
|
|
* @param int $id |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function destroy($id) |
95
|
|
|
{ |
96
|
|
|
$language = Language::find($id); |
97
|
|
|
$destroyResult = parent::destroy($id); |
98
|
|
|
|
99
|
|
|
if ($destroyResult) { |
100
|
|
|
\File::deleteDirectory(resource_path('lang/'.$language->abbr)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $destroyResult; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function showTexts(LangFiles $langfile, Language $languages, $lang = '', $file = 'site') |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
// SECURITY |
109
|
|
|
// check if that file isn't forbidden in the config file |
110
|
|
|
if (in_array($file, config('backpack.langfilemanager.language_ignore'))) { |
111
|
|
|
abort('403', trans('backpack::langfilemanager.cant_edit_online')); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($lang) { |
115
|
|
|
$langfile->setLanguage($lang); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$langfile->setFile($file); |
119
|
|
|
$this->data['crud'] = $this->crud; |
120
|
|
|
$this->data['currentFile'] = $file; |
121
|
|
|
$this->data['currentLang'] = $lang ?: config('app.locale'); |
122
|
|
|
$this->data['currentLangObj'] = Language::where('abbr', '=', $this->data['currentLang'])->first(); |
123
|
|
|
$this->data['browsingLangObj'] = Language::where('abbr', '=', config('app.locale'))->first(); |
124
|
|
|
$this->data['languages'] = $languages->orderBy('name')->get(); |
125
|
|
|
$this->data['langFiles'] = $langfile->getlangFiles(); |
126
|
|
|
$this->data['fileArray'] = $langfile->getFileContent(); |
127
|
|
|
$this->data['langfile'] = $langfile; |
128
|
|
|
$this->data['title'] = trans('backpack::langfilemanager.translations'); |
129
|
|
|
|
130
|
|
|
return view('langfilemanager::translations', $this->data); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function updateTexts(LangFiles $langfile, Request $request, $lang = '', $file = 'site') |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
// SECURITY |
136
|
|
|
// check if that file isn't forbidden in the config file |
137
|
|
|
if (in_array($file, config('backpack.langfilemanager.language_ignore'))) { |
138
|
|
|
abort('403', trans('backpack::langfilemanager.cant_edit_online')); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$message = trans('error.error_general'); |
|
|
|
|
142
|
|
|
$status = false; |
|
|
|
|
143
|
|
|
|
144
|
|
|
if ($lang) { |
145
|
|
|
$langfile->setLanguage($lang); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$langfile->setFile($file); |
149
|
|
|
|
150
|
|
|
$fields = $langfile->testFields($request->all()); |
151
|
|
|
if (empty($fields)) { |
152
|
|
|
if ($langfile->setFileContent($request->all())) { |
153
|
|
|
\Alert::success(trans('backpack::langfilemanager.saved'))->flash(); |
154
|
|
|
$status = true; |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} else { |
157
|
|
|
$message = trans('admin.language.fields_required'); |
|
|
|
|
158
|
|
|
\Alert::error(trans('backpack::langfilemanager.please_fill_all_fields'))->flash(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return redirect()->back(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.