52 lines
2.1 KiB
PHP
52 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Parameter;
|
|
use Auth;
|
|
use Session;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
class ParameterController extends Controller
|
|
{
|
|
public function index() {
|
|
$data = Parameter::orderBy('parameter_order', 'asc')->get();
|
|
$title = 'Cấu hình hệ thống';
|
|
return view('backend.parameter.index')->with('data', $data)->with('title', $title);
|
|
}
|
|
|
|
public function update (Request $request) {
|
|
foreach ($request->except('_token') as $key => $value) {
|
|
$object = Parameter::where('parameter_key', $key)->first();
|
|
if ($object != '') {
|
|
if ($key == 'logo_image' || $key == 'banner_image') {
|
|
$this->updateParameterImage($key, $request, $object);
|
|
} else {
|
|
$object->parameter_value = $value;
|
|
$object->save();
|
|
}
|
|
}
|
|
}
|
|
return redirect()->back()->with('success', 'Cập nhật thành công');
|
|
}
|
|
public function updateParameterImage ($input_name, $request, $object) {
|
|
if($request->file($input_name)) {
|
|
$imageName = $input_name . '.' . $request->file($input_name)->extension();
|
|
$counter = 0;
|
|
// Vòng lặp kiểm tra tên file và thêm số nếu trùng
|
|
while (File::exists(public_path('images') . '/parameter/' . $imageName)) {
|
|
$counter++;
|
|
$imageName = $input_name . '(' . $counter . ').' . $request->file($input_name)->extension();
|
|
}
|
|
$request->file($input_name)->move(public_path('images/parameter'), $imageName);
|
|
$previous_img_path = $object->parameter_value;
|
|
$object->parameter_value = 'parameter/' . $imageName;
|
|
$object->save();
|
|
if($previous_img_path != '' && File::exists(public_path('images') . '/' . $previous_img_path)) {
|
|
File::delete(public_path('images') . '/' . $previous_img_path);
|
|
}
|
|
}
|
|
}
|
|
}
|