function recipe_mastercook4_import_single in Recipe 7
Same name and namespace in other branches
- 6 plugins/recipe_mastercook4.module \recipe_mastercook4_import_single()
- 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) {
$recipe_txt = fixEncoding($recipe_txt);
$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' => '',
);
$last_ingred_key = NULL;
$region = $reg['head'];
$recipe_lines = explode("\n", $recipe_txt);
foreach ($recipe_lines as $line) {
$trimmed_line = trim($line);
if ($region == $reg['head']) {
if ($trimmed_line == '') {
$region++;
continue;
}
}
if ($region == $reg['title']) {
if ($trimmed_line != '') {
$recipe['title'] = $trimmed_line;
}
else {
$region++;
continue;
}
}
if ($region == $reg['meta']) {
if (preg_match('/Recipe +By *: *(.*)/i', $line, $matches)) {
$recipe['source'] = $matches[1];
}
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;
}
}
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 ($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)) {
}
elseif (preg_match('/-------- +------------ +--------------------------------/', $line)) {
}
elseif ($trimmed_line != '') {
$q = trim(substr($line, 0, 8));
$u = trim(substr($line, 10, 12));
$i = trim(substr($line, 24));
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');
}
$ing['ingred_obj'] = recipe_ingredient_match($ing['ingredient_name']);
$recipe['ingredients'][] = $ing;
end($recipe['ingredients']);
$last_ingred_key = key($recipe['ingredients']);
}
}
else {
$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;
}