You are here

function recipe_save_ingredients in Recipe 5

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

Saves the changed ingredients of a recipe node to the database (by comparing the old and new ingredients first)

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 696
recipe.module - share recipes for drupal 5.x

Code

function recipe_save_ingredients($node) {
  if (!$node->ingredients) {
    $node->ingredients = array();
  }
  $changes = recipe_ingredients_diff($node->ingredients, recipe_load_ingredients($node));
  if (count($changes->remove) > 0) {
    $ids = implode(',', $changes->remove);
    db_query("DELETE FROM {recipe_node_ingredient} WHERE id IN (%s)", $ids);
  }
  foreach ($changes->add as $ingredient) {
    $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) VALUES (%d,%d,%f,%d)", $node->nid, $ingredient->id, $ingredient->quantity, $ingredient->unit_id);
  }
  foreach ($changes->update as $ingredient) {
    $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 quantity='%f', unit_id='%d' WHERE nid='%d' AND  ingredient_id='%d'", $ingredient->quantity, $ingredient->unit_id, $node->nid, $ingredient->id);
  }
}