You are here

function pdf_to_imagefield_node_presave in PDF to ImageField 7

Same name and namespace in other branches
  1. 6.2 pdf_to_imagefield.module \pdf_to_imagefield_node_presave()

hook_node_presave() nodapi subroutine

Check to see if this node has a pdf file that needs processing immediately - or later.

File

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

Code

function pdf_to_imagefield_node_presave(&$node) {
  $type = content_types($node->type);
  $source_filefield_def = pdf_to_imagefield_source_filefield($node);
  $source_filefield = $source_filefield_def['field_name'];
  $target_imagefield = $source_filefield_def['widget']['target_imagefield'];
  $target_imagefield_def = $type['fields'][$target_imagefield];

  // Is the source filefield full and the target image holder empty?
  if (!pdf_to_imagefield_empty_filefield($node->{$source_filefield}) && pdf_to_imagefield_empty_filefield($node->{$target_imagefield})) {

    // Select the pdf entry(s?) in the specified filefield.
    // Usually just one
    foreach ($node->{$source_filefield} as $pdf_file) {
      if ($target_imagefield_def['multiple']) {

        // Add this job to the queue;
        // Only if there is a nid to tell it (node is not new)
        if ($node->nid) {
          pdf_to_imagefield_set_batch($node, $pdf_file, $source_filefield_def);
        }
      }
      else {

        // Multiple is off, so we will only do the cover page.
        // We can do this immediately.
        // Generate the cover page as a managed image file.
        watchdog('pdf_to_imagefield', 'Generating a cover page for PDF %title .', array(
          '%title' => $node->title,
        ), WATCHDOG_NOTICE);
        $image_file = pdf_to_imagefield_generate_page($pdf_file, $source_filefield_def, 0);

        // CCK complains a little if the new field doesn't have a data array attached.
        // Pointless, but it removes a strict warning.
        $image_file->data = array();

        // And attach to the node
        if ($image_file) {
          $node->{$target_imagefield}[0] = (array) $image_file;
          watchdog('pdf_to_imagefield', 'Attaching new file as a cover page to %title .', array(
            '%title' => $node->title,
          ), WATCHDOG_NOTICE);
        }
      }

      // node_save will do the rest later
    }
  }

  // On the other hand, if the filefield is empty and the images full,
  // the original has just been deleted. filefield widget does that
  // earlier - to early for me to get in on it.
  // So, delete these derived images.
  if (pdf_to_imagefield_empty_filefield($node->{$source_filefield}) && !pdf_to_imagefield_empty_filefield($node->{$target_imagefield})) {
    watchdog('pdf_to_imagefield', 'Deleting all the image fields previously attached to %title as derivatives of the deleted PDF file.', array(
      '%title' => $node->title,
    ), WATCHDOG_NOTICE);
    foreach ($node->{$target_imagefield} as $i => $image_file) {
      $removed_file = field_file_load($image_file['fid']);
      field_file_delete($image_file);
      $node->{$target_imagefield}[$i] = array();
    }
  }
}