[development] Clone Node with File Attachments

Ron Parker sysop at scbbs.com
Thu Dec 13 19:07:32 UTC 2007


OK, I got it figured out, mostly.

First, I use hook_insert to execute my file handling code (see below).  
However, I've still got one problem: I only want to execute this code if 
the node being inserted has been "cloned".   You see I've used  "if 
$node->agenda == 'clone'", but this obviously doesn't work since there 
is no $node->agenda item.

I need a way to tell the system that this node being inserted is a 
cloned node.  Any suggestions on how to do this?  I was thinking about 
using a SESSION variable, but am not sure if that's the most efficient 
way to do it.

Also, if I add files to the cloned node which already contains files, I 
need a way to determine which files in $node->files have actually been 
uploaded, and which have only been copied from the original node.  Any 
suggestions for that?

  // Continue processing if this is a cloned agenda item;
  if ($node->agenda == 'clone') {
    if (!is_array($node->files)) {
      return;
    }
    foreach ($node->files as $fid => $file) {
      // Convert file to object for compatibility
      $file = (object)$file;

      // New file upload
      if ($file = file_save_upload($file, $file->filename)) {
        $file->fid = db_next_id('{files}_fid');
        db_query("INSERT INTO {files} (fid, nid, filename, filepath, 
filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, 
$node->nid, $file->filename, $file->filepath, $file->filemime, 
$file->filesize);
        db_query("INSERT INTO {file_revisions} (fid, vid, list, 
description) VALUES (%d, %d, %d, '%s')", $file->fid, $node->vid, 
$file->list, $file->description);
        // Tell other modules where the file was stored.
        $node->files[$fid] = $file;
      }
    }
  }

-- 
Ron Parker
Software Creations               http://www.scbbs.com
Self-Administration Web Site     http://saw.scbbs.com
SDSS Subscription Mgmt Service   http://sdss.scbbs.com
Central Ave Dance Ensemble       http://www.centralavedance.com
R & B Salsa                      http://www.randbsalsa.com



More information about the development mailing list