You are here

function commerce_invoice_pdf_download_invoices in Commerce Invoice 7.2

Action callback to generate a PDF.

File

modules/pdf/commerce_invoice_pdf.module, line 401
The Commerce Invoice PDF module.

Code

function commerce_invoice_pdf_download_invoices($invoice, $context) {
  global $user;
  $destination = $context['destination'];
  _commerce_invoice_pdf_ensure_default_theme();

  // Get the invoice file.
  if ($file = commerce_invoice_pdf_create($invoice)) {
    $zip = new ZipArchive();

    // If the archive already exists, open it. If not, create it.
    if (file_exists($destination)) {
      $opened = $zip
        ->open(drupal_realpath($destination));
    }
    else {
      $opened = $zip
        ->open(drupal_realpath($destination), ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
    }
    if ($opened) {
      $zip
        ->addFile(drupal_realpath($file->uri), $file->filename);
      $zip
        ->close();
    }
  }

  // The operation is complete, create a temporary file entity and provide a
  // download link to the user.
  if ($context['progress']['current'] == $context['progress']['total']) {
    $archive_file = new stdClass();
    $archive_file->uri = $destination;
    $archive_file->filename = basename($destination);
    $archive_file->filemime = file_get_mimetype($destination);
    $archive_file->uid = $user->uid;
    $archive_file->status = FALSE;
    clearstatcache();
    file_save($archive_file);
    $url = file_create_url($archive_file->uri);
    $url = l($url, $url, array(
      'absolute' => TRUE,
    ));
    _views_bulk_operations_log(t('An archive has been created and can be downloaded from: !url', array(
      '!url' => $url,
    )));
  }
}