193 lines
6.4 KiB
PHP
193 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\ExternalLink;
|
|
use App\MenuPlan;
|
|
use App\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
|
|
class MenuPlanController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware([ "auth" ]);
|
|
$this->middleware([ "lang" ]);
|
|
|
|
$this->middleware([ "check.auth:menuplan.list" ])->only("index");
|
|
$this->middleware([ "check.auth:menuplan.show" ])->only("show");
|
|
$this->middleware([ "check.auth:menuplan.create" ])->only("create", "store");
|
|
$this->middleware([ "check.auth:menuplan.edit" ])->only("edit", "update");
|
|
$this->middleware([ "check.auth:menuplan.delete" ])->only("delete");
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$menuPlans = MenuPlan::query()->paginate($request->input("limit", 20));
|
|
|
|
return Response::detect("menuplans.index", [ "menuPlans" => $menuPlans]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
return Response::detect("menuplans.create");
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$requestMenuPlanCreate = $request->validate([
|
|
"week" => "required|max:2",
|
|
"monday" => "required|max:255",
|
|
"tuesday" => "required|max:255",
|
|
"wednesday" => "required|max:255",
|
|
"thursday" => "required|max:255"
|
|
]);
|
|
|
|
$menuPlans = new MenuPlan($requestMenuPlanCreate);
|
|
|
|
$allMenuPlans = MenuPlan::query()->where('week', '=', $request->week)->get();
|
|
|
|
if(count($allMenuPlans) > 0)
|
|
return redirect()->route("menu-plans.index")->with('WeekExists', '<p><b>Der findes allerede en menuplan for den uge!</b></p>');
|
|
else {
|
|
$menuPlans->save();
|
|
$menuPlans = MenuPlan::query()->paginate($request->input("limit", 20));
|
|
return redirect()->route("menu-plans.index", ['menuPlans' => $menuPlans]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(MenuPlan $id)
|
|
{
|
|
return Response::detect("menuplans.show", [ "menuplan" => $id]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$menuplan = MenuPlan::find($id);
|
|
return Response::detect("menuplans.edit", ["menuplan" => $menuplan]);
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
//FORCED UPDATE
|
|
|
|
$menuplan = MenuPlan::find($id);
|
|
|
|
$allMenuPlans = MenuPlan::query()->where('week', '=', $request->week)->where('id', '!=', $id)->get();
|
|
|
|
if(count($allMenuPlans) > 0){
|
|
return redirect()->route("menu-plans.index")->with('WeekExists', '<p><b>Der findes allerede en menuplan for den uge!</b></p>');
|
|
}else{
|
|
$menuplan->update($data);
|
|
$menuplan->save();
|
|
|
|
$menuPlans = MenuPlan::query()->paginate($request->input("limit", 20));
|
|
return redirect()->route("menu-plans.index", ['menuPlans' => $menuPlans]);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
* Test delete return index function.
|
|
* @param int $id
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$menuplan = MenuPlan::find($id);
|
|
$menuplan->delete();
|
|
return redirect()->route("menu-plans.index");
|
|
}
|
|
|
|
|
|
public function search(Request $request){
|
|
if($request->ajax()){
|
|
$output = "<tr>".
|
|
"<th>Uge</th>".
|
|
"<th>Mandag</th>".
|
|
"<th>Tirsdag</th>".
|
|
"<th>Onsdag</th>".
|
|
"<th>Torsdag</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>";
|
|
$menuplans = DB::table('menu_plans')->where('week', 'LIKE',$request->search.'%')->get();
|
|
|
|
if(count($menuplans) !== 0){
|
|
foreach ($menuplans as $key => $menuplan){
|
|
$output.='<tr>'.
|
|
'<td>' . $menuplan->week . '</td>'.
|
|
'<td>' . $menuplan->monday . '</td>'.
|
|
'<td>' . $menuplan->tuesday . '</td>'.
|
|
'<td>' . $menuplan->wednesday .'</td>'.
|
|
'<td>' . $menuplan->thursday .'</td>'.
|
|
'<td><a href="'. route("menu-plans.edit", [ "menu_plan" => $menuplan->id ]) . '"><img class="w-100" src="'. asset('/images/icons/pencil-dark.svg') . '" alt="Update"></a></td>'.
|
|
'<td><form method="post" action="' .route("menu-plans.destroy", [ "menu_plan" => $menuplan->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>';
|
|
}
|
|
}else{
|
|
$output.='<tr>'.
|
|
'<td>Intet match</td>'.
|
|
'<td></td>'.
|
|
'<td></td>'.
|
|
'<td></td>'.
|
|
'<td></td>'.
|
|
'<td></td>'.
|
|
'<td></td>'.
|
|
'</tr>';
|
|
}
|
|
return Response($output);
|
|
}
|
|
}
|
|
|
|
|
|
}
|