Index: og_subgroups.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og_subgroups/og_subgroups.module,v
retrieving revision 1.14
diff -r1.14 og_subgroups.module
230a231
>   
234c235,249
<       $form = array_merge($form, og_subgroups_form($node));
---
>       if (user_access('edit subgroups hierarchy') && variable_get('og_subgroups_' . $node->type . '_set_parents', 1)) {
>         $selected = array();
>     	  if (isset($node->nid)) {
>           $selected = og_subgroups_get_parents($node->nid);
>     	  }
>         og_subgroups_form_add_audience($form_id, &$form);
>       }
> 
>       if (user_access('edit subgroups members') && variable_get('og_subgroups_' . $node->type . '_set_members', 1)) {
>         $selected = array();
>         if (isset($node->nid)) {
>           $selected = og_subgroups_get_users('names',$node->nid,0);
>     	  }
>         $form['members'] = og_subgroups_members_select($selected);
>       }  
239,240c254,267
< function og_subgroups_form($node) {
<   $form = array();
---
> 
> function og_subgroups_get_sql_args($type) {
>   if (empty($type)) {
>     $types = variable_get('og_node_types', array('og'));
>   }
>   else {
>     $types = variable_get('og_subgroups_' . $type . '_parents', array());
>   }
>     
>   $in = implode(', ', array_fill(0, count($types), "'%s'"));
>   return array($types, $in);
> }
> 
> function og_subgroups_selective_groups_options($type) {
242,247c269,279
<   if (user_access('edit subgroups hierarchy') && variable_get('og_subgroups_' . $node->type . '_set_parents', 1)) {
<     $selected = array();
< 	if (isset($node->nid)) {
<       $selected = og_subgroups_get_parents($node->nid);
< 	}
<     $form['parents'] = og_subgroups_parents_select(t('Parent(s)'), 'parents', $selected, $node->nid, $node->type, array($node->nid));
---
>   //Note that these two things are possible: 
>   //1) Type is not set, in which case we return a non-selective list of node types.
>   //2) Type is set but this type has no parent types available
>   
>   if (empty($type) && !in_array($type, variable_get('og_node_types', array('og')))) { //type is not set or node is not an og_type
>     return og_all_groups_options();
>   }
>   
>   else {
>     $types = variable_get('og_subgroups_' . $type . '_parents', array());
>   }
248a281,329
>   if (!empty($types) && count($types) > 0) { //Does $types have any contents?
>     list($types, $in) = og_subgroups_get_sql_args($type);
>     
>     $sql = "SELECT n.title, n.nid FROM {node} n WHERE n.type IN ($in) AND n.status = 1 ORDER BY n.title ASC";
>     $result = db_query($sql, $types);
>     while ($row = db_fetch_object($result)) {
>       $options[$row->nid] = $row->title;
>     }
>     return $options ? $options : array();
>   }
>   else {
>     return array(); //If not, return an empty array.
>   }
> }
> 
> /**
>  * A modified version of og_form_add_og_audience that takes into account appropriate parent types of OG homepage nodes.
>  */
> function og_subgroups_form_add_audience($form_id, &$form) {
>   global $user;
>   
>   $node = $form['#node'];
>   $prop_type = variable_get('og_subgroups_prop_type', 'none');
>   $filtered_options = og_subgroups_selective_groups_options($node->type);
>   $og_node_types = variable_get('og_node_types', array('og'));
>   
>   $parent_node_types = variable_get('og_subgroups_' . $node->type .'_parents', array($og_node_types));  //Avoided nesting another variable_get here.
>   // determine the selected groups if fapi doesn't tell us.
>   if ($_SERVER["REQUEST_METHOD"] == 'GET') {
>     $gids = $_GET['gids'];
>   }
>   elseif ($_POST['og_groups_hidden']) {
>     $gids = unserialize($_POST['og_groups_hidden']);
>   }
>   
> 
>   $required = variable_get('og_audience_required', 0) && !user_access('administer nodes');
>   
>   
>   // Determine the list of groups that are shown.
>   // Start by collecting all groups that the user is a member of.
>   $subs = og_get_subscriptions($user->uid);
>   $options = array();
>   
>   foreach ($subs as $key => $val) {
>     if (isset($parent_node_types[$val['type']])) {
>       $options[$key] = $val['title'];
>     }
>     
249a331,335
>   if (user_access('administer nodes')) {
>     drupal_set_message("admin nodes");
>     // Node admins see all of groups.
>     $other = array_diff_assoc($filtered_options, $options);    
>     // Use an optgroup if admin is not a member of all groups.
251,256c337,377
<   if (user_access('edit subgroups members') && variable_get('og_subgroups_' . $node->type . '_set_members', 1)) {
<     $selected = array();
<     if (isset($node->nid)) {
<       $selected = og_subgroups_get_users('names',$node->nid,0);
< 	}
<     $form['members'] = og_subgroups_members_select($selected);
---
>     if ($other) {
>       $options = array(
>         t('My subscribed groups') => $options,
>         t('Other groups') => $other,
>       );
>       $is_optgroup = TRUE;
>     }
>     else {
>       $options = $all;
>     }
>   }
>   else {
>     // Classify those groups which the node already has but the author does not.
>     if (function_exists('og_node_groups_distinguish')) {
>       $current_groups = og_node_groups_distinguish($node->og_groups, $node->og_groups_names);
>     }
> 
>     
>     // Put inaccessible groups in the $form so that they can persist. See og_submit_group()
>     $form['og_invisible']['og_groups_inaccessible'] = array('#type' => 'value', '#value' => $current_groups['inaccessible']);
>     
>     // Add the accessible groups that they node already belongs to.
>     if ($current_groups['accessible']) {  
>       // Use an optgroup to distinguish between my subscriptions and additional groups in the Audience select. 
>       // There is code below which assumes that $options does not have optgroups but that code is within a $simple check 
>       // So we are OK as long as $simple does not apply to node edits.
>       // NOTE: If you form_alter the audience element, beware that it can sometimes be an optgroup.
>       
>       foreach ($current_groups['accessible'] as $key => $val) {
>         $other[$key] = $val['title'];
>       }
>       
>       $options = array(
>         t('My subscribed groups') => $options,
>         t('Other groups') => $other,
>       );
>       $is_optgroup = TRUE;
>     }
>     else {
>       $is_optgroup = FALSE;
>     }
259c380,493
<   return $form;
---
>   // show read only item if we are non-admin, and in simple mode (i.e. non-checkboxes) and at least one group is in querystring
>   $simple = !user_access('administer organic groups') && !variable_get('og_audience_checkboxes', TRUE) && count($gids);
>   
>   // determine value of audience multi-select
>   if (count($options) == 1 && $required) {
>     $gids = array_keys($options);
>     $gid = $gids[0];
>     $groups = array($gid);
>     // also show read only mode if user has 1 option and we are in required mode
>     $simple = TRUE;
>   }
>   elseif ($gids) {
>    // populate field from the querystring if sent
>    $groups = $gids;
>    if (!user_access('administer nodes') && $simple) {
>      // filter out any groups where author is not a member. we cannot rely on fapi to do this when in simple mode. 
>      $groups = array_intersect($gids, array_keys($options));
>    }
>   }
>   elseif ($node->nid || $node->og_groups) {
>     $groups = $node->og_groups;
>   }
>   else {
>     $groups = array();
>   }
> 
>   // don't bother with visibility if access control is disabled. all is public.
>   if (variable_get('og_enabled', FALSE)) {
>     // get the visibility for normal users
>     $vis = variable_get('og_visibility', 0);
> 
>     // override visibility for og admins and when author only has 1 group
>     if (user_access('administer organic groups') && $vis < 2) {
>       $vis = $vis == OG_VISIBLE_GROUPONLY ? OG_VISIBLE_CHOOSE_PRIVATE : OG_VISIBLE_CHOOSE_PUBLIC;
>     }
>     elseif (!count($options)) {
>       // don't show checkbox if no subscriptions. must be public.
>       $vis = OG_VISIBLE_BOTH;
>     }
> 
>     // If the post is to a private group, visibility must default to one of the private options.
>     // Try not to show checkbox if admin likes to reduce decisions for node authors.
>     $selected_groups = isset($form['#post']['og_groups']) ? array_filter($form['#post']['og_groups']) : $groups;
>     if (count($selected_groups)) {  
>       foreach ($selected_groups as $gid) {
>         $group_node = new stdClass();
>         $group_node->nid = $gid;
>         og_load_group($group_node);
>          if ($group_node->og_private) {
>            $vis = variable_get('og_visibility', 0) == OG_VISIBLE_BOTH ? OG_VISIBLE_GROUPONLY : OG_VISIBLE_CHOOSE_PRIVATE;
>           break;
>         }
>       }
>     } 
> 
>     switch ($vis) {
>       case OG_VISIBLE_BOTH:
>         $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 1);
>         break;
>       case OG_VISIBLE_GROUPONLY:
>         $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 0);
>         break;
> 
>       //user decides how public the post is.
>       case OG_VISIBLE_CHOOSE_PUBLIC:
>         $form['og_nodeapi']['visible']['og_public'] = array('#type' => 'checkbox', '#title' => t('Public'), '#default_value' => $node->nid ? $node->og_public : 1, '#description' => t('Show this post to everyone, or only to subscribers of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
>         break;
>       case OG_VISIBLE_CHOOSE_PRIVATE:
>         $form['og_nodeapi']['visible']['og_public'] = array('#type' => 'checkbox', '#title' => t('Public'), '#default_value' => $node->nid ? $node->og_public : 0, '#description' => t('Show this post to everyone, or only to subscribers of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
>         break;
>     }
>   }
> 
>   // Emit the audience form element.
>   if ($simple) {
>     // 'simple' mode. read only.
>     if (count($groups)) {
>       foreach ($groups as $gid) {
>         $titles[] = $options[$gid];
>         $item_value = implode(', ', $titles);
>       }
>       $form['og_nodeapi']['visible']['og_groups_visible'] = array('#type' => 'item', '#title' => t('Audience'), '#value' => $item_value);
>       $assoc_groups = drupal_map_assoc($groups);
>       // this hidden element persists audience values during a Preview cycle. avoids errors on Preview.
>       $form['og_nodeapi']['invisible']['og_groups_hidden'] = array('#type' => 'hidden', '#value' => serialize($assoc_groups));
>       // this 'value' element persists the audience value during submit process
>       $form['og_nodeapi']['invisible']['og_groups'] = array('#type' => 'value', '#value' => $assoc_groups);
>     }
>   }
>   elseif ($cnt = count($options, COUNT_RECURSIVE)) {
>     drupal_add_js(drupal_get_path('module', 'og'). '/og.js');
>     // show multi-select. if less than 20 choices, use checkboxes.
>     $type = $cnt >= 20 || $is_optgroup ? 'select' : 'checkboxes';
>     $form['og_nodeapi']['visible']['og_groups'] = array(
>       '#type' => $type, 
>       '#title' => t('Audience'), 
>       '#attributes' => array('class' => 'og-audience'),
>       '#options' => $options, 
>       '#required' => $required, 
>       '#description' =>  format_plural(count($options), 'Show this post in this group.', 'Show this post in these groups.'),
>       '#default_value' => $groups, 
>       '#required' => $required, 
>       '#multiple' => TRUE);
>   }
>   else if ($required) {
>     form_set_error('title', t('You must !join before posting a %type.', array('!join' => l(t('join a group'), 'og'), '%type' => node_get_types('name', $node->type))));
>   }
>   
>   if (count($form['og_nodeapi']['visible']) > 1) {
>     $form['og_nodeapi']['#type'] = 'fieldset';
>     $form['og_nodeapi']['#title'] = t('Groups');
>     $form['og_nodeapi']['#collapsible'] = TRUE;
>     $form['og_nodeapi']['#collapsed'] = $gids ? TRUE : FALSE;
>   }
416,417c650
<   $result = db_query('SELECT oga.nid, n.title, n.type FROM {og_ancestry} oga INNER JOIN {node} n where oga.group_nid=%d AND oga.nid=n.nid', $gid);
<   
---
>   $result = db_query('SELECT oga.nid, n.title, n.type FROM {og_ancestry} oga INNER JOIN {node} n WHERE oga.group_nid=%d AND oga.nid=n.nid', $gid);
421,422c654,655
< 	  $children[$row->nid] = $row->title;
< 	}
---
> 	    $children[$row->nid] = $row->title;
> 	  }
424d656
<   
683c915
<   $result = db_query('SELECT n.title, n.type, n.status, oga.group_nid as nid, 1 as is_active FROM {og_ancestry} oga INNER JOIN {node} n where oga.nid=%d AND oga.group_nid=n.nid', $gid);
---
>   $result = db_query('SELECT n.title, n.type, n.status, oga.group_nid as nid, 1 as is_active FROM {og_ancestry} oga INNER JOIN {node} n WHERE oga.nid=%d AND oga.group_nid=n.nid', $gid);
690c922
<   }
---
> }
