--- orig/recipe.module 2005-09-21 10:23:47.491212360 +0000 +++ recipe.module 2005-09-21 10:26:10.938405072 +0000 @@ -169,6 +169,11 @@ 'type' => MENU_CALLBACK, 'access' => user_access('access content') ); + $items[] = array('path' => 'recipe_import', + 'title' => t('recipe import'), + 'callback' => 'recipe_import_page', + 'access' => user_access('create recipes'), + 'type' => MENU_NORMAL_ITEM); } else { theme_add_style('modules/recipe/recipe.css'); @@ -702,5 +707,156 @@ return $units_array[$string]; } +function recipe_import_page() { + $op = $_POST['op'] ? $_POST['op'] : arg(1); + $edit = $_POST['edit']; + + if (is_numeric($op)) { + $op = (arg(2) && !is_numeric(arg(2))) ? arg(2) : 'view'; + } + + switch ($op) { + case t('Import'): + case t('Preview'): + unset($GLOBALS['error'], $GLOBALS['recipe'], $GLOBALS['data_string'], $GLOBALS['yield'], $GLOBALS['recipes']); + global $error, $recipe, $data_string, $yield, $recipes; + if ($path = $_FILES['edit']['tmp_name']['recipe_import_file']) { + $data = file_get_contents($path); + + $xml_parser = drupal_xml_parser_create($data); + xml_set_element_handler($xml_parser, 'recipe_import_element_start', 'recipe_import_element_end'); + xml_set_character_data_handler($xml_parser, 'recipe_import_element_data'); + + if (!xml_parse($xml_parser, $data, 1)) { + $message = t('Failed to parse RecipeML feed %site: %error at line %line.', + array( + '%site' => theme('placeholder', $feed['title']), + '%error' => xml_error_string(xml_get_error_code($xml_parser)), + '%line' => xml_get_current_line_number($xml_parser))); + watchdog('recipe_import', $message, WATCHDOG_WARNING); + drupal_set_message($message, 'error'); + } + xml_parser_free($xml_parser); + if (!node_access("create", $node)) { + $error['access'] = message_access(); + } + if ($error) { + $output .= ($error); + } else if ($op == t('Import')) { + foreach($recipes as $recipe) { + $recipe = node_validate($recipe, $error); + if ($nid = node_save($recipe)) { + $output .= l($recipe->title, "node/{$recipe->nid}")."
"; + } else { + $output .= "could not save {$recipe->title}
"; + } + } + } else { + $output .= node_form($recipes[0],1); + } + } + break; + case t('Submit'): + if ($nid = node_submit($edit)) { + if (node_access('view', $edit)) { + drupal_goto('node/'. $nid); + } + else { + drupal_goto(); + } + } + else { + drupal_set_title(t('Submit')); + $output .= node_preview($edit); + } + break; + default: + $output .= form_file(t('RecipeML File'), "recipe_import_file", 34, t("A Recipe in RecipeML format, see http://www.formatdata.com/recipeml")); + $output .= form_submit(t('Preview')); + $output .= form_submit(t('Import')); + $output = form($output, 'post', 0, array("enctype" => "multipart/form-data")); + } + echo theme('page', $output); +} + +/** + * Call-back function used by the XML parser. + */ +function recipe_import_element_start($parser, $name, $attributes) { + global $recipe, $yield, $ingredient; + + switch ($name) { + case 'RECIPE': + $recipe = new StdClass(); + $recipe->type = "recipe"; + break; + case 'ING': + $ingredient = new StdClass(); + break; + case 'YIELD': + $yield = true; + break; + } +} + +/** + * Call-back function used by the XML parser. + */ +function recipe_import_element_end($parser, $name) { + global $ingredient, $yield, $data_string, $recipe, $user, $recipes; + + switch ($name) { + case 'RECIPE': + $recipe->uid = $user->uid; + $recipe->name = $user->name; + if ($recipe->yield == 0) $recipe->yield = 1; + if (!$recipe->notes) $recipe->notes = "Imported from RecipeML file"; + if (!$recipe->title) { + $recipe->title = "RecipeML autotitle"; + } else { + if (!$recipe->teaser) $recipe->teaser= $recipe->title." recipe"; + if (!$recipe->body) $recipe->body = $recipe->title." recipe"; + } + $recipes[] = $recipe; + break; + case 'ING': + $recipe->ingredients[] = $ingredient; + break; + case 'YIELD': + $yield = false; + break; + case "TITLE": + $recipe->title = trim($data_string); + break; + case 'QTY': + if ($yield) $recipe->yield = trim($data_string); + else $ingredient->quantity = trim($data_string); + break; + case 'UNIT': + if ($unit = recipe_unit_from_name(trim($data_string))) + $ingredient->unit_id = $unit->id; + break; + case 'ITEM': + $ingredient->name = trim($data_string); + break; + case 'YIELD': + $yield = false; + break; + case 'DIRECTIONS': + $recipe->instructions .= trim($data_string); + break; + case 'STEP': + $recipe->instructions .= trim($data_string)."\n"; + } + $data_string = ''; +} + +/** + * Call-back function used by the XML parser. + */ +function recipe_import_element_data($parser, $data) { + global $data_string; + $data_string .= $data; +} ?>