From 55c42dc04350f89ab4019732ca311c82c5ad7e24 Mon Sep 17 00:00:00 2001 From: Beth Parker Date: Sat, 12 Feb 2022 20:52:57 -0600 Subject: [PATCH] Added recipe creation --- app/Http/Controllers/RecipeController.php | 158 +++++++++++++---- resources/views/recipes/create.blade.php | 200 ++++++++++++++++++++++ resources/views/recipes/edit.blade.php | 13 +- 3 files changed, 331 insertions(+), 40 deletions(-) create mode 100644 resources/views/recipes/create.blade.php 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') +
+ {{ Form::model(null, array('route' => array('recipes.store'), 'method' => 'POST', 'id' => 'recipeForm')) }} +
+
+
+ {{ Form::label('name', 'Recipe Name:') }} + {{ Form::text('name', null, array('class' => 'form-control', 'id' => 'name')) }} +
+
+
+ {{ Form::label('author', 'Created By:') }} + {{ Form::text('author', null, array('class' => 'form-control', 'id' => 'authorFilter')) }} +
+
+ {{ Form::label('user_id', 'Maintained By:') }} + +
+ +
Entered On: {{now()->format('Y/m/d') }}
+
Last Changed: {{now()->format('Y/m/d') }}
+
+
+
+ {{ Form::label('servings', 'Servings:') }} + {{ Form::text('servings', null, array('class' => 'form-control','id' => 'servings')) }} +
+
+ {{ Form::label('servings', 'Serving Size:') }} + {{ Form::text('serving_size', null, array('class' => 'form-control','id' => 'serving_size')) }} +
+
+
+

Categories

+
+
+
+
+
{{ Form::button("Add", array('id'=>'addCategory','class'=>'form-control'))}}
+
+
+

Description

+ {{ Form::textarea('description', null, array('class' => 'form-control', 'id' => 'descriptionEditor')) }} +
+

Ingredients

+
+ + + + + + + + + + + + + + + + + + + + + + +
OrderAlternative?QuantityMeasurementName          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'))}}
+
+ +

+

Instructions

+ {{ Form::textarea('instructions', null, array('class' => 'form-control', 'id' => 'instructionsEditor')) }} +
+ +{{ Form::submit('Create Recipe', array('class' => 'btn btn-primary','id'=>'submit')) }} + +{{ Form::close() }} +@endsection + +@section('scripts') + + + +@endsection + +@section('styles') + +@endsection diff --git a/resources/views/recipes/edit.blade.php b/resources/views/recipes/edit.blade.php index d7a34a7..0a699be 100644 --- a/resources/views/recipes/edit.blade.php +++ b/resources/views/recipes/edit.blade.php @@ -2,11 +2,17 @@ @extends('content_wrappers.md-10') @section('title', ' | Edit '.$recipe->name) -@section('heading', 'Edit '.$recipe->name) +@section('heading', 'Edit Recipe: '.$recipe->name) @section('content')
{{ Form::model($recipe, array('route' => array('recipes.update', $recipe->id), 'method' => 'PUT', 'id' => 'recipeForm')) }} +
+
+ {{ Form::label('name', 'Recipe Name:') }} + {{ Form::text('name', null, array('class' => 'form-control', 'id' => 'name')) }} +
+
{{ Form::label('author', 'Created By:') }} @@ -20,7 +26,6 @@ @endforeach
-
Entered On: {{$recipe->date_entered->format('Y/m/d') }}
Last Changed: {{$recipe->date_modified->format('Y/m/d') }}
@@ -86,7 +91,7 @@ - {{Form::checkbox($ingredient->alternative,null,false,array('class' => 'form-control ingredientAlternative','id'=>'addIngredientAlternative'))}} + {{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'))}} @@ -101,7 +106,7 @@ {{ Form::textarea('instructions', $recipe->instructions, array('class' => 'form-control', 'id' => 'instructionsEditor')) }}
-{{ Form::submit('Save', array('class' => 'btn btn-primary','id'=>'submit')) }} +{{ Form::submit('Update Recipe', array('class' => 'btn btn-primary','id'=>'submit')) }} {{ Form::close() }} @endsection