You are here

function upload_nodeapi in Drupal 4

Same name and namespace in other branches
  1. 5 modules/upload/upload.module \upload_nodeapi()
  2. 6 modules/upload/upload.module \upload_nodeapi()

Implementation of hook_nodeapi().

File

modules/upload.module, line 379
File-handling and attaching files to nodes.

Code

function upload_nodeapi(&$node, $op, $teaser) {
  switch ($op) {
    case 'load':
      $output = '';
      if (variable_get("upload_{$node->type}", 1) == 1) {
        $output['files'] = upload_load($node);
        return $output;
      }
      break;
    case 'prepare':
      _upload_prepare($node);
      break;
    case 'validate':
      _upload_validate($node);
      break;
    case 'view':
      if (isset($node->files) && user_access('view uploaded files')) {

        // Manipulate so that inline references work in preview
        if (!variable_get('clean_url', 0)) {
          $previews = array();
          foreach ($node->files as $file) {
            if (strpos($file->fid, 'upload') !== false) {
              $previews[] = $file;
            }
          }

          // URLs to files being previewed are actually Drupal paths. When Clean
          // URLs are disabled, the two do not match. We perform an automatic
          // replacement from temporary to permanent URLs. That way, the author
          // can use the final URL in the body before having actually saved (to
          // place inline images for example).
          foreach ($previews as $file) {
            $old = file_create_filename($file->filename, file_create_path());
            $new = url($old);
            $node->body = str_replace($old, $new, $node->body);
            $node->teaser = str_replace($old, $new, $node->teaser);
          }
        }

        // Add the attachments list to node body
        if (count($node->files) && !$teaser) {
          $node->body .= theme('upload_attachments', $node->files);
        }
      }
      break;
    case 'insert':
    case 'update':
      if (user_access('upload files')) {
        upload_save($node);
      }
      break;
    case 'delete':
      upload_delete($node);
      break;
    case 'delete revision':
      upload_delete_revision($node);
      break;
    case 'search result':
      return is_array($node->files) ? format_plural(count($node->files), '1 attachment', '%count attachments') : null;
    case 'rss item':
      if (is_array($node->files)) {
        $files = array();
        foreach ($node->files as $file) {
          if ($file->list) {
            $files[] = $file;
          }
        }
        if (count($files) > 0) {

          // RSS only allows one enclosure per item
          $file = array_shift($files);
          return array(
            array(
              'key' => 'enclosure',
              'attributes' => array(
                'url' => file_create_url($file->filepath),
                'length' => $file->filesize,
                'type' => $file->filemime,
              ),
            ),
          );
        }
      }
      return array();
  }
}