You are here

function configuration_import_form_submit in Configuration Management 7

Submit handler for importing configs.

File

./configuration.admin.inc, line 439

Code

function configuration_import_form_submit($form, &$form_state) {
  module_load_include('inc', 'configuration', 'configuration.export');
  $path = 'public://configuration_migrate';
  file_prepare_directory($path, FILE_CREATE_DIRECTORY);
  drupal_chmod($path, 0777);
  $validators = array(
    'file_validate_extensions' => array(
      'tar',
    ),
  );
  if ($file = file_save_upload('import_configurations', $validators, $path)) {

    // dpm($file);
    $archive = archiver_get_archiver($file->uri);
    $files = $archive
      ->listContents();
    foreach ($files as $filename) {
      if (is_file($path . '/' . $filename)) {
        file_unmanaged_delete($path . '/' . $filename);
      }
    }
    $archive
      ->extract($path);
    if (!is_file($path . "/configuration/config.export")) {
      drupal_set_message(t('Could not find config.export file in Import'), 'error');
      return;
    }
    $config_migrate = drupal_parse_info_file($path . "/configuration/config.export");
    $components = configuration_get_components();
    foreach ($config_migrate['config'] as $component => $info) {
      if (!array_key_exists($component, $components)) {
        continue;
      }
      if (!array_key_exists('default_file', $components[$component])) {
        $config_info['configuration.inc'][$component] = $components[$component];
      }
      elseif ($components[$component]['default_file'] == CONFIGURATION_DEFAULTS_INCLUDED) {
        $config_info['configuration.' . $component . '.inc'][$component] = $components[$component];
      }
      elseif ($components[$component]['default_file'] == CONFIGURATION_DEFAULTS_CUSTOM) {
        $config_info[$components[$component]['default_filename'] . '.inc'][$component] = $components[$component];
      }
    }
    $revert = array();
    foreach (file_scan_directory($path, '/.*inc$/') as $config_file) {
      if (in_array($config_file->filename, array_keys($config_info))) {

        // Rewrite function signatures in the migrate files to allow the
        // ability to load migrate files and restore
        $content = file_get_contents($path . '/configuration/' . $config_file->filename);
        $content = preg_replace('/function configuration_/', 'function migrate_configuration_', $content);
        file_put_contents($path . '/configuration/' . $config_file->filename, $content);

        // Load the migrate file
        include_once $path . '/configuration/' . $config_file->filename;
        $revert_files[] = $config_file->filename;
      }
    }
    foreach ($revert_files as $migrate_file) {
      foreach ($config_info[$migrate_file] as $component => $info) {
        $revert[$component] = array(
          '#import_all' => TRUE,
        );
      }
    }
    configuration_revert($revert, 'migrate_configuration');

    // Delete the uploaded file
    file_delete($file);
    drupal_set_message(t('Migrated %component configurations to the activestore', array(
      '%component' => join(', ', array_keys($revert)),
    )));
  }
}