You are here

function recipe_recipeML_import_form_submit in Recipe 7.2

Same name and namespace in other branches
  1. 6 plugins/recipe_recipeML.module \recipe_recipeML_import_form_submit()
  2. 7 includes/recipe_recipeML.module \recipe_recipeML_import_form_submit()

File

modules/recipe_recipeML.module, line 228
Enables importing and exporting of recipeML format recipes.

Code

function recipe_recipeML_import_form_submit($form, &$form_state) {
  global $recipes, $recipe, $data_string;
  $data = '';
  $validators = array(
    'file_validate_extensions' => array(
      'mxp xml',
    ),
  );
  if ($file = file_save_upload('recipe_import_file', $validators, FALSE, FILE_EXISTS_RENAME)) {

    // Load the xml string.
    $data = file_get_contents($file->uri);
    drupal_set_message(t('The attached file was successfully uploaded'));
  }
  else {
    drupal_set_message(t('The attched file failed to upload.'), 'error');
    return;
  }

  // Parse the data.
  $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)) {
    watchdog('recipe', 'Failed to parse RecipeML file: @error at line @line.', array(
      '@error' => xml_error_string(xml_get_error_code($xml_parser)),
      '@line' => xml_get_current_line_number($xml_parser),
    ), WATCHDOG_WARNING);
  }

  // Free the parser.
  xml_parser_free($xml_parser);
  if ($_POST['op'] == t('Import')) {

    //    $vocabs = taxonomy_get_vocabularies('recipe');
    //    list($lightest_vid, $vocab) = each($vocabs);
    //    reset($vocabs);
    foreach ($recipes as $import_recipe_array) {

      //      $recipe->taxonomy = array();

      /*
            foreach ($recipe->_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) ) {
                $recipe->taxonomy[] = $term->tid;
              }

            }
      */
      if (($node = recipe_import_get_node($import_recipe_array)) != FALSE) {
        node_save($node);
      }
    }
  }
}