diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 34b7a1f..0039f4e 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -37,6 +37,118 @@ class RecipeController extends Controller { $recipe = Recipe::with('ingredients')->with('categories')->findOrFail($id); + $this->validate($request, [ + 'name'=>'required|max:500', + 'author'=>'required|max:500', + 'user_id'=>'required', + 'servings'=>'required|integer', + 'serving_size'=>'required|max:40', + 'description'=>'max:5000', + 'instructions'=>'required|max:5000' + ]); + + //break categories and ingredients into arrays for validation + $categories = array(); + $ingredients = array(); + foreach($request->all() as $element => $value){ + if(preg_match('/category_.*/', $element)){ + $explosion = explode('_',$element); + $categories[$explosion[1]][$explosion[2]]=$value; + }else if(preg_match('/ingredient_.*/', $element)){ + $explosion = explode('_',$element); + $ingredients[$explosion[1]][$explosion[2]]=$value; + } + } + + foreach($categories as $category){ + $newreq = new Request($category); + $this->validate($newreq, [ + 'name'=>'required|max:50', + ]); + } + + foreach($ingredients as $index => $ingredient){ + $newreq = new Request($ingredient); + $this->validate($newreq, [ + 'order'=>'required|integer', + 'quantity'=>['required','regex:/(^\d+\/\d+$)|(^\d*\.\d+$)|(^\d+ \d\/\d$)|(^\d+$)/i'], + 'measurement'=>'required|max:40', + 'name'=>'required|max:500', + 'notes'=>'max:500' + ]); + if ($ingredient['notes'] == null){ + $ingredients[$index]['special_notes'] = ''; + }else{ + $ingredients[$index]['special_notes'] = $ingredient['notes']; + } + if(array_key_exists('alternative', $ingredient) && $ingredient['alternative'] == 'on'){ + $ingredients[$index]['alternative'] = true; + }else{ + $ingredients[$index]['alternative'] = false; + } + } + + $update = $request->only('name','author','user_id','servings','serving_size','last_modified','description','instructions'); + $update['last_modified']=now(); + $recipe->fill($update)->save(); + + //Delete categories that don't exist in new category array + foreach($recipe->categories as $category){ + if(!array_key_exists($category->id,$categories)){ + $category->delete(); + } + } + + //Delete ingredients that don't exist in new ingredient array + foreach($recipe->ingredients as $ingredient){ + if(!array_key_exists($ingredient->id,$ingredients)){ + $ingredient->delete(); + } + } + + //create/update categories + foreach($categories as $categoryid => $category){ + //Add recipe_id + $category["recipe_id"]=$id; + $newreq = new Request($category); + //Find category if exists, create if it doesn't + $currentcategory = RecipeCategory::where('id','=',$categoryid)->first(); + if($currentcategory === null){ + RecipeCategory::create($newreq->only('recipe_id','name')); + }else{ + $update = $newreq->only('recipe_id','name'); + $currentcategory->fill($update)->save(); + } + } + + //create/update ingredients + foreach($ingredients as $ingredientid => $ingredient){ + //Add recipe_id + $ingredient["recipe_id"]=$id; + $newreq = new Request($ingredient); + //Find category if exists, create if it doesn't + $currentingredient = RecipeIngredient::where('id','=',$ingredientid)->first(); + if($currentingredient === null){ + RecipeIngredient::create($newreq->only('recipe_id','order','alternative','quantity','measurement','name','special_notes')); + }else{ + $update = $newreq->only('recipe_id','order','alternative','quantity','measurement','name','special_notes'); + $currentingredient->fill($update)->save(); + } + } + return redirect()->route('recipes.index')->with('message','Recipe successfully edited.'); + } + + public function create(){ + $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.create')->with('lists',$lists); + } + + public function store(Request $request){ + $this->validate($request, [ 'author'=>'required|max:500', 'user_id'=>'required', @@ -87,51 +199,25 @@ class RecipeController extends Controller } } - $update = $request->only('author','user_id','servings','serving_size','description','instructions'); - $recipe->fill($update)->save(); + $create=$request->only('author','user_id','servings','serving_size','name','description','instructions'); + $create['date_entered']=now(); + $create['date_modified']=now(); + $recipe = Recipe::create($create); - //Delete categories that don't exist in new category array - foreach($recipe->categories as $category){ - if(!array_key_exists($category->id,$categories)){ - $category->delete(); - } - } - - //Delete ingredients that don't exist in new ingredient array - foreach($recipe->ingredients as $ingredient){ - if(!array_key_exists($ingredient->id,$ingredients)){ - $ingredient->delete(); - } - } - - //create/update categories + //create categories foreach($categories as $categoryid => $category){ //Add recipe_id - $category["recipe_id"]=$id; + $category["recipe_id"]=$recipe->id; $newreq = new Request($category); - //Find category if exists, create if it doesn't - $currentcategory = RecipeCategory::where('id','=',$categoryid)->first(); - if($currentcategory === null){ - RecipeCategory::create($newreq->only('recipe_id','name')); - }else{ - $update = $newreq->only('recipe_id','name'); - $currentcategory->fill($update)->save(); - } + RecipeCategory::create($newreq->only('recipe_id','name')); } - //create/update ingredients + //create ingredients foreach($ingredients as $ingredientid => $ingredient){ //Add recipe_id - $ingredient["recipe_id"]=$id; + $ingredient["recipe_id"]=$recipe->id; $newreq = new Request($ingredient); - //Find category if exists, create if it doesn't - $currentingredient = RecipeIngredient::where('id','=',$ingredientid)->first(); - if($currentingredient === null){ - RecipeIngredient::create($newreq->only('recipe_id','order','alternative','quantity','measurement','name','special_notes')); - }else{ - $update = $newreq->only('recipe_id','order','alternative','quantity','measurement','name','special_notes'); - $currentingredient->fill($update)->save(); - } + RecipeIngredient::create($newreq->only('recipe_id','order','alternative','quantity','measurement','name','special_notes')); } return redirect()->route('recipes.index')->with('message','Recipe successfully edited.'); } diff --git a/resources/views/recipes/create.blade.php b/resources/views/recipes/create.blade.php new file mode 100644 index 0000000..cd41ca1 --- /dev/null +++ b/resources/views/recipes/create.blade.php @@ -0,0 +1,200 @@ +@extends('layouts.default') +@extends('content_wrappers.md-10') + +@section('title', ' | Create Recipe') +@section('heading', 'Create Recipe') + +@section('content') +
Order | +Alternative? | +Quantity | +Measurement | +Name           | +Notes | ++ |
---|---|---|---|---|---|---|
+ | {{Form::checkbox(null,null,false,array('class' => 'form-control ingredientAlternative','id'=>'addIngredientAlternative'))}} | +{{Form::text(null,null,array('class' => 'form-control ingredientQuantity'))}} | +{{Form::text(null,null,array('class' => 'form-control ingredientMeasurement'))}} | +{{Form::text(null,null,array('class' => 'form-control ingredientName'))}} | +{{Form::text(null,null,array('class' => 'form-control ingredientNotes'))}} | +{{Form::button("Add",array('class' => 'form-control','id' => 'addIngredient'))}} | +