2020-06-29 09:57:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Staff;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
|
|
class StaffController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// $this->middleware([ "auth" ])->only("logout");
|
|
|
|
// $this->middleware([ "guest" ])->only("login");
|
|
|
|
//
|
2020-06-29 10:29:23 +00:00
|
|
|
// $this->middleware([ "permission:staff.list", "role:admin" ])->only("index");
|
|
|
|
// $this->middleware([ "permission:staff.show", "role:admin" ])->only("show");
|
|
|
|
// $this->middleware([ "permission:staff.edit", "role:admin" ])->only([ "edit", "update" ]);
|
|
|
|
// $this->middleware([ "permission:staff.delete", "role:admin" ])->only("delete");
|
2020-06-29 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2020-06-29 10:29:23 +00:00
|
|
|
$staffs = Staff::query()->paginate($request->query("page", 20));
|
2020-06-29 09:57:11 +00:00
|
|
|
|
2020-06-29 10:29:23 +00:00
|
|
|
return Response::detect("staff.index", [ "staffs" => $staffs ]);
|
2020-06-29 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return Response::detect("staff.create");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$data = $request->validate([
|
|
|
|
"name_first" => "required|max:255",
|
|
|
|
"name_last" => "required|max:255",
|
2020-06-29 10:29:23 +00:00
|
|
|
"email" => "required|email|unique:staff",
|
2020-06-29 09:57:11 +00:00
|
|
|
"password" => "required|max:60",
|
2020-06-29 11:36:51 +00:00
|
|
|
"phone" => "required|unique:staff"
|
2020-06-29 09:57:11 +00:00
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
$staff = new Staff($data);
|
|
|
|
$staff->save();
|
|
|
|
|
2020-06-29 12:26:39 +00:00
|
|
|
return Response::detect("staff.store");
|
2020-06-29 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$staff = Staff::find($id);
|
|
|
|
|
|
|
|
return Response::detect("staff.show", [
|
|
|
|
"staff" => $staff
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
$staff = Staff::find($id);
|
|
|
|
|
|
|
|
return Response::detect("staff.edit", [
|
|
|
|
"staff" => $staff
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
$data = $request->all();
|
|
|
|
|
|
|
|
// $data = $request->validate([
|
|
|
|
// "name_first" => "max:255",
|
|
|
|
// "name_last" => "max:255",
|
2020-06-29 10:29:23 +00:00
|
|
|
// "email" => "email|unique:staff",
|
2020-06-29 09:57:11 +00:00
|
|
|
// "password" => "max:60",
|
2020-06-29 10:29:23 +00:00
|
|
|
// "phone" => "unique:staff",
|
2020-06-29 09:57:11 +00:00
|
|
|
// ]);
|
|
|
|
|
2020-06-29 10:29:23 +00:00
|
|
|
// Validates if the staff is updating itself or another staff.
|
2020-06-29 09:57:11 +00:00
|
|
|
// if($id === Auth::id()) {
|
2020-06-29 10:29:23 +00:00
|
|
|
// $staff = Auth::staff();
|
2020-06-29 09:57:11 +00:00
|
|
|
//
|
2020-06-29 10:29:23 +00:00
|
|
|
// $staff->update($data);
|
2020-06-29 09:57:11 +00:00
|
|
|
//
|
2020-06-29 10:29:23 +00:00
|
|
|
// $staff->save();
|
|
|
|
// return Response::detect("staff.edit", [
|
|
|
|
// "staff" => $staff
|
2020-06-29 09:57:11 +00:00
|
|
|
// ]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
//TODO: Implement when security's ready!!!
|
2020-06-29 10:29:23 +00:00
|
|
|
// else if(Auth::staff()->hasPermissionTo("staff.edit")) {
|
2020-06-29 09:57:11 +00:00
|
|
|
$staff = Staff::find($id);
|
|
|
|
|
|
|
|
/** @var Staff $staff */
|
|
|
|
$staff->update($data);
|
|
|
|
|
|
|
|
$staff->save();
|
|
|
|
// }
|
|
|
|
|
|
|
|
$staffs = Staff::query()->paginate(20);
|
|
|
|
|
2020-06-29 10:29:23 +00:00
|
|
|
return Response::detect("staff.index", [
|
2020-06-29 09:57:11 +00:00
|
|
|
"staffs" => $staffs
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
2020-06-29 12:26:39 +00:00
|
|
|
public function destroy(Staff $id)
|
2020-06-29 09:57:11 +00:00
|
|
|
{
|
2020-06-29 12:26:39 +00:00
|
|
|
$id->delete();
|
2020-06-29 09:57:11 +00:00
|
|
|
return redirect()->route("staff.index");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************/
|
|
|
|
/* Authentication */
|
|
|
|
/*******************************************/
|
|
|
|
|
|
|
|
public function showLogin() {
|
|
|
|
return view("admin.staff.login");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function login(Request $request) {
|
|
|
|
$data = $request->only("email", "password");
|
|
|
|
|
|
|
|
if(Auth::attempt($data)) {
|
|
|
|
//TODO: Implement home?
|
|
|
|
return redirect()->route("staff.index");
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back(303);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function logout(Request $request) {
|
|
|
|
Auth::logout();
|
|
|
|
|
|
|
|
return redirect()->to("/");
|
|
|
|
}
|
|
|
|
}
|