You are here

function drush_print_pdf_download in Printer, email and PDF versions 7

Same name and namespace in other branches
  1. 6 print_pdf/print_pdf.drush.inc \drush_print_pdf_download()
  2. 7.2 print_pdf/print_pdf.drush.inc \drush_print_pdf_download()

Download and extract PDF archive.

File

print_pdf/print_pdf.drush.inc, line 49
drush integration for print_pdf module PDF libraries download.

Code

function drush_print_pdf_download($library) {
  if (isset($library)) {
    $download_url = _drush_print_pdf_download_url($library);
    if ($download_url) {
      $path = drush_get_option('path');
      if (empty($path)) {
        $path = drush_get_context('DRUSH_DRUPAL_ROOT') . '/sites/all/libraries';
      }

      // Create the path if it does not exist.
      if (!is_dir($path)) {
        drush_op('mkdir', $path);
        drush_log(dt('Directory @path was created', array(
          '@path' => $path,
        )), 'notice');
      }

      // Chdir to the download location.
      $olddir = getcwd();
      drush_op('chdir', $path);

      // Warn about an existing dir
      if (is_dir($library)) {

        // drush_op('rmdir', $library); // Directory must be empty for the php rmdir to work..
        drush_log(dt('An existing @library was overwritten at @path', array(
          '@library' => $library,
          '@path' => $path . '/' . $library,
        )), 'notice');
      }

      // Download the archive
      $filename = _drush_print_pdf_download_file($download_url);
      if ($filename) {
        $extract_ret = _drush_print_pdf_download_extract($filename);
        if ($extract_ret) {

          // Remove the archive
          drush_op('unlink', $filename);
          drush_log(dt('@file has been downloaded and extracted in @path', array(
            '@file' => $filename,
            '@path' => $path,
          )), 'success');
        }
        else {
          drush_log(dt('@file has been downloaded to @path, but extract failed. Check that you have the necessary program installed, and if necessary extract it manually.', array(
            '@file' => $filename,
            '@path' => $path,
          )), 'warning');
        }
      }
      else {
        drush_log(dt('Drush was unable to download @library to @path', array(
          '@library' => $library,
          '@path' => $path,
        )), 'error');
      }

      // Set working directory back to the previous working directory.
      drush_op('chdir', $olddir);
    }
  }
  else {
    drush_log(dt('Please specify a PDF library. Currently supported libraries are dompdf, tcpdf and wkhtmltopdf.'), 'error');
  }
}