first commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\User;
|
||||
use Auth;
|
||||
use Session;
|
||||
|
||||
class BackendController extends Controller
|
||||
{
|
||||
public function login () {
|
||||
return view('backend.login');
|
||||
}
|
||||
|
||||
public function loginAdmin (Request $request) {
|
||||
$request->validate([
|
||||
'email'=>'required',
|
||||
'password' => 'required'
|
||||
], [
|
||||
'email.required' => 'Nhập tên đăng nhập',
|
||||
'password.required' => 'Nhập mật khẩu'
|
||||
]);
|
||||
|
||||
$login = [
|
||||
'email' => $request->email,
|
||||
'password' => $request->password,
|
||||
'status' => 1
|
||||
];
|
||||
if (Auth::attempt($login)) {
|
||||
session_start();
|
||||
$_SESSION['IsAuthorized'] = true;
|
||||
return redirect()->route('admin/account')->withCookie(cookie()->forever('allowCkfinder', "1"));;
|
||||
} else {
|
||||
return redirect()->back()->with('error', 'Email hoặc Password không chính xác');
|
||||
}
|
||||
}
|
||||
|
||||
public function account () {
|
||||
$title = 'Thông tin tài khoản';
|
||||
$data = User::find(Auth::user()->id);
|
||||
return view('backend.user.account')->with('data', $data)->with('title', $title);
|
||||
}
|
||||
|
||||
public function logout () {
|
||||
Auth::logout();
|
||||
return redirect('/wp-admin');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Auth;
|
||||
use App\Models\User;
|
||||
|
||||
class GameController extends Controller
|
||||
{
|
||||
public function luckyWheel()
|
||||
{
|
||||
$game_data = [
|
||||
['id' => '1', 'label' => 'Giải 1'],
|
||||
['id' => '2', 'label' => 'Giải 2'],
|
||||
['id' => '3', 'label' => 'Giải 3'],
|
||||
['id' => '4', 'label' => 'Giải 4'],
|
||||
['id' => '5', 'label' => 'Giải 5'],
|
||||
['id' => '6', 'label' => 'Giải 6']
|
||||
];
|
||||
if (Auth::check()) {
|
||||
if (Auth::user()->prize != '') {
|
||||
$game_data_user = json_decode(Auth::user()->prize);
|
||||
$id = 1;
|
||||
$game_data = [];
|
||||
foreach ($game_data_user as $game_data_item) {
|
||||
$game_data[] = [
|
||||
'id' => $id,
|
||||
'label' => $game_data_item
|
||||
];
|
||||
$id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return view('frontend.game.luckywheel')->with('game_data', $game_data);
|
||||
}
|
||||
|
||||
public function updatePrize(Request $request)
|
||||
{
|
||||
$user_obj = User::find(Auth::user()->id);
|
||||
$user_obj->prize = json_encode($request->prizes);
|
||||
$user_obj->save();
|
||||
return response()->json(['success' => 'Thành công']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\PrizeUser;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Prize;
|
||||
use App\Models\User;
|
||||
use App\Models\Parameter;
|
||||
use Auth;
|
||||
use Validator;
|
||||
use Redirect;
|
||||
class IndexController extends Controller
|
||||
{
|
||||
|
||||
public function game() {
|
||||
$prize_obj = Prize::leftJoin('prize_users as prizeuser', 'prizes.id', '=', 'prizeuser.prize_id')
|
||||
->where('prize_status', 1)
|
||||
->selectRaw('prizes.id, prize_name, prize_order, prize_number, count(prizeuser.id) as count_user, prize_image')
|
||||
->groupBy('prizes.id', 'prize_name', 'prize_order', 'prize_number', 'prize_image')
|
||||
->orderBy('prize_order', 'asc')->get();
|
||||
$prize_arr = [];
|
||||
$user_id = '';
|
||||
if (Auth::check()) {
|
||||
$user_id = Auth::user()->id;
|
||||
}
|
||||
$lasted_prize = PrizeUser::where('user_id', $user_id)->latest()->first();
|
||||
$lasted_prize_id = '';
|
||||
$lasted_prize_name = '';
|
||||
if ($lasted_prize != null) {
|
||||
$lasted_prize_id = $lasted_prize->prize_id;
|
||||
}
|
||||
foreach ($prize_obj as $prize_item) {
|
||||
if ($prize_item->count_user < $prize_item->prize_number) {
|
||||
$prize_arr[] = Array(
|
||||
'id' => $prize_item->id,
|
||||
'image' => 'public/images/' . $prize_item->prize_image,
|
||||
'label' => "Giải " . $prize_item->prize_order,
|
||||
'detail' => $prize_item->prize_name
|
||||
);
|
||||
}
|
||||
if ($lasted_prize_id == $prize_item->id) {
|
||||
$lasted_prize_name = $prize_item->prize_name;
|
||||
}
|
||||
}
|
||||
$is_login = Auth::check();
|
||||
$spined = 0;
|
||||
if ($is_login) {
|
||||
$spined = Auth::user()->spined;
|
||||
}
|
||||
// lay tham so he thong
|
||||
$parameter_data = [];
|
||||
$parameter_obj = Parameter::whereRaw('parameter_key in ("logo_image", "banner_image")')->get();
|
||||
foreach ($parameter_obj as $parameter_item) {
|
||||
$parameter_data[$parameter_item->parameter_key] = $parameter_item->parameter_value;
|
||||
}
|
||||
return view('frontend.index')->with('game_data', $prize_arr)
|
||||
->with('is_login', $is_login)->with('spined', $spined)
|
||||
->with('lasted_prize_name', $lasted_prize_name)
|
||||
->with('parameter_data', $parameter_data);
|
||||
}
|
||||
|
||||
public function login () {
|
||||
if (Auth::check()) {
|
||||
// lấy thông tin của lịch sử nhận giải
|
||||
$history_data = PrizeUser::leftJoin('prizes as prize', 'prize_users.prize_id', '=', 'prize.id')
|
||||
->where('prize_users.user_id', Auth::user()->id)
|
||||
->select('prize.prize_name', 'prize_users.created_at')
|
||||
->orderBy('created_at', 'desc')->get();
|
||||
// nếu đã đăng nhập thì về trang chi tiết tài khoản
|
||||
return view('frontend.customer.account')->with('history_data', $history_data);
|
||||
} else {
|
||||
return view('frontend.customer.account');
|
||||
}
|
||||
}
|
||||
|
||||
public function updatePrizeUser (Request $request) {
|
||||
$request_data = [
|
||||
'user_id' => Auth::user()->id,
|
||||
'prize_id' => $request->prize_id
|
||||
];
|
||||
PrizeUser::create($request_data);
|
||||
$user_obj = User::find(Auth::user()->id);
|
||||
$user_obj->spined = 1;
|
||||
$user_obj->save();
|
||||
$prize_obj = Prize::find($request->prize_id);
|
||||
return view('frontend.game.luckywheel_prize')->with('prize_obj', $prize_obj)->render();
|
||||
}
|
||||
|
||||
public function updateAccount (Request $request) {
|
||||
$user_id = '';
|
||||
if (!Auth::check()) {
|
||||
return redirect()->back()->with('error', 'Có lỗi xảy ra, vui lòng đăng nhập lại');
|
||||
}
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required|max:255',
|
||||
'phone' => [
|
||||
'required',
|
||||
'regex:/^0[0-9]{9}$/',
|
||||
],
|
||||
'address' => 'required|max:255',
|
||||
'known_sis' => 'required|in:0,1',
|
||||
'job' => 'required|max:255',
|
||||
'password' => 'nullable|max:10'
|
||||
], [
|
||||
'name.required' => 'Nhập tên hiển thị',
|
||||
'name.max' => 'Tên hiển thị quá dài',
|
||||
'phone.required' => 'Nhập số điện thoại',
|
||||
'phone.regex' => 'Số điện thoại không đúng định dạng',
|
||||
'job.required' => 'Nhập nghề nghiệp',
|
||||
'job.max' => 'Nghề nghiệp quá dài',
|
||||
'password.max' => 'Độ dài tối đa 10'
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
return Redirect::back()->withErrors($validator)->withInput();
|
||||
}
|
||||
$user_obj = User::find(Auth::user()->id);
|
||||
$user_obj->name = mb_strtoupper($request->name, 'UTF-8');
|
||||
$user_obj->phone = $request->phone;
|
||||
$user_obj->address = $request->address;
|
||||
$user_obj->known_sis = $request->known_sis;
|
||||
$user_obj->job = $request->job;
|
||||
if ($request->password != '') {
|
||||
$user_obj->password = bcrypt($request->password);
|
||||
}
|
||||
$user_obj->save();
|
||||
|
||||
return redirect('game')->with('success', 'Cập nhật tài khoản thành công');
|
||||
}
|
||||
|
||||
public function createAccount (Request $request) {
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required|max:255',
|
||||
'phone' => [
|
||||
'required',
|
||||
'regex:/^0[0-9]{9}$/',
|
||||
],
|
||||
'address' => 'required|max:255',
|
||||
'known_sis' => 'required|in:0,1',
|
||||
'job' => 'required|max:255',
|
||||
'password' => 'nullable|max:10'
|
||||
], [
|
||||
'name.required' => 'Nhập tên hiển thị',
|
||||
'name.max' => 'Tên hiển thị quá dài',
|
||||
'phone.required' => 'Nhập số điện thoại',
|
||||
'phone.regex' => 'Số điện thoại không đúng định dạng',
|
||||
'address.required' => 'Nhập nơi sinh sống',
|
||||
'address.max' => 'Nơi sinh sống quá dài',
|
||||
'known_sis.required' => 'Chọn Có/Không cho câu hỏi về SIS',
|
||||
'known_sis.in' => 'Giá trị không hợp lệ cho câu hỏi về SIS',
|
||||
'job.required' => 'Nhập nghề nghiệp',
|
||||
'job.max' => 'Nghề nghiệp quá dài',
|
||||
'password.max' => 'Độ dài tối đa 10'
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
return Redirect::back()->withErrors($validator)->withInput();
|
||||
}
|
||||
$user_exist = User::where('phone', $request->phone)->first();
|
||||
if ($user_exist != null) {
|
||||
$user_exist->name = mb_strtoupper($request->name, 'UTF-8');
|
||||
$user_exist->address = $request->address;
|
||||
$user_exist->known_sis = $request->known_sis;
|
||||
$user_exist->job = $request->job;
|
||||
$user_exist->save();
|
||||
Auth::loginUsingId($user_exist->id);
|
||||
session()->regenerate();
|
||||
return redirect('game')->with('success', 'Đăng nhập thành công');
|
||||
}
|
||||
$request_data = [
|
||||
'name' => mb_strtoupper($request->name, 'UTF-8'),
|
||||
'phone' => $request->phone,
|
||||
'address' => $request->address,
|
||||
'known_sis' => $request->known_sis,
|
||||
'job' => $request->job,
|
||||
'email' => $request->phone,
|
||||
'password' => bcrypt('dq123'),
|
||||
'status' => 1
|
||||
];
|
||||
$user_obj = User::create($request_data);
|
||||
Auth::loginUsingId($user_obj->id);
|
||||
session()->regenerate();
|
||||
|
||||
return redirect('game')->with('success', 'Đăng nhập thành công');
|
||||
}
|
||||
|
||||
public function logout () {
|
||||
Auth::logout();
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
public function register () {
|
||||
if (Auth::check()) {
|
||||
// nếu đã đăng nhập thì về trang chi tiết tài khoản
|
||||
return view('frontend.customer.account');
|
||||
} else {
|
||||
return view('frontend.customer.register');
|
||||
}
|
||||
}
|
||||
|
||||
public function getGameRule () {
|
||||
$data = Parameter::where('parameter_key', 'game_rule')->first();
|
||||
return view('frontend.game.rule_popup')->with('data', $data)->render();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Prize;
|
||||
use App\Models\PrizeUser;
|
||||
use DateTime;
|
||||
use Auth;
|
||||
use File;
|
||||
|
||||
class PrizeController extends Controller
|
||||
{
|
||||
public function index() {
|
||||
$title = "Danh sách giải thưởng";
|
||||
$route_name_update = 'admin/prize/update';
|
||||
$data = Prize::leftJoin('prize_users as user', 'prizes.id', '=', 'user.prize_id')
|
||||
->groupBy('prizes.id', 'prizes.prize_name', 'prizes.prize_image', 'prizes.prize_content', 'prizes.prize_status',
|
||||
'prizes.prize_number', 'prizes.prize_order')
|
||||
->selectRaw('prizes.id, prizes.prize_name, prizes.prize_image, prizes.prize_content, prizes.prize_status,
|
||||
prizes.prize_number, prizes.prize_order, count(user.id) as prize_count')
|
||||
->orderBy('prize_status', 'desc')->orderBy('prize_order', 'asc')->orderBy('prize_name')->get();
|
||||
return view('backend.prize.index')->with('title', $title)
|
||||
->with('route_name_update', $route_name_update)
|
||||
->with('data', $data);
|
||||
}
|
||||
|
||||
public function getPrize(Request $request) {
|
||||
$title = 'Cập nhật giải thưởng';
|
||||
if ($request->id == 0) {
|
||||
$title = 'Thêm mới giải thưởng';
|
||||
}
|
||||
$data = Prize::find($request->id);
|
||||
|
||||
return view('backend.prize.detail')
|
||||
->with('data', $data)
|
||||
->with('title', $title);
|
||||
}
|
||||
|
||||
public function updatePrize (Request $request) {
|
||||
$request->validate([
|
||||
'prize_name'=>'required|max:255',
|
||||
'prize_image'=>'mimes:jpg,png,jpeg,webp,jfif|max:2048|nullable',
|
||||
'prize_status' => 'required|numeric',
|
||||
'prize_number' => 'nullable|numeric',
|
||||
'prize_order' => 'required|numeric'
|
||||
], [
|
||||
'prize_name.required' => 'Nhập tên giải thưởng',
|
||||
'prize_name.max' => 'Tên giải thưởng không được quá 255 kí tự',
|
||||
'prize_image.mimes' => 'Định dạng file không đúng',
|
||||
'prize_image.max' => 'File upload quá lớn',
|
||||
'prize_status.required' => 'Vui lòng chọn trạng thái',
|
||||
'prize_status.numeric' => 'Trạng thái không đúng định dạng',
|
||||
'prize_number.numeric' => 'Số lượng giải phải là kiểu số',
|
||||
'prize_order.required' => 'Nhập số thứ tự giải',
|
||||
'prize_order.numeric' => 'Số thứ tự giải phải là số'
|
||||
]);
|
||||
|
||||
if ($request->id == 0){
|
||||
// thêm mới
|
||||
$request_data = $request->except(['id', 'prize_image']);
|
||||
$request_data['user_id'] = Auth::user()->id;
|
||||
$prize_obj = Prize::create($request_data);
|
||||
$id = $prize_obj->id;
|
||||
|
||||
$this->updatePrizeImage($request, $prize_obj);
|
||||
|
||||
return redirect()->route('admin/prize/update', array('id' => $id))->with('success', 'Thêm mới thành công');
|
||||
} else {
|
||||
// cap nhat
|
||||
$id = $request->id;
|
||||
$prize_obj = Prize::find($id);
|
||||
$prize_obj->prize_name = $request->prize_name;
|
||||
$prize_obj->prize_content = $request->prize_content;
|
||||
$prize_obj->prize_status = $request->prize_status;
|
||||
$prize_obj->prize_number = $request->prize_number;
|
||||
$prize_obj->prize_order = $request->prize_order;
|
||||
$prize_obj->save();
|
||||
|
||||
$this->updatePrizeImage($request, $prize_obj);
|
||||
|
||||
return redirect()->back()->with('success', 'Cập nhật thành công');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function updatePrizeImage ($request, $prize_obj) {
|
||||
if($request->prize_image) {
|
||||
$imageName = time(). '_' . Auth::user()->id . '.'.$request->prize_image->extension();
|
||||
$request->prize_image->move(public_path('images/prize'), $imageName);
|
||||
$previous_img_path = $prize_obj->prize_image;
|
||||
$prize_obj->prize_image = 'prize/' . $imageName;
|
||||
$prize_obj->save();
|
||||
if($previous_img_path != '' && File::exists(public_path('images') . '/' . $previous_img_path)) {
|
||||
File::delete(public_path('images') . '/' . $previous_img_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function deletePrize ($id) {
|
||||
$prize_obj = Prize::find($id);
|
||||
$previous_image_path = $prize_obj->prize_image;
|
||||
if ($prize_obj->delete()) {
|
||||
if($previous_image_path != '' && File::exists(public_path('images') . '/' . $previous_image_path)) {
|
||||
File::delete(public_path('images') . '/' . $previous_image_path);
|
||||
}
|
||||
} else {
|
||||
return redirect()->back()->with('error', 'Xóa giải thưởng thất bại');
|
||||
}
|
||||
return redirect()->back()->with('success', 'Xóa giải thưởng thành công');
|
||||
}
|
||||
|
||||
public function prizeuser (Request $request) {
|
||||
$title = "Danh sách người trúng giải";
|
||||
$data_obj = PrizeUser::leftJoin('prizes as prize', 'prize_users.prize_id', '=', 'prize.id')
|
||||
->leftJoin('users as user', 'prize_users.user_id', '=', 'user.id')
|
||||
->select('user.id', 'user.name', 'user.phone', 'user.job', 'user.address', 'user.known_sis', 'prize.prize_name', 'prize_users.created_at');
|
||||
$fromdate = $request->tungay ?? date('d/m/Y', strtotime('-30 days'));
|
||||
$todate = $request->denngay ?? date('d/m/Y');
|
||||
|
||||
if ($fromdate != '') {
|
||||
$fromdate_arr = explode('/', $fromdate);
|
||||
$from_date = DateTime::createFromFormat('Y-m-d', $fromdate_arr[2] . '-' . $fromdate_arr[1] . '-' . $fromdate_arr[0])->format('Y-m-d');
|
||||
$data_obj = $data_obj->where('prize_users.created_at', '>=', $from_date);
|
||||
}
|
||||
|
||||
if ($todate != '') {
|
||||
$todate_arr = explode('/', $todate);
|
||||
$to_date = DateTime::createFromFormat('Y-m-d', $todate_arr[2] . '-' . $todate_arr[1] . '-' . $todate_arr[0])->format('Y-m-d');
|
||||
$data_obj = $data_obj->where('prize_users.created_at', '<=', $to_date . ' 23:59:00');
|
||||
}
|
||||
$data = $data_obj->orderBy('created_at', 'desc')->get();
|
||||
return view('backend.prizeuser.index')
|
||||
->with('title', $title)->with('data', $data)
|
||||
->with('fromdate', $fromdate)->with('todate', $todate);
|
||||
}
|
||||
|
||||
public function exportPrizeUser (Request $request) {
|
||||
$data_obj = PrizeUser::leftJoin('prizes as prize', 'prize_users.prize_id', '=', 'prize.id')
|
||||
->leftJoin('users as user', 'prize_users.user_id', '=', 'user.id')
|
||||
->select('user.id', 'user.name', 'user.phone', 'user.job', 'user.address', 'user.known_sis', 'prize.prize_name', 'prize_users.created_at');
|
||||
$fromdate = $request->tungay;
|
||||
$todate = $request->denngay;
|
||||
|
||||
if ($fromdate != '') {
|
||||
$fromdate_arr = explode('/', $fromdate);
|
||||
$from_date = DateTime::createFromFormat('Y-m-d', $fromdate_arr[2] . '-' . $fromdate_arr[1] . '-' . $fromdate_arr[0])->format('Y-m-d');
|
||||
$data_obj = $data_obj->where('prize_users.created_at', '>=', $from_date);
|
||||
}
|
||||
|
||||
if ($todate != '') {
|
||||
$todate_arr = explode('/', $todate);
|
||||
$to_date = DateTime::createFromFormat('Y-m-d', $todate_arr[2] . '-' . $todate_arr[1] . '-' . $todate_arr[0])->format('Y-m-d');
|
||||
$data_obj = $data_obj->where('prize_users.created_at', '<=', $to_date);
|
||||
}
|
||||
$data = $data_obj->orderBy('created_at', 'desc')->get();
|
||||
return view('backend.prizeuser.excel')->with('data', $data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\User;
|
||||
use App\Models\Role;
|
||||
use App\Models\UserRole;
|
||||
use Auth;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function users (Request $request) {
|
||||
$data_obj = User::where('status', '!=', '');
|
||||
if ($request->type != '') {
|
||||
$data_obj = $data_obj->where('account_type', $request->type);
|
||||
}
|
||||
$data = $data_obj->orderBy('name')->get();
|
||||
$route_name_update = 'admin/users/update';
|
||||
$title = 'Danh mục người dùng';
|
||||
return view('backend.user.index')->with('data', $data)
|
||||
->with('title', $title)
|
||||
->with('route_name_update', $route_name_update)
|
||||
->with('request_type', $request->type);
|
||||
}
|
||||
|
||||
public function getUser(Request $request) {
|
||||
$title = 'Cập nhật người dùng';
|
||||
if ($request->id == 0) {
|
||||
$title = 'Thêm mới người dùng';
|
||||
}
|
||||
$data = User::find($request->id);
|
||||
$role_data = Role::where('role_status', 1)
|
||||
->orderBy('role_order', 'asc')
|
||||
->orderBy('role_name', 'asc')->get();
|
||||
$user_role_data = UserRole::where('user_id', $request->id)->get();
|
||||
$user_role = [];
|
||||
foreach ($user_role_data as $user_role_item) {
|
||||
$user_role[] = $user_role_item['role_id'];
|
||||
}
|
||||
|
||||
return view('backend.user.detail')
|
||||
->with('data', $data)
|
||||
->with('title', $title)
|
||||
->with('role_data', $role_data)
|
||||
->with('user_role', $user_role);
|
||||
}
|
||||
|
||||
public function updateUser(Request $request) {
|
||||
if ($request->type == 'account') {
|
||||
// cap nhat tai khoan ca nhan
|
||||
$request->validate([
|
||||
'name'=>'required|max:50',
|
||||
'email'=>'required|unique:users,email,' . $request->id . ',id'
|
||||
], [
|
||||
'name.required' => 'Nhập tên hiển thị',
|
||||
'name.max' => 'Tên hiển thị vượt quá ký tự cho phép',
|
||||
'email.required' => 'Nhập tên đăng nhập',
|
||||
'email.unique' => 'Tên đăng nhập bị trùng'
|
||||
]);
|
||||
} else {
|
||||
$request->validate([
|
||||
'name'=>'required|max:50',
|
||||
'email'=>'required|unique:users,email,' . $request->id . ',id',
|
||||
'status' => 'required'
|
||||
], [
|
||||
'name.required' => 'Nhập tên hiển thị',
|
||||
'name.max' => 'Tên hiển thị vượt quá ký tự cho phép',
|
||||
'email.required' => 'Nhập tên đăng nhập',
|
||||
'email.unique' => 'Tên đăng nhập bị trùng',
|
||||
'status.required' => 'Nhập trạng thái'
|
||||
]);
|
||||
}
|
||||
|
||||
if ($request->id == 0){
|
||||
$request_data = $request->except('id');
|
||||
$request_data['name'] = $request->name;
|
||||
$request_data['email'] = $request->email;
|
||||
if ($request->password != '') {
|
||||
$request_data['password'] = bcrypt($request->password);
|
||||
} else {
|
||||
$request_data['password'] = bcrypt('123');
|
||||
}
|
||||
$request_data['status'] = $request->status;
|
||||
$id = User::create($request_data)->id;
|
||||
$role_obj = Role::where('role_code', 'admin_account')->first();
|
||||
if ($role_obj != null) {
|
||||
$role_id = $role_obj->id;
|
||||
UserRole::create(Array(
|
||||
'user_id' => $id,
|
||||
'role_id' => $role_id
|
||||
));
|
||||
}
|
||||
return redirect()->route('admin/users/update', array('id' => $id));
|
||||
} else {
|
||||
// cap nhat
|
||||
$id = $request->id;
|
||||
$user_obj = User::find($id);
|
||||
$user_obj->name = $request->name;
|
||||
if ($request->status != '') {
|
||||
$user_obj->status = $request->status;
|
||||
}
|
||||
if ($request->password != '') {
|
||||
$user_obj->password = bcrypt($request->password);
|
||||
}
|
||||
$user_obj->save();
|
||||
if (!empty($request->user_role)) {
|
||||
UserRole::where('user_id', $id)->delete();
|
||||
foreach ($request->user_role as $role_id) {
|
||||
UserRole::create(Array(
|
||||
'user_id' => $id,
|
||||
'role_id' => $role_id
|
||||
));
|
||||
}
|
||||
}
|
||||
return redirect()->back()->with('success', 'Cập nhật thành công ');
|
||||
}
|
||||
}
|
||||
|
||||
public function updateSpined (Request $request) {
|
||||
$user_id = $request->user_id;
|
||||
$user_obj = User::find($user_id);
|
||||
if ($user_obj != null) {
|
||||
$user_obj->spined = 0;
|
||||
$user_obj->save();
|
||||
}
|
||||
return response()->json(['success' => 'Thành công']);
|
||||
}
|
||||
|
||||
public function checkspined () {
|
||||
if (!Auth::check()) {
|
||||
return response()->json(['error' => 'Chưa đăng nhập', 'message' => 'Người dùng phải đăng nhập trước khi thực hiện']);
|
||||
}
|
||||
$user_obj = User::find(Auth::user()->id);
|
||||
if ($user_obj == null) {
|
||||
return response()->json(['error' => 'Không tồn tại tài khoản', 'message' => 'Không tìm thấy tài khoản']);
|
||||
}
|
||||
return response()->json(['success' => 'Lấy dữ liệu thành công', 'spined' => $user_obj->spined]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user