json file update

pull/36/head
A1Gard 1 year ago
parent 1c374b068b
commit 5e759b1342

@ -10,23 +10,26 @@ use Illuminate\Support\Facades\Artisan;
use function Xmen\StarterKit\Helpers\logAdmin; use function Xmen\StarterKit\Helpers\logAdmin;
use function Xmen\StarterKit\Helpers\logAdminBatch; use function Xmen\StarterKit\Helpers\logAdminBatch;
const PREFIX_PATH = __DIR__ . '/../../../../';
class XlangController extends Controller class XlangController extends Controller
{ {
public function createOrUpdate(Xlang $xlang, XlangSaveRequest $request) { public function createOrUpdate(Xlang $xlang, XlangSaveRequest $request)
{
$xlang->name = $request->input('name'); $xlang->name = $request->input('name');
$xlang->tag = $request->input('tag'); $xlang->tag = $request->input('tag');
$xlang->rtl = $request->has('rtl'); $xlang->rtl = $request->has('rtl');
if ($request->hasFile('img')) { if ($request->hasFile('img')) {
$name = time().'.'.request()->img->getClientOriginalExtension(); $name = time() . '.' . request()->img->getClientOriginalExtension();
$xlang->img = $name; $xlang->img = $name;
$request->file('img')->storeAs('public/langz', $name); $request->file('img')->storeAs('public/langz', $name);
} }
$xlang->save(); $xlang->save();
return $xlang; return $xlang;
} }
/** /**
* Display a listing of the resource. * Display a listing of the resource.
* *
@ -36,8 +39,8 @@ class XlangController extends Controller
{ {
// //
Artisan::call('translator:update'); Artisan::call('translator:update');
$langs = Xlang::paginate(99); $langs = Xlang::paginate(99);
return view('admin.langs.langIndex',compact('langs')); return view('admin.langs.langIndex', compact('langs'));
} }
/** /**
@ -54,25 +57,25 @@ class XlangController extends Controller
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function store(XlangSaveRequest $request) public function store(XlangSaveRequest $request)
{ {
// //
define("TRANSLATE_CONFIG_PATH", __DIR__ . '/../../../../config/translator.php'); define("TRANSLATE_CONFIG_PATH", PREFIX_PATH . 'config/translator.php');
define("TRANSLATE_NEW_FILE",__DIR__ . '/../../../../resources/lang/'.$request->tag.'.json' ); define("TRANSLATE_NEW_FILE", PREFIX_PATH . 'resources/lang/' . $request->tag . '.json');
$config = file_get_contents(TRANSLATE_CONFIG_PATH); $config = file_get_contents(TRANSLATE_CONFIG_PATH);
$re = '/\'languages\' \=\> (.*)\,/m'; $re = '/\'languages\' \=\> (.*)\,/m';
preg_match_all($re, $config, $matches, PREG_SET_ORDER, 0); preg_match_all($re, $config, $matches, PREG_SET_ORDER, 0);
$oldLangs = $matches[0][1]; $oldLangs = $matches[0][1];
$newLans = json_encode(array_unique(array_merge(json_decode($oldLangs),[$request->tag]))); $newLans = json_encode(array_unique(array_merge(json_decode($oldLangs), [$request->tag])));
$newConfig = (str_replace($oldLangs,$newLans,$config)); $newConfig = (str_replace($oldLangs, $newLans, $config));
file_put_contents(TRANSLATE_CONFIG_PATH,$newConfig); file_put_contents(TRANSLATE_CONFIG_PATH, $newConfig);
if (!file_exists(TRANSLATE_NEW_FILE)){ if (!file_exists(TRANSLATE_NEW_FILE)) {
file_put_contents(TRANSLATE_NEW_FILE,'{}'); file_put_contents(TRANSLATE_NEW_FILE, '{}');
} }
$xlang = new Xlang(); $xlang = new Xlang();
@ -84,7 +87,7 @@ class XlangController extends Controller
/** /**
* Display the specified resource. * Display the specified resource.
* *
* @param \App\Models\Xlang $xlang * @param \App\Models\Xlang $xlang
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function show(Xlang $xlang) public function show(Xlang $xlang)
@ -95,20 +98,20 @@ class XlangController extends Controller
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
* *
* @param \App\Models\Xlang $xlang * @param \App\Models\Xlang $xlang
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function edit(Xlang $xlang) public function edit(Xlang $xlang)
{ {
// //
return view('admin.langs.langForm',compact('xlang')); return view('admin.langs.langForm', compact('xlang'));
} }
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \App\Models\Xlang $xlang * @param \App\Models\Xlang $xlang
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function update(XlangSaveRequest $request, Xlang $xlang) public function update(XlangSaveRequest $request, Xlang $xlang)
@ -122,7 +125,7 @@ class XlangController extends Controller
/** /**
* Remove the specified resource from storage. * Remove the specified resource from storage.
* *
* @param \App\Models\Xlang $xlang * @param \App\Models\Xlang $xlang
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function destroy(Xlang $xlang) public function destroy(Xlang $xlang)
@ -133,17 +136,40 @@ class XlangController extends Controller
return redirect()->route('admin.lang.index')->with(['message' => __('Lang') . ' ' . __('deleted successfully')]); return redirect()->route('admin.lang.index')->with(['message' => __('Lang') . ' ' . __('deleted successfully')]);
} }
public function translate(){ public function translate()
{
$langs = Xlang::all();
return view('admin.langs.translateIndex', compact('langs'));
}
public function download($tag)
{
define("TRANSLATE_FILE", PREFIX_PATH . 'resources/lang/' . $tag . '.json');
return response()->download(TRANSLATE_FILE, $tag . '.json');
} }
public function upload($tag, Request $request)
{
define("TRANSLATE_FILE", PREFIX_PATH . 'resources/lang/' . $tag . '.json');
if (!$request->hasFile('json')) {
return redirect()->back();
}
$data = (file_get_contents($request->file('json')->getRealPath()));
if (json_decode($data) == null) {
return redirect()->back()->withErrors(__("Invalid json file!"));
}
file_put_contents(TRANSLATE_FILE, $data);
return redirect()->back()->with(['message' => __("Translate updated")]);
}
public function bulk(Request $request) { public function bulk(Request $request)
{
switch ($request->input('bulk')) { switch ($request->input('bulk')) {
case 'delete': case 'delete':
$msg = __('transports deleted successfully'); $msg = __('transports deleted successfully');
logAdminBatch(__METHOD__.'.'.$request->input('bulk'),XlangController::class,$request->input('id')); logAdminBatch(__METHOD__ . '.' . $request->input('bulk'), XlangController::class, $request->input('id'));
XlangController::destroy($request->input('id')); XlangController::destroy($request->input('id'));
break; break;
default: default:

@ -3601,6 +3601,12 @@ nav a {
text-decoration: none !important; text-decoration: none !important;
} }
.lang-item {
background: #ddd;
border-radius: 7px;
padding: 1rem;
}
.x64 { .x64 {
width: 64px; width: 64px;
} }

@ -255,11 +255,12 @@
"Is pinned news?": "آیا این مطلب سنجاق شود", "Is pinned news?": "آیا این مطلب سنجاق شود",
"Is pinned posts?": "آیا این مطلب سنجاق شود", "Is pinned posts?": "آیا این مطلب سنجاق شود",
"Key": "کلید", "Key": "کلید",
"LTR": "راست به چپ", "LTR": "چپ به راست",
"Label": "برچسب", "Label": "برچسب",
"Lang": "زبان", "Lang": "زبان",
"Language list": "", "Language list": "فهرست زبان‌ها",
"Languages": "", "Languages": "زبان‌ها",
"Languages translate": "",
"Last update": "آخرین به‌روز‌رسانی", "Last update": "آخرین به‌روز‌رسانی",
"Last video": "واپسین فیلم", "Last video": "واپسین فیلم",
"Leave your comment": "ارسال دیدگاه", "Leave your comment": "ارسال دیدگاه",
@ -614,4 +615,4 @@
"user": "کاربر", "user": "کاربر",
"weight": "وزن", "weight": "وزن",
"yesterday": "دیروز" "yesterday": "دیروز"
} }

@ -196,6 +196,7 @@
"Lang": "", "Lang": "",
"Language list": "", "Language list": "",
"Languages": "", "Languages": "",
"Languages translate": "",
"Last update": "", "Last update": "",
"Link": "", "Link": "",
"Login": "", "Login": "",
@ -470,4 +471,4 @@
"transports deleted successfully": "", "transports deleted successfully": "",
"updated successfully": "", "updated successfully": "",
"weight": "" "weight": ""
} }

@ -46,6 +46,11 @@ nav {
} }
} }
.lang-item{
background: #ddd;
border-radius: 7px;
padding: 1rem;
}
.x64 { .x64 {
width: 64px; width: 64px;
} }

@ -0,0 +1,38 @@
@extends('admin.adminlayout')
@section('page_title')
{{__("Languages translate")}}
-
@endsection
@section('content')
<div class="container">
@include('starter-kit::component.err')
<div class="text-center pt-3">
<div class="row">
@foreach($langs as $lang)
<div class="col-md-4">
<div class="lang-item">
<h5>
{{$lang->name}}
</h5>
<a href="{{route('admin.lang.download',$lang->tag)}}" class="btn btn-outline-dark w-100 mb-3 btn-sm">
<i class="ri-download-2-line"></i>
{{__("Download json file")}}
</a>
<form action="{{route('admin.lang.upload',$lang->tag)}}" method="post" enctype="multipart/form-data">
@csrf
<input type="file" name="json" id="file" class="form-control">
<button class="btn btn-outline-primary btn-sm w-100 mt-2">
<i class="ri-upload-2-line"></i>
{{__("Upload file")}}
</button>
</form>
</div>
</div>
@endforeach
</div>
</div>
</div>
@endsection

@ -40,6 +40,8 @@ Route::prefix(config('starter-kit.uri'))->name('admin.')->group(
Route::get('/edit/{xlang}', [\App\Http\Controllers\Admin\XlangController::class,'edit'])->name('edit'); Route::get('/edit/{xlang}', [\App\Http\Controllers\Admin\XlangController::class,'edit'])->name('edit');
Route::post('/update/{xlang}', [\App\Http\Controllers\Admin\XlangController::class,'update'])->name('update'); Route::post('/update/{xlang}', [\App\Http\Controllers\Admin\XlangController::class,'update'])->name('update');
Route::post('bulk', [\App\Http\Controllers\Admin\XlangController::class, "bulk"])->name('bulk'); Route::post('bulk', [\App\Http\Controllers\Admin\XlangController::class, "bulk"])->name('bulk');
Route::get('/download/{tag}', [\App\Http\Controllers\Admin\XlangController::class,'download'])->name('download');
Route::post('/upload/{tag}', [\App\Http\Controllers\Admin\XlangController::class,'upload'])->name('upload');
}); });

Loading…
Cancel
Save