Index: og.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/og/og.module,v
retrieving revision 1.365
diff -u -r1.365 og.module
--- og.module	4 Jun 2007 03:11:15 -0000	1.365
+++ og.module	29 Aug 2007 05:00:01 -0000
@@ -13,6 +13,12 @@
 define('OG_VISIBLE_CHOOSE_PUBLIC', 2);
 define('OG_VISIBLE_CHOOSE_PRIVATE', 3);
 
+// visibility states for private groups. site admin chooses in og_settings()
+define('OG_PRIVATE_OPTION_NEVER', 0);
+define('OG_PRIVATE_OPTION_ALWAYS', 1);
+define('OG_PRIVATE_OPTION_CHOOSE_TRUE', 2);
+define('OG_PRIVATE_OPTION_CHOOSE_FALSE', 3);
+
 // site admin chooses in og_settings() whether group creator can put his group on the registration form
 define('OG_REGISTRATION_NEVER', 0);
 define('OG_REGISTRATION_ALWAYS', 1);
@@ -1106,10 +1112,6 @@
   
   $edit = $_POST['edit'];
 
-  // all group home pages are publically accessible as far as og is concerned. their posts may or may not be.
-  // change this via hook_form_alter() if you want subscriber only group home pages. this may become part of og.module one day
-  $form['og_public'] = array('#type' => 'value', '#value' => TRUE);
-  
   $form['og_description'] = array('#type' => 'textfield', '#title' => t('Description'), '#default_value' => $node->og_description, '#size' => 70, '#maxlength' => 150, '#required' => true, '#description' => t('A brief description for the group details block and the group directory.'), '#weight' => -4);
   $form['og_website'] = array('#type' => 'textfield', '#title' => t('Group website'), '#default_value' => $node->og_website, '#description' => t('If your group has its own website, enter the address here.'));
 
@@ -1170,7 +1172,45 @@
       $form['og_directory'] = array('#type' => 'checkbox', '#title' => t('list in groups directory'),  '#default_value' => $node->nid ? $node->og_directory : $default, '#description' => t('Should this group appear on the !page?', array('!page' => l(t('list of groups page'),'og'))));
       break;    
   }
-  
+
+  // private groups
+  $visibility = variable_get('og_private_option', OG_PRIVATE_OPTION_CHOOSE_FALSE);
+  // override setting for admins - get right default
+  if (user_access('administer nodes')) {
+    $visibility = in_array($visibility, array(OG_PRIVATE_OPTION_NEVER, 
+                                              OG_PRIVATE_OPTION_CHOOSE_FALSE)) 
+                  ? OG_PRIVATE_OPTION_CHOOSE_FALSE : OG_PRIVATE_OPTION_CHOOSE_TRUE;
+  }
+
+  $default = FALSE;
+  switch ($visibility) {
+    case OG_PRIVATE_OPTION_NEVER :
+      $form['og_private'] = array (
+        '#type' => 'value',
+        '#value' => 0 
+      );
+      break;
+    
+    case OG_PRIVATE_OPTION_ALWAYS :
+      $form['og_private'] = array (
+        '#type' => 'value',
+        '#value' => 1
+      );
+      break;
+
+    case OG_PRIVATE_OPTION_CHOOSE_TRUE :
+      $default = TRUE;
+      // fall through
+
+    case OG_PRIVATE_OPTION_CHOOSE_FALSE :
+      $form['og_private'] = array (
+        '#type' => 'checkbox',
+        '#title' => t('private group'), 
+        '#default_value' => $node->nid ? $node->og_private : $default, 
+        '#description' => t('Should this group be visible only by its subscribers?'));
+        break;
+  }
+
   // language
   if (module_exists('locale') && $languages = locale_supported_languages()) {
     if (count($languages['name']) > 1) {
@@ -1235,20 +1275,20 @@
 }
 
 function og_load_group(&$node) {
-  $sql = 'SELECT selective AS og_selective, description AS og_description, theme AS og_theme, website AS og_website, register AS og_register, directory AS og_directory, notification AS og_notification, language AS og_language FROM {og} WHERE nid = %d';
+  $sql = 'SELECT selective AS og_selective, description AS og_description, theme AS og_theme, website AS og_website, register AS og_register, directory AS og_directory, notification AS og_notification, language AS og_language, private as og_private FROM {og} WHERE nid = %d';
   $result = db_query($sql, $node->nid);
   $node = (object) array_merge((array)$node, (array)db_fetch_array($result));
   $node->comment = COMMENT_NODE_DISABLED; // we don't use comments on og nodes. technically not needed since we set this on node submit
 }
 
 function og_insert_group($node) {
-  $sql = "INSERT INTO {og} (nid, theme, selective, description, website, register, directory, notification, language) VALUES (%d, '%s', %d, '%s', '%s', %d, %d, %d, '%s')";
-  db_query($sql, $node->nid, $node->theme, $node->og_selective, $node->og_description, $node->og_website, $node->og_register, $node->og_directory, $node->og_notification, $node->og_language);
+  $sql = "INSERT INTO {og} (nid, theme, selective, description, website, register, directory, notification, language, private) VALUES (%d, '%s', %d, '%s', '%s', %d, %d, %d, '%s', %d)";
+  db_query($sql, $node->nid, $node->theme, $node->og_selective, $node->og_description, $node->og_website, $node->og_register, $node->og_directory, $node->og_notification, $node->og_language, $node->og_private);
 }
 
 function og_update_group($node) {
-  $sql = "UPDATE {og} SET theme = '%s', selective = %d, register = %d, description = '%s', website = '%s', directory = %d, notification = %d, language = '%s' WHERE nid = %d";
-  db_query($sql, $node->theme, $node->og_selective, $node->og_register, $node->og_description, $node->og_website, $node->og_directory, $node->og_notification, $node->og_language, $node->nid);
+  $sql = "UPDATE {og} SET theme = '%s', selective = %d, register = %d, description = '%s', website = '%s', directory = %d, notification = %d, language = '%s', private = %d WHERE nid = %d";
+  db_query($sql, $node->theme, $node->og_selective, $node->og_register, $node->og_description, $node->og_website, $node->og_directory, $node->og_notification, $node->og_language, $node->og_private, $node->nid);
 }
 
 // returns TRUE if node type should generate email notifications when posted to a group.
@@ -1527,6 +1567,20 @@
     $vis = OG_VISIBLE_BOTH;
   }
   
+  // If the post is to a private group, and $vis is not OG_VISIBLE_GROUPONLY, 
+  // override to OG_VISIBLE_CHOOSE_PRIVATE so that posts in private groups will never default to public.
+  if (count($gids) && $vis > OG_VISIBLE_GROUPONLY) {  
+    foreach ($gids as $gid) {
+      $group_node = new stdClass();
+      $group_node->nid = $gid;
+      og_load_group($group_node);
+       if ($group_node->og_private) {
+         $vis = OG_VISIBLE_CHOOSE_PRIVATE;
+        break;
+      }
+    }
+  } 
+  
   switch ($vis) {
     case OG_VISIBLE_BOTH:
       $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 1);
@@ -2003,8 +2057,15 @@
   if (og_is_group_type($node->type)) {
     // this grant allows group admins to manage stuff
     $grants[] = array('realm' => 'og_subscriber', 'gid' => $node->nid, 'grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1);
-    // this one lets everyone see group homepage. see 'private groups' issue if you don't want this. we need help.
-    $grants[] = array('realm' => 'og_public', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0);
+    // If the group is not marked private let everyone view the group homepage.
+    if (!$node->og_private) {
+      $grants[] = array (
+        'realm' => 'og_public',
+        'gid' => 0,
+        'grant_view' => 1,
+        'grant_update' => 0,
+        'grant_delete' => 0 );
+    }
   }
   elseif (is_array($node->og_groups)) {
     // applies to non group nodes
@@ -2365,6 +2426,15 @@
               t('Group creator chooses whether her group appears on the registration form. Defaults to %out.', array('%out' => t('not on form'))),
             );
   $form['og_settings']['group_details']['og_visibility_registration'] = array('#type' => 'radios', '#title' => t('Registration form control'), '#default_value' => variable_get('og_visibility_registration', OG_REGISTRATION_CHOOSE_FALSE), '#description' =>t('OG admins always see the checkbox for adding a group to the %dir. Note that changing this setting has no effect on existing posts. Re-save those posts to acquire this new setting.', array('%dir' => t('registration form'))), '#options' => $options);
+
+  // private groups control
+  $options = array(t('New groups are always public.'),
+               t('New groups are always private.'),
+               t('Group creator chooses whether her group is private or not. Defaults to %yes.', array('%yes' => t('private'))),
+               t('Group creator chooses whether her group is private or not. Defaults to %no.', array('%no' => t('public'))),
+             );
+  $form['og_settings']['group_details']['og_private_option'] = array('#type' => 'radios', '#title' => t('Private groups control'), '#default_value' => variable_get('og_private_option', OG_PRIVATE_OPTION_CHOOSE_FALSE), '#description' =>t('A private group does not expose its group homepage to non subscribers. This has no impact on posts <b>within</b> the group, which can still be public or private. OG admins always see the checkbox for set groups as private. Note that changing this setting has no effect on existing groups.'), '#options' => $options);
+
     
   // email notifications default
   $options = array(OG_NOTIFICATION_SELECTIVE => t('New registrants are not subscribed to group email notifications by default. A user may choose to enable this from her profile page or her my subscriptions page.'),
Index: og.install
===================================================================
RCS file: /cvs/drupal/contributions/modules/og/og.install,v
retrieving revision 1.30
diff -u -r1.30 og.install
--- og.install	4 Jun 2007 03:36:36 -0000	1.30
+++ og.install	29 Aug 2007 05:00:00 -0000
@@ -15,6 +15,7 @@
         directory int(1) NOT NULL default 0,
         notification int(1) NOT NULL default 0,
         language varchar(12) NOT NULL default '',
+        private int(1) NOT NULL default 0,        
         PRIMARY KEY  (nid)
       ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
   
@@ -55,6 +56,7 @@
         directory numeric(1) NOT NULL default 0,
         notification numeric(1) NOT NULL default 0,
         language varchar(12) NOT NULL default '',
+        private numeric(1) NOT NULL default 0,        
         PRIMARY KEY  (nid)
       );");
 
@@ -422,6 +424,18 @@
   return array();
 }
 
+function og_update_19() {
+  switch ($GLOBALS['db_type']) {
+  case 'mysql':
+  case 'mysqli':
+    $ret[] = update_sql("ALTER TABLE {og} ADD private int(1) NOT NULL default 0");
+    break;
+  case 'pgsql':
+    $ret[] = update_sql("ALTER TABLE {og} ADD private numeric(1) NOT NULL default 0");
+    break;
+  }
+  return $ret;
+}
 
 // An implementation of hook_disable
 // Causes a node_access rebuild
