Recipes/app/Http/Controllers/RecipeController.php

97 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Recipe;
use App\RecipeIngredient;
use App\RecipeCategory;
use App\User;
class RecipeController extends Controller
{
public function index()
{
$recipes = Recipe::with('user')->get();
$categories=array_values(RecipeCategory::get()->sortby('name')->pluck('name')->unique()->toArray());
return view('recipes.index')->with('recipes', $recipes)->with('categories',$categories);
}
public function show($id)
{
$recipe=Recipe::with('user')->findOrFail($id);
return view('recipes.show')->with('recipe', $recipe);
}
public function edit($id){
$recipe=Recipe::with('user')->findOrFail($id);
$lists['ingredients']=array_values(RecipeIngredient::get()->sortby('name')->pluck('name')->unique()->toArray());
$lists['measurements']=array_values(RecipeIngredient::get()->sortby('measurement')->pluck('measurement')->unique()->toArray());
$lists['categories']=array_values(RecipeCategory::get()->sortby('name')->pluck('name')->unique()->toArray());
$lists['authors']=array_values(Recipe::get()->sortby('author')->pluck('author')->unique()->toArray());
$lists['owners']=User::select('id','name')->get()->sortby('name');
return view('recipes.edit')->with('recipe', $recipe)->with('lists',$lists);
}
public function update(Request $request, $id)
{
$recipe = Recipe::with('ingredients')->with('categories')->findOrFail($id);
return $recipe;
$this->validate($request, [
]);
return $request;
//Find and update FormYear and Forms after validating
$formyear = FormYear::with('forms')->findOrFail($id);
$this->validate($request, [
'year'=>'required|max:120',
'postcard_notice'=>'max:5000',
'show_date'=>'required|date_format:"m/d/Y"|before:hide_date',
'hide_date'=>'required|date_format:"m/d/Y"'
]);
//break forms into array for validation
$forms = array();
foreach($request->all() as $element => $value){
if(preg_match('/forms_.*/', $element)){
$explosion = explode('_',$element);
$forms[$explosion[1]][$explosion[2]]=$value;
}
}
foreach($forms as $form){
$newreq = new Request($form);
$this->validate($newreq, [
'filename'=>'required|max:500',
'title'=>'required|max:120',
'description'=>'required|max:5000',
]);
}
if($request->get('postcard_notice') == null){
$request->merge(['postcard_notice'=>'']);
}
$update = $request->only('year','postcard_notice','show_date','hide_date');
$formyear->fill($update)->save();
//Delete forms that don't exist in new forms array
foreach($formyear->forms as $form){
if(!array_key_exists($form->id,$forms)){
$form->delete();
}
}
foreach($forms as $formid => $form){
//Add form_year_id
$form["form_year_id"]=$id;
$newreq = new Request($form);
//Find form if exists, create if it doesn't
$currentform = Form::where('id','=',$formid)->first();
if($currentform === null){
$form = Form::create($newreq->only('form_year_id','filename','title','description'));
}else{
$update = $newreq->only('filename','title','description');
$currentform->fill($update)->save();
}
}
return redirect()->route('forms.index')->with('message','FormYear successfully edited.');
}
}