You are here

function webfm_nodeapi in Web File Manager 5

Same name and namespace in other branches
  1. 5.2 webfm.module \webfm_nodeapi()

Implementation of hook_nodeapi().

File

./webfm.module, line 550

Code

function webfm_nodeapi(&$node, $op, $teaser) {
  global $user;
  switch ($op) {
    case 'load':
      if (variable_get("wfm_attach_{$node->type}", 1) == 1 && user_access('view webfm attachments')) {
        $output['webfm_files'] = webfm_get_attachments($node->nid);
      }
      return $output;
      break;
    case 'view':

      // Add the attachments list to node body if configured to appear in body.
      if (variable_get('webfm_attach_body', '')) {

        // We could be viewing or previewing this node.
        // Loading a node defines $node->webfm_files, possibly as empty list,
        // but loading an edit form unsets $node->webfm_files again. Thus, if
        // $node->webfm is not set, we are previewing this node.
        // Two cases then:
        // 1) The user has no webfm access rights, so he can't change anything
        // and $_POST['attachlist'] is empty. Simulate the 'load' operation
        // to get the files from the database to show them in the preview.
        // 2) If user has webfm access, all attachments (saved/unsaved)
        // are in $_POST['attachlist']. But $_POST['attachlist'] is also set if
        // we preview e.g. a comment where this node is shown, too, but then
        // the attachlist is not ours. So we check for webfm_files *before*
        // checking for $_POST to get around this.
        if (!user_access('access webfm') && variable_get("wfm_attach_{$node->type}", 1) == 1 && user_access('view webfm attachments')) {
          $node->webfm_files = webfm_get_attachments($node->nid);
        }
        if (is_array($node->webfm_files)) {
          if (count($node->webfm_files) && !$teaser) {
            $show_files = $node->webfm_files;
          }
        }
        elseif ($_POST['attachlist'] && user_access('view webfm attachments')) {
          $show_files = webfm_get_temp_attachments($_POST['attachlist']);
        }
        if ($show_files) {
          $node->content['webfm_attachments'] = array(
            '#value' => theme('webfm_attachments', $show_files),
            '#weight' => 10,
          );
          drupal_add_css(drupal_get_path('module', 'webfm') . '/css/webfm.css');
        }
      }
      break;
    case 'validate':

      // When form_validate fails for preview or save, we must reinitialize
      // javascript, otherwise webfm doesn't work anymore.
      $modulepath = drupal_get_path('module', 'webfm');
      drupal_add_js($modulepath . '/js/webfm.js');
      drupal_add_css($modulepath . '/css/webfm.css');
      if (is_null($inline_js)) {
        $clean_url = variable_get('clean_url', 0);
        $clean = $clean_url == 0 || $clean_url == '0' ? FALSE : TRUE;
        $inline_js = webfm_inline_js($base_url, $clean, $user->uid);
      }
      break;
    case 'insert':

      // We saved the attachment list for preview. Remove before saving.
      unset($node->attachlist);
      if ($_POST['attachlist']) {
        $files = explode(',', $_POST['attachlist']);
        $i = 0;
        foreach ($files as $fid) {
          if ($fid) {

            // weight argument determined by position in csv
            webfm_dbinsert_attach($node->nid, $fid, $i++);
          }
        }
      }
      break;
    case 'update':

      // If the user cannot access webfm, $_POST['attachlist'] is always empty
      // and therefore will delete existing attachments from the node.
      if (user_access('access webfm')) {
        $files = explode(',', $_POST['attachlist']);
        webfm_dbupdate_attach($node->nid, $files);
      }
      break;
    case 'delete':
      webfm_dbdelete_attachments($node->nid);
      break;
  }
}