--- og_user_roles.module.5.x-3.1.orig	2008-05-11 11:41:52.000000000 -0700
+++ og_user_roles.module	2008-05-21 03:14:34.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 3.1.1.2 2008/05/17 19:50:44 somebodysysop Exp $
 
 /**
  * @file
@@ -45,7 +45,7 @@ function og_user_roles_access($op, $node
 function og_user_roles_admin_settings() {
 
   // Get list of all og-enabled node types
-  $group_types = variable_get('og_node_types', array('og'));
+  $group_types = og_get_types('group');
   foreach ($group_types as $type) {
     $types[$type] = node_get_types('name', $type);
   }
@@ -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') ); 
   }
 
@@ -735,7 +734,7 @@ function theme_og_user_roles_page_form($
  */
 function og_user_roles_is_allowed($nid) {
   $node = node_load($nid);
-  if (in_array($node->type, variable_get('og_node_types', array('og'))) && variable_get("og_user_roles_roles_$node->type", NULL)) {
+  if (in_array($node->type, og_get_types('group')) && variable_get("og_user_roles_roles_$node->type", NULL)) {
     return TRUE;
   }
   else {
@@ -937,7 +936,7 @@ function og_user_roles_views_tables() {
  */
 function views_handler_filter_group() {
   $list = array();
-  $list = variable_get('og_node_types', array('og'));
+  $list = og_get_types('group');
 
 // Modification as per: http://drupal.org/node/184357
 //  $group_types = implode(',', $list);
@@ -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);
     }
   }
@@ -1419,7 +1449,9 @@ function og_user_roles_og($op, $nid, $ui
       }
 
       $user = user_load(array('uid' => $uid));
-      $node = node_load($nid);
+//      $node = node_load($nid);
+      // --- Modification - 2008-05-20 - as per: http://drupal.org/node/259956
+      $node = db_fetch_object(db_query("SELECT type, title FROM {node} WHERE nid=%d", $nid));
 
       // --- Modification - 2008-03-10 - as per: http://drupal.org/node/229715
       // Places all new group subscribers into default role per group type.
@@ -1499,32 +1531,13 @@ function og_user_roles_node_info() {
  * Implementation of hook_nodeapi().
  */
 function og_user_roles_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
+  global $user;
+
   switch ($op) {
     case 'access':
       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':
@@ -1573,63 +1586,124 @@ function og_user_roles_nodeapi(&$node, $
       break;
 
     case 'validate':
+      // Check to see if this user can post into other groups
+      $uid = $user->uid;
+      if ($node->type) {
+        $type = $node->type;
+      } elseif (arg(1) == 'add') {
+        $type = arg(2);
+      } elseif (arg(1) == 'ognodeadd') {
+        $type = $_GET['type'];
+      }
+      // Check to see if this is a group post;
+      // Do we have group context?
+      if ($group_node = og_get_group_context()) {
+        // If this is a node that can be posted into groups
+        if (og_is_group_post_type($type)) {
+	      // Has this node been assigned to groups?
+          if ($node->og_groups) {
+		    foreach ($node->og_groups as $gid) {
+              if (arg(1) == 'add' || arg(1) == 'ognodeadd') {
+			    $access = FALSE;
+                // Get the module
+                $module = node_get_types('module', $type);
+				// Get all permissions for this module
+                $perms = module_invoke($module, 'perm');
+				// Look for the 'create' permission
+				foreach ($perms as $string) {
+				  // If this is a create permission
+				  if (preg_match('/create/', $string)) {
+				    // See if the user has this permission in this group
+                    if (og_user_roles_user_access($string, $gid, $uid) === TRUE) {
+		              $access = TRUE;
+                    }
+				  }
+				}
+                if ($access === FALSE) {
+                  $temp_node = node_load($gid);
+				  $group_name = $temp_node->title;
+                  form_set_error('og_groups', t('You do not have permission to post this content type (' . $type . ') into Group: <strong>' . $group_name . '</strong> (' . $gid . ')'));
+                }
+              }
+              if (arg(2) == 'edit' || arg(2) == 'delete') {
+			    $access = FALSE;
+                $module = node_get_types('module', $type);
+                $perms = module_invoke($module, 'perm');
+				foreach ($perms as $string) {
+				  // If this is an edit string
+				  if (preg_match('/edit/', $string)) {
+                    if (og_user_roles_user_access($string, $gid, $uid) === TRUE) {
+		              $access = TRUE;
+                    }
+				  }
+				}
+                if ($access === FALSE) {
+                  $temp_node = node_load($gid);
+				  $group_name = $temp_node->title;
+                  form_set_error('og_groups', t('You do not have permission to edit or delete this content type (' . $type . ') in Group: <strong>' . $group_name . '</strong> (' . $gid . ')'));
+                }
+              }
+            }
+          }
+        }
+      }
       // Check to see if this is a group post, and if so, if public posts are allowed;
-        // Do we have group context?
-		if ($group_node = og_get_group_context()) {
-		  // Get the group node id
-		  $gid = $group_node->nid;
-
-		  // If this is a group node
-		  if (in_array($node->type, variable_get('og_node_types', array('og')))) {
-		  }
-
-		  // Check the nopublic variable for this group
-          if (variable_get('og_user_roles_assign_nopublic_' . $gid, 0) == 1) { 
-		    // Check public status of this post
-		    if ($node->og_public) { 
-			  form_set_error('og_public', t('No public posts allowed in this group.  You may NOT check the <strong>Public</strong> box on this post.'));
-			}
-			// Check forum status of this post
-			if (module_exists('og_forum')) {
-			  if ($node->type == 'forum') {
-                // Get the forum vocabulary ID
-                $vid = _forum_get_vid();
-                $ok = FALSE;
-				// Get all forums for this group
-				  $sql = "SELECT tid FROM {og_term} WHERE nid = %d";
-                  $forums = array();
-                  $result = db_query($sql, $gid);
-                  while ($nid = db_fetch_array($result)){
-                    $forums[] = $nid['tid'];
-                  }
-                // Get the forum containers only for this group
-				$containers = variable_get('forum_containers', array());
-				$my_containers = array_intersect($containers, $forums);
-				// Get the list of names for forum containers for this group
-				$names = "";
-                foreach ($my_containers as $item) {
-                  $temp = taxonomy_get_term($item);
-                  $names .= $temp->name . "  ";
-                }
-                // Get the vocabulary terms for this node
-                foreach ($node->taxonomy as $term) {
-                  // Check to make sure this term is in the forum vocabulary
-                  if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vid))) {
-                    // Make sure this term is in the same group
-                    if ($gid == og_user_roles_gid_from_tid($term)) {
-                      $tid = $term;
-        			  $ok = TRUE;
-                    }
+      // Do we have group context?
+	  if ($group_node = og_get_group_context()) {
+	   // Get the group node id
+	   $gid = $group_node->nid;
+
+	   // If this is a group node
+	   if (in_array($node->type, og_get_types('group'))) {
+	   }
+
+	   // Check the nopublic variable for this group
+        if (variable_get('og_user_roles_assign_nopublic_' . $gid, 0) == 1) { 
+	      // Check public status of this post
+	      if ($node->og_public) { 
+	        form_set_error('og_public', t('No public posts allowed in this group.  You may NOT check the <strong>Public</strong> box on this post.'));
+	      }
+		  // Check forum status of this post
+		  if (module_exists('og_forum')) {
+		    if ($node->type == 'forum') {
+              // Get the forum vocabulary ID
+              $vid = _forum_get_vid();
+              $ok = FALSE;
+   			  // Get all forums for this group
+     		  $sql = "SELECT tid FROM {og_term} WHERE nid = %d";
+              $forums = array();
+              $result = db_query($sql, $gid);
+              while ($nid = db_fetch_array($result)){
+                $forums[] = $nid['tid'];
+              }
+              // Get the forum containers only for this group
+		   	  $containers = variable_get('forum_containers', array());
+			  $my_containers = array_intersect($containers, $forums);
+			  // Get the list of names for forum containers for this group
+			  $names = "";
+              foreach ($my_containers as $item) {
+                $temp = taxonomy_get_term($item);
+                $names .= $temp->name . "  ";
+              }
+              // Get the vocabulary terms for this node
+              foreach ($node->taxonomy as $term) {
+                // Check to make sure this term is in the forum vocabulary
+                if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vid))) {
+                  // Make sure this term is in the same group
+                  if ($gid == og_user_roles_gid_from_tid($term)) {
+                    $tid = $term;
+        		    $ok = TRUE;
                   }
                 }
-			    // Check the gid of this tid against this gid
-			    if (!$ok) {
-                  form_set_error('tid', t('You have selected the wrong <strong>Forum</strong> for this post.  You may only select a forum from the <strong>Forums</strong> pull-down menu that is in the same group as this post.  Please try again. Group ('.$gid.') Term ('.$term.') Suggested container(s): %names', array('%names' => $names)));
-				}
+              }
+			  // Check the gid of this tid against this gid
+			  if (!$ok) {
+                form_set_error('tid', t('You have selected the wrong <strong>Forum</strong> for this post.  You may only select a forum from the <strong>Forums</strong> pull-down menu that is in the same group as this post.  Please try again. Group ('.$gid.') Term ('.$term.') Suggested container(s): %names', array('%names' => $names)));
 			  }
-			}
+		    }
 		  }
 		}
+	  }
       break;
 
     case 'delete':
@@ -1644,6 +1718,47 @@ function og_user_roles_nodeapi(&$node, $
   } // end switch
 } // end og_user_roles_nodeapi function
 
+/**
+ * Use this function to determine whether a user has a given privilege
+ * in a particular group.
+ *
+ * @param $string
+ *   The permission, such as "administer nodes", being checked for.
+ * @param $gid
+ *   The group id of the OG group.
+ * @param $uid
+ *   The uid of the user.
+ *
+ * @return
+ *   boolean TRUE if the current user has the requested permission.
+ */
+function og_user_roles_user_access($string, $gid, $uid) {
+  $roles = array();
+  $perms = "";
+  if ($uid) {
+    $roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
+  }
+  else {
+    $roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
+  }
+  // Get the normal roles
+  $result = db_query('SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d', $uid);
+  while ($role = db_fetch_object($result)) {
+    $roles[$role->rid] = $role->name;
+  }
+  // Get the group roles
+  $result = db_query('SELECT r.rid, r.name FROM {role} r INNER JOIN {og_users_roles} ogr ON r.rid = ogr.rid INNER JOIN {og_uid} ogu ON ogu.uid = ogr.uid AND ogu.nid = ogr.gid WHERE ogr.uid = %d AND ogr.gid = %d AND ogu.is_active = 1', $uid, $gid);
+  while ($role = db_fetch_object($result)) {
+    $roles[$role->rid] = $role->name;
+  }
+  $rids = array_keys($roles);
+  $placeholders = implode(',', array_fill(0, count($rids), '%d'));
+  $result = db_query("SELECT DISTINCT(p.perm) FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid WHERE r.rid IN ($placeholders)", $rids);
+  while ($row = db_fetch_object($result)) {
+    $perms .= "$row->perm, ";
+  }
+  return strpos($perms, "$string, ") !== FALSE;
+}
 
 /**
  * Returns all groups so long as this is not a group itself.
@@ -2344,7 +2459,7 @@ function og_user_roles_ognodeadd() {
      * If no (valid) node type has been provided, display a node type overview.
 	 * http://drupal.org/node/203875
 	 */
-    $exempt = array_merge(variable_get('og_node_types', array('og')), variable_get('og_omitted', array()));
+    $exempt = array_merge(og_get_types('group'), variable_get('og_omitted', array()));
     foreach (node_get_types() as $type) {
       // we used to check for node_access(create) but then node admins would get a false positive and see node types that they could not create
       if (!in_array($type->type, $exempt) && module_invoke(node_get_types('module', $type), 'access', 'create', $type)) {
@@ -2566,13 +2681,16 @@ function og_user_roles_oglogo($uid) {
     $logo = variable_get('og_user_roles_logo_' . $gid, '');
   }
 
+  if (empty($logo))  {
+     $location .= "C";
+     $logo = theme_get_setting('logo_path');
+  }
+
   // Write the test data: http://drupal.org/node/164038
   if (variable_get('og_user_roles_test_default', 0) == 1) {
     og_user_roles_write_test($uid, $location, $gid02, $gid, $uri_request_id, $d, $ref);
   }
 
-  if (empty($logo)) $logo = theme_get_setting('logo_path');
-
   // Return image information
   $info = image_get_info($logo);
 
@@ -2733,6 +2851,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
@@ -2741,7 +2860,7 @@ function og_user_roles_form_alter($form_
     $gid = arg(1);
 
     // First, we need to get a list of all og-enabled node types
-    $group_types = variable_get('og_node_types', array('og'));
+    $group_types = og_get_types('group');
     foreach ($group_types as $type) {
       // Get list of assignable group roles for this group type
       $role_ids = variable_get("og_user_roles_roles_{$type}", array());
@@ -2782,7 +2901,7 @@ function og_user_roles_form_alter($form_
     $gid = arg(1);
 
     // First, we need to get a list of all og-enabled node types
-    $group_types = variable_get('og_node_types', array('og'));
+    $group_types = og_get_types('group');
     foreach ($group_types as $type) {
       // Get list of assignable group roles for this group type
       $role_ids = variable_get("og_user_roles_roles_{$type}", array());
@@ -2813,6 +2932,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 +3003,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 +3063,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 +3140,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));
@@ -3033,6 +3181,11 @@ function og_user_roles_theme() {
       $group_node = og_set_theme(arg(2));
     }
   }
+  elseif (arg(0) == 'user' && arg(1) == 'register' && isset($_REQUEST['gids'])) {
+    $gid = intval(current($_REQUEST['gids']));
+    $group_node = node_load($gid);
+    $custom_theme = $group_node->og_theme;
+  }
 
   og_set_group_context($group_node);
 }
@@ -3044,11 +3197,30 @@ 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 +3506,29 @@ function og_user_roles_db_rewrite_sql($q
   }
 }
 
+/**
+ * Implementation of hook_cron().
+ */
+function og_user_roles_cron() {
+
+  // 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
