You are here

function icon_provider_import_form_validate in Icon API 8

Same name and namespace in other branches
  1. 7 includes/import.inc \icon_provider_import_form_validate()

Validate callback for 'icon_provider_import_form'.

File

includes/import.inc, line 58
import.inc Provides import administrative callbacks and tasks.

Code

function icon_provider_import_form_validate($form, &$form_state) {

  // Detect the selected provider.
  $form_state['provider'] = icon_providers($form_state['values']['provider']);
  $provider =& $form_state['provider'];

  // Construct the beginnings of a bundle.
  $form_state['bundle'] = icon_bundle_defaults();
  $bundle =& $form_state['bundle'];
  $bundle['title'] = $form_state['values']['title'];
  $bundle['name'] = $form_state['values']['name'];

  // Validate supported archive extensions.
  $valid_extensions = archiver_get_extensions();
  $validators = array(
    'file_validate_extensions' => array(
      $valid_extensions,
    ),
  );
  if (!($file = file_save_upload('file', $validators, NULL, FILE_EXISTS_REPLACE))) {
    form_set_error('file');
    return;
  }

  // Extract the archive. This needs to be done now so providers can validate
  // against the archive file contents.
  $temp_dir = 'temporary://icon_api_' . md5(REQUEST_TIME);
  try {
    $archiver = icon_archive_extract(\Drupal::service("file_system")
      ->realpath($file->uri), \Drupal::service("file_system")
      ->realpath($temp_dir));
  } catch (Exception $e) {
    form_set_error('file', $e
      ->getMessage());
    file_delete($file);
    file_unmanaged_delete_recursive($temp_dir);
    return;
  }

  // Set the archive extraction path temporarily as the bundle path.
  $bundle['path'] = $archiver->sub_directory ? $temp_dir . '/' . $archiver->sub_directory : $temp_dir;

  // Automatic detection. Iterate over each provider and find the first one that
  // can import the archive file.
  if (!$provider) {
    foreach (icon_providers() as $_provider) {
      $bundle['provider'] = $_provider['name'];
      $validation = icon_extension_invoke($_provider['type'], $_provider[$_provider['type']], 'icon_' . $_provider['name'] . '_import_validate', $bundle);

      // Providers must return a validation of TRUE if it passes. Otherwise
      // they should send an error message to display instead.
      if ($validation === TRUE) {
        $provider = $_provider;
        break;
      }
    }
    if (!$provider) {
      form_set_error('file', t('Unable to automatically detect which provider should be used to import %file. Try manually selecting which provider to use or re-download the archive file from the provider.', array(
        '%file' => $file->filename,
      )));

      // Close opened archive before trying to delete it.
      $archiver
        ->getArchive()
        ->close();
      file_delete($file);
      file_unmanaged_delete_recursive($temp_dir);
      return;
    }
  }
  else {
    $bundle['provider'] = $provider['name'];
    $validation = icon_extension_invoke($provider['type'], $provider[$provider['type']], 'icon_' . $provider['name'] . '_import_validate', $bundle);
    if ($validation !== TRUE) {
      form_set_error('file', $validation);

      // Close opened archive before trying to delete it.
      $archiver
        ->getArchive()
        ->close();
      file_delete($file);
      file_unmanaged_delete_recursive($temp_dir);
      return;
    }
  }

  // A provider has been determined. Merge in default bundle settings.
  $bundle = array_merge_recursive($bundle, $provider['default bundle']);
  $bundle['provider'] = $provider['name'];
  $bundle['import'] = TRUE;

  // Move the extracted archive to a permanent location.
  if (!($bundle['path'] = icon_file_move_recursive($bundle['path'], 'public://icon/' . $bundle['provider'], $bundle['name']))) {
    form_set_error('file', t('An error occured while attempting to extract the uploaded archive. Check the logs for more details.'));
  }

  // Close opened archive before trying to delete it.
  $archiver
    ->getArchive()
    ->close();

  // Delete the uploaded archive file and temporary directory.
  file_delete($file);
  file_unmanaged_delete_recursive($temp_dir);
}