You are here

function _forum_access_node_form in Forum Access 6

Same name and namespace in other branches
  1. 7 forum_access.node.inc \_forum_access_node_form()

Rewrite the taxonomy item on the node form.

1 call to _forum_access_node_form()
forum_access_form_alter in ./forum_access.module
Implementation of hook_form_alter().

File

./forum_access.node.inc, line 15
forum_access.node.inc

Code

function _forum_access_node_form(&$form, &$form_state) {
  global $user;
  $vid = _forum_access_get_vid();
  if (!isset($form['taxonomy'][$vid]['#options'])) {
    return;
  }

  // True node administrators are all powerful and do NOT get their forms rewritten here.
  if (user_access('administer nodes') && empty($user->_forum_access_moderator)) {
    return;
  }
  $roles = array_keys($user->roles);
  $result = db_query("SELECT tid FROM {forum_access} WHERE rid IN (" . db_placeholders($roles) . ") AND grant_create = 1", $roles);
  while ($obj = db_fetch_object($result)) {
    $tids[$obj->tid] = $obj->tid;
  }

  // Also get all forums they happen to be able to moderate.
  $result = db_query("SELECT a.number AS tid FROM {acl} a INNER JOIN {acl_user} u ON a.acl_id = u.acl_id WHERE a.module = 'forum_access' AND u.uid = %d", $user->uid);
  while ($obj = db_fetch_object($result)) {
    $tids[$obj->tid] = $obj->tid;
  }

  // Ensure the forum they're trying to post to directly is allowed, otherwise
  // there will be much confusion.
  $forum_tid = arg(3);
  if (isset($forum_tid) && is_numeric($forum_tid) && !isset($tids[$forum_tid])) {
    drupal_access_denied();
    module_invoke_all('exit');
    exit;
  }
  foreach ($form['taxonomy'][$vid]['#options'] as $tid => $name) {
    if (!is_numeric($tid)) {
      $options[$tid] = $name;
    }
    elseif (is_object($name)) {
      foreach ($name->option as $sub_tid => $sub_name) {
        if (!empty($tids[$sub_tid])) {
          $options[$tid]->option[$sub_tid] = $sub_name;
        }
      }
    }
    elseif ($tids[$tid]) {
      $options[$tid] = $name;
    }
  }
  if ($options) {
    $form['taxonomy'][$vid]['#options'] = $options;
  }
  else {
    unset($form['taxonomy'][$vid]);
  }

  // Apply modifications for Moderators (by role or uid).
  if (!empty($user->_forum_access_moderator)) {

    // We gave this user the 'administer nodes' permission, which he doesn't
    // normally have. Remove controls that should be reserved to true node
    // administrators.
    _forum_access_disable_moderator();

    // not needed anymore
    $allowed_elements = variable_get('forum_access_allowed_node_edit_elements', array(
      'nid',
      'vid',
      'uid',
      'created',
      'type',
      'changed',
      'title',
      'shadow',
      'body_field',
      'revision_information',
      'form_build_id',
      'form_token',
      'form_id',
      'comment_settings',
      'taxonomy',
      'attachments',
    ));
    $allowed_options = variable_get('forum_access_allowed_node_edit_options', array(
      'status',
      'sticky',
      'subscriptions_notify',
    ));
    foreach (element_children($form) as $key) {
      switch ($key) {
        case 'options':
          foreach (element_children($form[$key]) as $key2) {
            if (array_search($key2, $allowed_options) === FALSE) {
              $form[$key][$key2]['#access'] = FALSE;
            }
          }
          break;
        case 'buttons':
          $tid = $form['taxonomy'][$vid]['#default_value'][0];
          if (!forum_access_access($tid, 'update')) {
            $form['buttons']['submit']['#access'] = FALSE;
            $form['buttons']['preview']['#access'] = FALSE;
          }
          if (!forum_access_access($tid, 'delete')) {
            $form['buttons']['delete']['#access'] = FALSE;
          }
          break;
        default:
          if (array_search($key, $allowed_elements) === FALSE) {
            $form[$key]['#access'] = FALSE;
          }
      }
    }
  }
}