--- og_user_roles.module.5.x-3.1.orig	2008-05-11 11:41:52.000000000 -0700
+++ og_user_roles.module	2008-05-15 00:33:19.000000000 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: og_user_roles.module,v 1.1.2.14 2008/05/11 04:27:55 somebodysysop Exp $
+// $Id: og_user_roles.module,v 1.1.2.3 2007/07/08 19:50:44 somebodysysop Exp $
 
 /**
  * @file
@@ -237,7 +237,7 @@ function og_user_roles_admin_settings() 
     '#rows' => 2,
     '#description' => t('Specify here the node IDs of nodes which you want placed into all groups, each separated by a comma. Structure your entries like this:
         <p><strong>63,20<br /></strong></p>
-        <p>Each node ID separated by a comma.'),
+        <p>Each node ID separated by a comma. Note that this process is completed at the next scheduled cron run.  New and existing groups are only updated when cron is run.  A log of updated groups is also written.  Also removing the node from here does NOT remove it from the group.  If you wish to remove nodes that have been placed into groups here, you must do that manually.'),
     );
 
   $form['og_user_roles_tac_og_default'] = array(
@@ -380,8 +380,7 @@ function og_user_roles_multinode($theme 
     $form[$realm]['realm'] = array('#type' => 'textfield', '#size' => 25, '#disabled' => TRUE, '#value' => check_plain($realm), '#default_value' => $realm); 
     $form[$realm]['group'] = array('#type' => 'textfield', '#size' => 5, '#default_value' => (isset($existing[$realm]->groupname) ? $existing[$realm]->groupname : '') ); 
     $form[$realm]['logic'] = array('#type' => 'select', '#default_value' => (isset($existing[$realm]->logic) ? $existing[$realm]->logic : 'AND'), '#options' => array('AND' => 'AND', 'OR' => 'OR') ); 
-//    $form[$realm]['weight'] = array('#type' => 'select', '#default_value' => (isset($existing[$realm]->weight) ? $existing[$realm]->weight : 'A'), '#options' => array('A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G') ); 
-    $form[$realm]['weight'] = array('#type' => 'select', '#default_value' => (isset($existing[$realm]->weight) ? $existing[$realm]->weight : '0'), '#options' => array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6') ); 
+    $form[$realm]['weight'] = array('#type' => 'select', '#default_value' => (isset($existing[$realm]->weight) ? $existing[$realm]->weight : '0'), '#options' => array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9') ); 
     $form[$realm]['check'] = array('#type' => 'select', '#default_value' => (isset($existing[$realm]->checkstatus) ? $existing[$realm]->checkstatus : '0'), '#options' => array('0' => '0', '1' => '1') ); 
   }
 
@@ -992,6 +991,33 @@ function og_user_roles_init() {
 	 */
     $exempted = array('ad','minutes');
 
+    $orig_query = $_SERVER['QUERY_STRING'];
+	// Get rid of Create Content 'node/add?gids[]'
+      $pattern = '/q\=.*node\/add\&gids\[\]\=(\d+)/i';
+      $orig_query = preg_replace($pattern, "", $orig_query);
+	// Get rid of Create Content 'node/ognodeadd&gids[]'
+      $pattern = '/q\=.*node\/ognodeadd\&gids\[\]\=(\d+)/i';
+      $orig_query = preg_replace($pattern, "", $orig_query);
+	// Get rid of Create Content 'node/ognodeadd&type='
+      $pattern = '/q\=.*node\/ognodeadd\&type\=(\w+)/i';
+      $orig_query = preg_replace($pattern, "", $orig_query);
+	// Get rid of 'node/add followed by &'
+      $pattern = '/q\=.*node\/add\/[^\&]+\&/i';
+      $orig_query = preg_replace($pattern, "", $orig_query);
+	// Get rid of 'node/add followed which ends the line'
+      $pattern = '/q\=.*node\/add\/[^\&]+$/i';
+      $orig_query = preg_replace($pattern, "", $orig_query);
+	// Get rid of 'gids[]='
+      $pattern = '/.*gids\[\]\=(\d+)[\&]?/i';
+      $orig_query = preg_replace($pattern, "", $orig_query);
+	// Get rid of 'node/ognodeadd'
+      $pattern = '/q\=.*node\/ognodeadd/i';
+      $orig_query = preg_replace($pattern, "", $orig_query);
+	// Get rid of '&' if it starts a line
+      $pattern = '/^\&/i';
+      $orig_query = preg_replace($pattern, "", $orig_query);
+//    drupal_set_message($orig_query);
+
     // If this is a group node/add, re-direct to ognodeadd
     if (arg(0) == 'node' && arg(1) == 'add' && isset($_REQUEST['gids']) && (!in_array(arg(2), $exempted)) ) {
       
@@ -1029,6 +1055,7 @@ function og_user_roles_init() {
         $path .= '/book/parent/'. arg(4);
       }
 
+      if ($orig_query) $query = $query . '&' . $orig_query;
       drupal_goto($path, $query);
     }
 
@@ -1043,6 +1070,7 @@ function og_user_roles_init() {
       if (!empty($gid) && $gid != 0) { 
         $path = 'node/ognodeadd';
         $query = 'type='. $type .'&gids[]='. $gid .'&tid='. $tid;
+        if ($orig_query) $query = $query . '&' . $orig_query;
         drupal_goto($path, $query);
       }
     }
@@ -1087,6 +1115,7 @@ function og_user_roles_init() {
           $path .= '/book/parent/'. arg(4);
         }
 
+        if ($orig_query) $query = $query . '&' . $orig_query;
         drupal_goto($path, $query);
       }
     }
@@ -1271,6 +1300,7 @@ function og_user_roles_init() {
       if (module_exists('book') && $type == 'book' && arg(3) == 'parent' && is_numeric(arg(4))) {
         $path .= '/book/parent/'. arg(4);
       }
+       if ($orig_query) $query = $query . '&' . $orig_query;
        drupal_goto($path, $query);
     }
   }
@@ -1504,27 +1534,6 @@ function og_user_roles_nodeapi(&$node, $
       break;
 
     case 'submit':
-      // Put appropriate nodes into all groups upon node submission;
-
-      // Get list of nodes to put into all groups;
-      $nodelist = variable_get('og_user_roles_all_groups_nodes', '');
-
-      if (!empty($nodelist)) {
-        $nodelist_array = explode(',', $nodelist);
-
-        foreach ($nodelist_array as $nodelist_item) {
-          if ($node->nid == $nodelist_item) {
-              if ($grps = og_user_roles_get_groups($node)) {
-                $node->og_groups = array_keys($grps);
-                $node->og_groups_names = array_values($grps);
-                $public = db_result(db_query_range("SELECT is_public FROM {og_ancestry} WHERE nid = %d", $node->nid, 0, 1));
-                $node->og_public = $public ? TRUE : FALSE;
-              } // end $grps if
-          } // end $node->nid if
-        } // end foreach
-
-      } // end $nodelist if
-
       break;
 
     case 'insert':
@@ -2733,6 +2742,7 @@ function og_user_roles_form_alter($form_
       '#description' => t('If you received a group registration code, enter it here.'),
     );
   }
+  $roles = array();
   // If this is a group node edit (use og_last to determine if this is a group node) and
   // the user has the 'configure member roles' permission, and we allow group admins
   // to set default group roles, then print the 'basicgrouprole' form on the
@@ -2813,6 +2823,27 @@ function og_user_roles_form_alter($form_
           $form['#validate'] += array('og_user_roles_logo_form_validate' => array($form_id, &$form));          
           $form['#submit'] += array('og_user_roles_logo_form_submit' => array($form_id, &$form));          
 
+        // Add the code to display 'Create content' link and/or remove 'create_' links.
+
+          $form['og_user_roles_links_gid'] = array(
+            '#type' => 'fieldset',
+            '#title' => t('Control group menu content links.'),
+            '#collapsible' => TRUE,
+            );
+          $form['og_user_roles_links_gid']['og_user_roles_links_content_' . $gid] = array(
+            '#type' => 'checkbox',
+            '#title' => t('Display the <strong>Create content</strong> link?'),
+            '#default_value' => variable_get('og_user_roles_links_content_' . $gid, 0),
+            '#description' => t('Do you wish to display the OGR <strong>Create content</strong> link on this group\'s menu?'),
+            );
+          $form['og_user_roles_links_gid']['og_user_roles_links_remove_' . $gid] = array(
+            '#type' => 'checkbox',
+            '#title' => t('Remove the default <strong>Create</strong> links?'),
+            '#default_value' => variable_get('og_user_roles_links_remove_' . $gid, 0),
+            '#description' => t('Do you wish to remove the default OG <strong>Create</strong> content links on this group\'s menu?'),
+            );
+          $form['#submit'] += array('og_user_roles_links_form_submit' => array($form_id, &$form));          
+
         // Add the code to disallow public posts to the
         // group edit form.
 
@@ -2863,19 +2894,22 @@ function og_user_roles_form_alter($form_
               <p><strong>Code1,Code2,Code3<br /></strong></p>
               <p>Each registration code separated by a comma. No spaces.'),
             );
-          $form['og_user_roles_regcode_gid']['og_user_roles_assign_regcoderole_' . $gid] = array(
-            '#type' => 'checkbox',
-            '#title' => t('Set default basic group (group limited) role for users who join this group using this registration code?'),
-            '#default_value' => variable_get('og_user_roles_assign_regcoderole_' . $gid, 0),
-            '#description' => t('Do you wish to automatically assign a specific "registration code group role" to new subscribers to this group <b>who use this registration code to subscribe</b> to this group? The role is limited to this group. This role assignment can be be removed by the groups\' admin(s)'),
-            );
-          $form['og_user_roles_regcode_gid']['og_user_roles_regcoderole_value_' . $gid] = array(
-            '#type' => 'select',
-            '#title' => t('Role to assign'),
-            '#options' => $roles,
-            '#default_value' => variable_get('og_user_roles_regcoderole_value_' . $gid, 0),
-            '#description' => t('Select the role you wish to use as the "registration code group role" for every new subscriber who subscribes to this group using one of the registration codes above.'),
-            );      
+      	  // They only see this if they can configure user roles
+          if (user_access('configure member roles')) {
+            $form['og_user_roles_regcode_gid']['og_user_roles_assign_regcoderole_' . $gid] = array(
+              '#type' => 'checkbox',
+              '#title' => t('Set default basic group (group limited) role for users who join this group using this registration code?'),
+              '#default_value' => variable_get('og_user_roles_assign_regcoderole_' . $gid, 0),
+              '#description' => t('Do you wish to automatically assign a specific "registration code group role" to new subscribers to this group <b>who use this registration code to subscribe</b> to this group? The role is limited to this group. This role assignment can be be removed by the groups\' admin(s)'),
+              );
+            $form['og_user_roles_regcode_gid']['og_user_roles_regcoderole_value_' . $gid] = array(
+              '#type' => 'select',
+              '#title' => t('Role to assign'),
+              '#options' => $roles,
+              '#default_value' => variable_get('og_user_roles_regcoderole_value_' . $gid, 0),
+              '#description' => t('Select the role you wish to use as the "registration code group role" for every new subscriber who subscribes to this group using one of the registration codes above.'),
+              );      
+          }
           $form['#validate'] += array('og_user_roles_regcode_form_validate' => array($form_id, &$form));          
           $form['#submit'] += array('og_user_roles_regcode_form_submit' => array($form_id, &$form));          
         }
@@ -2920,6 +2954,11 @@ function og_user_roles_logo_form_validat
 	}
   }
 }
+function og_user_roles_links_form_submit($form_id, $form_values) {
+  $gid = arg(1);
+  variable_set('og_user_roles_links_content_' . $gid, $form_values['og_user_roles_links_content_' . $gid]);
+  variable_set('og_user_roles_links_remove_' . $gid, $form_values['og_user_roles_links_remove_' . $gid]);
+}
 function og_user_roles_nopublic_form_submit($form_id, $form_values) {
   $gid = arg(1);
   variable_set('og_user_roles_assign_nopublic_' . $gid, $form_values['og_user_roles_assign_nopublic_' . $gid]);
@@ -2992,7 +3031,7 @@ function og_user_roles_theme() {
   global $custom_theme;
   $group_node = NULL; // a node object containing the 'active' group for this request
 
-  $type = $_GET['type'];
+  $type = $_GET['type'];
 
   if (arg(0) == 'og' && is_numeric(arg(2))) {
     $group_node = og_set_theme(arg(2));
@@ -3044,10 +3083,29 @@ function og_user_roles_theme() {
  * $group is an object containing the group node
  */
 function og_user_roles_og_create_links($group) {
-  $links[] = l(t('Create content'), "node/add", array('title' => t('Add new content in this group.')), "gids[]=$group->nid");
-  return $links;
+  if (variable_get('og_user_roles_links_content_' . $group->nid, 0) == 1) {
+    $links[] = l(t('Create content'), "node/add", array('title' => t('Add new content in this group.')), "gids[]=$group->nid");
+    return $links;
+  }
+}
+
+/**
+ * hook_og_link_alter
+ *
+ * Modification as per: http://drupal.org/node/2517905
+ * Optionally remove create_ links.
+ */
+function og_user_roles_og_link_alter(&$links, $group_node) {
+  if (variable_get('og_user_roles_links_remove_' . $group_node->nid, 0) == 1) {
+    foreach($links as $key => $value) {
+      if(substr($key,0,7) == 'create_') {
+        unset($links["$key"]);
+      }
+    }
+  }
 }
 
+
 /**
  * hook_block
  *
@@ -3334,4 +3392,31 @@ function og_user_roles_db_rewrite_sql($q
   }
 }
 
+/**
+ * Implementation of hook_cron().
+ */
+function og_user_roles_cron() {
+
+  include_once(drupal_get_path('module', 'date_api') .'/date.inc');
+
+  // Insert nodes into all groups.
+  $nodelist = variable_get('og_user_roles_all_groups_nodes', '');
+
+  if (!empty($nodelist)) {
+    $nodelist_array = explode(',', $nodelist);
+    $result = db_query("SELECT nid FROM {og}");
+ 
+    while ($row = db_fetch_object($result)) {
+      foreach ($nodelist_array as $nodelist_item) {
+        $count = db_result(db_query("SELECT COUNT(nid) FROM {og_ancestry} WHERE nid = %d AND group_nid = %d", $nodelist_item, $row->nid));
+        // Insert if it doesn't already belong.
+        if ($count == 0) {
+          db_query('INSERT INTO {og_ancestry} (nid,group_nid,is_public) VALUES (%d,%d,0)', $nodelist_item, $row->nid);
+          watchdog('og_user_roles', t('Inserted nid: %nid into group: %gid.', array('%nid' => $nodelist_item, '%gid' => $row->nid)));
+	    }
+      } // end foreach
+    }
+  }
+
+}
 ?>
\ No newline at end of file
