You are here

function recipe_mastercook4_import_single in Recipe 7

Same name and namespace in other branches
  1. 6 plugins/recipe_mastercook4.module \recipe_mastercook4_import_single()
  2. 7.2 modules/recipe_mastercook4.module \recipe_mastercook4_import_single()
1 call to recipe_mastercook4_import_single()
recipe_mastercook4_import_form_submit in includes/recipe_mastercook4.module
2 string references to 'recipe_mastercook4_import_single'
RecipeImportFormsTest::testMasterCook4SingleImport in src/Tests/RecipeImportFormsTest.php
Test import a recipe in MasterCook4 format with the single import form.
recipe_mastercook4_recipeio in includes/recipe_mastercook4.module
Implementation of hook_recipeio($type).

File

includes/recipe_mastercook4.module, line 258
recipe_mastercook4.module - Enables importing and exporting of MasterCook4 format recipes.

Code

function recipe_mastercook4_import_single($recipe_txt = NULL) {

  // loose bad characters.
  $recipe_txt = fixEncoding($recipe_txt);

  // region constants
  $reg = array(
    'head' => 0,
    'title' => 1,
    'meta' => 2,
    'ingredients' => 3,
    'directions' => 4,
    'notes' => 5,
    'eor' => 6,
  );
  $recipe = array(
    'title' => '',
    'yield' => '1',
    'yield_unit' => 'Servings',
    'preptime' => 0,
    'cooktime' => 0,
    'categories' => array(),
    'ingredients' => array(),
    'instructions' => '',
    'notes' => '',
    'source' => '',
  );

  // A reference to the last ingredient added.
  $last_ingred_key = NULL;
  $region = $reg['head'];
  $recipe_lines = explode("\n", $recipe_txt);
  foreach ($recipe_lines as $line) {
    $trimmed_line = trim($line);

    // Head
    if ($region == $reg['head']) {

      // blank line in head section, move to next section.
      if ($trimmed_line == '') {
        $region++;
        continue;
      }
    }

    // Title
    if ($region == $reg['title']) {

      // Capture title.
      if ($trimmed_line != '') {
        $recipe['title'] = $trimmed_line;
      }
      else {

        // blank line in title section, move to next section.
        $region++;
        continue;
      }
    }
    if ($region == $reg['meta']) {

      // Get the source.
      if (preg_match('/Recipe +By *: *(.*)/i', $line, $matches)) {
        $recipe['source'] = $matches[1];
      }

      // Get the categories.
      if (preg_match('/Categories *: *(.*)/i', $line, $matches)) {
        $cat1 = trim(substr($matches[1], 0, 33));
        $cat2 = trim(substr($matches[1], 33, 33));
        if ($cat1 != '') {
          $recipe['categories'][] = $cat1;
        }
        if ($cat2 != '') {
          $recipe['categories'][] = $cat2;
        }
      }

      // Category continuation.
      if (count($recipe['categories']) > 0 && preg_match('/^ {16}(.*)/', $line, $matches)) {
        $cat1 = trim(substr($matches[1], 0, 33));
        $cat2 = trim(substr($matches[1], 33, 33));
        if ($cat1 != '') {
          $recipe['categories'][] = $cat1;
        }
        if ($cat2 != '') {
          $recipe['categories'][] = $cat2;
        }
      }

      // blank line in meta section, move to next section.
      if ($trimmed_line == '' || preg_match('/Amount +Measure +Ingredient +-- +Preparation Method/i', $line)) {
        $region++;
        continue;
      }
    }
    if ($region == $reg['ingredients']) {
      if (preg_match('/Amount +Measure +Ingredient +-- +Preparation Method/i', $line)) {

        // Do nothing.
      }
      elseif (preg_match('/-------- +------------ +--------------------------------/', $line)) {

        // Do nothing.
      }
      elseif ($trimmed_line != '') {
        $q = trim(substr($line, 0, 8));
        $u = trim(substr($line, 10, 12));
        $i = trim(substr($line, 24));

        // If you have an ingredient continuation, add note to previous ingredient.
        // Ingredient line continuation must start with a -- in the ingredient name position.
        if ($q == '' && $u == '' && $last_ingred_key != NULL && preg_match('/^ *--/i', $i)) {
          $recipe['ingredients'][$last_ingred_key]['ingredient_note'] .= ' ' . $i;
        }
        else {
          $ing = array(
            'ingredient_name' => '',
            'ingredient_note' => '',
            'quantity' => '',
            'unit_key' => '',
          );
          $ing['quantity'] = recipe_ingredient_quantity_from_fraction($q);
          $ing['unit_name'] = $u;
          if (preg_match('/(.*?) ?-- ?(.*)/', $i, $matches)) {
            $ing['ingredient_name'] = $matches[1];
            $ing['ingredient_note'] = $matches[2];
          }
          else {
            $ing['ingredient_name'] = $i;
          }
          $ing['unit_key'] = recipe_unit_fuzzymatch($ing['unit_name']);
          if ($ing['unit_key'] == FALSE) {
            $ing['ingredient_note'] = '!' . $ing['unit_name'] . ' ' . $ing['ingredient_note'];
            $ing['unit_key'] = 'unit';
            drupal_set_message(t('Could not find the ingredient units in :recipe (:line)', array(
              ':recipe' => $recipe['title'],
              ':line' => $line,
            )), 'warning');
          }

          // Look up the ingredient, if it is not found it will be added later at node_save.
          $ing['ingred_obj'] = recipe_ingredient_match($ing['ingredient_name']);
          $recipe['ingredients'][] = $ing;
          end($recipe['ingredients']);
          $last_ingred_key = key($recipe['ingredients']);
        }
      }
      else {

        // blank line in ingredient section, move to next section.
        $region++;
        continue;
      }
    }
    elseif ($region == $reg['directions']) {
      if (preg_match('/- - - - - - - - - - - - - - - - - -/', $line)) {
        $region++;
        continue;
      }
      if (preg_match('/^Notes: +(.*)/i', $line, $matches)) {
        $recipe['notes'] .= $matches[1] . "\n";
        $region++;
        continue;
      }
      else {
        $recipe['instructions'] .= $line . "\n";
      }
    }
    elseif ($region == $reg['notes']) {
      $recipe['notes'] .= $line . "\n";
    }
  }
  return $recipe;
}