You are here

function _forum_access_node_form in Forum Access 7

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

Rewrites the taxonomy item on the node form.

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

File

./forum_access.node.inc, line 89
forum_access.node.inc

Code

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

  // True node administrators are all powerful and do NOT get their forms rewritten here.
  if (user_access('bypass node access') && empty($user->_forum_access_moderator)) {
    return;
  }
  $roles = array_keys($user->roles);
  $tids = array();
  $result = db_query("SELECT tid FROM {forum_access} WHERE rid IN (:roles) AND grant_create = 1", array(
    ':roles' => $roles,
  ));
  foreach ($result as $obj) {
    $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 = :uid", array(
    ':uid' => $user->uid,
  ));
  foreach ($result as $obj) {
    $tids[$obj->tid] = $obj->tid;
  }
  $nid = $form['nid']['#value'];
  if (!empty($nid)) {

    // Edit an existing node.
    if (!forum_access_access('update', $form['forum_tid']['#value']) && !user_access('edit any forum content') && !(user_access('edit own forum content') && $form['uid']['#value'] == $user->uid)) {
      drupal_access_denied();
      drupal_exit();
    }
    $forum_tid = $form['forum_tid']['#value'];
    $tids[$forum_tid] = $forum_tid;
  }
  else {

    // Create a new node -- ensure the forum they're trying to post to directly
    // is allowed, otherwise there will be much confusion.
    $forum_tid = arg(3);
    if (!empty($forum_tid) && is_numeric($forum_tid) && !isset($tids[$forum_tid])) {
      drupal_access_denied();
      drupal_exit();
    }
  }
  $form_options =& $form['taxonomy_forums'][$form['taxonomy_forums']['#language']]['#options'];
  $options = array();
  foreach ($form_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 (isset($tids[$tid])) {
      $options[$tid] = $name;
    }
  }
  $form_options = $options;

  // Apply modifications for Moderators (by role or uid).
  if (!user_access('administer nodes') && forum_access_is_moderator($user, $forum_tid)) {
    $allowed_options = variable_get('forum_access_allowed_node_edit_options', array(
      'status',
      'sticky',
      'subscriptions_notify',
    ));
    foreach (element_children($form) as $key) {
      switch ($key) {
        case 'buttons':
          $tid = $form['taxonomy'][$vid]['#default_value'][0];
          if (!forum_access_access('update', $tid)) {
            $form['buttons']['submit']['#access'] = FALSE;
            $form['buttons']['preview']['#access'] = FALSE;
          }
          if (!forum_access_access('delete', $tid)) {
            $form['buttons']['delete']['#access'] = FALSE;
          }
          break;
        case 'author':
          $form['author']['#disabled'] = TRUE;
          break;
        case 'options':
          foreach (element_children($form['options']) as $key2) {
            if (array_search($key2, $allowed_options) === FALSE) {
              $form['options'][$key2]['#access'] = FALSE;
            }
          }
          $form['options']['#access'] = 1;
          break;
        case 'revision_information':
          $form['revision_information']['#access'] = 1;
          break;
        case 'comment_settings':
          $form['comment_settings']['#access'] = 1;
          break;
        case 'shadow':
          $form['shadow']['#description'] .= '<br />' . t('Note: Access to this topic and its shadows is controlled by the forum that contains the topic.');
          break;
        case 'taxonomy_forums':
          $desc =& $form['taxonomy_forums'][$form['taxonomy_forums']['#language']]['#description'];
          if (!empty($desc)) {
            $desc .= '<br />';
          }
          $desc .= t('Note: Moving a topic to a different forum may change its accessibility.');
      }
    }
  }
}