You are here

function migrate_migration_info in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 migrate_ui/migrate_ui.pages.inc \migrate_migration_info()

Menu callback function for migration view page.

1 string reference to 'migrate_migration_info'
migrate_ui_menu in migrate_ui/migrate_ui.module
Implementation of hook_menu().

File

migrate_ui/migrate_ui.pages.inc, line 720
Pages for managing migration processes.

Code

function migrate_migration_info($form, $form_state, $group_name, $migration_name) {
  $migration = Migration::getInstance($migration_name);
  $has_mappings = $migration && method_exists($migration, 'getFieldMappings');
  $form = array();
  if ($has_mappings) {
    $field_mappings = $migration
      ->getFieldMappings();

    // Identify what destination and source fields are mapped
    foreach ($field_mappings as $mapping) {
      $source_field = $mapping
        ->getSourceField();
      $destination_field = $mapping
        ->getDestinationField();
      $source_fields[$source_field] = $source_field;
      $destination_fields[$destination_field] = $destination_field;
    }
    $form['detail'] = array(
      '#type' => 'vertical_tabs',
      '#attached' => array(
        'js' => array(
          drupal_get_path('module', 'migrate_ui') . '/migrate_ui.js',
        ),
        'css' => array(
          drupal_get_path('module', 'migrate_ui') . '/migrate_ui.css',
        ),
      ),
    );
  }
  else {
    $form['detail'] = array(
      '#type' => 'fieldset',
    );
  }
  $form['overview'] = array(
    '#type' => 'fieldset',
    '#title' => t('Overview'),
    '#group' => 'detail',
  );
  if (!$migration) {
    return $form;
  }
  $team = array();
  foreach ($migration
    ->getTeam() as $member) {
    $email_address = $member
      ->getEmailAddress();
    $team[$member
      ->getGroup()][] = $member
      ->getName() . ' <' . l($email_address, 'mailto:' . $email_address) . '>';
  }
  foreach ($team as $group => $list) {
    $form['overview'][$group] = array(
      '#type' => 'item',
      '#title' => filter_xss_admin($group),
      '#markup' => filter_xss_admin(implode(', ', $list)),
    );
  }
  $dependencies = $migration
    ->getHardDependencies();
  if (count($dependencies) > 0) {
    $form['overview']['dependencies'] = array(
      '#title' => t('Dependencies'),
      '#markup' => filter_xss_admin(implode(', ', $dependencies)),
      '#type' => 'item',
    );
  }
  $soft_dependencies = $migration
    ->getSoftDependencies();
  if (count($soft_dependencies) > 0) {
    $form['overview']['soft_dependencies'] = array(
      '#title' => t('Soft Dependencies'),
      '#markup' => filter_xss_admin(implode(', ', $soft_dependencies)),
      '#type' => 'item',
    );
  }
  $form['overview']['group'] = array(
    '#title' => t('Group:'),
    '#markup' => filter_xss_admin($migration
      ->getGroup()
      ->getTitle()),
    '#type' => 'item',
  );
  if ($has_mappings) {
    switch ($migration
      ->getSystemOfRecord()) {
      case Migration::SOURCE:
        $system_of_record = t('Source data');
        break;
      case Migration::DESTINATION:
        $system_of_record = t('Destination data');
        break;
      default:
        $system_of_record = t('Unknown');
        break;
    }
    $form['overview']['system_of_record'] = array(
      '#type' => 'item',
      '#title' => t('System of record:'),
      '#markup' => $system_of_record,
    );
  }
  $form['overview']['description'] = array(
    '#title' => t('Description:'),
    '#markup' => filter_xss_admin($migration
      ->getDescription()),
    '#type' => 'item',
  );
  if ($has_mappings) {

    // Destination field information
    $form['destination'] = array(
      '#type' => 'fieldset',
      '#title' => t('Destination'),
      '#group' => 'detail',
      '#description' => t('<p>These are the fields available in the destination of this migration
           task. The machine names listed here are those available to be used
           as the first parameter to $this->addFieldMapping() in your Migration
           class constructor. <span class="error">Unmapped fields are red</span>.</p>'),
    );
    $destination = $migration
      ->getDestination();
    $form['destination']['type'] = array(
      '#type' => 'item',
      '#title' => t('Type'),
      '#markup' => filter_xss_admin((string) $destination),
    );
    $dest_key = $destination
      ->getKeySchema();
    $header = array(
      t('Machine name'),
      t('Description'),
    );
    $rows = array();
    foreach ($destination
      ->fields($migration) as $machine_name => $description) {
      $classes = array();
      if (isset($dest_key[$machine_name])) {

        // Identify primary key
        $machine_name .= ' ' . t('(PK)');
      }
      else {

        // Add class for mapped/unmapped. Used in summary.
        $classes[] = !isset($destination_fields[$machine_name]) ? 'migrate-error' : '';
      }
      $rows[] = array(
        array(
          'data' => check_plain($machine_name),
          'class' => $classes,
        ),
        array(
          'data' => filter_xss_admin($description),
          'class' => $classes,
        ),
      );
    }
    $classes = array();
    $form['destination']['fields'] = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => t('No fields'),
    );

    // TODO: Get source_fields from arguments
    $form['source'] = array(
      '#type' => 'fieldset',
      '#title' => t('Source'),
      '#group' => 'detail',
      '#description' => t('<p>These are the fields available from the source of this migration
           task. The machine names listed here are those available to be used
           as the second parameter to $this->addFieldMapping() in your Migration
           class constructor. <span class="error">Unmapped fields are red</span>.</p>'),
    );
    $source = $migration
      ->getSource();
    $form['source']['query'] = array(
      '#type' => 'item',
      '#title' => t('Query'),
      '#markup' => '<pre>' . filter_xss_admin($source) . '</pre>',
    );
    $source_key = $migration
      ->getMap()
      ->getSourceKey();
    $header = array(
      t('Machine name'),
      t('Description'),
    );
    $rows = array();
    foreach ($source
      ->fields() as $machine_name => $description) {
      if (isset($source_key[$machine_name])) {

        // Identify primary key
        $machine_name .= ' ' . t('(PK)');
      }
      else {

        // Add class for mapped/unmapped. Used in summary.
        $classes = !isset($source_fields[$machine_name]) ? 'migrate-error' : '';
      }
      $rows[] = array(
        array(
          'data' => check_plain($machine_name),
          'class' => $classes,
        ),
        array(
          'data' => filter_xss_admin($description),
          'class' => $classes,
        ),
      );
    }
    $classes = array();
    $form['source']['fields'] = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => t('No fields'),
    );
    $header = array(
      t('Destination'),
      t('Source'),
      t('Default'),
      t('Description'),
      t('Priority'),
    );

    // First group the mappings
    $descriptions = array();
    $source_fields = $source
      ->fields();
    $destination_fields = $destination
      ->fields($migration);
    foreach ($field_mappings as $mapping) {

      // Validate source and destination fields actually exist
      $source_field = $mapping
        ->getSourceField();
      $destination_field = $mapping
        ->getDestinationField();
      if (!is_null($source_field) && !isset($source_fields[$source_field])) {
        drupal_set_message(t('"!source" was used as source field in the
          "!destination" mapping but is not in list of source fields', array(
          '!source' => filter_xss_admin($source_field),
          '!destination' => filter_xss_admin($destination_field),
        )), 'warning');
      }
      if (!is_null($destination_field) && !isset($destination_fields[$destination_field])) {
        drupal_set_message(t('"!destination" was used as destination field in
          "!source" mapping but is not in list of destination fields', array(
          '!source' => filter_xss_admin($source_field),
          '!destination' => filter_xss_admin($destination_field),
        )), 'warning');
      }
      $descriptions[$mapping
        ->getIssueGroup()][] = $mapping;
    }

    // Put out each group header
    foreach ($descriptions as $group => $mappings) {
      $form[$group] = array(
        '#type' => 'fieldset',
        '#title' => t('Mapping: !group', array(
          '!group' => filter_xss_admin($group),
        )),
        '#group' => 'detail',
        '#attributes' => array(
          'class' => array(
            'migrate-mapping',
          ),
        ),
      );
      $rows = array();
      foreach ($mappings as $mapping) {
        $default = $mapping
          ->getDefaultValue();
        if (is_array($default)) {
          $default = implode(',', $default);
        }
        $issue_priority = $mapping
          ->getIssuePriority();
        if (!is_null($issue_priority)) {
          $classes[] = 'migrate-priority-' . drupal_html_class($issue_priority);
          $priority = MigrateFieldMapping::$priorities[$issue_priority];
          $issue_pattern = $migration
            ->getIssuePattern();
          $issue_number = $mapping
            ->getIssueNumber();
          if (!is_null($issue_pattern) && !is_null($issue_number)) {
            $priority .= ' (' . l(t('#') . $issue_number, str_replace(':id:', $issue_number, $issue_pattern)) . ')';
          }
          if ($issue_priority != MigrateFieldMapping::ISSUE_PRIORITY_OK) {
            $classes[] = 'migrate-error';
          }
        }
        else {
          $priority = t('OK');
          $classes[] = 'migrate-priority-' . 1;
        }
        $destination_field = $mapping
          ->getDestinationField();
        $source_field = $mapping
          ->getSourceField();

        // Highlight any mappings overridden in the database.
        if ($mapping
          ->getMappingSource() == MigrateFieldMapping::MAPPING_SOURCE_DB) {
          $destination_field = "<em>{$destination_field}</em>";
          $source_field = "<em>{$source_field}</em>";
        }
        $row = array(
          array(
            'data' => filter_xss_admin($destination_field),
            'class' => $classes,
          ),
          array(
            'data' => filter_xss_admin($source_field),
            'class' => $classes,
          ),
          array(
            'data' => filter_xss_admin($default),
            'class' => $classes,
          ),
          array(
            'data' => filter_xss_admin($mapping
              ->getDescription()),
            'class' => $classes,
          ),
          array(
            'data' => filter_xss_admin($priority),
            'class' => $classes,
          ),
        );
        $rows[] = $row;
        $classes = array();
      }
      $form[$group]['table'] = array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows,
      );
    }
  }
  return $form;
}