public static function addFood($id, &$shoppingArray, $quantity = 0) { if ($id == 0) { return array(); } // Prepare needed data $food = Food::getFood($id); $foodComponents = FoodComponent::getComponents($id); if (sizeof($foodComponents) == 0) { // Default to serving size in this case if ($quantity == 0) { $quantity = $food["quantity"]; } // No components, just add entry for self self::addShopping($shoppingArray, $id, $food, $quantity); } else { // Default to recipe quantity in this case if ($quantity == 0) { $quantity = $food["recipeQuantity"]; } if ($quantity == 0) { $quantity = $food["quantity"]; } // Return shopping for each component foreach ($foodComponents as $foodComponent) { // Determine component quantity needed if ($food["recipeQuantity"] == 0) { $food["recipeQuantity"] = $food["quantity"]; } if ($food["recipeQuantity"] == 0) { $food["recipeQuantity"] = 1; } $componentQuantity = $foodComponent["quantity"] * $quantity / $food["recipeQuantity"]; self::addFood($foodComponent["componentId"], $shoppingArray, $componentQuantity); } } }
public static function calculateComponents($parentId) { $returnArray = array(); if ($parentId == 0) { return $returnArray; } // Prepare needed data $parentFood = Food::getFood($parentId); $foodComponents = FoodComponent::getComponents($parentId); // Calculate nutrition for each component foreach ($foodComponents as $foodComponent) { // Determine quantity needed for one serving of parent food if ($parentFood["recipeQuantity"] == 0) { $parentFood["recipeQuantity"] = 1; } $quantity = $foodComponent["quantity"] * $parentFood["quantity"] / $parentFood["recipeQuantity"]; // Calculate component nutrition at that quantity $componentNutrition = self::calculate($foodComponent["componentId"], $quantity); // Add component nutrition to output array $returnArray[] = $componentNutrition; } return $returnArray; }