Index: og_forum.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og_forum/Attic/og_forum.module,v
retrieving revision 1.11.2.11
diff -u -p -r1.11.2.11 og_forum.module
--- og_forum.module	3 May 2007 05:15:12 -0000	1.11.2.11
+++ og_forum.module	3 May 2007 05:38:33 -0000
@@ -144,6 +144,26 @@ function og_forum_settings_submit($form_
 }
 
 /**
+ * using the tid, set the group context
+ */
+function og_forum_set_og_group_context_from_tid($tid = 0) {
+  $sql = "SELECT nid FROM {og_term} WHERE tid = %d";
+  if ($gid = db_result(db_query($sql, $tid))) {
+  $group_node = node_load($gid);
+  og_set_group_context($group_node);
+  }
+}
+
+/**
+ * using the tid, get the group id
+ */
+function og_forum_gid_from_tid($tid = 0) {
+  $sql = "SELECT nid FROM {og_term} WHERE tid = %d";
+  $gid = db_result(db_query($sql, $tid));
+  return $gid;
+}
+
+/**
  * Implementation of hook_db_rewrite_sql().
  *
  * Restricts forum viewing by organic group.
@@ -151,33 +171,72 @@ function og_forum_settings_submit($form_
 function og_forum_db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid') {
   global $user;
 
+  $restrict = FALSE;
+  $restrict2 = FALSE;
   if ($primary_field == 'tid') {
 
-    // Only do this on forum nodes that belong to groups
-    if (arg(3)) {
-      $result = db_result(db_query("SELECT nid FROM {og_term} WHERE tid = %d", arg(3)));
+    // Only do this on forum nodes
+    if (((arg(0) == 'forum') || (arg(0) == 'og_forum') || (db_result(db_query('SELECT type FROM {node} WHERE nid = %d', arg(1))) == 'forum')) && ($user->uid != 1)) {
+      $restrict = TRUE;
     }
-    if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum' && (isset($_GET['gids']) || isset($result))) {
+    elseif ((arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum' && arg(3)) && ($user->uid != 1)) {
       $restrict = TRUE;
+      $restrict2 = TRUE;
     }
 
     // If on a forum node, prevent display of all forums; only the ones for this organic group
     // The forum vocab should have a lower weight than any other vocabulary assigned to forum nodes.
     static $og_vocab = FALSE;
     if ($restrict) {
-      $return['join'] = "INNER JOIN {og_term} ogt ON $primary_table.tid = ogt.tid INNER JOIN {og_uid} ogu ON ogt.nid = ogu.nid AND ogu.uid = $user->uid";
-      $return['where'] = 'ogu.is_active=1';
-      if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum') {
-        $og_nid = intval($_GET['edit']['og_groups'][0]);
+      //This query and the one below are what makes regular forums work side by side with og forums.
+      //Esentially, instead of sequentially restricting our selection based on whether the forum is in an organic group,
+      //we find which organic group forums we don't want to show and remove them from the site-wide forum list.
+      //What remains are all regular forums, plus those the user is subscribed to in each group and which is_active is set to 1.
+      $return['where'] = 't.tid NOT IN (
+        SELECT x . tid FROM (
+          SELECT ogt.tid FROM {og_term} ogt
+          INNER JOIN {og_uid} ogu
+          ON ogt.nid = ogu.nid AND ogu.uid != 2
+        ) x
+        WHERE x.tid NOT IN (
+          SELECT ogt.tid FROM {og_term} ogt
+          INNER JOIN {og_uid} ogu
+          ON ogt.nid = ogu.nid AND ogu.uid = 2
+        )
+      )
+      AND t.tid NOT IN (
+        SELECT ogt.tid FROM {og_term} ogt
+        INNER JOIN {og_uid} ogu
+        ON ogt.nid = ogu.nid AND ogu.uid = 2
+        WHERE ogu.is_active = 0
+      )';
+      if ($restrict2) {
+        $og_nid = og_forum_gid_from_tid(arg(3));
         if ($og_nid && !$og_vocab) {
           $og_vocab = TRUE;
-          $return['where'] = "ogt.nid = $og_nid";
+          //Same as above, except without removing is_active == 0 forums since we are looking for a particular node.
+          //Need to put the join in here since we need to match ogt.nid
+          $return['join'] = "LEFT JOIN {og_term} ogt ON $primary_table.tid = ogt.tid";
+          $return['where'] = "t.tid NOT IN (
+            SELECT x.tid FROM (
+              SELECT ogt.tid FROM {og_term} ogt
+              INNER JOIN {og_uid} ogu
+              ON ogt.nid = ogu.nid AND ogu.uid != 2
+            ) x
+            WHERE x.tid NOT IN (
+              SELECT ogt.tid FROM {og_term} ogt
+              INNER JOIN {og_uid} ogu
+              ON ogt.nid = ogu.nid AND ogu.uid = 2
+            )
+          )
+          AND ogt.nid = $og_nid";
         }
       }
       $return['distinct'] = TRUE;
       return $return;
     }
     else {
+      $og_vocab = FALSE;
       return NULL;
     }
   }
@@ -188,12 +247,17 @@ function og_forum_db_rewrite_sql($query,
  *
  * Automatically creates a forum container and forum each time an organic group is added.
  */
-function og_forum_nodeapi($node, $op, $teaser = NULL) {
+function og_forum_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
   switch ($op) {
+    case 'load':
+      if ($node->type == 'forum' && $og_forum_nid = db_result(db_query('SELECT nid FROM {og_term} WHERE tid = %d', $node->tid))) {
+        $node->og_forum_nid = $og_forum_nid;
+      }
+      break;
     case 'prepare':
-      if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum') {
-        if (!isset($_GET['edit']['og_groups'][0])) {
-          return FALSE;
+      if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum' && arg(3)) {
+        if (!og_get_group_context()) {
+          og_forum_set_og_group_context_from_tid(arg(3));
         }
       }
       break;
@@ -223,6 +287,13 @@ function og_forum_nodeapi($node, $op, $t
         db_query('DELETE FROM {og_term} WHERE nid = %d', $node->nid);
       }
       break;
+    case 'view':
+      // If we're viewing a forum post in a group forum, set that
+      // group as the context.
+      if ($page && $node->og_forum_nid) {
+        og_set_group_context(node_load($node->og_forum_nid));
+      }
+      break;
   }
 }
 
@@ -282,25 +353,20 @@ function og_forum_retroactively_apply() 
 function og_forum_form_alter($form_id, &$form) {
   // Auto-select group's forum when adding/editing a forum topic
   if ($form_id == 'forum_node_form') {
-    $gid = db_result(db_query("SELECT nid FROM {og_term} WHERE tid = %d", arg(3)));
-    if (!empty($gid)) {
-      $group = node_load($gid);
-      og_set_group_context($group);
-      $vid = _forum_get_vid();
-      $old_form = $form['taxonomy'][$vid];
-      foreach ($form['taxonomy'] as $key => $value) {
-        if (substr($key, 0, 1) != '#') {
-          unset($form['taxonomy'][$key]);
-        }
-      }
-      $form['taxonomy'][$vid] = $old_form;
-      if (user_access('administer organic groups') || variable_get('og_audience_checkboxes', TRUE)) {
-        $form['og_nodeapi']['visible']['og_groups']['#default_value'][] = $gid;
-      }
-      else {
-        $form['og_nodeapi']['invisible']['og_groups']['#default_value'][] = $gid;
+    $group = og_get_group_context();
+    if ($group->nid) {
+      $vid =  _forum_get_vid();
+      $term = arg(3);
+      $form['taxonomy'][$vid]['#default_value'] = array($term);
+      if (!user_access('administer forums')) {
+        unset($form['taxonomy'][$vid]['#default_value']);
+        $form['taxonomy']['#type'] = 'fieldset';
+        $form['taxonomy']['#title'] = 'Forum context';//comment this line out to hide the drop down; i couldn't get [#type] = 'hidden' to work
+        $form['taxonomy']['#collapsible'] = TRUE;
+        $form['taxonomy']['#collapsed'] = TRUE;
+        $form['taxonomy'][$vid]['#value'] = $term;
+        $form['taxonomy'][$vid]['#description'] = 'When inside a group forum, changing this value has no effect.';
       }
-      $form['og_nodeapi']['#collapsed'] = $gid ? TRUE : FALSE;
     }
   }
 }
@@ -322,10 +388,10 @@ function og_forum_manage($group) {
     foreach ($tree as $term) {
       if (in_array($term->tid, $og_terms)) {
         if (in_array($term->tid, variable_get('forum_containers', array()))) {
-          $rows[] = array(str_repeat(' -- ', $term->depth) .' '. check_plain($term->name), l(t('add forum'), "og_forum/manage/add/$group->nid/$term->tid"));
+          $rows[] = array(str_repeat('-', $term->depth) .' '. check_plain($term->name), l(t('add forum'), "og_forum/manage/add/$group->nid/$term->tid"));
         }
         else {
-          $rows[] = array(str_repeat(' -- ', $term->depth) .' '. check_plain($term->name), l(t('edit forum'), "og_forum/manage/edit/$group->nid/$term->tid"));
+          $rows[] = array(str_repeat('-', $term->depth) .' '. check_plain($term->name), l(t('edit forum'), "og_forum/manage/edit/$group->nid/$term->tid"));
         }
       }
     }
@@ -337,22 +403,31 @@ function og_forum_manage($group) {
 /**
  * Select the right form to display
  */
-function og_forum_form_forum($edit = array(), $gid = 0, $tid = 0) {
-  og_set_theme($gid);
-  $group = node_load($gid);
-  og_set_group_context($group);
-  _og_forum_manage_breadcrumb($group);
-  // Handle a delete operation.
-  if ($_POST['op'] == t('Delete') || $_POST['edit']['confirm']) {
-    return drupal_get_form('og_forum_confirm_delete', $edit['tid'], $gid);
+function og_forum_form_main($type, $edit = array()) {
+  if ($_POST['op'] == t('Delete') || $_POST['confirm']) {
+    return drupal_get_form('og_forum_confirm_delete', $edit['tid'], $group_id);
+  }
+  switch ($type) {
+    case 'add':
+      $group_id = $edit['group_id'];
+      return drupal_get_form('og_forum_form_forum', $edit, $group_id, $edit['tid']);
+      break;
+    case 'edit':
+      $group_id = $edit['group_id'];
+      return drupal_get_form('og_forum_form_forum', $edit, $group_id);
+      break;
   }
-  return drupal_get_form('og_forum_form', $edit, $gid, $tid);
 }
 
 /**
  * Render forum creation form
  */
-function og_forum_form($edit = array(), $gid = 0, $tid = 0) {
+function og_forum_form_forum($edit = array(), $gid = 0, $tid = 0) {
+  og_set_theme($gid);
+  if (!$edit['tid']) {
+    $tid = $edit['parent'];
+  }
+  _og_forum_manage_breadcrumb(node_load($group_id));
   $form['name'] = array('#type' => 'textfield',
     '#title' => t('Forum name'),
     '#default_value' => $edit['name'],
@@ -392,6 +467,7 @@ function og_forum_form($edit = array(), 
     $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
     $form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']);
   }
+  $form['#base'] = 'og_forum_form';
   return $form;
 }
 
