2018-07-14 18:23:41 +02:00
< ? php
namespace Grocy\Controllers ;
2020-08-31 20:40:31 +02:00
use Grocy\Services\RecipesService ;
2022-02-11 17:49:30 +01:00
use Grocy\Helpers\Grocycode ;
2023-05-13 14:43:51 +02:00
use Psr\Http\Message\ResponseInterface as Response ;
use Psr\Http\Message\ServerRequestInterface as Request ;
2018-07-14 18:23:41 +02:00
class RecipesController extends BaseController
{
2022-02-11 17:49:30 +01:00
use GrocycodeTrait ;
2023-05-13 14:43:51 +02:00
public function MealPlan ( Request $request , Response $response , array $args )
2018-07-14 18:23:41 +02:00
{
2021-07-16 17:32:08 +02:00
$start = date ( 'Y-m-d' );
if ( isset ( $request -> getQueryParams ()[ 'start' ]) && IsIsoDate ( $request -> getQueryParams ()[ 'start' ]))
2021-07-10 22:56:39 +02:00
{
2021-07-16 17:32:08 +02:00
$start = $request -> getQueryParams ()[ 'start' ];
2021-07-10 22:56:39 +02:00
}
2021-07-16 17:32:08 +02:00
$days = 6 ;
if ( isset ( $request -> getQueryParams ()[ 'days' ]) && filter_var ( $request -> getQueryParams ()[ 'days' ], FILTER_VALIDATE_INT ) !== false )
2021-07-10 22:56:39 +02:00
{
2021-07-16 17:32:08 +02:00
$days = $request -> getQueryParams ()[ 'days' ];
2021-07-10 22:56:39 +02:00
}
2023-04-01 22:04:30 +02:00
$mealPlanWhereTimespan = " day BETWEEN DATE(' $start ', '- $days days') AND DATE(' $start ', '+ $days days') " ;
2020-08-31 20:40:31 +02:00
2021-07-16 17:32:08 +02:00
$recipes = $this -> getDatabase () -> recipes () -> where ( 'type' , RecipesService :: RECIPE_TYPE_NORMAL ) -> fetchAll ();
2020-08-31 20:40:31 +02:00
$events = [];
2021-07-10 22:56:39 +02:00
foreach ( $this -> getDatabase () -> meal_plan () -> where ( $mealPlanWhereTimespan ) as $mealPlanEntry )
2020-08-31 20:40:31 +02:00
{
$recipe = FindObjectInArrayByPropertyValue ( $recipes , 'id' , $mealPlanEntry [ 'recipe_id' ]);
$title = '' ;
if ( $recipe !== null )
{
$title = $recipe -> name ;
}
$productDetails = null ;
if ( $mealPlanEntry [ 'product_id' ] !== null )
{
$productDetails = $this -> getStockService () -> GetProductDetails ( $mealPlanEntry [ 'product_id' ]);
}
$events [] = [
'id' => $mealPlanEntry [ 'id' ],
'title' => $title ,
'start' => $mealPlanEntry [ 'day' ],
'date_format' => 'date' ,
'recipe' => json_encode ( $recipe ),
'mealPlanEntry' => json_encode ( $mealPlanEntry ),
'type' => $mealPlanEntry [ 'type' ],
'productDetails' => json_encode ( $productDetails )
];
}
return $this -> renderPage ( $response , 'mealplan' , [
'fullcalendarEventSources' => $events ,
'recipes' => $recipes ,
2021-07-10 22:56:39 +02:00
'internalRecipes' => $this -> getDatabase () -> recipes () -> where ( " id IN (SELECT recipe_id FROM meal_plan_internal_recipe_relation WHERE $mealPlanWhereTimespan ) " ) -> fetchAll (),
2022-01-16 16:08:57 +01:00
'recipesResolved' => $this -> getRecipesService () -> GetRecipesResolved ( " recipe_id IN (SELECT recipe_id FROM meal_plan_internal_recipe_relation WHERE $mealPlanWhereTimespan ) " ),
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2021-07-15 17:54:48 +02:00
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved (),
'mealplanSections' => $this -> getDatabase () -> meal_plan_sections () -> orderBy ( 'sort_number' ),
2023-04-01 22:04:30 +02:00
'usedMealplanSections' => $this -> getDatabase () -> meal_plan_sections () -> where ( " id IN (SELECT section_id FROM meal_plan WHERE $mealPlanWhereTimespan ) " ) -> orderBy ( 'sort_number' ),
'weekRecipe' => $this -> getDatabase () -> recipes () -> where ( " type = 'mealplan-week' AND name = LTRIM(STRFTIME('%Y-%W', DATE(' $start ')), '0') " ) -> fetch ()
2020-08-31 20:40:31 +02:00
]);
2018-07-14 18:23:41 +02:00
}
2023-05-13 14:43:51 +02:00
public function Overview ( Request $request , Response $response , array $args )
2018-07-14 18:23:41 +02:00
{
2020-11-17 19:11:02 +01:00
$recipes = $this -> getDatabase () -> recipes () -> where ( 'type' , RecipesService :: RECIPE_TYPE_NORMAL ) -> orderBy ( 'name' , 'COLLATE NOCASE' );
2021-07-10 22:56:39 +02:00
$recipesResolved = $this -> getRecipesService () -> GetRecipesResolved ( 'recipe_id > 0' );
2018-07-15 10:16:36 +02:00
2018-07-15 09:56:10 +02:00
$selectedRecipe = null ;
if ( isset ( $request -> getQueryParams ()[ 'recipe' ]))
{
2020-03-01 23:47:47 +07:00
$selectedRecipe = $this -> getDatabase () -> recipes ( $request -> getQueryParams ()[ 'recipe' ]);
2018-07-15 09:56:10 +02:00
}
2018-07-15 10:16:36 +02:00
else
{
foreach ( $recipes as $recipe )
{
$selectedRecipe = $recipe ;
2020-08-31 20:40:31 +02:00
break ;
2018-07-15 10:16:36 +02:00
}
}
2018-07-15 09:56:10 +02:00
2020-11-08 21:44:39 +01:00
$totalCosts = null ;
$totalCalories = null ;
if ( $selectedRecipe )
{
$totalCosts = FindObjectInArrayByPropertyValue ( $recipesResolved , 'recipe_id' , $selectedRecipe -> id ) -> costs ;
$totalCalories = FindObjectInArrayByPropertyValue ( $recipesResolved , 'recipe_id' , $selectedRecipe -> id ) -> calories ;
}
2020-04-24 11:41:57 -04:00
2022-12-26 14:25:33 +01:00
$viewData = [
2020-04-24 11:41:57 -04:00
'recipes' => $recipes ,
'recipesResolved' => $recipesResolved ,
2022-01-16 15:48:49 +01:00
'recipePositionsResolved' => $this -> getDatabase () -> recipes_pos_resolved () -> where ( 'recipe_id' , $selectedRecipe -> id ),
2020-04-24 11:41:57 -04:00
'selectedRecipe' => $selectedRecipe ,
'products' => $this -> getDatabase () -> products (),
'quantityUnits' => $this -> getDatabase () -> quantity_units (),
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'recipes' ),
'userfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'recipes' ),
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved (),
2020-11-08 21:44:39 +01:00
'selectedRecipeTotalCosts' => $totalCosts ,
2022-12-26 14:25:33 +01:00
'selectedRecipeTotalCalories' => $totalCalories ,
'mealplanSections' => $this -> getDatabase () -> meal_plan_sections () -> orderBy ( 'sort_number' )
2020-04-24 11:41:57 -04:00
];
2020-03-22 09:27:49 +01:00
if ( $selectedRecipe )
{
2020-11-17 19:11:02 +01:00
$selectedRecipeSubRecipes = $this -> getDatabase () -> recipes () -> where ( 'id IN (SELECT includes_recipe_id FROM recipes_nestings_resolved WHERE recipe_id = :1 AND includes_recipe_id != :1)' , $selectedRecipe -> id ) -> orderBy ( 'name' , 'COLLATE NOCASE' ) -> fetchAll ();
2020-08-29 16:41:27 +02:00
2020-08-31 20:40:31 +02:00
$includedRecipeIdsAbsolute = [];
2020-03-22 09:24:49 +01:00
$includedRecipeIdsAbsolute [] = $selectedRecipe -> id ;
2020-08-31 20:40:31 +02:00
foreach ( $selectedRecipeSubRecipes as $subRecipe )
2020-03-22 09:24:49 +01:00
{
$includedRecipeIdsAbsolute [] = $subRecipe -> id ;
}
2020-03-22 09:27:49 +01:00
2021-01-03 22:26:08 +01:00
// TODO: Why not directly use recipes_pos_resolved for all recipe positions here (parent and child)?
// This view already correctly recolves child recipe amounts...
2020-08-31 20:40:31 +02:00
$allRecipePositions = [];
foreach ( $includedRecipeIdsAbsolute as $id )
2020-04-24 11:41:57 -04:00
{
$allRecipePositions [ $id ] = $this -> getDatabase () -> recipes_pos_resolved () -> where ( 'recipe_id = :1 AND is_nested_recipe_pos = 0' , $id ) -> orderBy ( 'ingredient_group' , 'ASC' , 'product_group' , 'ASC' );
2021-01-03 22:26:08 +01:00
foreach ( $allRecipePositions [ $id ] as $pos )
{
if ( $id != $selectedRecipe -> id )
{
2021-01-03 22:40:33 +01:00
$pos2 = $this -> getDatabase () -> recipes_pos_resolved () -> where ( 'recipe_id = :1 AND recipe_pos_id = :2 AND is_nested_recipe_pos = 1' , $selectedRecipe -> id , $pos -> recipe_pos_id ) -> fetch ();
$pos -> recipe_amount = $pos2 -> recipe_amount ;
2021-01-03 22:42:16 +01:00
$pos -> missing_amount = $pos2 -> missing_amount ;
2021-01-03 22:26:08 +01:00
}
}
2020-04-24 11:41:57 -04:00
}
2022-12-26 14:25:33 +01:00
$viewData [ 'selectedRecipeSubRecipes' ] = $selectedRecipeSubRecipes ;
$viewData [ 'includedRecipeIdsAbsolute' ] = $includedRecipeIdsAbsolute ;
$viewData [ 'allRecipePositions' ] = $allRecipePositions ;
2020-03-22 09:27:49 +01:00
}
2020-03-22 09:24:49 +01:00
2022-12-26 14:25:33 +01:00
return $this -> renderPage ( $response , 'recipes' , $viewData );
2018-07-14 18:23:41 +02:00
}
2023-05-13 14:43:51 +02:00
public function RecipeEditForm ( Request $request , Response $response , array $args )
2018-07-14 18:23:41 +02:00
{
$recipeId = $args [ 'recipeId' ];
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'recipeform' , [
2020-08-31 20:40:31 +02:00
'recipe' => $this -> getDatabase () -> recipes ( $recipeId ),
'recipePositions' => $this -> getDatabase () -> recipes_pos () -> where ( 'recipe_id' , $recipeId ),
'mode' => $recipeId == 'new' ? 'create' : 'edit' ,
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-03-01 23:47:47 +07:00
'quantityunits' => $this -> getDatabase () -> quantity_units (),
2020-11-17 19:11:02 +01:00
'recipes' => $this -> getDatabase () -> recipes () -> where ( 'type' , RecipesService :: RECIPE_TYPE_NORMAL ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'recipeNestings' => $this -> getDatabase () -> recipes_nestings () -> where ( 'recipe_id' , $recipeId ),
2020-03-01 23:47:47 +07:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'recipes' ),
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved ()
2018-07-14 18:23:41 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function RecipePosEditForm ( Request $request , Response $response , array $args )
2018-07-14 18:23:41 +02:00
{
if ( $args [ 'recipePosId' ] == 'new' )
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'recipeposform' , [
2018-07-14 18:23:41 +02:00
'mode' => 'create' ,
2020-03-01 23:47:47 +07:00
'recipe' => $this -> getDatabase () -> recipes ( $args [ 'recipeId' ]),
2019-09-21 13:08:42 +02:00
'recipePos' => new \stdClass (),
2021-06-27 20:34:18 +02:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-17 19:11:02 +01:00
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-03-01 23:47:47 +07:00
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved ()
2018-07-14 18:23:41 +02:00
]);
}
else
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'recipeposform' , [
2018-07-14 18:23:41 +02:00
'mode' => 'edit' ,
2020-08-31 20:40:31 +02:00
'recipe' => $this -> getDatabase () -> recipes ( $args [ 'recipeId' ]),
2020-03-01 23:47:47 +07:00
'recipePos' => $this -> getDatabase () -> recipes_pos ( $args [ 'recipePosId' ]),
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-03-01 23:47:47 +07:00
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved ()
2018-07-14 18:23:41 +02:00
]);
}
}
2019-05-06 19:38:47 +02:00
2023-05-13 14:43:51 +02:00
public function RecipesSettings ( Request $request , Response $response , array $args )
2020-02-10 11:04:57 -06:00
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'recipessettings' );
2020-02-10 11:04:57 -06:00
}
2023-05-13 14:43:51 +02:00
public function MealPlanSectionEditForm ( Request $request , Response $response , array $args )
2021-07-15 17:54:48 +02:00
{
if ( $args [ 'sectionId' ] == 'new' )
{
return $this -> renderPage ( $response , 'mealplansectionform' , [
'mode' => 'create'
]);
}
else
{
return $this -> renderPage ( $response , 'mealplansectionform' , [
'mealplanSection' => $this -> getDatabase () -> meal_plan_sections ( $args [ 'sectionId' ]),
'mode' => 'edit'
]);
}
}
2023-05-13 14:43:51 +02:00
public function MealPlanSectionsList ( Request $request , Response $response , array $args )
2021-07-15 17:54:48 +02:00
{
return $this -> renderPage ( $response , 'mealplansections' , [
'mealplanSections' => $this -> getDatabase () -> meal_plan_sections () -> where ( 'id > 0' ) -> orderBy ( 'sort_number' )
]);
}
2022-02-11 17:49:30 +01:00
2023-05-13 14:43:51 +02:00
public function RecipeGrocycodeImage ( Request $request , Response $response , array $args )
2022-02-11 17:49:30 +01:00
{
$gc = new Grocycode ( Grocycode :: RECIPE , $args [ 'recipeId' ]);
return $this -> ServeGrocycodeImage ( $request , $response , $gc );
}
2018-07-14 18:23:41 +02:00
}