Exemple #1
0
 public static function copy($params)
 {
     if (!isset($params["id"])) {
         throw new Exception("Missing id parameter");
     }
     // Connect to database
     $mysqli = FoodAppDatabase::connect();
     // Copy FOOD table record
     $foodFields = implode(",", array_slice(static::$fieldMap, 1));
     $queryString = "INSERT into food (" . $foodFields . ") ";
     $queryString .= "SELECT " . $foodFields . " FROM food ";
     $queryString .= "WHERE id='" . $params["id"] . "'";
     // Run query
     if (!$mysqli->query($queryString)) {
         throw new Exception($mysqli->error);
     }
     $newFoodId = $mysqli->insert_id;
     // Copy Reviews
     Review::copy($params["id"], $newFoodId);
     // Copy Components
     FoodComponent::copy($params["id"], $newFoodId);
     // Copy Recipe Steps
     RecipeStep::copy($params["id"], $newFoodId);
     // Copy Complements
     FoodComplement::copy($params["id"], $newFoodId);
     // Copy Alternates
     FoodAlternate::copy($params["id"], $newFoodId);
 }
Exemple #2
0
 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;
 }
Exemple #3
0
 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;
 }
Exemple #5
0
 public function destroy()
 {
     return FoodComponent::destroy($this->params);
 }