You are here

function upload_nodeapi in Drupal 6

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

Implementation of hook_nodeapi().

File

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

Code

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

        // Add the attachments list to node body with a heavy
        // weight to ensure they're below other elements
        if (count($node->files)) {
          if (!$teaser && user_access('view uploaded files')) {
            $node->content['files'] = array(
              '#value' => theme('upload_attachments', $node->files),
              '#weight' => 50,
            );
          }
        }
      }
      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 isset($node->files) && is_array($node->files) && user_access('view uploaded files') ? format_plural(count($node->files), '1 attachment', '@count attachments') : NULL;
    case 'rss item':
      if (is_array($node->files) && user_access('view uploaded 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();
  }
}