You are here

function recipe_import_from_46 in Recipe 5

1 string reference to 'recipe_import_from_46'
recipe_menu in ./recipe.module
Implementation of hook_menu().

File

./recipe.module, line 1592
recipe.module - share recipes for drupal 5.x

Code

function recipe_import_from_46() {
  $rcount = 0;
  $icount = 0;
  $out = "<p>";
  $old = db_query("SELECT * FROM {recipe_old}");
  while ($o = db_fetch_object($old)) {
    db_query("INSERT INTO {recipe} (nid, source, yield, preptime, notes, instructions) VALUES('%d', '%s', '%d', '%d', '%s', '%s')", $o->nid, $o->source, intval($o->yield), $o->preptime, $o->notes, $o->instructions);
    $out .= t('Imported recipe @recipeid', array(
      '@recipeid' => $o->nid,
    )) . '<br />';
    $rcount++;
  }
  $out .= "</p>";
  $out .= "<p><pre>";
  $ings = db_query("SELECT * FROM {recipe_ingredients}");
  while ($i = db_fetch_object($ings)) {
    $unitid = 29;

    // "unknown"
    $quantity = recipe_ingredient_quantity_from_fraction($i->ingredient);

    // best guess
    $out .= "\n\n" . t('Parsing @ingredient', array(
      '@ingredient' => $i->ingredient,
    )) . "\n";
    $nmatches = preg_match("/([^a-zA-Z]*)([^ ]*) (.*)/", $i->ingredient, $matches);

    //print_r($matches);
    if ($nmatches > 0) {
      $quantity = recipe_ingredient_quantity_from_fraction($matches[1]);
      $unit = $matches[2];
      $name = $matches[3];
      $out .= t('looking up unit for "@unit"', array(
        '@unit' => strtolower($unit),
      )) . "\n";
      $unit_rs = db_query("SELECT id FROM {recipe_unit} WHERE '%s' LIKE CONCAT(LOWER(name),'%%') OR abbreviation='%s'", strtolower($unit), $unit);

      // allow pints to match pint etc
      if ($unito = db_fetch_object($unit_rs)) {
        $out .= t('Got unit id @unitid', array(
          '@unitid' => $unito->id,
        )) . "\n";
        $unitid = $unito->id;
      }
      else {
        $name = $unit . " " . $name;
      }
    }
    $iid = recipe_ingredient_id_from_name($name);
    $out .= t('Got id for "@name": @iid', array(
      '@name' => $name,
      '@iid' => $iid,
    ));
    db_query("INSERT INTO {recipe_node_ingredient} (nid, unit_id, quantity, ingredient_id) VALUES('%d', '%d', '%f', '%d')", $i->nid, $unitid, $quantity, $iid);
    $icount++;
  }
  $out .= "</pre></p>";
  $out .= '<p>' . t('Imported @rcount recipes and @icount ingredients.', array(
    '@rcount' => $rcount,
    '@icount' => $icount,
  )) . '</p>';
  return $out;
}