You are here

function taxonomy_migrate_import_term in Migrate 6

Implementation of hook_migrate_import_term().

File

modules/taxonomy.migrate.inc, line 53
Implementation of taxonomy destination handling

Code

function taxonomy_migrate_import_term($tblinfo, $row) {
  static $vocabularies = NULL;
  if (!isset($vocabularies)) {
    $vocabularies = array();
    $sql = "SELECT vid,name FROM {vocabulary}";
    $result = db_query($sql);
    while ($vocabrow = db_fetch_object($result)) {
      $vocabularies[$vocabrow->name] = $vocabrow->vid;
    }
  }
  $sourcekey = $tblinfo->sourcekey;
  $errors = array();

  // Begin building term array.
  $term = array();

  // Handle an update operation
  if ($row->destid) {
    $term['tid'] = $row->destid;
  }
  else {
    if (isset($tblinfo->fields['tid'])) {
      $term = taxonomy_get_term($tblinfo->fields['tid']);
    }
  }

  // Data which might be useful for taxonomy hooks.
  $term['migrate_tblinfo'] = $tblinfo;
  foreach ($tblinfo->fields as $destfield => $values) {
    if ($values['srcfield'] && $row->{$values}['srcfield']) {
      $source_value = trim($row->{$values}['srcfield']);
    }
    else {
      $source_value = $values['default_value'];
    }
    switch ($destfield) {
      case 'vid':

        // Get vocabulary by name
        if (!is_numeric($source_value)) {
          if (isset($vocabularies[$source_value])) {
            $source_value = $vocabularies[$source_value];
          }
          else {
            $errors[] = migrate_message(t('Provided vocabulary "!vocab" not found', array(
              '!vocab' => $source_value,
            )));
          }
        }
        $term['vid'] = $source_value;
        break;
      case 'parent':

        // @TODO: what if parent term is not already loaded into database?
        // Interpret a numeric parent as the source ID of the parent term
        if (is_numeric($source_value)) {
          $parent = db_result(db_query("SELECT destid FROM {" . $tblinfo->maptable . "} WHERE sourceid = %d", $source_value));
          $term[$destfield] = $parent >= 0 ? $parent : 0;
        }
        elseif ($source_value) {

          // Interpret a string parent as the name of the parent term
          $matches = taxonomy_get_term_by_name($source_value);

          // No matches - maybe the parent isn't migrated yet
          if (count($matches) == 0) {
            $errors[] = migrate_message(t('Parent term "!parent" not found. You may need
              to change the ordering of your content set view so all parents are migrated
              before their children.', array(
              '!parent' => $source_value,
            )));
          }
          else {

            // If there's no explicit vocabulary for the term...
            if (!$term['vid']) {

              // ...use a single match...
              if (count($matches) == 1) {
                $term['parent'] = $matches[0]->tid;
              }
              else {
                $errors[] = migrate_message(t('Multiple potential parent terms named "!parent"
                  were found.', array(
                  '!parent' => $source_value,
                )));
              }
            }
            else {
              foreach ($matches as $parent_term) {
                if ($parent_term->vid == $term['vid']) {
                  $term['parent'] = $parent_term->tid;
                  break;
                }
              }

              // No parent found
              if (!isset($term['parent'])) {
                $errors[] = migrate_message(t('Parent term "!parent" not found in vocabulary !vid.
                  You may need to change the ordering of your content set view so all parents are
                  migrated before their children.', array(
                  '!parent' => $source_value,
                  '!vid' => $term['vid'],
                )));
              }
            }
          }
        }
        break;
      default:
        $term[$destfield] = $source_value;
        break;
    }
  }

  // Prepare the term for import.
  $errors = array_merge($errors, migrate_destination_invoke_all('prepare_term', $term, $tblinfo, $row));
  $success = TRUE;
  foreach ($errors as $error) {
    if ($error['level'] != MIGRATE_MESSAGE_INFORMATIONAL) {
      $success = FALSE;
      break;
    }
  }
  if ($success) {
    timer_start('taxonomy_save');
    taxonomy_save_term($term);
    timer_stop('taxonomy_save');

    // Call completion hooks, for any processing which needs to be done after node_save
    timer_start('taxonomy completion hooks');
    $errors = migrate_destination_invoke_all('complete_term', $term, $tblinfo, $row);
    timer_stop('taxonomy completion hooks');

    // @TODO: Check first for existence, we may have updated an existing term - do we care about duplicates?
    migrate_add_mapping($tblinfo->mcsid, $row->{$sourcekey}, $term['tid']);
  }
  return $errors;
}