middleware([ "auth" ]); $this->middleware([ "lang" ]); $this->middleware([ "check.auth:menuplan.show" ])->only("show", "index"); $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()->get(); 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", "monday" => "required", "tuesday" => "required", "wednesday" => "required", "thursday" => "required", ]); $menuPlan = new MenuPlan($requestMenuPlanCreate); $allMenuPlans = MenuPlan::query()->where('week', '=', $request->week)->get(); if(count($allMenuPlans) > 0) return redirect()->route("menu-plans.index"); else { $menuPlan->save(); $menuPlans = MenuPlan::query()->get(); if($request->newsoption == true){ $news = new News(); $news->name = "Ny menuplan"; $news->subname = "Menuplan for uge " . $menuPlan->week; $news->content = '
'.$menuPlan->monday.'
'.$menuPlan->tuesday.'
'.$menuPlan->wednesday.'
'.$menuPlan->thursday.'
'; $news->type_id = '2'; $news->arrangement_id = $menuPlan->id; if($request->file("resource")) { $news->resource_id = ResourceController::store($request)->id; } NewsController::storeAndGet($news); } 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"); }else{ $menuplan->update($data); $menuplan->save(); $menuPlans = MenuPlan::query()->get(); //Create news if($request->newsoption == true){ $news = new News(); $news->name = "Opdateret menuplan"; $news->subname = "Opdateret menuplan for uge " . $menuplan->week; $news->arrangement_id = $menuplan->id; $news->type_id = '2'; $news->content = ''.$menuplan->monday.'
'.$menuplan->tuesday.'
'.$menuplan->wednesday.'
'.$menuplan->thursday.'
'; $oldResourceID = News::query()->where('arrangement_id', '=', $menuplan->id)->first(); if ($oldResourceID != null) $news->resource_id = $oldResourceID->resource_id; NewsController::storeAndGet($news); } return redirect()->route("menu-plans.index", ['menuPlans' => $menuPlans]); } } /** * Remove the specified resource from storage. * Test delete return index function. * @param int $id * @return RedirectResponse */ public function destroy($id) { // Find the menu you want to remove $menuplan = MenuPlan::find($id); // Find the news the you made when creating the menu (if there is a news) $news = News::query()->join('news_types', 'news_types.id', '=', 'news.type_id')->where('type', '=', 'Menu')->where('arrangement_id', '=', $id); //Delete the news or/and menu $news->delete(); $menuplan->delete(); return redirect()->route("menu-plans.index"); } /** * Remove the specified resource from storage. * Test delete return index function. * @param $Id * @return RedirectResponse */ public function genPDF($Id){ $menuPlan = MenuPlan::query()->where("id", "=", $Id)->first(); $var = 250; $menuPlan->monday = substr($menuPlan->monday, 0, $var); $menuPlan->tuesday = substr($menuPlan->tuesday, 0, $var); $menuPlan->wednesday = substr($menuPlan->wednesday, 0, $var); $menuPlan->thursday = substr($menuPlan->thursday, 0, $var); $pdf = PDF::loadView('pdf.menuplan', ["menuPlan" => $menuPlan]); $pdf->setPaper('A4', 'portrait'); return $pdf->stream("menuplan_uge_".$menuPlan->week.".pdf"); } public function nameCheck(Request $request){ $menuplan = MenuPlan::query()->where('week', 'LIKE',$request->nameCheck)->get(); if(count($menuplan) > 0 && $request->nameCheck !== NULL){ return 1; } } public function nameCheckUpdate(Request $request){ $menuplan = MenuPlan::query()->where('week', 'LIKE',$request->nameCheck)->where('id', '!=', $request->id)->get(); if(count($menuplan) > 0 && $request->nameCheck !== NULL){ return 1; } } public function previewPages(Request $request){ $event = MenuPlan::find($request->preview); $convertToJsonArray = json_encode($event); return $convertToJsonArray; } }