public static function calculate($id) { if ($id == 0) { return array(); } $food = Food::getFood($id); $returnArray = array(); // Search food components table for input food $foodComponents = FoodComponent::getComponentsWithChild($id); foreach ($foodComponents as $foodComponent) { $parentFood = Food::getFood($foodComponent["foodId"]); $returnArray[] = array("foodId" => $parentFood["id"], "foodName" => $parentFood["name"], "componentId" => $food["id"], "componentName" => $food["name"], "quantity" => $foodComponent["quantity"], "unitOfMeasure" => $food["unitOfMeasure"]); } return $returnArray; }
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); } } }
<?php require 'classes/Food.php'; // get all food items route $app->get('/food/', function () use($session) { // if($session->isValidRequest() == 0){ // $session->goodnight(); // } try { $return = array(); $return['success'] = true; $return['data'] = Food::getFood(); $return['time'] = time(); //$return['id'] = $_SESSION['user_id']; $return['sid'] = session_id(); echo json_encode($return); } catch (Exception $e) { echo json_encode($e->getMessage()); } });
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; }