Index: og_subgroups.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/og_subgroups/og_subgroups.module,v
retrieving revision 1.8
diff -r1.8 og_subgroups.module
52a53
> 	
54a56,68
>   
>     $items[] = array('path' => 'admin/og/subgroups', 'title' => 'OG Subgroups Settings',
>       'callback' => 'drupal_get_form',
>       'callback arguments' => array('og_subgroups_settings'),
>       'access' => user_access('administer og_subgroups'),
>       'weight' => 1);
>       
>       $items[] = array(
>       'path' => 'og/subgroups/autocomplete',
>       'title' => t('OG Subgroups autocomplete'),      
>       'callback' => 'og_subgroups_parents_autocomplete',
>       'type' => MENU_CALLBACK
>     );
56d69
< 
59a73,152
> function og_subgroups_settings() {
> 
> 
> /*
> ideally, all of the settings data for all nodetypes would be stored in a single variable, a multidimensional array that would consolidate  all of the information for building the og subgroups parent form into one query:
> $a = array(
>   0 => array(
>     'type' => 'venue',
>     'child_types' => array('concert',),
>     'form_settings' => array(
>       'select_parents' => 1,
>       'select_children' => 0,
>       'separate_by_type' => 1,
>     ),
>   ),
> );
> */
>   $form['og_subgroups_settings']['og_subgroups_parent_children_types'] = array(
>     '#type' => 'fieldset',
>     '#title' => t('Parent-Child Group Relationships'),
>     '#collapsible' => TRUE,
>     '#collapsed' => FALSE,
>    );
>    
>   $og_types = variable_get('og_node_types', array());
> 
>   foreach ($og_types as $child_type) {
> 
>     $form['og_subgroups_settings']['og_subgroups_parent_children_types']['og_subgroups_child_' . $child_type] = array(
>       '#type' => 'fieldset',
>       '#collapsible' => TRUE,
>       '#collapsed' => TRUE,
>       '#title' => t($child_type),
>       );
>       
>     $form['og_subgroups_settings']['og_subgroups_parent_children_types']['og_subgroups_child_' . $child_type]['og_subgroups_form_settings_' . $child_type] = array(
>       '#type' => 'fieldset',
>       '#collapsible' => TRUE,
>       '#collapsed' => FALSE,
>       '#title' => t('Form Settings'),
>       );
> 
>     
>      $form['og_subgroups_settings']['og_subgroups_parent_children_types']['og_subgroups_child_' . $child_type]['og_subgroups_form_settings_' . $child_type]['og_subgroups_' . $child_type . '_set_parents'] = array(
>       '#type' => 'checkbox',
>       '#title' => t("Provide a 'set parents' form element"),
>       '#default_value' =>  variable_get('og_subgroups_' . $child_type . '_set_parents', 1),
>       );
>       
>        $form['og_subgroups_settings']['og_subgroups_parent_children_types']['og_subgroups_child_' . $child_type]['og_subgroups_form_settings_' . $child_type]['og_subgroups_' . $child_type . '_set_members'] = array(
>         '#type' => 'checkbox',
>         '#title' => t("Provide a 'set members' form element"),
>         '#default_value' =>  variable_get('og_subgroups_' . $child_type . '_set_members', 1),
>         );
>         
>         /*
>        $form['og_subgroups_settings']['og_subgroups_parent_children_types']['og_subgroups_child_' . $child_type]['og_subgroups_form_settings_' . $child_type]['og_subgroups_' . $child_type . '_autocomplete_text'] = array(
>               '#type' => 'checkbox',
>               '#title' => t("Provide an autocomplete form element for this node type."),
>               '#default_value' =>  variable_get('og_subgroups_' . $child_type . '_autocomplete_text', 0),
>               );
>               */
>         
>       $other_og_types = $og_types;
>       unset($other_og_types[$child_type]);
> //      drupal_set_message("other og types" . print_r($other_og_types));
>       $other_og_types['none'] = 'None';
>       $form['og_subgroups_settings']['og_subgroups_parent_children_types']['og_subgroups_child_' . $child_type]['og_subgroups_' . $child_type . '_parents'] = array(
>         '#type' => 'select',
>         '#multiple' => TRUE,
>         '#options' => $other_og_types,
>         '#validate' => array('og_subgroups_parent_types_validate' => array()),
>         '#title' => t('Parent Types'),
>         '#default_value' => variable_get('og_subgroups_' . $child_type . '_parents', $og_other_types),
>         );
>   }
>   
>   
>   return system_settings_form($form);
> }
62a156,165
>  
> function og_subgroups_parent_types_validate($element) {
>   $value = $element['#value'];
>   if (array_key_exists('none', $value)) {
>     unset($value['none']);
>     if (count($value) > 0)
>     form_error($element, t("Please choose either 'none' or one or more node types."));
>   }
> }
> 
64c167
<     return array('edit subgroups hierarchy', 'edit subgroups members');
---
>     return array('edit subgroups hierarchy', 'edit subgroups members', 'administer og_subgroups');
66a170
> 
82a187,193
>     case 'load':
>       if ($grps = og_subgroups_get_node_groups($node)) {
>         // TODO: Refactor so we don't need 2 arrays.
>         $node->og_groups = array_keys($grps);
>         $node->og_groups_names = array_values($grps);
>       }
>       break;
103a215,216
>     case 'submit':
>       drupal_set_message("Node parents = " . $node->parents);
106a220,231
> // returns all the group affiliations for a given subgroup.
> function og_subgroups_get_node_groups($node) {
>   $groups = array();
>   $parent_types = variable_get('og_subgroups_' . $node->type . '_parents', array());
>   if (og_is_group_type($node->type && !in_array('none', $parent_types))) {
>     $result = og_get_node_groups_result($node->nid);
>     while ($row = db_fetch_object($result)) {
>       $groups[$row->group_nid] = $row->title;
>     }
>     return $groups;
>   }
> }
122c247
<   if (user_access('edit subgroups hierarchy')) {
---
>   if (user_access('edit subgroups hierarchy') && variable_get('og_subgroups_' . $node->type . '_set_parents', 1)) {
125c250,259
< 	$form['parents'] = og_subgroups_parents_select(t('Parent(s)'), 'parents', $selected, $node->nid, array($node->nid));
---
>     if (variable_get('og_subgroups_' . $node->type . '_autocomplete_text', 0)) {
>       $gid = $node->nid;
>       if (!$gid) {
>         $gid = 0;
>       }
>       $form['parents'] = og_subgroups_parents_autocomplete_form("Test Autocomplete", $gid, $node->type);
>     }
>     else {
> 	    $form['parents'] = og_subgroups_parents_select(t('Parent(s)'), 'parents', $selected, $node->nid, $node->type, array($node->nid));
>     }
126a261
> 
128c263
<   if (user_access('edit subgroups members')) {
---
>   if (user_access('edit subgroups members') && variable_get('og_subgroups_' . $node->type . '_set_members', 1)) {
136a272,280
> function og_subgroups_parents_autocomplete_form($title, $gid, $type) {
>   
>   $form['parents'] = array(
>     '#type' => 'textfield',
>     '#autocomplete_path' => 'og/subgroups/autocomplete/' . $gid . '/' . $type . '/',
>     '#title' => t($title),
>   );
>   return $form;
> }
139a284,285
>  
> 
156,162c302,327
< function og_subgroups_parents_select($title, $name, $selected, $gid, $exclude = array()) {
<   $options = array();
<   $result = db_query('SELECT og.nid, n.title FROM {og} og INNER JOIN {node} n where og.nid=n.nid AND og.nid <> %d', $gid);
<   while ($row = db_fetch_object($result)) {
<     if (!in_array($row->nid, $exclude)) {
< 	  $options[$row->nid] = $row->title;
< 	}
---
> 
> function og_subgroups_parents_autocomplete($gid = 0, $type, $string = '') {
> //  drupal_set_message("gid ='" . $gid ."' type='" . $type . "' string='" . $string);
>     if ($string) {
> 
>       $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
>       preg_match_all($regexp, $string, $matches);
>       $array = $matches[1];
>       // Fetch last node
>       $last_string = trim(array_pop($array));
>       if ($last_string != '') {
>         $query = "SELECT og.nid, n.title FROM {og} og INNER JOIN {node} n WHERE og.nid=n.nid and LOWER(n.title) LIKE LOWER('%%%s%%') and n.nid <> %s";
>         $result = db_query_range($query, $last_string,$gid, 0, 10);
>       $prefix = count($array) ? implode(', ', $array) .', ' : '';
>       $matches = array();
>       while ($node = db_fetch_object($result)) {
> //       drupal_set_message("result " . dprint_r($result));
>         $n = $node->title;
>         if (strpos($node->title, ',') !== FALSE || strpos($node->title, '""') !== FALSE) {
>           $n = '""' . str_replace('"', '""', $node->title) .'"';
>         }
>         //$matches[$prefix . $n] = check_plain($node->title);
>        // drupal_set_message("prefix: " . $prefix . " n: " . $n);
>         $matches[check_plain($node->title . '[nid:'. $node->nid . ']')] = check_plain($node->title);
>       }
>     }
163a329,331
>   print drupal_to_js($matches); 
>   exit();  
> }
165,177c333,373
<   $selected = array_keys($selected);
<   if (count($options)) {
<     return array(
<       '#type' => 'select',
<       '#title' => $title,
<       '#default_value' => $selected,
<       '#options' => $options,
<       '#description' => '',
<       '#multiple' => 1,
<       '#size' => min(9, count($options)),
<       '#weight' => 0,
<     );
<   } else {
---
> 
> function og_subgroups_parents_select($title, $name, $selected, $gid, $type, $exclude = array()) {
>   $options = array();
>   $query = "SELECT og.nid, n.title FROM {og} og INNER JOIN {node} n where og.nid=n.nid AND og.nid <> %d and (n.type = '";
>   $child_types = variable_get('og_subgroups_' . $type . '_parents', array());
>   if (!array_key_exists('none', $child_types)) {
>     foreach ($child_types as $type) {
>       $query .= $type . "' or n.type ='";
>     }
> //    drupal_set_message("end of q:" . substr($query, -13));
>     if (substr($query, -13) == " or n.type ='") {
>       $query = substr($query, 0, -13);
>     }
>     $query .= ")";
>   //  drupal_set_message("Query:" . $query);
>     $result = db_query($query, $gid);
>     while ($row = db_fetch_object($result)) {
>       if (!in_array($row->nid, $exclude)) {
> 	      $options[$row->nid] = $row->title;
> 	    }
>     }
> 
>     $selected = array_keys($selected);
>     if (count($options)) {
>       return array(
>         '#type' => 'select',
>         '#title' => $title,
>         '#default_value' => $selected,
>         '#options' => $options,
>         '#description' => '',
>         '#multiple' => 1,
>         '#size' => min(9, count($options)),
>         '#weight' => 0,
>         );
>       }
>     else {
>         return "";
>     }
>     //not an error:
>   }
>   else {
181d376
< 
318a514
>   drupal_set_message("save parents = ". $parents);
332a529,532
>   
>   else {
>     //parse the nodereference field
>   }
443,446c643,648
<   foreach ($members as $uid=>$active) {
<     if ($active>0) {
<       og_save_subscription($gid, $uid, array('is_active' => 1));
<       $new_group[] = $uid;
---
>   if (is_array($members)) {
>     foreach ($members as $uid=>$active) {
>       if ($active>0) {
>         og_save_subscription($gid, $uid, array('is_active' => 1));
>         $new_group[] = $uid;
>       }
