You are here

function recipe_ingredient_id_from_name in Recipe 6

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

Converts a recipe ingredient name to and ID

1 call to recipe_ingredient_id_from_name()
recipe_save_ingredients in ./recipe.module
Saves the ingredients of a recipe node to the database.

File

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

Code

function recipe_ingredient_id_from_name($name) {
  static $cache;
  if (!$cache[$name]) {
    $ingredient_id = db_result(db_query("SELECT id FROM {recipe_ingredient} WHERE LOWER(name)='%s'", trim(strtolower($name))));
    if (!$ingredient_id) {
      global $active_db;
      $node_link = db_result(db_query("SELECT nid FROM {node} WHERE title = '%s'", $name));
      if (!$node_link) {
        $node_link = 0;
      }

      // Don't convert to lowercase if there is a ® (registered trademark symbol).
      if (variable_get('recipe_ingredient_name_normalize', 0) == 1 && !preg_match('/®/', $name)) {
        $name = trim(strtolower($name));
      }
      db_query("INSERT INTO {recipe_ingredient} (name, link) VALUES ('%s', %d)", $name, $node_link);
      $ingredient_id = db_result(db_query("SELECT id FROM {recipe_ingredient} WHERE LOWER(name)='%s'", trim(strtolower($name))));
    }
    $cache[$name] = $ingredient_id;
  }
  return $cache[$name];
}