<?php
// $Id: forum_access.install,v 1.16 2009/12/03 23:40:19 salvis Exp $

/**
 * Implementation of hook_install().
 */
function forum_access_install() {
  drupal_install_schema('forum_access');
  db_query("UPDATE {system} SET weight = 2 WHERE name = 'forum_access'");
  
  if ($vid = variable_get('forum_nav_vocabulary', FALSE)) {
    foreach (array(DRUPAL_ANONYMOUS_RID => 0, DRUPAL_AUTHENTICATED_RID => 1) as $rid => $grant_create) {
      db_query("
        INSERT INTO {forum_access} (tid, rid, grant_view, grant_update, grant_delete, grant_create, priority)
        SELECT t.tid, %d, 1, 0, 0, %d, 0
          FROM {forum_access} fa RIGHT JOIN {term_data} t ON fa.tid = t.tid AND t.vid = %d AND fa.rid <> %d
            WHERE fa.tid IS NULL",
        $rid, $grant_create, DRUPAL_AUTHENTICATED_RID, $vid
      );
    }
  }
}

/**
 * Implementation of hook_schema().
 */
function forum_access_schema() {
  $schema['forum_access'] = array(
    'description'     => t('The base Forum Access Control table.'),
    'fields'          => array(
      'tid'           => array(
        'description' => t('The {term_data}.tid to which this {forum_access} entry applies.'),
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0),
      'rid'           => array(
        'description' => t('The {role}.rid to which this {forum_access} entry applies.'),
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0),
      'grant_view'    => array(
        'description' => t('Whether to grant "view" permission.'),
        'type'        => 'int',
        'size'        => 'tiny',
        'unsigned'    => TRUE,
        'not null'    => TRUE,
        'default'     => 0),
      'grant_update'  => array(
        'description' => t('Whether to grant "update" permission.'),
        'type'        => 'int',
        'size'        => 'tiny',
        'unsigned'    => TRUE,
        'not null'    => TRUE,
        'default'     => 0),
      'grant_delete'  => array(
        'description' => t('Whether to grant "delete" permission.'),
        'type'        => 'int',
        'size'        => 'tiny',
        'unsigned'    => TRUE,
        'not null'    => TRUE,
        'default'     => 0),
      'grant_create'  => array(
        'description' => t('Whether to grant "create" permission.'),
        'type'        => 'int',
        'size'        => 'tiny',
        'unsigned'    => TRUE,
        'not null'    => TRUE,
        'default'     => 0),
      'priority'  => array(
        'description' => t('The priority of this grant.'),
        'type'        => 'int',
        'size'        => 'small',
        'not null'    => TRUE,
        'default'     => 0)),
    'indexes'         => array(
      'tid'           => array('tid'),
      'rid'           => array('rid')),
  );
  return $schema;
}


/*
 * Implementation of hook_uninstall
 */
function forum_access_uninstall() {
  drupal_uninstall_schema('forum_access');
  variable_del('forum_access_allowed_node_edit_elements');
  variable_del('forum_access_allowed_node_edit_options');
  variable_del('forum_access_batch_threshold');
  variable_del('forum_access_default_template_tid');
  variable_del('forum_access_new_template_tid');
  variable_del('forum_access_provide_moderators_template_variable');
  variable_del('forum_access_rids');
}

/**
 * Purge orphaned grants that were left behind when deleting roles.
 */
function forum_access_update_1() {
  $ret = array();
  db_query("DELETE FROM {forum_access} WHERE rid NOT IN (SELECT rid from {role})");
  db_query("DELETE FROM {node_access} WHERE realm = 'forum_access' AND gid NOT IN (SELECT rid from {role})");
  return $ret;
}

/**
 * Add a priority column (will probably not be used until D6).
 */
function forum_access_update_2() {
  $ret = array();
  db_add_field($ret, 'forum_access', 'priority', array(
    'description' => t('The priority of this grant.'),
    'type'        => 'int',
    'size'        => 'small',
    'not null'    => TRUE,
    'default'     => 0));
  return $ret;
}

/**
 * Warn users upgrading from Drupal 5.
 */
function forum_access_update_6100() {
  drupal_set_message('<b>Upgrading Forum Access from Drupal 5?&nbsp; Then please read:</b><br />In Drupal 5, comment posting was not restricted by Forum Access; users with <em>View</em> access (and the <em>post comments</em> permission) were always allowed to post forum comments. Starting with Drupal 6, posting comments is now restricted to users with <em>Post</em> access. If you prefer the old behavior, then go to '. l('Forum Settings', 'admin/content/forum/settings', array('fragment' => 'edit-forum-admin-settings-forum-access')) .' and turn <em>Drupal 5 legacy mode</em> on.', 'warning', FALSE);
  return array();
}

/**
 * Clean out {forum_access} table and remove Forum Moderator assignments.
 */
function forum_access_update_6101() {
  $ret = array();
  $forum_vid = variable_get('forum_nav_vocabulary', 0);
  $ret[] = update_sql("DELETE FROM {forum_access} WHERE tid NOT IN (SELECT tid FROM {term_data} WHERE vid = $forum_vid)");
  $ret[] = update_sql("DELETE FROM {forum_access} WHERE rid NOT IN (SELECT rid FROM {role})");
  if ($moderator_rid = variable_get('forum_access_moderator_rid', FALSE)) {
    $ret[] = update_sql("DELETE FROM {users_roles} WHERE rid = $moderator_rid");
    $ret[] = update_sql("UPDATE {permission} SET perm = 'administer comments, administer nodes' WHERE rid = $moderator_rid");
  }
  return $ret;
}

/**
 * Set the proper permissions for the Forum Moderator role.
 */
function forum_access_update_6102() {
  $ret = array();
  $moderator_rid = forum_access_query_moderator_rid();
  if (isset($moderator_rid)) {
    if (db_result(db_query("SELECT COUNT(*) FROM {permission} WHERE rid = $moderator_rid"))) {
      $ret[] = update_sql("UPDATE {permission} SET perm = 'administer comments, administer nodes' WHERE rid = $moderator_rid");
    }
    else {
      $ret[] = update_sql("INSERT INTO {permission} (rid, perm) VALUES ($moderator_rid, 'administer comments, administer nodes')");
    }
  }
  return $ret;
}

