2020-06-30 09:35:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
2020-07-27 14:03:49 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-06-30 09:35:32 +00:00
|
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
|
|
|
|
class rolesController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
$roles = Role::query()->paginate($request->input("limit", 20));
|
|
|
|
|
2020-06-30 09:51:20 +00:00
|
|
|
return Response::detect("roles.index", [ "roles" => $roles]);
|
2020-06-30 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return Response::detect("roles.create");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$requestRole = $request->validate([
|
2020-06-30 09:55:11 +00:00
|
|
|
"name" => "required|max:255",
|
|
|
|
"description" => "required|max:255"
|
2020-06-30 09:35:32 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$role = new Role($requestRole);
|
2020-07-01 07:16:53 +00:00
|
|
|
$saved = $role->save();
|
|
|
|
|
|
|
|
if(!$saved){
|
|
|
|
return Response::detect("roles.store");
|
|
|
|
}else{
|
|
|
|
$roles = Role::query()->paginate($request->input("limit", 20));
|
|
|
|
return Response::detect("roles.index", ['roles' => $roles]);
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:35:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
return Response::detect("roles.show", [ "role" => $id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
$role = Role::find($id);
|
|
|
|
return Response::detect("roles.edit", ["role" => $role]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
$data = $request->all();
|
2020-06-30 10:02:32 +00:00
|
|
|
$role = Role::find($id);
|
2020-06-30 09:35:32 +00:00
|
|
|
$role->update($data);
|
2020-07-01 07:16:53 +00:00
|
|
|
$saved = $role->save();
|
|
|
|
|
|
|
|
if(!$saved){
|
|
|
|
return Response::detect("roles.update", [ "role" => $role ]);
|
|
|
|
}else{
|
|
|
|
$roles = Role::query()->paginate($request->input("limit", 20));
|
|
|
|
return Response::detect("roles.index", ['roles' => $roles]);
|
|
|
|
}
|
2020-06-30 09:35:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2020-06-30 10:02:32 +00:00
|
|
|
$role = Role::find($id);
|
2020-06-30 09:35:32 +00:00
|
|
|
$role->delete();
|
|
|
|
return redirect()->route("roles.index");
|
|
|
|
}
|
2020-07-27 14:03:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
public function search(Request $request){
|
|
|
|
if($request->ajax()){
|
|
|
|
$output = "<tr>".
|
|
|
|
"<th>Navn</th>".
|
|
|
|
"<th>Beskrivelse</th>".
|
|
|
|
"<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/pencil.svg\" alt=\"Update\"></th>".
|
|
|
|
"<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/trashcan.svg\" alt=\"Delete\"></th>".
|
|
|
|
"</tr>";
|
|
|
|
$roles = DB::table('roles')->where('name', 'LIKE',$request->search.'%')->get();
|
|
|
|
|
|
|
|
if(count($roles) !== 0){
|
|
|
|
foreach ($roles as $key => $role){
|
|
|
|
$output.='<tr>'.
|
|
|
|
'<td>' . $role->name . '</td>'.
|
|
|
|
'<td>' . $role->description . '</td>'.
|
|
|
|
'<td><a href="'. route("roles.edit", [ "role" => $role->id ]) . '"><img class="w-100" src="'. asset('/images/icons/pencil-dark.svg') . '" alt="Update"></a></td>'.
|
|
|
|
'<td><form method="post" action="' .route("roles.destroy", [ "role" => $role->id ]). '" class="w-100 nostyle">'.
|
|
|
|
csrf_field().
|
|
|
|
method_field("delete").
|
|
|
|
|
|
|
|
'<button class="w-100 nostyle" onclick="return confirm(\'Are you sure you want to delete?\');" type="submit"><img class="w-100 cursor-pointer" src="'. asset('/images/icons/trashcan-dark.svg') . '" alt="Delete"></button>'.
|
|
|
|
'</form>'.
|
|
|
|
'</td>'.
|
|
|
|
'</tr>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Response($output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-30 09:35:32 +00:00
|
|
|
}
|