Ekapp/skolehjem/app/Http/Controllers/MenuPlanController.php

194 lines
5.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App;
use App\ExternalLink;
use App\MenuPlan;
use App\News;
use App\User;
use Illuminate\Http\RedirectResponse;
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",
"menu" => "required",
]);
$menuPlan = 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 {
$menuPlan->save();
$menuPlans = MenuPlan::query()->paginate($request->input("limit", 20));
if($request->newsoption == true){
$news = new News();
$news->name = "Ny menuplan";
$news->subname = "Uge " . $menuPlan->week;
$news->content = $menuPlan->menu;
$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")->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));
if($request->newsoption == true){
$news = new News();
$news->name = "Opdateret menuplan";
$news->subname = "Uge " . $menuplan->week;
$news->arrangement_id = $menuplan->id;
$news->type_id = '2';
$news->content = $menuplan->menu;
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)
{
$menuplan = MenuPlan::find($id);
$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();
//$images = App\Resource::query()->where("id", '=', $menuPlan->resource_id)->first();
//$imagesSize = getimagesize($images->filename);
$pdf = \Illuminate\Support\Facades\App::make('dompdf.wrapper');
$data = '<div style="text-align: center">'. '<h1 style="font-size: 36px">Uge '. $menuPlan->week. '</h1>' .$menuPlan->menu. /*$imagesSize .'<img class="w-100" src="' . asset($images->filename). '">'. */'</div>';
$pdf->loadHTML($data);
return $pdf->stream("menuplan_uge_".$menuPlan->week.".pdf");
}
}