diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php
index 78a2ee2..34b7a1f 100644
--- a/app/Http/Controllers/RecipeController.php
+++ b/app/Http/Controllers/RecipeController.php
@@ -24,7 +24,7 @@ class RecipeController extends Controller
}
public function edit($id){
- $recipe=Recipe::with('user')->findOrFail($id);
+ $recipe=Recipe::with('user')->with('categories')->with('ingredients')->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());
@@ -36,62 +36,103 @@ class RecipeController extends Controller
public function update(Request $request, $id)
{
$recipe = Recipe::with('ingredients')->with('categories')->findOrFail($id);
- return $recipe;
$this->validate($request, [
+ 'author'=>'required|max:500',
+ 'user_id'=>'required',
+ 'servings'=>'required|integer',
+ 'serving_size'=>'required|max:40',
+ 'description'=>'max:5000',
+ 'instructions'=>'required|max:5000'
]);
- return $request;
- //Find and update FormYear and Forms after validating
- $formyear = FormYear::with('forms')->findOrFail($id);
+ //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;
+ }
+ }
- $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"'
+ foreach($categories as $category){
+ $newreq = new Request($category);
+ $this->validate($newreq, [
+ 'name'=>'required|max:50',
]);
- //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'=>'']);
+ 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'];
}
- $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();
- }
+ if(array_key_exists('alternative', $ingredient) && $ingredient['alternative'] == 'on'){
+ $ingredients[$index]['alternative'] = true;
+ }else{
+ $ingredients[$index]['alternative'] = false;
}
- 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();
- }
+ }
+
+ $update = $request->only('author','user_id','servings','serving_size','description','instructions');
+ $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();
}
- return redirect()->route('forms.index')->with('message','FormYear successfully edited.');
+ }
+
+ //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.');
}
}
diff --git a/app/Recipe.php b/app/Recipe.php
index 2b80cce..9515e9f 100644
--- a/app/Recipe.php
+++ b/app/Recipe.php
@@ -8,7 +8,7 @@ use Carbon\Carbon;
class Recipe extends Model
{
- protected $fillable = ['name','maintainer','author','servings','serving_size','date_entered','date_modified','description','instructions'];
+ protected $fillable = ['name','user_id','author','servings','serving_size','date_entered','date_modified','description','instructions'];
protected $dates = ['date_entered','date_modified'];
public function setDateEnteredAttribute($value){
diff --git a/resources/views/recipes/edit.blade.php b/resources/views/recipes/edit.blade.php
index d3c46a9..d7a34a7 100644
--- a/resources/views/recipes/edit.blade.php
+++ b/resources/views/recipes/edit.blade.php
@@ -13,8 +13,8 @@
{{ Form::text('author', null, array('class' => 'form-control', 'id' => 'authorFilter')) }}
- {{ Form::label('user', 'Maintained By:') }}
-
- @endif
- {{ Form::textarea('instructions', null, array('class' => 'form-control', 'id' => 'instructionsEditor','name'=>'instructionsEditor')) }}
+ {{ Form::textarea('instructions', $recipe->instructions, array('class' => 'form-control', 'id' => 'instructionsEditor')) }}
{{ Form::submit('Save', array('class' => 'btn btn-primary','id'=>'submit')) }}
@@ -133,8 +132,8 @@
source: measurements
});
- $("#categories").on("click",'.category',function(){
- $(this).remove();
+ $("#categories").on("click",'.category_button',function(){
+ $(this).parent().remove();
});
$("#addCategory").on("click", function(){
@@ -204,77 +203,6 @@
$('#recipeForm').submit();
});
- /*$('#submit').on("click",function(){
- var recipe = @json($recipe->toArray());
- recipe['author'] = $("#authorFilter").val();
- recipe['user']['id'] = $("#owner option:selected").val();
- recipe['servings'] = $("#servings").val();
- recipe['serving_size'] = $("#serving_size").val();
-
- recipe['categories'] = [];
- $(".category").each(function(){
- recipe['categories'].push($(this).text());
- });
-
- recipe['description'] = $("#descriptionEditor").val();
-
- recipe['ingredients'] = [];
- $('.recipeIngredient').each(function(index){
-
- var ingredientAlternative=($(this).find('.ingredientAlternative').prop("checked")) ? 1 : 0;
- var ingredientQuantity=$(this).find('.ingredientQuantity').val();
- var ingredientMeasurement=$(this).find('.ingredientMeasurement').val();
- var ingredientName=$(this).find('.ingredientName').val();
- var ingredientNotes=$(this).find('.ingredientNotes').val();
- var newIngredient = {
- "recipe_id":{{$recipe->id}},
- "order":index,
- "alternative":ingredientAlternative,
- "quantity":ingredientQuantity,
- "measurement":ingredientMeasurement,
- "name":ingredientName,
- "special_notes":ingredientNotes
- };
- recipe['ingredients'].push(newIngredient);
- });
-
- recipe['instructions'] = $("#instructionsEditor").val();
-
- fetch('{{route('recipes.update', $recipe->id)}}', {
- method: 'PUT',
- body: JSON.stringify({'test':'test'}),
- headers: {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
- },
- });
- /*$.ajax({
- type: "POST",
- url: "{{route('users.update', $recipe->id)}}",
- data: {'test':'test'},
- success: function(){},
- dataType: "json",
- contentType : "application/json",
- 'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
- });*/
-/* var xhr = new XMLHttpRequest();
- //open the request
- xhr.open('PUT','{{route('recipes.update', $recipe->id)}}')
- xhr.setRequestHeader("Content-Type", "application/json");
-
- //send the form data
- xhr.send(JSON.stringify(recipe));
-
- xhr.onreadystatechange = function() {
- if (xhr.readyState == XMLHttpRequest.DONE) {
- //reset form after AJAX success or do something else
- }
- }
- //Fail the onsubmit to avoid page refresh.
- return false;*/
- //$('#recipeForm').submit();
- //});
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);