You are here

function forum_access_forum_form in Forum Access 5

Rewrite the forum administration page with our new access rules.

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

File

./forum_access.module, line 138
forum_access.module

Code

function forum_access_forum_form($form_id, &$form, $container) {
  $rids = array();
  $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
  while ($obj = db_fetch_object($result)) {
    $rids[$obj->rid] = $obj->name;
  }
  if (isset($form['tid']['#value'])) {

    // edit
    $result = db_query("SELECT * FROM {forum_access} where tid=%d", $form['tid']['#value']);
    while ($forum_access = db_fetch_object($result)) {
      $row_received = TRUE;
      if ($forum_access->grant_view) {
        $view[] = $forum_access->rid;
      }
      if ($forum_access->grant_update) {
        $update[] = $forum_access->rid;
      }
      if ($forum_access->grant_delete) {
        $delete[] = $forum_access->rid;
      }
      if ($forum_access->grant_create) {
        $create[] = $forum_access->rid;
      }
    }
    if (!isset($row_received) && empty($form['#post'])) {
      drupal_set_message(t('If you have only just installed !Forum_Access, then the posts in this forum may still be accessible, but once your permissions get rebuilt (intentionally or behind-the-scenes by some other module), they will vanish — so, be sure to set the desired Access Control below and save!', array(
        '!Forum_Access' => 'Forum Access',
      )), 'error');
    }
  }
  else {

    // create
    // Default to all users can read; all logged in users can post.
    $view = array(
      1,
      2,
    );
    $create = array(
      2,
    );
    $update = $delete = array();
  }
  $form['forum_access'] = array(
    '#type' => 'fieldset',
    '#title' => t('Access control'),
    '#collapsible' => TRUE,
    '#tree' => TRUE,
  );
  $form['forum_access']['view'] = array(
    '#type' => 'checkboxes',
    '#prefix' => '<div class="forum-access-div">',
    '#suffix' => '</div>',
    '#options' => $rids,
    '#title' => t('View this forum'),
    '#default_value' => $view,
  );
  $form['forum_access']['create'] = array(
    '#type' => 'checkboxes',
    '#prefix' => '<div class="forum-access-div">',
    '#suffix' => '</div>',
    '#options' => $rids,
    '#title' => t('Post in this forum'),
    '#default_value' => $create,
  );

  // Containers do not contain any nodes, so these fields become meaningless for them.
  if (!$container) {
    $form['forum_access']['update'] = array(
      '#type' => 'checkboxes',
      '#prefix' => '<div class="forum-access-div">',
      '#suffix' => '</div>',
      '#options' => $rids,
      '#title' => t('Edit posts'),
      '#default_value' => $update,
    );
    $form['forum_access']['delete'] = array(
      '#type' => 'checkboxes',
      '#prefix' => '<div class="forum-access-div">',
      '#suffix' => '</div>',
      '#options' => $rids,
      '#title' => t('Delete posts'),
      '#default_value' => $delete,
    );
  }

  // Find our moderator ACL:
  $form['forum_access']['clearer'] = array(
    '#value' => '<div class="forum-access-clearer"></div>',
  );
  drupal_add_css(drupal_get_path('module', 'forum_access') . '/forum_access.css');
  if (isset($form['tid']['#value'])) {

    // edit, not new
    $acl_id = db_result(db_query("SELECT acl_id from {acl} WHERE module = 'forum_access' AND name = '%d'", $form['tid']['#value']));
    if (!$acl_id) {

      // create one
      $acl_id = acl_create_new_acl('forum_access', $form['tid']['#value']);

      // update every existing node in this forum to use this acl.
      $result = db_query("SELECT nid FROM {term_node} WHERE tid = %d", $form['tid']['#value']);
      while ($node = db_fetch_object($result)) {

        // all privs to this ACL.
        acl_node_add_acl($node->nid, $acl_id, 1, 1, 1);
      }
    }
    $form['forum_access']['acl'] = acl_edit_form($acl_id, t('Moderators'));
  }
  $form['forum_access']['interference'] = array(
    '#type' => 'fieldset',
    '#title' => t('Module interference'),
    '#collapsible' => TRUE,
    '#attributes' => $is_conflict ? array(
      'class' => 'error',
    ) : array(),
  );
  $variables = array(
    '%content_type' => node_get_types('name', 'forum'),
    '!Forum_Access' => 'Forum Access',
    '!Content_Access' => 'Content Access',
    '!button' => t('Rebuild permissions'),
  );
  if (module_exists('content_access')) {
    $ca_settings = variable_get('content_access_settings', array());
    foreach (array(
      'view',
      'update',
      'delete',
      'per_node',
    ) as $type) {
      $value = content_access_get_settings($type, 'forum');
      if (!empty($value)) {
        $ca_interferes = TRUE;
      }
    }
    $ca_priority = content_access_get_settings('priority', 'forum');
    $fa_priority = 0;
    $is_conflict = $ca_priority >= $fa_priority && !empty($ca_interferes);
    $form['forum_access']['interference']['#attributes'] = $is_conflict ? array(
      'class' => 'error',
    ) : array();
    $variables['!link'] = l(t('!Content_Access configuration for the %content_type type', $variables), 'admin/content/types/forum/access', array(), NULL, NULL, FALSE, TRUE);
    $specifically = $ca_priority == $fa_priority ? t('Specifically, any grants given by !Content_Access cannot be taken back by !Forum_Access.', $variables) : '';
    if ($is_conflict) {
      $form['forum_access']['interference'][] = array(
        '#value' => '<p>' . t('You have set the !Content_Access module to control access to content of type %content_type&mdash;this can interfere with proper operation of !Forum_Access!', $variables) . " {$specifically}</p>",
      );
      if ($ca_priority == $fa_priority) {
        $form['forum_access']['interference'][] = array(
          '#value' => '<p>' . t("Unless you really know what you're doing, we recommend that you go to the !link page and clear all checkboxes.", $variables) . '</p>',
        );
      }
      else {
        $form['forum_access']['interference'][] = array(
          '#value' => '<div>' . t("The priority of !Content_Access ({$ca_priority}) is higher than the priority of !Forum_Access ({$fa_priority}), which means the latter is <b>completely disabled</b> for the %content_type type! Unless you really know what you're doing, we recommend that you go to the !link page and clear all checkboxes and/or change the priorities.", $variables) . '</div>',
        );
      }
    }
    else {
      $form['forum_access']['interference'][] = array(
        '#value' => '<p>' . t('Note: You have installed the !Content_Access module, which has the capability to grant access to content that would otherwise be protected by !Forum_Access. Be careful when configuring !Content_Access!', $variables) . '</p>',
      );
    }
  }
  $variables['!link'] = l('admin/content/node-settings', 'admin/content/node-settings');
  $form['forum_access']['interference'][] = array(
    // This doesn't apply to D6!
    '#value' => '<p>' . t("Note: If you have any other node access module installed besides !Forum_Access, and you've set that other module to use a grant priority (or 'weight') > 0 and to also control access to %content_type nodes, then you need to go to !link and click the [!button] button, <strong>after</strong> submitting this form. This is a limitation of Drupal 5 core. <br /> Be aware, however, that this may cause the !Forum_Access settings to be ignored in favor of that other modules' settings!", $variables) . '</p>',
  );

  // Move some stuff down so our block goes in a nice place.
  $form['submit']['#weight'] = 10;
  $form['delete']['#weight'] = 10;
  $form['#submit']['forum_access_form_submit'] = current($form['#submit']);
}