? og-154168-1.patch
Index: og.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og/og.install,v
retrieving revision 1.69
diff -u -p -r1.69 og.install
--- og.install	9 Oct 2008 15:06:31 -0000	1.69
+++ og.install	7 Jan 2009 02:19:12 -0000
@@ -147,6 +147,13 @@ function og_schema() {
         'default'         => 1,
         'not null'        => TRUE,
       ),
+      'og_primary'       => array(
+        'description'     => t('Is this the primary group for this post?'),
+        'type'            => 'int',
+        'size'            => 'tiny',
+        'default'         => 0,
+        'not null'        => TRUE,
+      ),        
     ),
     'indexes'         => array(
       'nid'             => array('nid'),
@@ -400,6 +407,17 @@ function og_update_6002() {
   return $ret;
 }
 
+// Add the og_primary field. This field is used to store a 1/0 flag if the group is the primary group for a node
+function og_update_6003() {
+  $ret = array();
+  
+  $schema = drupal_get_schema_unprocessed('og', 'og_ancestry');
+  $fields = $schema['fields'];
+  db_add_field($ret, 'og_ancestry', 'og_primary', $fields['og_primary']);
+
+  return $ret;
+}
+
 // end updates //
 
 function og_uninstall() {
Index: og.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og/og.module,v
retrieving revision 1.616
diff -u -p -r1.616 og.module
--- og.module	31 Dec 2008 01:59:52 -0000	1.616
+++ og.module	7 Jan 2009 02:19:13 -0000
@@ -492,6 +492,23 @@ function og_set_group_context($group_nod
   return $stored_group_node;
 }
 
+/**
+ * API function for getting the primary group context of a node. 
+ *
+ * @return $primary_group node object
+ */
+function og_get_primary_group($node = NULL) {
+  static $primary_group;
+  
+  if(!isset($primary_group)){
+    $sql = 'SELECT a.group_nid FROM {og_ancestry} a WHERE a.nid = %d AND a.og_primary = 1';
+    $result = db_query($sql, $node->nid);
+    $primary_group = node_load(db_result($result, 0));
+  }
+  
+  return $primary_group;
+}
+
 function og_set_theme($nid) {
   global $custom_theme, $user;
   $node = node_load(intval($nid));
@@ -1973,6 +1990,8 @@ function og_form_add_og_audience(&$form,
   $node = $form['#node'];
   $required = variable_get('og_audience_required', 0) && !user_access('administer nodes');
   $is_optgroup = FALSE;
+  $select_primary = variable_get('og_primary_group', FALSE);
+  $primary_group = og_get_primary_group($node);    
   
   // Determine the list of groups that are shown.
   // Start by collecting all groups that the user is a member of.
@@ -2076,6 +2095,9 @@ function og_form_add_og_audience(&$form,
       
       // this 'value' element persists the audience value during submit process
       $form['og_nodeapi']['invisible']['og_groups'] = array('#type' => 'value', '#value' => $assoc_groups);
+      
+      // persist the primary group
+      $form['og_nodeapi']['invisible']['og_primary_group'] = array('#type' => 'value', '#value' => $primary_group->nid);      
     }
   }
   elseif ($cnt = count($options, COUNT_RECURSIVE)) {
@@ -2091,6 +2113,20 @@ function og_form_add_og_audience(&$form,
       '#default_value' => $groups ? $groups : array(),
       '#required' => $required, 
       '#multiple' => TRUE);
+    
+    //specify a primary group
+    if($select_primary){
+      $form['og_nodeapi']['visible']['og_primary_group'] = array(
+        '#type' => 'select', 
+        '#title' => t('Primary Audience'), 
+        '#attributes' => array('class' => 'og-primary'),
+        '#options' => $options, 
+        '#required' => FALSE, 
+        '#description' =>  'Select a primary audience for this post.',
+        '#default_value' => $primary_group->nid,
+        '#size' => count($options) > 6 ? 6 : count($options),
+        '#multiple' => FALSE);
+    }    
   }
   else if ($required) {
     form_set_error('title', t('You must <a href="@join">join a group</a> before posting a %type.', array('@join' => url('og'), '%type' => node_get_types('name', $node->type))));
@@ -2245,6 +2281,7 @@ function og_save_ancestry($node) {
           'nid' => $node->nid,
           'group_nid' => $gid,
           'is_public' => $node->og_public,
+          'og_primary' => $gid == $node->og_primary_group ? 1 : 0,          
         );
         drupal_write_record('og_ancestry', $ancestry);
       }
Index: includes/og.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og/includes/og.admin.inc,v
retrieving revision 1.15
diff -u -p -r1.15 og.admin.inc
--- includes/og.admin.inc	19 Nov 2008 04:23:49 -0000	1.15
+++ includes/og.admin.inc	7 Jan 2009 02:19:13 -0000
@@ -85,6 +85,14 @@ function og_admin_settings() {
     '#description' => t('Do you require that all (non administrator) posts be affiliated with a group? Note that changing this setting will affect existing posts when they are edited.')
   );
   unset($options);
+  
+  // Primary Group
+  $form['og_settings']['group_details']['og_primary_group'] = array(
+    '#type' => 'checkbox', 
+    '#title' => t('Primary Audience'), 
+    '#default_value' => variable_get('og_primary_group', FALSE), 
+    '#description' => t('Allow content creators to choose a primary audience for a node.')
+  );   
 
   // Messages fieldset.
   $form['og_settings']['notifications'] = array(
