You are here

function recipe_mastercook4_import_single in Recipe 6

Same name and namespace in other branches
  1. 7.2 modules/recipe_mastercook4.module \recipe_mastercook4_import_single()
  2. 7 includes/recipe_mastercook4.module \recipe_mastercook4_import_single()
1 call to recipe_mastercook4_import_single()
recipe_mastercook4_import_form_submit in plugins/recipe_mastercook4.module
1 string reference to 'recipe_mastercook4_import_single'
recipe_mastercook4_recipeio in plugins/recipe_mastercook4.module
Implementation of hook_recipeio($type).

File

plugins/recipe_mastercook4.module, line 257
recipe_mastercook4.module - Enables importing and exporting of MasterCook4 format recipes.

Code

function recipe_mastercook4_import_single($recipe_txt = NULL) {

  // region constants
  define('HEAD', 0);
  define('TITLE', 1);
  define('META', 2);
  define('INGREDIENTS', 3);
  define('DIRECTIONS', 4);
  define('NOTES', 5);
  define('EOR', 6);
  $recipe = array(
    'title' => '',
    'categories' => array(),
    'ingredients' => array(),
    'instructions' => '',
    'notes' => '',
    'source' => '',
  );

  // A reference to the last ingredient added.
  $last_ingred_key = NULL;
  $region = HEAD;
  foreach (split("\n", $recipe_txt) as $line) {
    $trimmed_line = trim($line);

    // Head
    if ($region == HEAD) {

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

    // Title
    if ($region == TITLE) {

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

        // blank line in title section, move to next section.
        $region++;
        continue;
      }
    }
    if ($region == 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;
        }
      }

      // If you hit a blank line or the beginning ingredient header, move to next section.
      if ($trimmed_line == '' || preg_match('/Amount +Measure +Ingredient +-- +Preparation Method/i', $line)) {
        $region++;
        continue;
      }
    }
    if ($region == 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.
        if ($q == '' && $u == '' && $last_ingred_key != NULL) {
          $recipe['ingredients'][$last_ingred_key]['ingredient_note'] .= ' ' . $i;

          // For non-ingredient continuation, add ingredient like normal.
        }
        else {
          $ing = array();
          $ing['quantity'] = $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;
          }

          // FALSE if no-match
          $ing['unit_obj'] = recipe_unit_fuzzymatch($ing['unit_name']);

          // FALSE if no-match
          $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 == 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 == NOTES) {
      $recipe['notes'] .= $line . "\n";
    }
  }
  return $recipe;
}