<?php

// 

require_once('discussion_forums.features.inc');

/**
 * Make some changes to configurations after the feature has been enabled
 */
function discussion_forums_enable() {
  // Check that the forum access module has its internal role set up and that
  // it has been renamed from "Forum Moderator," which is ridiculous
  
  $better_name = t('forum control (system use only)');
  
  // Get the role ID of the forum access moderator role; this also sets up the
  // role if for some reason it has not already been done
  module_load_include('inc', 'forum_access', 'forum_access.node');
  $rid = _forum_access_get_moderator_rid();
  
  // The role export module really messes with role IDs... be sure things are
  // normalized before thinking that the forum access role ID won't change
  role_export_normalize();
  
  $roles = user_roles();
  
  if (isset($roles[$rid]) AND $roles[$rid] == $better_name) {
    // Life is good
  }
  else {
    $better_rid = array_search($better_name, $roles);
    $old_rid = array_search('Forum Moderator', $roles);
    if (!$better_rid) {
      if ($old_rid) {
        db_query("UPDATE {role} SET name = '%s' WHERE rid = %d",
          $better_name, $old_rid);
        $better_rid = $old_rid;
        $old_rid = NULL;
      }
      else {
        drupal_set_message(t('The Forum Access module is not set up right. Try disabling the Discussion Forums feature and then manually uninstall the Forum Access module before enabling Discussion Forums again.', 'error'));
      }
    }
    // Use our "better named" role for the forum access moderator
    variable_set('forum_access_moderator_rid', $better_rid);
    if ($old_rid) {
      // Delete the default forum access role
      db_query("DELETE FROM {role} WHERE rid = '%d'", $old_rid);
    }
  }
}

function discussion_forums_menu_alter(&$items) {
  // Change the forum menu item to use the path: community/forum
  $items['community'] = $items['forum'];
  $items['community']['title'] = t('Community');
  $items['community']['type'] = MENU_NORMAL_ITEM;
  $items['community']['menu_name'] = 'primary-links';
  $items['community/forum'] = $items['community'];
  $items['community/forum']['title'] = t('Message boards');
  unset($items['forum']);
}
