You are here

function migrate_ui_requirements in Migrate 6.2

Implementation of hook_requirements(). Checks installation requirements and do status reporting. TODO: Share code with migrate_migration_info

Parameters

phase 'install': ignored (no installation requirements): 'runtime': status reporting for migrations

Return value

A keyed array of requirements

File

migrate_ui/migrate_ui.install, line 17
Requirements hook for the migrate_ui_module.

Code

function migrate_ui_requirements($phase) {
  $reqs = array();

  // TODO: Needs work to support newly-protected fields
  return $reqs;
  $severity = REQUIREMENT_OK;

  // TODO: UI for disabling reports
  if ($phase == 'runtime' && variable_get('migrate_ui_status_report', TRUE)) {
    $migrations = migrate_migrations();
    $info = array();
    $rows = array();
    foreach ($migrations as $migration) {

      // Only track true migrations
      if (is_subclass_of($migration, 'Migration')) {
        $open = $unmapped_destination = $unmapped_source = $nofield = 0;

        // TODO: Ugh! Rename one pair or the other...
        $source_fields_keyed = $destination_fields_keyed = array();
        $source_fields = $migration
          ->getSource()
          ->fields();
        $destination_fields = $migration
          ->getDestination()
          ->fields();
        foreach ($migration
          ->getFieldMappings() as $mapping) {
          if ($mapping
            ->getIssuePriority() != MigrateFieldMapping::ISSUE_PRIORITY_OK) {
            $open++;
            $severity = REQUIREMENT_WARNING;
          }
          if ($mapping
            ->getSourceField() && !isset($source_fields[$mapping
            ->getSourceField()])) {
            $nofield++;
            $severity = REQUIREMENT_WARNING;
          }
          if ($mapping
            ->getDestinationField() && !isset($destination_fields[$mapping
            ->getDestinationField()])) {
            $nofield++;
            $severity = REQUIREMENT_WARNING;
          }
          $source_fields_keyed[$mapping
            ->getSourceField()] = $mapping
            ->getSourceField();
          $destination_fields_keyed[$mapping
            ->getDestinationField()] = $mapping
            ->getDestinationField();
        }
        $destKey = $migration
          ->getMap()
          ->getDestinationKey();
        foreach ($migration
          ->getDestination()
          ->fields() as $machine_name => $description) {
          if (!isset($destination_fields_keyed[$machine_name]) && !isset($destKey[$machine_name])) {
            $unmapped_destination++;
            $severity = REQUIREMENT_WARNING;
          }
        }
        $sourceKey = $migration
          ->getMap()
          ->getSourceKey();
        foreach ($migration
          ->getSource()
          ->fields() as $machine_name => $description) {
          if (!isset($source_fields_keyed[$machine_name]) && !isset($sourceKey[$machine_name])) {
            $unmapped_source++;
            $severity = REQUIREMENT_WARNING;
          }
        }
        $row = array();
        $row[] = l($migration
          ->getMachineName(), 'admin/migrate/' . $migration
          ->getMachineName());
        $row[] = $open;
        $row[] = $unmapped_destination;
        $row[] = $unmapped_source;
        $row[] = $nofield;
        $rows[] = $row;
      }
    }
    $description = t('Click on a migration name for full details.');
    $header = array(
      t('Migration'),
      t('Open mapping issues'),
      t('Unmapped destinations'),
      t('Unmapped sources'),
      t('Non-existent fields in mappings'),
    );

    // TODO: Headers inherit severity icons - we don't want that
    // TODO: Gets a white background - we do want to inherit that
    $description .= theme('table', array(
      'header' => $header,
      'rows' => $rows,
    ));
    if ($severity == REQUIREMENT_WARNING) {
      $status = t('Incomplete');
    }
    else {
      $status = t('Complete');
    }
    $reqs['migrate_ui'] = array(
      'title' => t('Migrations'),
      'value' => $status,
      'severity' => $severity,
      'description' => $description,
    );
  }
  return $reqs;
}