You are here

function pdf_to_imagefield_set_batch in PDF to ImageField 6.2

Same name and namespace in other branches
  1. 7 pdf_to_imagefield.module \pdf_to_imagefield_set_batch()

Prepare the batch job to process a PDF file.

Usually invoked on node_presave. This calls batch_set(), but doesn't trigger it. The queued batch job will run when a form is finished processing.

Parameters

object $node:

array $pdf the cck data row being processed.:

$source_filefield the cck filefield definition. It must name the target imagefield in its widget settings.:

2 calls to pdf_to_imagefield_set_batch()
pdf_to_imagefield_node_insert in ./pdf_to_imagefield.module
hook_node_insert() nodapi subroutine
pdf_to_imagefield_node_presave in ./pdf_to_imagefield.module
hook_node_presave() nodapi subroutine

File

./pdf_to_imagefield.module, line 143
PDF to ImageField core hooks and menu callbacks.

Code

function pdf_to_imagefield_set_batch($node, $pdf, $source_filefield = NULL) {
  if (empty($node->nid)) {
    watchdog('pdf_to_imagefield', "Invalid node or node identifier in %function - Not setting up batch.", array(
      '%function' => __FUNCTION__,
    ), WATCHDOG_ERROR);
    return;
  }
  $source_file = $pdf['filepath'];
  $count = pdf_to_imagefield_count_pages($source_file);

  // if count succeeded (some PDFs don't work) then set the batch job page-by-page
  // so we can see the progress
  if ($count) {
    $operations = array();
    for ($page = 0; $page < $count; $page++) {
      $args = array(
        $pdf,
        $source_filefield,
        $page,
      );
      $operations[] = array(
        'pdf_to_imagefield_generate_page_batched',
        $args,
      );
    }

    // Once they are done, attach all the files to the node
    $operations[] = array(
      'pdf_to_imagefield_attach_pages_batched',
      array(
        $node,
        $source_filefield,
      ),
    );
    batch_set(array(
      'title' => t('Converting PDF, %count pages', array(
        '%count' => $count,
      )),
      'operations' => $operations,
      'progress_message' => t('Processed @current out of @total.'),
    ));
  }
  else {

    // Try to do it in one, with no counter
    $args = array(
      $node->nid,
      $pdf,
      $source_filefield,
    );
    $operations[] = array(
      'pdf_to_imagefield_process_pdf',
      $args,
    );
    batch_set(array(
      'title' => t('Converting PDF', array(
        'count' => $count,
      )),
      'operations' => $operations,
      'progress_message' => t('Page count failed, not sure how long this will take.'),
    ));
  }
}