diff --git a/migrations/0050.sql b/migrations/0050.sql new file mode 100644 index 00000000..a7bd53c3 --- /dev/null +++ b/migrations/0050.sql @@ -0,0 +1,2 @@ +ALTER TABLE recipes +ADD picture_file_name TEXT; diff --git a/public/viewjs/recipeform.js b/public/viewjs/recipeform.js index c26dbc38..0fc4acce 100644 --- a/public/viewjs/recipeform.js +++ b/public/viewjs/recipeform.js @@ -5,10 +5,50 @@ var jsonData = $('#recipe-form').serializeJSON(); Grocy.FrontendHelpers.BeginUiBusy("recipe-form"); + if ($("#recipe-picture")[0].files.length > 0) + { + var someRandomStuff = Math.random().toString(36).substring(2, 100) + Math.random().toString(36).substring(2, 100); + jsonData.picture_file_name = someRandomStuff + $("#recipe-picture")[0].files[0].name; + } + + if (Grocy.DeleteRecipePictureOnSave) + { + jsonData.picture_file_name = null; + + Grocy.Api.DeleteFile(Grocy.RecipePictureFileName, 'recipepictures', {}, + function (result) + { + // Nothing to do + }, + function (xhr) + { + Grocy.FrontendHelpers.EndUiBusy("recipe-form"); + Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) + } + ); + } + Grocy.Api.Put('objects/recipes/' + Grocy.EditObjectId, jsonData, function(result) { - window.location.href = U('/recipes'); + if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteRecipePictureOnSave) + { + Grocy.Api.UploadFile($("#recipe-picture")[0].files[0], 'recipepictures', jsonData.picture_file_name, + function(result) + { + window.location.href = U('/recipes'); + }, + function (xhr) + { + Grocy.FrontendHelpers.EndUiBusy("recipe-form"); + Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) + } + ); + } + else + { + window.location.href = U('/recipes'); + } }, function(xhr) { @@ -306,6 +346,15 @@ $('#save-recipe-include-button').on('click', function(e) } }); +Grocy.DeleteRecipePictureOnSave = false; +$('#delete-current-recipe-picture-button').on('click', function (e) +{ + Grocy.DeleteRecipePictureOnSave = true; + $("#current-recipe-picture").addClass("d-none"); + $("#delete-current-recipe-picture-on-save-hint").removeClass("d-none"); + $("#delete-current-recipe-picture-button").addClass("disabled"); +}); + $('#description').summernote({ minHeight: '300px', lang: L('summernote_locale') diff --git a/views/recipeform.blade.php b/views/recipeform.blade.php index 4db67bfd..37d288cf 100644 --- a/views/recipeform.blade.php +++ b/views/recipeform.blade.php @@ -29,6 +29,10 @@ @if($mode == 'edit') + + @if(!empty($recipe->picture_file_name)) + + @endif @endif @@ -48,6 +52,15 @@ +
+ +
+ + +
+

{{ $L('If you don\'t select a file, the current picture will not be altered') }}

+
+ @@ -147,6 +160,19 @@ + +
+
+ + + @if(!empty($recipe->picture_file_name)) +

+

{{ $L('The current picture will be deleted when you save the recipe') }}

+ @else +

{{ $L('No picture available') }}

+ @endif +
+
diff --git a/views/recipes.blade.php b/views/recipes.blade.php index 2f9c4c71..c869644a 100644 --- a/views/recipes.blade.php +++ b/views/recipes.blade.php @@ -73,6 +73,10 @@

{{ $selectedRecipeSubRecipe->name }}

+ @if(!empty($selectedRecipeSubRecipe->picture_file_name)) +

+ @endif + @php $selectedRecipeSubRecipePositionsFiltered = FindAllObjectsInArrayByPropertyValue($selectedRecipeSubRecipesPositions, 'recipe_id', $selectedRecipeSubRecipe->id); @endphp @if(count($selectedRecipeSubRecipePositionsFiltered) > 0)
@@ -105,6 +109,10 @@ @endforeach + @if(!empty($selectedRecipe->picture_file_name)) +

+ @endif + @if($selectedRecipePositions->count() > 0)
{{ $L('Ingredients') }}