function recipe_mastercook4_import_form_submit in Recipe 6
Same name and namespace in other branches
- 7.2 modules/recipe_mastercook4.module \recipe_mastercook4_import_form_submit()
- 7 includes/recipe_mastercook4.module \recipe_mastercook4_import_form_submit()
File
- plugins/
recipe_mastercook4.module, line 184 - recipe_mastercook4.module - Enables importing and exporting of MasterCook4 format recipes.
Code
function recipe_mastercook4_import_form_submit($form, &$form_state) {
// save to a temp files
if ($file = file_save_upload('recipe_import_file', array(), 'files', FILE_EXISTS_RENAME)) {
drupal_set_message(t('The attached file was successfully uploaded'));
}
else {
drupal_set_message(t('The attched file failed to upload.'), 'error');
return;
}
if ($file) {
// Load the file into a string.
$fp = fopen($file->filepath, "r");
if ($fp) {
$recipe_txt = '';
// Get the vocabs first.
$vocabs = taxonomy_get_vocabularies('recipe');
list($lightest_vid, $vocab) = each($vocabs);
reset($vocabs);
while (!feof($fp)) {
$buf = fgets($fp);
if (preg_match("/\\* +Exported +from/i", $buf)) {
$recipe_txt = trim($recipe_txt);
// Save recipe
if (strlen($recipe_txt) > 0) {
$parsed_recipe_object = recipe_mastercook4_import_single($recipe_txt);
if (strlen($parsed_recipe_object['title']) > 0) {
if (($node = recipe_import_get_node($parsed_recipe_object)) != FALSE) {
// Save the taxonomy.
foreach ($parsed_recipe_object['categories'] as $category) {
// Search the lightest weight recipe vocab for this term.
$term = recipe_get_term_by_name($category, $lightest_vid);
// You didn't find that term, so add it.
if ($term == FALSE && isset($lightest_vid)) {
$term = array(
'name' => $category,
'vid' => $lightest_vid,
);
drupal_set_message(t('Adding term %term_name', array(
'%term_name' => $category,
)));
taxonomy_save_term($term);
// Cast back to object so it's like the return value from recipe_get_term_by_name().
$term = (object) $term;
}
// You have the term now (existing or new), link it ink.
if (isset($term)) {
$node->taxonomy[] = $term->tid;
}
}
// Save the recipe.
node_save($node);
}
}
}
// Clear recipe buffer.
$recipe_txt = '';
}
$recipe_txt .= $buf;
}
fclose($fp);
// Handle the last one needed.
$parsed_recipe_object = recipe_mastercook4_import_single($recipe_txt);
if (strlen($parsed_recipe_object['title']) > 0) {
if (($node = recipe_import_get_node($parsed_recipe_object)) != FALSE) {
node_save($node);
}
}
}
}
}