Allow recipe ingredients to be ignored when putting them on the shopping list (closes #125)

This commit is contained in:
Bernd Bestel 2019-03-03 14:49:46 +01:00
parent 89ad25c904
commit e9ef7ea6d8
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
6 changed files with 71 additions and 6 deletions

View File

@ -16,7 +16,15 @@ class RecipesApiController extends BaseApiController
public function AddNotFulfilledProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function AddNotFulfilledProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
$this->RecipesService->AddNotFulfilledProductsToShoppingList($args['recipeId']); $requestBody = $request->getParsedBody();
$excludedProductIds = null;
if ($requestBody !== null && array_key_exists('excludedProductIds', $requestBody))
{
$excludedProductIds = $requestBody['excludedProductIds'];
}
$this->RecipesService->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }

View File

@ -50,6 +50,13 @@ class RecipesController extends BaseController
$selectedSubRecipePosition->amount = $selectedSubRecipePosition->amount * ($selectedRecipe->desired_servings / $selectedRecipe->base_servings); $selectedSubRecipePosition->amount = $selectedSubRecipePosition->amount * ($selectedRecipe->desired_servings / $selectedRecipe->base_servings);
} }
$includedRecipeIdsAbsolute = array();
$includedRecipeIdsAbsolute[] = $selectedRecipe->id;
foreach($selectedRecipeSubRecipes as $subRecipe)
{
$includedRecipeIdsAbsolute[] = $subRecipe->id;
}
return $this->AppContainer->view->render($response, 'recipes', [ return $this->AppContainer->view->render($response, 'recipes', [
'recipes' => $recipes, 'recipes' => $recipes,
'recipesFulfillment' => $this->RecipesService->GetRecipesFulfillment(), 'recipesFulfillment' => $this->RecipesService->GetRecipesFulfillment(),
@ -59,7 +66,8 @@ class RecipesController extends BaseController
'products' => $this->Database->products(), 'products' => $this->Database->products(),
'quantityunits' => $this->Database->quantity_units(), 'quantityunits' => $this->Database->quantity_units(),
'selectedRecipeSubRecipes' => $selectedRecipeSubRecipes, 'selectedRecipeSubRecipes' => $selectedRecipeSubRecipes,
'selectedRecipeSubRecipesPositions' => $selectedRecipeSubRecipesPositions 'selectedRecipeSubRecipesPositions' => $selectedRecipeSubRecipesPositions,
'includedRecipeIdsAbsolute' => $includedRecipeIdsAbsolute
]); ]);
} }

View File

@ -1404,6 +1404,26 @@
} }
} }
], ],
"requestBody": {
"required": false,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"excludedProductIds": {
"type": "array",
"items":{
"type": "number",
"format": "integer"
},
"description": "An optional array of product ids to exclude them from being put on the shopping list"
}
}
}
}
}
},
"responses": { "responses": {
"204": { "204": {
"description": "The operation was successful" "description": "The operation was successful"

View File

@ -84,7 +84,7 @@ $(document).on('click', '.recipe-order-missing-button', function(e)
var objectId = $(e.currentTarget).attr('data-recipe-id'); var objectId = $(e.currentTarget).attr('data-recipe-id');
bootbox.confirm({ bootbox.confirm({
message: L('Are you sure to put all missing ingredients for recipe "#1" on the shopping list?', objectName), message: L('Are you sure to put all missing ingredients for recipe "#1" on the shopping list?', objectName) + "<br><br>" + L("Uncheck ingredients to not put them on the shopping list.") + $("#missing-recipe-pos-list")[0].outerHTML.replace("d-none", ""),
buttons: { buttons: {
confirm: { confirm: {
label: L('Yes'), label: L('Yes'),
@ -101,7 +101,13 @@ $(document).on('click', '.recipe-order-missing-button', function(e)
{ {
Grocy.FrontendHelpers.BeginUiBusy(); Grocy.FrontendHelpers.BeginUiBusy();
Grocy.Api.Post('recipes/' + objectId + '/add-not-fulfilled-products-to-shoppinglist', { }, var excludedProductIds = new Array();
$(".missing-recipe-pos-product-checkbox:checkbox:not(:checked)").each(function()
{
excludedProductIds.push($(this).data("product-id"));
});
Grocy.Api.Post('recipes/' + objectId + '/add-not-fulfilled-products-to-shoppinglist', { "excludedProductIds": excludedProductIds },
function(result) function(result)
{ {
window.location.href = U('/recipes'); window.location.href = U('/recipes');
@ -195,6 +201,16 @@ $('#servings-scale').keyup(function(event)
); );
}); });
$(document).on("click", ".missing-recipe-pos-select-button", function(e)
{
e.preventDefault();
var checkbox = $(this).find(".form-check-input");
checkbox.prop("checked", !checkbox.prop("checked"));
$(this).toggleClass("list-group-item-primary");
});
if (window.location.hash === "#fullscreen") if (window.location.hash === "#fullscreen")
{ {
$("#selectedRecipeToggleFullscreenButton").click(); $("#selectedRecipeToggleFullscreenButton").click();

View File

@ -26,14 +26,14 @@ class RecipesService extends BaseService
return $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); return $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ);
} }
public function AddNotFulfilledProductsToShoppingList($recipeId) public function AddNotFulfilledProductsToShoppingList($recipeId, $excludedProductIds = null)
{ {
$recipe = $this->Database->recipes($recipeId); $recipe = $this->Database->recipes($recipeId);
$recipePositions = $this->GetRecipesFulfillment(); $recipePositions = $this->GetRecipesFulfillment();
foreach ($recipePositions as $recipePosition) foreach ($recipePositions as $recipePosition)
{ {
if($recipePosition->recipe_id == $recipeId) if($recipePosition->recipe_id == $recipeId && !in_array($recipePosition->product_id, $excludedProductIds))
{ {
$product = $this->Database->products($recipePosition->product_id); $product = $this->Database->products($recipePosition->product_id);

View File

@ -162,4 +162,17 @@
</div> </div>
@endif @endif
</div> </div>
<div id="missing-recipe-pos-list" class="list-group d-none mt-3">
@foreach($recipesFulfillment as $recipePos)
@if(in_array($recipePos->recipe_id, $includedRecipeIdsAbsolute))
<a href="#" class="list-group-item list-group-item-action list-group-item-primary missing-recipe-pos-select-button">
<div class="form-check form-check-inline">
<input class="form-check-input missing-recipe-pos-product-checkbox" type="checkbox" data-product-id="{{ $recipePos->product_id }}" checked>
</div>
{{ FindObjectInArrayByPropertyValue($products, 'id', $recipePos->product_id)->name }}
</a>
@endif
@endforeach
</div>
@stop @stop