You are here

function crm_core_data_import_dashboard_form_submit in CRM Core 7

Submit handler for data import dashboard.

File

modules/crm_core_data_import/crm_core_data_import.admin.inc, line 87
Configuration pages for CRM Core Data Import.

Code

function crm_core_data_import_dashboard_form_submit(&$form, &$form_state) {
  $importers = $form_state['values']['items'];
  $operation = $form_state['values']['operation'];
  $operations = array();
  $options = array(
    'value' => 0,
    'unit' => 'items',
  );
  foreach ($importers as $importer_id) {
    if (!empty($importer_id)) {
      $importer = crm_core_data_import_load_importer($importer_id);
      switch ($operation) {
        case 'import':

          // If source not configured.
          $source_fields = $importer
            ->getSourceFields();
          if (!empty($source_fields)) {

            // Run import selected operation.
            $mapping = $importer
              ->getMappingSettings();
            if (!empty($mapping)) {
              _crm_core_data_import_build_entity_operations($operations, $mapping, $importer_id, $options);
            }
            else {
              drupal_set_message(t('Mapping is empty. Please configure your !mapping.', array(
                '!mapping' => l(t('mapping'), 'admin/structure/crm-core/data-import/' . $importer->id . '/field-mapping'),
              )), 'error');
            }
          }
          else {
            drupal_set_message(t('Source fields are empty. Please configure your !source.', array(
              '!source' => l(t('source'), 'admin/structure/crm-core/data-import/' . $importer->id . '/source-settings'),
            )), 'error');
          }
          break;
        case 'rollback':

          // Rollback for all registered migrations.
          $registered_migrations = variable_get('crm_core_data_import_migrations_' . $importer->id, array());
          foreach ($registered_migrations as $machine_name) {
            $migration = Migration::getInstance($machine_name);
            if ($migration) {
              $operations[] = array(
                'crm_core_data_import_batch',
                array(
                  'rollback',
                  $machine_name,
                  $options,
                  0,
                ),
              );
            }
          }
          break;
        case 'deregister':

          // Deregister all registered migrations.
          $registered_migrations = variable_get('crm_core_data_import_migrations_' . $importer->id, array());
          foreach ($registered_migrations as $machine_name) {
            $migration = Migration::getInstance($machine_name);
            if ($migration) {
              Migration::deregisterMigration($machine_name);
              drupal_set_message(t('Deregistered "@task_name" task', array(
                '@task_name' => $machine_name,
              )));
            }
          }
          _crm_core_data_import_migration_statistic_reset($importer);
          break;
      }
      if (count($operations) > 0) {
        $batch = array(
          'operations' => $operations,
          'title' => t('Import processing'),
          'file' => drupal_get_path('module', 'crm_core_data_import') . '/crm_core_data_import.admin.inc',
          'init_message' => t('Starting import process'),
          'progress_message' => '',
          'error_message' => t('An error occurred. Some or all of the import processing has failed.'),
          'finished' => 'crm_core_data_import_migration_batch_finish',
        );
        batch_set($batch);

        // Set importer id for finished callback. Might be improved.
        // There no another way to pass variable to finished callback.
        $_SESSION['data_import_batch_importer_id'] = $importer_id;
      }
    }
  }
}