forked from acearo/base-laravel
Added backend functionality for updating recipes
This commit is contained in:
parent
f6679194eb
commit
cdd3f36fe7
3 changed files with 112 additions and 143 deletions
|
@ -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.');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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){
|
||||
|
|
|
@ -13,8 +13,8 @@
|
|||
{{ Form::text('author', null, array('class' => 'form-control', 'id' => 'authorFilter')) }}
|
||||
</div>
|
||||
<div class="col-md-3 text-center">
|
||||
{{ Form::label('user', 'Maintained By:') }}
|
||||
<select name="owner" class="form-control" id="owner">
|
||||
{{ Form::label('user_id', 'Maintained By:') }}
|
||||
<select name="user_id" class="form-control" id="user">
|
||||
@foreach($lists['owners'] as $user)
|
||||
<option value="{{ $user->id}}" {{ $recipe->user->id == $user->id ? 'selected' : '' }}> {{ $user->name }}</option>
|
||||
@endforeach
|
||||
|
@ -37,9 +37,11 @@
|
|||
<br />
|
||||
<div class="row"><div class="panel-heading"><h1>Categories</h1></div></div>
|
||||
<div id="categories" class="row text-center">
|
||||
@foreach ($recipe->categories as $index => $category)
|
||||
{{ Form::hidden('category_'.$index.'_name', $category->name, array('class' => 'form-control category_name')) }}
|
||||
{{ Form::button(ucwords($category->name), array('class' => 'btn form-control category_button','style'=>'width:100px;margin-left:10px;margin-bottom:10px;'))}}
|
||||
@foreach ($recipe->categories as $category)
|
||||
<span>
|
||||
{{ Form::hidden('category_'.$category->id.'_name', $category->name, array('class' => 'form-control category_name')) }}
|
||||
{{ Form::button(ucwords($category->name), array('class' => 'btn form-control category_button','style'=>'width:100px;margin-left:10px;margin-bottom:10px;'))}}
|
||||
</span>
|
||||
@endforeach
|
||||
</div>
|
||||
<div class='row text-center'>
|
||||
|
@ -48,7 +50,7 @@
|
|||
</div>
|
||||
<br />
|
||||
<div class="row"><div class="panel-heading"><h1>Description</h1></div></div>
|
||||
{{ Form::textarea('description', null, array('class' => 'form-control', 'id' => 'descriptionEditor','name'=>'descriptionEditor')) }}
|
||||
{{ Form::textarea('description', $recipe->description, array('class' => 'form-control', 'id' => 'descriptionEditor')) }}
|
||||
<br />
|
||||
<div class="row"><div class="panel-heading"><h1>Ingredients</h1></div></div>
|
||||
<div class="table-responsive">
|
||||
|
@ -66,18 +68,18 @@
|
|||
</thead>
|
||||
@foreach ($recipe->ingredients as $index => $ingredient)
|
||||
<tr class="recipeIngredient">
|
||||
<td>{{Form::hidden("ingredient_".$index."_order",$ingredient->order,array('class' => 'form-control ingredientOrder'))}}</td>
|
||||
<td>{{Form::hidden("ingredient_".$ingredient->id."_order",$ingredient->order,array('class' => 'form-control ingredientOrder'))}}</td>
|
||||
<td>
|
||||
@if($index == 0)
|
||||
{{Form::checkbox("ingredient_".$index."_alternative",null,$ingredient->alternative,array('class' => 'form-control ingredientAlternative','disabled'))}}
|
||||
{{Form::checkbox("ingredient_".$ingredient->id."_alternative",null,$ingredient->alternative,array('class' => 'form-control ingredientAlternative','disabled'))}}
|
||||
@else
|
||||
{{Form::checkbox("ingredient_".$index."_alternative",null,$ingredient->alternative,array('class' => 'form-control ingredientAlternative'))}}
|
||||
{{Form::checkbox("ingredient_".$ingredient->id."_alternative",null,$ingredient->alternative,array('class' => 'form-control ingredientAlternative'))}}
|
||||
@endif
|
||||
</td>
|
||||
<td>{{Form::text("ingredient_".$index."_quantity",$ingredient->quantity,array('class' => 'form-control ingredientQuantity'))}}</td>
|
||||
<td>{{Form::text("ingredient_".$index."_measurement",$ingredient->measurement,array('class' => 'form-control ingredientMeasurement'))}}</td>
|
||||
<td>{{Form::text("ingredient_".$index."_name",$ingredient->name,array('class' => 'form-control ingredientName'))}}</td>
|
||||
<td>{{Form::text("ingredient_".$index."_notes",$ingredient->special_notes,array('class' => 'form-control ingredientNotes'))}}</td>
|
||||
<td>{{Form::text("ingredient_".$ingredient->id."_quantity",$ingredient->quantity,array('class' => 'form-control ingredientQuantity'))}}</td>
|
||||
<td>{{Form::text("ingredient_".$ingredient->id."_measurement",$ingredient->measurement,array('class' => 'form-control ingredientMeasurement'))}}</td>
|
||||
<td>{{Form::text("ingredient_".$ingredient->id."_name",$ingredient->name,array('class' => 'form-control ingredientName'))}}</td>
|
||||
<td>{{Form::text("ingredient_".$ingredient->id."_notes",$ingredient->special_notes,array('class' => 'form-control ingredientNotes'))}}</td>
|
||||
<td>{{Form::button("Remove",array('class' => 'form-control ingredientRemove'))}}</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
|
@ -85,21 +87,18 @@
|
|||
<tr id="addRow">
|
||||
<td></td>
|
||||
<td>{{Form::checkbox($ingredient->alternative,null,false,array('class' => 'form-control ingredientAlternative','id'=>'addIngredientAlternative'))}}</td>
|
||||
<td>{{Form::text("addIngredientAlternative",null,array('class' => 'form-control ingredientQuantity'))}}</td>
|
||||
<td>{{Form::text("addIngredientMeasurement",null,array('class' => 'form-control ingredientMeasurement'))}}</td>
|
||||
<td>{{Form::text("addIngredientName",null,array('class' => 'form-control ingredientName'))}}</td>
|
||||
<td>{{Form::text("addIngredientSpecial_notes",null,array('class' => 'form-control ingredientNotes'))}}</td>
|
||||
<td>{{Form::text(null,null,array('class' => 'form-control ingredientQuantity'))}}</td>
|
||||
<td>{{Form::text(null,null,array('class' => 'form-control ingredientMeasurement'))}}</td>
|
||||
<td>{{Form::text(null,null,array('class' => 'form-control ingredientName'))}}</td>
|
||||
<td>{{Form::text(null,null,array('class' => 'form-control ingredientNotes'))}}</td>
|
||||
<td>{{Form::button("Add",array('class' => 'form-control','id' => 'addIngredient'))}}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
@if(count($recipe->ingredients) > 0)
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<br /><br />
|
||||
<div class="row"><div class="panel-heading"><h1>Instructions</h1></div></div>
|
||||
{{ Form::textarea('instructions', null, array('class' => 'form-control', 'id' => 'instructionsEditor','name'=>'instructionsEditor')) }}
|
||||
{{ Form::textarea('instructions', $recipe->instructions, array('class' => 'form-control', 'id' => 'instructionsEditor')) }}
|
||||
</div>
|
||||
|
||||
{{ 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);
|
||||
|
|
Loading…
Add table
Reference in a new issue