You are here

function taxonomy_xml_invoke_import in Taxonomy import/export via XML 7

Same name and namespace in other branches
  1. 5.2 taxonomy_xml.module \taxonomy_xml_invoke_import()
  2. 5 taxonomy_xml.module \taxonomy_xml_invoke_import()
  3. 6.2 taxonomy_xml.module \taxonomy_xml_invoke_import()
  4. 6 taxonomy_xml.module \taxonomy_xml_invoke_import()

Do the actual importing from the given string.

Based on the parameters passed from the form.

Parameters

string $text: Vocabulary definition as a string.

array $params: Import options.

string|null $url: Base URL it was taken from, if relative links need resolving.

Return value

bool NULL. Writes success summary to the screen

See also

taxonomy_xml_formats()

taxonomy_xml_HOOK_parse()

3 calls to taxonomy_xml_invoke_import()
local_taxonomy_service_form_submit in services/local.taxonomy_service.inc
What to do when loading from this service
taxonomy_xml_invoke_import_on_url in ./taxonomy_xml.module
Load a vocabulary from the given URL and import it.
upload_taxonomy_service_form_submit in services/upload.taxonomy_service.inc
What to do when loading from this service

File

./taxonomy_xml.module, line 212
Make it possible to import and export taxonomies as XML documents.

Code

function taxonomy_xml_invoke_import($text, $params, $url = NULL) {
  watchdog('taxonomy_xml', __FUNCTION__, array(), WATCHDOG_DEBUG);
  module_load_include('inc', 'taxonomy_xml', 'taxonomy_xml.process');

  // Conditionally include and invoke the appropriate format library.
  $format = $params['format'];
  if (empty($format)) {
    drupal_set_message("No format defined. Cannot start import.", 'error');
    return FALSE;
  }
  module_load_include('inc', 'taxonomy_xml', 'formats/' . $format . '_format');
  $funcname = "taxonomy_xml_{$format}_parse";
  if (!function_exists($funcname)) {
    drupal_set_message("Unavailable format '{$format}'. {$funcname} was not found in formatting library {$format}_format .", 'error');
    return FALSE;
  }

  // HOUSEKEEPING
  // In taxonomy_xml_absorb_vocabularies,
  // we cached a list of vocabs and their guid.
  // Ensure it's not been deleted in the meantime
  // Or we'd be creating some bad references.
  $taxonomy_xml_vocabulary_ids = variable_get('taxonomy_xml_vocabulary_ids', array());
  foreach ($taxonomy_xml_vocabulary_ids as $guid => $vid) {
    if (!taxonomy_vocabulary_load($vid)) {
      unset($taxonomy_xml_vocabulary_ids[$guid]);
      variable_set('taxonomy_xml_vocabulary_ids', $taxonomy_xml_vocabulary_ids);
    }

    // Yes, it's actually better to have the variable_set inside the loop,
    // it almost never runs.
  }
  $vid = $params['vid'];
  if ($vid == TAXONOMY_XML_CREATE_NEW) {

    // Requested to create new vocab.
    if (!($newname = @$params['vocabulary_name'])) {
      $newname = !empty($params['file']) ? basename($params['file']->filename) : basename($url);
    }
    drupal_set_message("Creating new vocabulary called {$newname}");
    if (empty($newname)) {
      drupal_set_message("Cannot create an unnamed vocabulary", 'error');
      return FALSE;
    }
    $vocabulary = _taxonomy_xml_get_vocabulary_placeholder($newname);
    $vid = $vocabulary->vid;
    variable_set('taxonomy_xml_vid', $vid);
  }

  // All the action is here:
  watchdog('taxonomy_xml', "Running {$funcname}", array(), WATCHDOG_DEBUG);
  $modified_terms = $funcname($text, $vid, $url);

  // Func may have modified vocab or vid during its import.
  // Reload (just for these messages).
  $vocabulary = taxonomy_vocabulary_load($vid);
  if (empty($vocabulary)) {
    drupal_set_message("Failed to create or update vocabulary. Invalid ID", 'error');
    return FALSE;
  }
  if (!empty($modified_terms)) {
    if (is_array($modified_terms)) {
      $term_list = array();
      foreach ($modified_terms as $list_term) {
        $term_list[] = l($list_term->name, "taxonomy/term/{$list_term->tid}/edit");
      }
      drupal_set_message(t('Updated %count term(s)', array(
        '%count' => count($modified_terms),
      )) . ' <i>' . implode(', ', $term_list) . '.</i> ');
      drupal_set_message(t("\n        Imported vocabulary %vocab_name.\n        You may now need to <a href='!settings_link'>Review the vocabulary settings</a>\n        or <a href='!list_link'>List the terms</a>", array(
        '%vocab_name' => $vocabulary->name,
        '!settings_link' => url(TAXONOMY_XML_ADMIN . '/' . $vocabulary->machine_name . '/edit'),
        '!list_link' => url(TAXONOMY_XML_ADMIN . '/' . $vocabulary->machine_name),
      )));
    }
    else {

      // Returned something that was not an array.
      // Maybe it was just 'OK'
    }
    return TRUE;
  }
  else {
    drupal_set_message(t("Failed to import any new terms. This may be due to syntax or formattings errors in the import file.", array()), 'error');
    return FALSE;
  }
}