You are here

function recipe_save_ingredients in Recipe 6

Same name and namespace in other branches
  1. 5 recipe.module \recipe_save_ingredients()
  2. 7 recipe.module \recipe_save_ingredients()

Saves the ingredients of a recipe node to the database.

Parameters

$node: A node containing an ingredient list.

2 calls to recipe_save_ingredients()
recipe_insert in ./recipe.module
Implementation of hook_insert().
recipe_update in ./recipe.module
Implementation of hook_update().

File

./recipe.module, line 1047
recipe.module - share recipes

Code

function recipe_save_ingredients($node) {
  if (!$node->ingredients) {
    $node->ingredients = array();
  }
  foreach ($node->ingredients as $ingredient) {

    // Delete, if you have a valid ri_id and the ingredient name is blank.
    if (isset($ingredient['ri_id']) && $ingredient['name'] == '') {
      db_query("DELETE FROM {recipe_node_ingredient} WHERE id = %d", $ingredient['ri_id']);
    }
    elseif (isset($ingredient['ri_id']) && $ingredient['name'] != '') {
      $ingredient['id'] = recipe_ingredient_id_from_name($ingredient['name']);
      $ingredient['quantity'] = recipe_ingredient_quantity_from_fraction($ingredient['quantity']);
      db_query("UPDATE {recipe_node_ingredient} SET ingredient_id = %d, quantity = %f, unit_id = %d, weight = %d, note = '%s' WHERE id = %d", $ingredient['id'], $ingredient['quantity'], $ingredient['unit_id'], $ingredient['weight'], $ingredient['note'], $ingredient['ri_id']);
    }
    elseif (!isset($ingredient['ri_id']) && $ingredient['name'] != '') {
      $ingredient['id'] = recipe_ingredient_id_from_name($ingredient['name']);
      $ingredient['quantity'] = recipe_ingredient_quantity_from_fraction($ingredient['quantity']);
      db_query("INSERT INTO {recipe_node_ingredient} (nid, ingredient_id, quantity, unit_id, weight, note) VALUES (%d, %d, %f, %d, %d, '%s')", $node->nid, $ingredient['id'], $ingredient['quantity'], $ingredient['unit_id'], $ingredient['weight'], $ingredient['note']);
    }
  }
}