You are here

cloneimagefield.module in Node clone 5.2

Same filename and directory in other branches
  1. 5 cloneimagefield.module

File

cloneimagefield.module
View source
<?php

// $Name$

/**
 * Implementation of hook_help().
 */
function cloneimagefield_help($section) {
  switch ($section) {
    case 'admin/help#cloneimagefield':
      $output = '<p>' . t('The Clone Imagefield module works with the Clone Module and Imagefiled Module (a CCK field-type module) to copy attached images to be made when a node is cloned.') . '</p>';
      return $output;
  }
}

/**
* Implementation of hook_clone_node_alter() (a clone.module hook).
*/
function cloneimagefield_clone_node_alter(&$node, $original_node, $method) {
  $content_type = module_invoke('content', 'types', $node->type);
  $image_fields = array();

  // Find all the image fields
  foreach ($content_type['fields'] as $data) {
    if ($data['type'] == 'image') {
      $image_fields[] = $data['field_name'];
    }
  }
  $clone_imagefield = array();

  // Put back the imagefields from the original node.
  foreach ($image_fields as $key) {
    if (isset($original_node->{$key})) {
      $node->{$key} = $original_node->{$key};

      // Make a copy of the imagefield data.
      $clone_imagefield[$key] = $original_node->{$key};
    }
  }
  if ($method == "prepopulate") {

    // The imagefield data is a flag that we can find during the form_alter.
    $node->node_clone_imagefield = $clone_imagefield;
    if (empty($_POST['op'])) {
      drupal_set_message(t('Each pre-existing Imagefield file will be copied and attached when you submit (unless you select its "Delete" checkbox).'));
    }
  }
  elseif ($method == "save-edit") {
    foreach ($image_fields as $key) {
      foreach ($node->{$key} as $delta => $file) {
        _cloneimagefield_copy_file($node, $key, $delta);
      }
    }
  }
}

/**
 * Copy a specific attached file to a temporary location.
 */
function _cloneimagefield_copy_file(&$node, $key, $delta) {
  $source = $node->{$key}[$delta]['filepath'];

  // Create temporary name/path for duplicated files. On Windows, tempnam()
  // requires an absolute path, so we use realpath().
  $dest = tempnam(realpath(file_directory_temp()), 'tmp_');

  // Copy the file to the temporary location.
  if (file_copy($source, $dest)) {

    // $source now holds the actual filename used for the copy.
    $node->{$key}[$delta]['filepath'] = $source;
    $node->{$key}[$delta]['fid'] = 'upload';
    $node->{$key}[$delta]['nid'] = 0;
  }
  else {

    // Copy failed, remove the file from the list of attachements.
    unset($node->{$key}[$delta]);
  }
}

/**
 * Implementation of hook_nodeapi().
 *
 * Take advantage of the fact that imagefield.module does not use the 'submit'
 * $op to copy files at the 'submit' phase before the node is actually saved.
 * Only relevant when the "prepopulate" method is being used.
 */
function cloneimagefield_nodeapi(&$node, $op) {
  if ($op == 'submit' && isset($node->node_clone_imagefield)) {

    // The data was serialized so it could be passed as a hidden form field.
    $image_fields = unserialize($node->node_clone_imagefield);
    foreach (array_keys($image_fields) as $key) {
      foreach ($node->{$key} as $delta => $file) {

        // Check whether this file was attached to the original node, and that
        // it is not slated for removal.
        if ($file['filepath'] == $image_fields[$key][$delta]['filepath'] && empty($file['flags']['delete'])) {
          _cloneimagefield_copy_file($node, $key, $delta);
        }
      }
    }
  }
}

/**
* Implementation of hook_form_alter().
*/
function cloneimagefield_form_alter($form_id, &$form) {

  // Look for node forms.
  if (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id) {
    if (isset($form['#node']->node_clone_imagefield)) {

      // If we are using the "prepopulate" method, store information about files
      // attached to the original node as a hidden form field.
      $form['node_clone_imagefield'] = array(
        '#type' => 'hidden',
        '#value' => serialize($form['#node']->node_clone_imagefield),
      );
    }
  }
}

Functions

Namesort descending Description
cloneimagefield_clone_node_alter Implementation of hook_clone_node_alter() (a clone.module hook).
cloneimagefield_form_alter Implementation of hook_form_alter().
cloneimagefield_help Implementation of hook_help().
cloneimagefield_nodeapi Implementation of hook_nodeapi().
_cloneimagefield_copy_file Copy a specific attached file to a temporary location.