You are here

function apps_download_batch in Apps 7

Setup the download batch process.

Pass though to update_manager_batch_project_get we need this because in a batch set the file param is for both the operations as well as the other callbacks.

1 string reference to 'apps_download_batch'
apps_download_apps_batch in ./apps.installer.inc
Make the batch for downloading modules for app.

File

./apps.installer.inc, line 117
installer.inc hold all of the function used to download apps and thier deps

Code

function apps_download_batch($project, $url, $type, $version, $app, &$context) {
  module_load_include("inc", "update", "update.manager");

  // This is here to show the user that we are in the process of downloading.
  if (!isset($context['sandbox']['started'])) {
    $context['sandbox']['started'] = TRUE;
    $context['message'] = t('Downloading %project', array(
      '%project' => $project,
    ));
    $context['finished'] = 0;
    return;
  }

  // Actually try to download the file.
  if (!($local_cache = update_manager_file_get($url))) {
    $context['results']['errors'][$project] = t('Failed to download %project from %url', array(
      '%project' => $project,
      '%url' => $url,
    ));
    return;
  }

  // Extract it.
  $extract_directory = apps_extract_directory($type);
  try {
    update_manager_archive_extract($local_cache, $extract_directory);
  } catch (Exception $e) {
    $context['results']['errors'][$project] = $e
      ->getMessage();
    return;
  }

  // Update might not have been extracted to directory named after project.
  // Github for example extracts to [project name]-[release version].
  if (!is_dir("{$extract_directory}/{$project}")) {
    foreach (scandir("{$extract_directory}") as $file) {

      // Hopefully this is the directory, so move it to project directory for
      // update module.
      if ($file != '.' && $file != '..' && is_dir("{$extract_directory}/{$file}") && strpos($file, $project) === 0) {
        rename("{$extract_directory}/{$file}", "{$extract_directory}/{$project}");
      }
    }
  }

  // Update/create info file with download information for future updating.
  if (is_dir("{$extract_directory}/{$project}")) {

    // Need to make version is set.
    $add = array(
      '; Information created by apps module.',
      '',
    );
    $add[] = 'version = ' . $version;
    $add[] = 'source_app = ' . $app['machine_name'];
    $add[] = 'download_time = ' . time();
    $add[] = '';
    file_put_contents("{$extract_directory}/{$project}/" . APPS_APP_INFO, implode("\n", $add));
  }

  // Verify it.
  $archive_errors = update_manager_archive_verify($project, $local_cache, $extract_directory);
  $archive_errors = array();
  if (!empty($archive_errors)) {

    // We just need to make sure our array keys don't collide, so use the
    // numeric keys from the $archive_errors array.
    foreach ($archive_errors as $key => $error) {
      $context['results']['errors']["{$project}-{$key}"] = $error;
    }
    return;
  }

  // Yay, success.
  $context['results']['projects'][$type][$project] = $url;
  $context['finished'] = 1;
}