'. t('Allows group administrators to add members into Drupal system roles which are restricted to the group.') .'
'; } } /** * Implementation of hook_perm(). * * Modification as per: http://drupal.org/node/192747 * Added 'administer og_user_roles' * */ function og_user_roles_perm() { return array('administer og_user_roles', 'configure member roles', 'create og_subgroups', 'manage registration codes', 'use registration codes', 'no oguseredit'); } /** * Implementation of hook_access(). */ function og_user_roles_access($op, $node) { global $user; if ($op == 'create') { return user_access('create og_subgroups'); } } /** * Implementation of hook_settings(). */ function og_user_roles_admin_settings() { // Get list of all og-enabled node types $group_types = og_get_types('group'); foreach ($group_types as $type) { $types[$type] = node_get_types('name', $type); } // Get list of roles, not counting authenticated and anonymous user $all_roles = user_roles(); foreach ($all_roles as $rid => $role) { if ($rid > 2) { $roles[$rid] = $role; } } // If no assignable roles, advise user to add some. // Modification as per: http://drupal.org/node/167373 if (!count($roles)) { $form['og_user_roles'] = array( '#value' => ''. t('No assignable roles were found. Please create at least one role under !roles.', array('!roles' => l(t('Administer >> User management >> Roles'), 'admin/user/roles'))) .'
', ); return $form; } // Create a fieldset for each group type containing role selections foreach ($types as $type => $name) { $form["og_user_roles_$type"] = array( '#type' => 'fieldset', '#title' => t('%type_name role options', array('%type_name' => $name)), ); $form["og_user_roles_$type"]["og_user_roles_roles_$type"] = array( '#type' => 'checkboxes', '#title' => t('Assignable roles'), '#options' => $roles, '#default_value' => variable_get("og_user_roles_roles_$type", array()), ); $form["og_user_roles_$type"]["og_user_roles_assign_typegrouprole_$type"] = array( '#type' => 'checkbox', '#title' => t('Set default basic group (group limited) role for users who join groups of this type: %type_name?', array('%type_name' => $name)), '#default_value' => variable_get("og_user_roles_assign_typegrouprole_$type", 0), '#description' => t('Do you wish to automatically assign a specific "basic group role" to every new group subscriber at the time he subscribes to a group of this type: %type_name? The role is limited to the group that he is subscribed to. This role assignment can be removed by the groups\' admins', array('%type_name' => $name)), ); $form["og_user_roles_$type"]["og_user_roles_typegrouprole_value_$type"] = array( '#type' => 'select', '#title' => t('Role to use as a group type %type_name role', array('%type_name' => $name)), '#options' => $roles, '#default_value' => variable_get("og_user_roles_typegrouprole_value_$type", 0), '#description' => t('Select the role you wish to use as the "group role" for every new groupmember to groups of this type: %type_name.', array('%type_name' => $name)), ); } $form['og_user_roles_regcode_gid_default'] = array( '#type' => 'fieldset', '#title' => t('Allow Group Admins to define Registration Codes for new group subscribers to moderated groups.'), '#description' => t('Allows you to allow Group Administrators to define Registration Codes to allow users to subscribe to moderated groups without administrator approval. Your Group Admins must have the manage registration codes permission to use this setting.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_regcode_gid_default']['og_user_roles_assign_regcode_gid'] = array( '#type' => 'checkbox', '#title' => t('Allow Group Admins to set Registration Codes for users to join their moderated groups without admin approval?'), '#default_value' => variable_get('og_user_roles_assign_regcode_gid', 0), '#description' => t('Do you wish to allow Group Admins to define "Registration Codes" to allow users to subscribe to their groups without admin approval? The Group Admin will be able to define the Registration Codes on edit (not creation) of the group node. Group Admin must have manage registration codes permission.'), ); $form['og_user_roles_approval'] = array( '#type' => 'fieldset', '#title' => t('Allow group admins to approve new signups.'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#description' => t('If user registration to your site requires administrator approval and you allow users to subscribe to groups at registration, then you can optionally allow the administrator(s) of the group(s) to which the user is subscribing to approve the signup request. This will require that the administrator of each group that you wish to give this privilege have a role which includes the administer users permission. (Note that this feature requires ' . l("mimemail.module", "http://www.drupal.org/project/mimemail") . ' to be installed)' ), ); $form['og_user_roles_approval']['og_user_roles_approval_default'] = array( '#type' => 'checkbox', '#title' => t('Allow group admins to approve new signups?'), '#default_value' => variable_get('og_user_roles_approval_default', 0), '#description' => t('If your site requires administrator approval for signups: When a user registers and elects to subscribe to a group (group is on registration form), then the admin of the group will receive an email notification of the pending signup and be allowed to approve it. Note that this requires that the group admin have a group role which includes the administer users permission.'), ); $form['og_user_roles_default'] = array( '#type' => 'fieldset', '#title' => t('Default Non-Group Role for new users.'), '#description' => t('Allows you to select a role to automatically assign to all new signups to your site.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_default']['og_user_roles_assign_default'] = array( '#type' => 'checkbox', '#title' => t('Set default global (site-wide) role for new signups?'), '#default_value' => variable_get('og_user_roles_assign_default', 0), '#description' => t('Do you wish to automatically assign all new signups to your web site to a specific role (that is NOT group specific) upon registration?'), ); $form['og_user_roles_default']['og_user_roles_default_value'] = array( '#type' => 'select', '#title' => t('Selectable roles'), '#options' => $roles, '#default_value' => variable_get('og_user_roles_default_value', 0), ); $form['og_user_roles_basicgrouprole'] = array( '#type' => 'fieldset', '#title' => t('Default Basic Group Role for new group subscribers.'), '#description' => t('Allows you to select a group role to automatically assign to users who join a group on your site. The role is specific to the group(s) to which the user is subscribing. That is, the user will only have the privileges of the role in the group he is subscribed to.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_basicgrouprole']['og_user_roles_assign_basicgrouprole'] = array( '#type' => 'checkbox', '#title' => t('Set default basic group (group limited) role for users who join groups?'), '#default_value' => variable_get('og_user_roles_assign_basicgrouprole', 0), '#description' => t('Do you wish to automatically assign a specific "basic group role" to every new group subscriber at the time he subscribes to the group? The role is limited to the group that he is subscribed to. This role assignment can be removed by the groups\' admins'), ); $form['og_user_roles_basicgrouprole']['og_user_roles_basicgrouprole_value'] = array( '#type' => 'select', '#title' => t('Role to use as a basic group role'), '#options' => $roles, '#default_value' => variable_get('og_user_roles_basicgrouprole_value', 0), '#description' => t('Select the role you wish to use as the "basic group role" for every new groupmember.'), ); $form['og_user_roles_basicgrouprole_gid_default'] = array( '#type' => 'fieldset', '#title' => t('Allow Group Admins to define Default Basic Group Role for new group subscribers.'), '#description' => t('Allows you to allow Group Administrators to define a default group role to automatically assign to users who join their groups. The role is specific to the group to which the user is subscribing. That is, the user will only have the privileges of the role in the group he is subscribed to. Your Group Admins must have the configure member roles permission to use this setting.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_basicgrouprole_gid_default']['og_user_roles_assign_basicgrouprole_gid'] = array( '#type' => 'checkbox', '#title' => t('Allow Group Admins to set default basic group (group limited) role for users who join their groups?'), '#default_value' => variable_get('og_user_roles_assign_basicgrouprole_gid', 0), '#description' => t('Do you wish to allow Group Admins to define a specific "basic group role" for every new subscriber to their group at the time he subscribes to the group? The role is limited to the group that he is subscribed to. This role assignment can be be removed by the groups\' admin(s). The Group Admin will be able to define the default group role on edit (not creation) of the group node. Group Admin must have configure member roles permission.'), ); $form['og_user_roles_founder'] = array( '#type' => 'fieldset', '#title' => t('Default Founder Role for users who create groups.'), '#description' => t('Allows you to select a group role to automatically assign to users who create groups on your site. The role is specific to the group(s) this user creates. That is, the user will only have the privileges of the role in the group that he creates.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_founder']['og_user_roles_assign_founder'] = array( '#type' => 'checkbox', '#title' => t('Set default group founder (group limited) role for users who create groups?'), '#default_value' => variable_get('og_user_roles_assign_founder', 0), '#description' => t('Do you wish to automatically assign a user who creates a group (e.g. "founder") to a group specific role that is limited to the group that he creates?'), ); $form['og_user_roles_founder']['og_user_roles_founder_value'] = array( '#type' => 'select', '#title' => t('Role to use as founder role'), '#options' => $roles, '#default_value' => variable_get('og_user_roles_founder_value', 0), '#description' => t('Select the role you wish to use as the "founder" role.'), ); $form['og_user_roles_admingrouprole'] = array( '#type' => 'fieldset', '#title' => t('Default Group Role for new group administrator.'), '#description' => t('Allows you to select a group role to automatically assign to users who are elevated to group administrator. The role is specific to the group(s) in which the user is a group administrator. That is, the user will only have the privileges of the role in the group he is the administrator for.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_admingrouprole']['og_user_roles_assign_admingrouprole'] = array( '#type' => 'checkbox', '#title' => t('Set default group (group limited) administrator role for users who are elevated to group administrator?'), '#default_value' => variable_get('og_user_roles_assign_admingrouprole', 0), '#description' => t('Do you wish to automatically assign a specific "administrator group role" to every new group administrator at the time he is elevated to group administrator status? The role is limited to the group that he is the administrator for. This role assignment can only be removed by removing the user as a group administrator.'), ); $form['og_user_roles_admingrouprole']['og_user_roles_admingrouprole_value'] = array( '#type' => 'select', '#title' => t('Role to use as group administrator role'), '#options' => $roles, '#default_value' => variable_get('og_user_roles_admingrouprole_value', 0), '#description' => t('Select the role you wish to use as the "group administrator role" for every new group administrator.'), ); $form['og_user_roles_notify'] = array( '#type' => 'fieldset', '#title' => t('Default Group Admin Notification for new subscribers.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_notify']['og_user_roles_notify_default'] = array( '#type' => 'checkbox', '#title' => t('Send email notification to group admin when new subscriber is added to group?'), '#default_value' => variable_get('og_user_roles_notify_default', 0), '#description' => t('Do you wish to automatically send an email notification to the group administrator when a new subscriber is added to a group? (Note that this feature requires ' . l("mimemail.module", "http://www.drupal.org/project/mimemail") . ' to be installed)'), ); $form['og_user_roles_all_groups'] = array( '#type' => 'fieldset', '#title' => t('Nodes to automatically place into all available groups.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_all_groups']['og_user_roles_all_groups_nodes'] = array( '#type' => 'textarea', '#title' => t('Nodes to automatically place into all available groups'), '#default_value' => variable_get('og_user_roles_all_groups_nodes', ''), '#cols' => 50, '#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:63,20
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( '#type' => 'fieldset', '#title' => t('TAC / OG Access Control Integration.'), ); // Check to see if taxonomy_access is installed. $taxonomy_access = "not installed"; $disabled = 1; if (module_exists("taxonomy_access")) { $disabled = 0; $taxonomy_access = 'installed'; } $form['og_user_roles_tac_og_default']['og_user_roles_tac_og_value'] = array( '#type' => 'checkbox', '#title' => t('Integrate TAC and OG Access Control?'), '#default_value' => variable_get('og_user_roles_tac_og_value', 0), '#description' => t('Do you have Taxonomy Access installed and do you wish to make its access control work with Organic Groups? Note that this feature requires that you install the ' . l("Taxonomy Access Control Module", "http://www.drupal.org/project/taxonomy_access") . '. The taxonomy_access.module is currently ' . $taxonomy_access . '. Details here: ' . l("How to Make OG and TAC Work Together: Step 2", "http://groups.drupal.org/node/3700")), '#disabled' => $disabled, ); if (module_exists('og_subgroups')) { $form['og_user_roles_subgroup'] = array( '#type' => 'fieldset', '#title' => t('Create Subgroups.'), '#description' => t('Allows you to select a group type to use for displaying a "Create Subgroup" link on your group menus. Users will need to have the "create og_subgroups" permission in order to access this link on their group menus. Note that this functionality requires the og_subgroups.module'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_subgroup']['og_user_roles_create_subgroup'] = array( '#type' => 'checkbox', '#title' => t('Create link to "Create Subgroup" in group menu?'), '#default_value' => variable_get('og_user_roles_create_subgroup', 0), '#description' => t('Do you wish to have a "Create Subgroup" link appear on the group menu (if the user has the appropriate permissions)?'), ); $form['og_user_roles_subgroup']['og_user_roles_create_subgroup_value'] = array( '#type' => 'select', '#title' => t('Group type to use for subgroup creation'), '#options' => $types, '#default_value' => variable_get('og_user_roles_create_subgroup_value', ''), '#description' => t('Select the group type you wish to use for subgroup creation.'), ); } $form['og_user_roles_clear_cache'] = array( '#type' => 'fieldset', '#title' => t('Clear the cache?'), '#description' => t('We found that there are some modules which will return "Access denied" message even when OG User Roles returns permissions which allow the user access. This is due to cached permissions being used instead of the group permissions. This settiing allows you to reset these permissions for a user on an ongoing basis if you are having this problem.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $description = 'Do you wish to automatically clear the cache_menu item and reset the user_access() cached permissions for this user? See ' . l("OG User Roles and File Upload", "http://drupal.org/node/166566") . ' and ' . l("OG User Roles: Clearing the Cache", "http://drupal.org/node/177948") . ' for details. Please note that this setting creates a heavy overload as permissions must be re-created on each page load. Use this setting only if absolutely necessary.'; $form['og_user_roles_clear_cache']['og_user_roles_clear_cache_value'] = array( '#type' => 'checkbox', '#title' => t('Clear the user_access() and cache_menu caches for user?'), '#default_value' => variable_get('og_user_roles_clear_cache_value', 0), '#description' => t($description), ); $form['og_user_roles_test'] = array( '#type' => 'fieldset', '#title' => t('Test/Debug.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_test']['og_user_roles_test_default'] = array( '#type' => 'checkbox', '#title' => t('Output debug data to og_user_test table?'), '#default_value' => variable_get('og_user_roles_test_default', 0), '#description' => t('Every time user_access() is called, it will call og_user_all_roles() which adds group roles to $user->roles. Do you wish to see the output from this function? (Note that this feature is for testing/debug purposes, and could create a very large output file. This feature also requires that the table og_user_test already exist. Details here: ' . l("OG User Roles: Test", "http://drupal.org/node/164038")), ); $form['og_user_roles_menu_options'] = array( '#type' => 'fieldset', '#title' => t('Group Menu Options.'), '#description' => t('Give group admins control of the display of OGUR and default OG create links in the groups menu.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_menu_options']['og_user_roles_menu_options_enabled'] = array( '#type' => 'checkbox', '#title' => t('Enable menu control?'), '#default_value' => variable_get('og_user_roles_menu_options_enabled', 1), '#description' => t("This will add two form elements, inside a field set, to the OG node's edit page, enabling this feature."), ); $form['og_user_roles_logo'] = array( '#type' => 'fieldset', '#title' => t('Group Logo Attributes.'), '#description' => t('Group admins can upload a logo for their group. Here you enter the maximimum width and height attributes for Group Logos.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['og_user_roles_logo']['og_user_roles_logo_enabled'] = array( '#type' => 'checkbox', '#title' => t('Enable group logo?'), '#default_value' => variable_get('og_user_roles_logo_enabled', 1), '#description' => t("This will add a text box onto the OG node's edit page, enabling this feature."), ); $form['og_user_roles_logo']['og_user_roles_logo_width'] = array( '#type' => 'textfield', '#size' => 10, '#title' => t('Maximum width for display of group logo?'), '#default_value' => variable_get('og_user_roles_logo_width', '135'), '#description' => t('Enter the maximim pixel (px) width at which group logos will be displayed'), ); $form['og_user_roles_logo']['og_user_roles_logo_height'] = array( '#type' => 'textfield', '#size' => 10, '#title' => t('Maximimum height for display of group logo?'), '#default_value' => variable_get('og_user_roles_logo_height', '113'), '#description' => t('Enter the maximum pixel (px) height at which group logos will be displayed'), ); $form['og_user_roles_counter'] = array( '#type' => 'textfield', '#size' => 10, '#title' => t('Counter for og_users_roles table'), '#default_value' => variable_get('og_user_roles_counter', 0), '#description' => t('This is the counter for the og_users_roles table. You do not need to set this. It will update itself. It should NOT be set lower than the number of records currently in the table.'), ); return system_settings_form($form); } /** * Generate multinode access UI form. */ function og_user_roles_multinode($theme = NULL) { global $theme_key, $custom_theme, $user; $uid = $user->uid; // Get whatever is stored in multinode table. $existing = array(); $result = db_query("SELECT * FROM {multinode_access}"); while ($row = db_fetch_object($result)) { $existing[$row->realm] = $row; } // Get all rule realms from node_access table. $result = db_query("SELECT DISTINCT realm FROM {node_access}"); while ($row = db_fetch_object($result)) { $rules[] = $row->realm; } // Get all rule realms from modules. User doing this should have grants in all modules affected. $op = 'view'; $mrules = node_access_grants($op, $uid); foreach ($mrules as $realm => $grants) { $rules[] = $realm; } // Get only unique values $rules = array_unique($rules); // Sort the rule realms asort($rules); $form['#tree'] = TRUE; foreach ($rules as $realm) { $form[$realm]['checkbox'] = array('#type' => 'checkbox', '#default_value' => (isset($existing[$realm]->realm) ? 1 : 0)); $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 : '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') ); } $form['submit'] = array('#type' => 'submit', '#value' => t('Save changes')); return $form; } /** * Theme function to render the table for the og_user_roles_multinode * Multinode access UI */ function theme_og_user_roles_multinode($form) { $output .= "\n
'. t('@items in moderation', array('@items' => format_plural(0, '1 post', '@count posts'))). '
'; } return $output; } /** * Form for user roles page. */ function og_user_roles_page_form($gid, $roles, $result) { $form['user_roles'] = array('#tree' => TRUE); // Make sure form array isn't flattened while ($account = db_fetch_object($result)) { $form['user_roles']['users'][$account->uid] = array( '#type' => 'value', '#value' => $account->uid ); $title = theme('username', $account) .' ('. l(t('unsubscribe'), "og/unsubscribe/$gid/$account->uid", array(), "destination=og/users/$gid") .')'; $form['user_roles']['roles'][$account->uid] = array( '#type' => 'checkbox_columns', '#title' => $title, '#default_value' => _og_user_roles_get_roles($account->uid, $roles), '#columns' => 4, '#options' => $roles, '#suffix' => 'You can click " . l("here", "$view_user/edit", array(), "gids[]=$gid", NULL, TRUE) . " to Activate, Block or Delete the user. (Don't forget to first log into group for which you are admin.)";
$result = db_query("SELECT uid FROM {og_uid} WHERE is_admin = 1 AND nid = %d", $gid);
while ($obj = db_fetch_object($result)) {
$recipient_uid = $obj->uid;
$recipient_user = user_load(array('uid' => $recipient_uid));
$recipient = $recipient_user->mail;
if (module_exists('mimemail')) mimemail($sender, $recipient, $subject, $message);
} // end while
}
}
}
// If site admin opted to create a default role
if (variable_get('og_user_roles_assign_default', 0) == 1) {
// Check to see if this user doesn't already have this role;
// If not, then assign it.
$rid = variable_get('og_user_roles_default_value', 0);
$sql = "SELECT COUNT(*) FROM {users_roles} WHERE uid = %d AND rid = %d";
$result = db_query($sql, $user->uid, $rid);
$output = (db_result($result));
if ($output == 0) {
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $user->uid, $rid);
}
}
}
if ($op == 'validate') {
// Registration Code
if (isset($edit['og_user_roles_regcode'])) { //only present during registration
if (variable_get('og_user_roles_assign_regcode_gid', 0) == 1 && $edit['og_user_roles_regcode'] != '') {
$gid = og_user_roles_gid_from_regcode($edit['og_user_roles_regcode']);
if ($gid == 0) {
form_set_error('og_user_roles_regcode', "Not a valid group registration code. If you do not have a registration code, please make sure the Group registration code box is empty.");
}
}
}
}
// Add the group roles to $user->roles if this is a group
// This should only be effective until the next global $user call
if ($op == 'load') {
$roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
$user->roles = $roles;
} // end $op load
// Remove user info from og_users_roles table
if ($op == 'delete') {
db_query("DELETE FROM {og_users_roles} WHERE uid = %d", $user->uid); // delete all records for this user
} // end $op delete
}
/**
* implementation of hook_og();
*
* @param $op string, 'user insert', 'user update', 'user delete'
* @param $nid node ID of the group
* @param $uid the user ID
* @param $args associative array containing details about the subscription
*/
function og_user_roles_og($op, $nid, $uid, $args = array()) {
switch ($op) {
case 'user insert':
global $base_url;
// --- Modification - 2007-06-18 - as per: http://drupal.org/node/152442
// Adding every user to a basic group restricted role, that all users in the group should have.
// I don't know a better way, so I'll do it like this. This is based on the creating of a founder role,
// just that every user in the group should automagically get this role.
// -Bibo
if (variable_get('og_user_roles_assign_basicgrouprole', 0) == 1) {
$rid = variable_get('og_user_roles_basicgrouprole_value', 0);
og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
}
// --- Modification - 2007-09-20 - as per: http://drupal.org/node/177016
// Places all new group subscribers into default role.
// Same as above, except these settings are set by Group Admin in the
// group node. So, the variables are set by group.
if (variable_get('og_user_roles_assign_basicgrouprole_' . $nid, 0)) { // check to see if this variable exists
if (variable_get('og_user_roles_assign_basicgrouprole_gid', 0) == 1 && variable_get('og_user_roles_assign_basicgrouprole_' . $nid, 0) == 1 ) {
$rid = variable_get('og_user_roles_basicgrouprole_value_' . $nid, 0);
og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
}
}
// --- Modification - 2008-01-10 - as per: http://drupal.org/node/197489
// Places all new group administrators into default group administrator role.
$admin = $args['is_admin'];
if (variable_get('og_user_roles_assign_admingrouprole', 0) == 1 && $admin == 1) {
$rid = variable_get('og_user_roles_admingrouprole_value', 0);
og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
}
$user = user_load(array('uid' => $uid));
// --- 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.
$type = $node->type;
if (variable_get('og_user_roles_assign_typegrouprole_' . $type, 0)) {
if (variable_get('og_user_roles_assign_typegrouprole_' . $type, 0) == 1) {
$rid = variable_get('og_user_roles_typegrouprole_value_' . $type, 0);
og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
}
}
// Send notification of new og subscription to administrators of group;
if (variable_get('og_user_roles_notify_default', 0) == 1 && module_exists('mimemail')) {
$group = check_plain($node->title);
$sender = variable_get('site_mail', '');
$subject = t('User @user added to group: @group', array('@user' => $user->name, '@group' => $group));
if (variable_get('og_user_roles_approval_default', 0) == 1) {
$view_user = "oguseredit/$user->uid";
}else{
$view_user = "user/$user->uid";
}
// http://www.scbbs.com/node/135/219#comment-219
$message = "User " . l($user->name, $view_user, array(), "gids[]=$nid", NULL, TRUE) . " added to group: " . l($group, "node/$nid", array(), NULL, NULL, TRUE);
$result = db_query("SELECT uid FROM {og_uid} WHERE is_admin = 1 AND nid = %d", $nid);
while ($obj = db_fetch_object($result)) {
$recipient_uid = $obj->uid;
$recipient_user = user_load(array('uid' => $recipient_uid));
$recipient = $recipient_user->mail;
mimemail($sender, $recipient, $subject, $message);
// drupal_mail('og_user_roles_new_subscription', $recipient, $subject, $message, $sender);
} // end while
} // end if
break;
case 'user update':
// --- Modification - 2008-01-10 - as per: http://drupal.org/node/197489
// Places all new group administrators into default group administrator role.
$admin = $args['is_admin'];
if (variable_get('og_user_roles_assign_admingrouprole', 0) == 1 && $admin === 0) {
$rid = variable_get('og_user_roles_admingrouprole_value', 0);
og_user_roles_role_delete($uid, $rid, $nid); // delete this admin role for this user in this group (if it exists)
}
if (variable_get('og_user_roles_assign_admingrouprole', 0) == 1 && $admin === 1) {
$rid = variable_get('og_user_roles_admingrouprole_value', 0);
og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
}
break;
case 'user delete':
// Remove all roles for this user in this group if he is being unsubscribed;
og_user_roles_role_leave($uid, $nid);
break;
} // end switch
}
/**
* Implementation of hook_node_info().
*/
function og_user_roles_node_info() {
// If og_subgroups is installed AND og_user_roles_create_subgroup checkbox is checked;
if (module_exists('og_subgroups') && variable_get('og_user_roles_create_subgroup', 0) == 1) {
return array(
'og_user_roles_subgroup' => array(
'name' => t('Subgroup'),
'module' => 'og_user_roles',
'description' => t('Create a basic subgroup.'),
'help' => t('Create a basic subgroup.'),
'body_label' => t('Description'),
),
);
} // end if
}
/**
* Implementation of hook_nodeapi().
*/
function og_user_roles_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
global $user;
switch ($op) {
case 'access':
break;
case 'submit':
break;
case 'insert':
// If this is a group node, put the group creator into a group role upon creation;
if (variable_get('og_user_roles_assign_founder', 0) == 1) {
// If this is a group node
if (og_is_group_type($node->type)) {
// Get the requisite data
$uid = $node->uid; // user ID
$rid = variable_get('og_user_roles_founder_value', 0);
$gid = $node->nid; // group ID
og_user_roles_role_join($uid, $rid, $gid); // assign user to group role in that group
}
}
// Create a subgroup record for this group
// 'ognodeadd' is my node add callback, so I know if this node is a group and
// here, then we need to process it node as a subgroup
if ($node->type == variable_get('og_user_roles_create_subgroup_value', '') && arg(1) == 'ognodeadd') {
$gids = $_GET['gids'];
$gid = $gids[0];
$subgroup = $node->nid;
$sql = 'INSERT INTO {og_ancestry} (nid,group_nid,is_public) VALUES (%d,%d,0)';
db_query($sql, $subgroup, $gid);
if (module_exists('og_subgroups')) db_query('INSERT INTO {og_subgroups} (gid, parent) VALUES (%d, %d)', $subgroup, $gid);
}
break;
case 'update':
// Modification. 2007-09-24 as per: http://drupal.org/node/178257
// If this is a group node, put the group creator into a group role upon creation
// OR when the group node is updated. If user already is in that role in the group,
// the og_user_roles_role_join command will ignore the request.
if (variable_get('og_user_roles_assign_founder', 0) == 1) {
// If this is a group node
if (og_is_group_type($node->type)) {
// Get the requisite data
$uid = $node->uid; // user ID
$rid = variable_get('og_user_roles_founder_value', 0);
$gid = $node->nid; // group ID
og_user_roles_role_join($uid, $rid, $gid); // assign user to group role in that group
}
}
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)) {
// Does this node have groups?
if ($node->og_groups) {
foreach ($node->og_groups as $gid) {
if ((arg(1) == 'add' || arg(1) == 'ognodeadd') && $gid > 0) {
$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) || preg_match('/edit own blog/', $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: ' . $group_name . ' (' . $gid . ')'));
}
}
if ((arg(2) == 'edit' || arg(2) == 'delete') && $gid > 0) {
$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) || preg_match('/manage/', $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: ' . $group_name . ' (' . $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, 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 Public 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 Forum for this post. You may only select a forum from the Forums 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':
// Remove og_users_roles and variables related to group node if it is deleted;
// Also remove from og_subgroups if installed.
// If this is a group node
if (og_is_group_type($node->type)) {
og_user_roles_role_remove($node->nid);
og_user_roles_variable_remove($node->nid);
if (module_exists('og_subgroups')) db_query('DELETE FROM {og_subgroups} WHERE gid = %d', $node->nid);
}
break;
} // 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 = "";
// User #1 has all privileges:
if ($uid == 1) {
return TRUE;
}
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.
*/
function og_user_roles_get_groups($node) {
$groups = array();
if (!og_is_group_type($node->type)) {
$result = og_user_roles_get_groups_result();
while ($row = db_fetch_object($result)) {
$groups[$row->group_nid] = $row->title;
}
return $groups;
}
}
/**
* Just the query for the get_node_groups function.
*/
function og_user_roles_get_groups_result() {
$sql = "SELECT oga.group_nid, n.title FROM {og_ancestry} oga INNER JOIN {node} n ON oga.group_nid = n.nid";
$result = db_query(db_rewrite_sql($sql, 'oga', 'group_nid'), '');
return $result;
}
/**
* This function takes the global $user (or $account) value and returns a list
* of group and non-group roles for this user.
*/
function og_user_roles_all_roles($user) {
// This will be the process to get BOTH the group and non-group roles for a user
$uid = $user->uid;
$gid = 0;
$gids = array();
$nid = 0;
$location = 0;
$uri_request_id = request_uri();
$arg = explode('/', $uri_request_id);
$ogroles = array();
$x1 = 0;
// Need to use the referrer to get OG group in some cases;
$ref = $_SERVER["HTTP_REFERER"];
$ref_url = parse_url($ref);
$ref_path = $ref_url[path];
$ref_query = $ref_url[query];
$ref_arg = explode('/', $ref_path);
//
// This will by default add the anonymous user role (no need since we merge user->roles)
//
// We get the groupID
//
$group_node = og_get_group_context();
$gid02 = $group_node->nid;
$gid = $gid02;
if ($gid02 === NULL) {
$gid = 0;
}else{
if (isset($_SESSION['og_last'])) $_SESSION['og_last'] = $group_node;
$_SESSION['og_user_role'] = $gid;
}
//
// If the above doesn't get the groupID, then we start trying stuff
//
if ($gid == 0) {
// http://doadance.scbbs.com/drupal03/node/79
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
$location = 1;
$nid = (int)arg(1);
$gid = og_user_roles_getgid($nid, $uid);
}
// Translate pages using i18n translation module as per: http://drupal.org/node/203395
if (arg(0) == 'node' && arg(1) == 'add' && isset($_REQUEST['translation'])) {
$nid = intval($_REQUEST['translation']);
$gid = og_user_roles_getgid($nid, $uid);
if ($gid) {
// Troubleshooting
// drupal_set_message('recognized translation node add, gid = '.$gid);
$_GET['gids'] = array($gid); // inject gid
}
}
// http://doadance.scbbs.com/node/79/edit
// http://doadance.scbbs.com/node/79/outline
// http://doadance.scbbs.com/node/79/track
// http://doadance.scbbs.com/node/79/modr8
// http://doadance.scbbs.com/node/79/ogmodr8
// Modified 2007-07-05 for webform "results";
// Modified 2007-07-23 for content_access "access";
// Modified 2007-08-06 for modr8 "modr8" and "ogmodr8" (our own callback);
// Modified 2007-09-13 for "galleries";
// Modified 2007-09-14 for "email";
// Modified 2007-12-20 for "revisions": http://drupal.org/node/202196
// Modified 2008-01-10 for "workflow": http://drupal.org/node/208363
// Modified 2008-03-21 for "feedapi": http://drupal.org/node/237258
if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'outline' || arg(2) == 'track' || arg(2) == 'results' || arg(2) == 'access' || arg(2) == 'modr8' || arg(2) == 'ogmodr8' || arg(2) == 'delete' || arg(2) == 'galleries' || arg(2) == 'email' || arg(2) == 'revisions' || arg(2) == 'signups' || arg(2) == 'agenda' || arg(2) == 'workflow' || arg(2) == 'votes' || arg(2) == 'refresh' || arg(2) == 'purge' || arg(2) == 'modify')) {
$location = 2;
$nid = (int)arg(1);
$gid = og_user_roles_getgid($nid, $uid);
}
// poll.module
if (arg(0) == 'poll' && is_numeric(arg(2)) && (arg(1) == 'cancel')) {
$location = 20;
$nid = (int)arg(2);
$gid = og_user_roles_getgid($nid, $uid);
}
// Edit users (og_user_roles_approval)
//
// This will get a group context if the current user and the
// user he is trying to access both belong to at least the same
// group that is also the last group set by OG in "og_last" session
// (which logically should be the current group);
//
// http://doadance.scbbs.com/oguseredit/72?gids[]=47
// http://doadance.scbbs.com/oguseredit/72/delete?gids[]=47
// http://doadance.scbbs.com/oguseredit/72/edit?gids[]=47
if (arg(0) == 'oguseredit' && is_numeric(arg(1)) && (is_null(arg(2)) || arg(2) == 'edit' || arg(2) == 'delete') ) {
if (isset($_REQUEST['gids'])) {
$location = 200;
$gids = $_GET['gids'];
$gid = intval(current($_REQUEST['gids']));
}
}
// print.module (printer friendly)
//
// http://www.mysite.com/print/14
if (arg(0) == 'print' && is_numeric(arg(1))) {
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
cache_clear_all();
$location = 201;
$nid = (int)arg(1);
$gid = og_user_roles_getgid($nid, $uid);
// Set group context.
$group_node = node_load($gid);
og_set_group_context($group_node);
}
// oglogo (printer friendly)
//
// http://www.mysite.com/oglogo
if (arg(0) == 'oglogo') {
if (isset($_SESSION['og_last'])) {
$location = 202;
$gid = $_SESSION['og_last'];
// Set group context.
$group_node = node_load($gid);
og_set_group_context($group_node);
}
}
// Manage users (og_user_roles_approval)
//
// This will get a group context from og_last session
// and list all users in the group if the user listing
// is admin for the group and has the 'administer users' permission.
//
// http://doadance.scbbs.com/ogusermanage/47
if (arg(0) == 'ogusermanage' AND is_numeric(arg(1))) {
$gid = (int)arg(1);
}
// 0 1 2
// http://www.mysite.com/comment/edit/14
if (arg(0) == 'comment' && is_numeric(arg(2)) && arg(1) == 'edit') {
$location = 3;
$comment = _comment_load(arg(2));
$nid = $comment->nid;
$gid = og_user_roles_getgid($nid, $uid);
// Set group context.
$group_node = node_load($gid);
og_set_group_context($group_node);
}
// 0 1 2
// http://www.mysite.com/comment/delete/14
if (arg(0) == 'comment' && is_numeric(arg(2)) && arg(1) == 'delete') {
$location = 31;
$comment = _comment_load(arg(2));
$nid = $comment->nid;
$gid = og_user_roles_getgid($nid, $uid);
}
// 0 1 2
// http://www.mysite.com/comment/reply/128#comment_form
if (arg(0) == 'comment' && arg(1) == 'reply') {
$location = 4;
$subsections = explode('#', arg(2));
$nid = (int)$subsections[0];
$gid = og_user_roles_getgid($nid, $uid);
}
// og_term access
// 0 1 2 3
// http://www.mysite.com/node/add/forum/121
if (arg(0) == 'node' && is_numeric(arg(3)) && arg(1) == 'add') {
$location = 5;
$nid = (int)arg(3);
$gid = og_user_roles_getgid($nid, $uid);
}
// Here we get the gid directly
//
// http://doadance.scbbs.com/node/79/view/tree
// http://doadance.scbbs.com/node/79/view/members
// http://doadance.scbbs.com/node/79/view
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'view') {
$location = 21;
$gid = (int)arg(1);
}
// Here we get the gid directly
//
// http://doadance.scbbs.com/og/users/72
// http://doadance.scbbs.com/og/manage/72
if (arg(0) == 'og' && is_numeric(arg(2)) && is_null(arg(3))) {
$location = 6;
$gid = (int)arg(2);
}
// og_theme_settings
//
// http://doadance.scbbs.com/admin/og/themes/edit/72
if (arg(1) == 'og' && is_numeric(arg(4)) && arg(2) == 'themes') {
$location = 60;
$gid = (int)arg(4);
// Set group context.
$group_node = node_load($gid);
og_set_group_context($group_node);
}
// og_vocab
//
// http://doadance.scbbs.com/node/29/og/vocab
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'og') {
$location = 61;
$gid = (int)arg(1);
}
// og_vocab
//
// http://clients.brixrealtyinc.com/node/ogtermedit?destination=node%2F29%2Fog%2Fvocab%2Fterms%2F26&gids[]=29&tid=116
//
// Modification as per: http://drupal.org/node/162649
//
if (arg(0) == 'node' && arg(1) == 'ogtermedit') {
// Modification -- Get the gid from the variable instead of trying to calculate it.
if (isset($_REQUEST['gids'])) {
$location = 62;
$gids = $_GET['gids'];
$gid = intval(current($_REQUEST['gids']));
}
}
// og_user_roles
//
// http://doadance.scbbs.com/og/users/72/roles
// http://doadance.scbbs.com/og/users/72/add
if (arg(0) == 'og' && arg(1) == 'users' && is_numeric(arg(2)) && is_null(arg(4))) {
$location = 63;
$gid = (int)arg(2);
}
// og unsubscribe
// og_user_roles
//
// This is sort of a catch all for "og" and gid in this order
// og/ '. filter_xss_admin($content_type->help) .' Code1,Code2,Code3 Each registration code separated by a comma. No spaces.'),
);
// 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 who use this registration code to subscribe 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));
}
}
}
}
// If this is a role deletion function
if (arg(0) == 'admin' && arg(1) == 'user' && arg(2) == 'roles' && arg(3) == 'edit' && is_numeric(arg(4))) {
$rid = arg(4);
// If the form loaded is this type then we want to run it by our submit function
// which will test to see if it is a delete operation
if ($form_id == "user_admin_role") {
$form['#submit'] += array('og_user_roles_role_submit' => array($form_id, &$form));
}
}
}
function og_user_roles_basicgrouprole_form_submit($form_id, $form_values) {
$gid = arg(1);
variable_set('og_user_roles_assign_basicgrouprole_' . $gid, $form_values['og_user_roles_assign_basicgrouprole_' . $gid]);
variable_set('og_user_roles_basicgrouprole_value_' . $gid, $form_values['og_user_roles_basicgrouprole_value_' . $gid]);
}
function og_user_roles_logo_form_submit($form_id, $form_values) {
$gid = arg(1);
variable_set('og_user_roles_logo_' . $gid, $form_values['og_user_roles_logo_' . $gid]);
}
/*
* Here we want to make sure that the submitted logo does not exceed size restrictions.
*/
function og_user_roles_logo_form_validate($form_id, $form_values) {
$gid = arg(1);
if ($form_values['og_user_roles_logo_' . $gid]) {
$width = variable_get('og_user_roles_logo_width', '135');
$height = variable_get('og_user_roles_logo_height', '113');
$image = image_get_info($form_values['og_user_roles_logo_' . $gid]);
$img_width = $image['width'];
$img_height = $image['height'];
if ($img_width > $width || $img_height > $height) {
form_set_error('og_user_roles_logo_' . $gid, "Your submitted group logo image exceeds maximim image dimension limits.");
}
}
}
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]);
}
function og_user_roles_regcode_form_submit($form_id, $form_values) {
$gid = arg(1);
variable_set('og_user_roles_assign_regcode_' . $gid, $form_values['og_user_roles_assign_regcode_' . $gid]);
variable_set('og_user_roles_delete_regcode_' . $gid, $form_values['og_user_roles_delete_regcode_' . $gid]);
variable_set('og_user_roles_regcode_value_' . $gid, $form_values['og_user_roles_regcode_value_' . $gid]);
variable_set('og_user_roles_assign_regcoderole_' . $gid, $form_values['og_user_roles_assign_regcoderole_' . $gid]);
variable_set('og_user_roles_regcoderole_value_' . $gid, $form_values['og_user_roles_regcoderole_value_' . $gid]);
}
/*
* Here we want to make sure that the submitted regcode value is not duplicated in another group.
*/
function og_user_roles_regcode_form_validate($form_id, $form_values) {
$gid = arg(1);
// No need to go through any of this if the 'Allow Group Admins to set Registration Codes' is not set
if (variable_get('og_user_roles_assign_regcode_gid', 0) == 1) {
// If this group allows for registration codes to be set
if ($form_values['og_user_roles_assign_regcode_' . $gid] == 1) {
// If this value is blank, return error
$regcode_array = explode(',', $form_values['og_user_roles_regcode_value_' . $gid]);
if (empty($regcode_array[0])) {
form_set_error('og_user_roles_regcode_value_' . $gid, "No valid registration code entered.");
}
// Get the list of OG groups
$groups = og_all_groups_options();
foreach ($groups as $group => $title) {
// Get all groups except current one.
if ($group != $gid) {
// Only process groups that are allowed to have registration codes
if (variable_get('og_user_roles_assign_regcode_' . $group, 0) == 1) {
// Get the array of codes for this group.
$group_array = explode(',', variable_get('og_user_roles_regcode_value_' . $group, ''));
// Check to see this array as any elements which are duplicated
if ($duplicated = array_intersect($regcode_array, $group_array)) {
// If there are duplicates, then display the group in which they are duplicated ONLY if this is a site admin
if (user_access('administer nodes')) {
form_set_error('og_user_roles_regcode_value_' . $gid, "Registration codes " . implode(', ', $duplicated) . " duplicated in group " . l($title, 'node/' . $group));
} else {
form_set_error('og_user_roles_regcode_value_' . $gid, "Registration codes " . implode(', ', $duplicated) . " duplicated.");
}
}
}
}
}
}
}
}
function og_user_roles_role_submit($form_id, $form_values) {
// If this is a delete role operation
if ($form_values['op'] == t('Delete role')) {
// Remove from og_users_roles table all rows with this rid:
db_query('DELETE FROM {og_users_roles} WHERE rid = %d', $form_values['rid']);
}
}
/**
* Modification as per http://drupal.org/node/183860
* Override theme based on what group is being displayed (if any).
* Be smart about selecting the 'active' group for ambigous urls like node/$nid
*
* @param
* none
* @return
* none
*/
function og_user_roles_theme() {
global $custom_theme;
$group_node = NULL; // a node object containing the 'active' group for this request
$type = $_GET['type'];
if (arg(0) == 'og' && is_numeric(arg(2))) {
$group_node = og_set_theme(arg(2));
}
elseif (arg(0) == 'node' && is_numeric(arg(1))) {
$group_node = og_set_theme(arg(1));
}
elseif (arg(0) == 'node' && (arg(1) == 'add' || arg(1) == 'ognodeadd') && (arg(2) == 'book' || $type == 'book') && arg(3) == 'parent') {
$group_node = og_set_theme(arg(4));
$_REQUEST['edit']['og_groups'][] = $group_node->nid; // checks the right box on node form
}
elseif (arg(0) == 'node' && (arg(1) == 'add' || arg(1) == 'ognodeadd') && isset($_REQUEST['gids'])) {
$gid = intval(current($_REQUEST['gids']));
$group_node = node_load($gid);
$custom_theme = $group_node->og_theme;
}
// As per: http://drupal.org/node/194214
elseif (arg(0) == 'forum' && is_numeric(arg(1))) {
$tid = intval(arg(1));
$gid = og_user_roles_gid_from_tid($tid);
if (!empty($gid)) {
$group_node = node_load($gid);
$custom_theme = $group_node->og_theme;
}
}
elseif (arg(0) == 'og_calendar' && is_numeric(arg(1))) {
$gid = intval(arg(1));
$group_node = node_load($gid);
$custom_theme = $group_node->og_theme;
}
elseif (arg(0) == 'comment' && is_numeric(arg(2))) {
if (arg(1) == 'edit') {
$comment = _comment_load(arg(2));
$nid = $comment->nid;
$gid = og_user_roles_getgid($nid, $uid);
$group_node = og_set_theme($gid);
} else {
$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);
}
/**
* hook_og_create_links
*
* Modification as per: http://drupal.org/node/203875
* $group is an object containing the group node
*/
function og_user_roles_og_create_links($group) {
if (variable_get('og_user_roles_links_content_' . $group->nid, 0) == 1) {
$links[] = l(t('Create content'), "node/ognodeadd", 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
*
*/
function og_user_roles_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
if ($op == 'list') {
$blocks[0] = array('info' => t('Register Codes'),
'weight' => 0, 'enabled' => 1, 'region' => 'left');
$blocks[1] = array('info' => t('Group Logo'),
'weight' => 0, 'enabled' => 0);
return $blocks;
}
else if ($op == 'view') {
switch($delta) {
case 0:
if ($node = og_get_group_context()) {
$subscription = og_user_roles_og_status($node);
if ($subscription == 'none') {
if ($user->uid) {
if (variable_get('og_user_roles_assign_regcode_gid', 0) == 1 && variable_get('og_user_roles_assign_regcode_' . $node->nid, 0) == 1 ) {
$block = array('subject' => t('Register Codes'), 'content' => l('Enter your registration code', 'og/regcode'));
}
}
}
}
break;
case 1:
if ($node = og_get_group_context()) {
if ($logo = variable_get('og_user_roles_logo_' . $node->nid, '')) {
// $width = variable_get('og_user_roles_logo_width', '135');
// $height = variable_get('og_user_roles_logo_height', '113');
// $attributes = array('width' => $width, 'height' => $height);
$attributes = array();
$imglink = theme_image($logo, $alt = $node->title, $title = $node->title, $attributes, $getsize = FALSE);
// Modification as per: http://drupal.org/node/287571
// $output = "" . $imglink . "";
$output = "nid) . "'>" . $imglink . "";
$block = array('content' => $output);
}
}
break;
}
return $block;
}
}
/**
* hook_og_block_details
*
* Trying to get this to work to put "Enter registration code" link in group menu
* if in moderation.
*/
function og_user_roles_og_block_details($group) {
if ($group->og_selective == OG_MODERATED && variable_get('og_user_roles_assign_regcode_gid', 0) == 1 && variable_get('og_user_roles_assign_regcode_' . $group->nid, '') == 1) {
$links[] = l(t('Enter registration code'), "og/regcode", array('title' => t('Subscribe to this group using a registration code.')));
}
$oldblock = og_og_block_details($group);
$block['content'] = $oldblock['content'] . theme('item_list', $links);
$block['subject'] = $oldblock['subject'];
return $block;
}
/**
* Menu callback function to register with a code.
*
*/
function og_user_roles_register() {
global $user;
if ($user->uid) {
drupal_set_message(t('If you have received a registration code for access to a group, enter it here.'));
} else {
drupal_set_message(l('You must register/login use registration codes.', 'user/login'));
}
$form['og_user_roles_regcode'] = array(
'#type' => 'textfield',
'#title' => t('Group registration code'),
'#required' => TRUE,
'#size' => 25,
'#access' => $user->uid,
);
if ($user->uid > 0) {
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit code') );
}
return $form;
}
function og_user_roles_register_validate($form_id, $form_values, $form) {
global $user;
$gid = og_user_roles_gid_from_regcode($form_values['og_user_roles_regcode']);
if ($gid == 0) {
form_set_error('og_user_roles_regcode', "Not a valid group registration code.");
}
if (!user_access('use registration codes')) {
form_set_error('og_user_roles_regcode', "You do not have permission to use registration codes!");
}
if (!$user->uid) {
form_set_error('og_user_roles_regcode', "You must be logged in to use registration codes!");
}
}
function og_user_roles_register_submit($form_id, $form_values) {
global $user;
if ($user->uid) {
$gid = og_user_roles_gid_from_regcode($form_values['og_user_roles_regcode']);
if ($gid > 0) {
$node = node_load($gid);
$title = $node->title;
$return = og_save_subscription($gid, $user->uid, array('is_active' => 1)); // as per http://drupal.org/node/156224
drupal_set_message(t('Subscription request to ' . l($title, 'node/'. $gid) . ' approved.'));
// Delete this registration code if 'delete_regcode' is set
if (variable_get('og_user_roles_delete_regcode_' . $gid, 0) == 1) og_user_roles_delete_regcode($gid, $form_values['og_user_roles_regcode']);
// Places all new registration code group subscribers into default regcode role.
if (variable_get('og_user_roles_assign_regcoderole_' . $gid, 0)) { // check to see if this variable exists
if (variable_get('og_user_roles_assign_regcode_gid', 0) == 1 && variable_get('og_user_roles_assign_regcoderole_' . $gid, 0) == 1 ) {
$rid = variable_get('og_user_roles_regcoderole_value_' . $gid, 0);
og_user_roles_role_join($user->uid, $rid, $gid); // assign user to group role in that group
}
}
}
} else {
drupal_access_denied();
}
}
function og_user_roles_delete_regcode($gid, $regcode) {
// Get the array of registration codes for this $gid
$group_array = explode(',', variable_get('og_user_roles_regcode_value_' . $gid, ''));
// Create new array that contains all codes for this one.
foreach ($group_array as $item) {
if ($item != $regcode) {
$new_array[] = $item;
}
}
// Implode the new array
$new = implode(",", $new_array);
// Write the new value.
variable_set('og_user_roles_regcode_value_' . $gid, $new);
}
/**
* Determine the subscription status of this user in this group
*
* @param
* Group node
* @return status
* The status of subscriber containing 'active' or 'requested' or 'none'
*/
function og_user_roles_og_status($node) {
global $user;
$subscription = 'none';
$result = db_query('select * from {og_uid} where nid = %d', $node->nid);
while ($row = db_fetch_object($result)) {
if ($row->uid == $user->uid) {
if ($row->is_active) {
$subscription = 'active';
} else {
$subscription = 'requested';
}
}
}
return $subscription;
}
/*
* Implementation of hook_node_grants
*/
function og_user_roles_node_grants($account, $op) {
$array = array('ogr_access' => array());
$result = db_query("SELECT ogr_id FROM {og_users_roles} WHERE uid = %d", $account->uid);
while ($row = db_fetch_object($result)) {
$array['ogr_access'][] = $row->ogr_id;
}
return !empty($array['ogr_access']) ? $array : NULL;
}
/*
* Implementation of hook_node_access_records
*/
function og_user_roles_node_access_records($node) {
$grants = array();
if (variable_get('og_user_roles_tac_og_value', 0) == 1) {
// we need to explicitly label public nodes it seems. OG doesn't label nodes
// of types it doesn't apply to, or the "implicitly public" nodes created by
// leaving all the audience checkboxes blank.
// From: http://drupal.org/node/234087#comment-788876
if (og_is_omitted_type($node->type) || (is_array($node->og_groups) && count($node->og_groups) === 0) || (!is_array($node->og_groups) && !og_is_group_type($node->type))) {
$grants[] = array (
'realm' => 'og_public',
'gid' => 0,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0 );
}
// This creates all ogr grants
$where = "WHERE ta.rid = ogr.rid AND n.nid = ".$node->nid;
$result = db_query("SELECT n.nid, ta.rid, ogr.ogr_id, BIT_OR(ta.grant_view) AS grant_view, BIT_OR(ta.grant_update) AS grant_update, BIT_OR(ta.grant_delete) AS grant_delete FROM {term_node} n INNER JOIN {term_access} ta ON n.tid = ta.tid INNER JOIN {og_ancestry} oa ON n.nid = oa.nid INNER JOIN {og_users_roles} ogr ON oa.group_nid = ogr.gid $where GROUP BY n.nid, ta.rid");
while($row = db_fetch_object($result)) {
if ($row) {
$grant_view = ($row->grant_view == 1) ? 1 : 0;
$grant_update = ($row->grant_update == 1) ? 1 : 0;
$grant_delete = ($row->grant_delete == 1) ? 1 : 0;
$grants[] = array(
'realm' => 'ogr_access',
'gid' => $row->ogr_id,
'grant_view' => $grant_view,
'grant_update' => $grant_update,
'grant_delete' => $grant_delete,
'priority' => 0,
);
}
}
return $grants;
}
}
/**
* Implementation of hook_db_rewrite_sql()
*
* Used to include OGUR roles in taxonomy/term
*
*/
function og_user_roles_db_rewrite_sql($query, $table, $field) {
// Only run this if TAC is installed
// if (!user_access('administer taxonomy') && ($field =='vid' || $field =='tid') && module_exists('taxonomy_access')) {
// Modification as per: http://drupal.org/node/300784
//
if (module_exists('taxonomy_access') && !user_access('administer taxonomy') && ($field =='vid' || $field =='tid')) {
global $user;
$op = (arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit')) ? 'create' : 'list';
// let's cache
static $taxonomy_access_sql_clause;
$clause = array();
if (!isset($taxonomy_access_sql_clause)) {
$taxonomy_access_sql_clause = array();
}
if (!isset($taxonomy_access_sql_clause[$op][$field])) {
if (isset($user) && is_array($user->roles)) {
$rids = array_keys($user->roles);
}
else {
$rids[] = 1;
}
if (isset($user)) {
$query = db_query('SELECT ogr_id FROM {og_users_roles} where uid = %d', $user->uid);
while($row = db_fetch_array($query)){
$ogr[] = $row['ogr_id'];
}
}
else {
$ogr[] = 0;
}
$sql = db_query("SELECT t.tid, d.vid, BIT_OR(t.grant_$op) AS grant_$op FROM {term_access} t INNER JOIN {term_data} d ON t.tid = d.tid LEFT OUTER JOIN {og_users_roles} ogr ON ogr.rid = t.rid WHERE (t.rid IN ('".implode("','",$rids)."') OR ogr.uid = ".$user->uid.") AND grant_$op = 1 GROUP BY t.tid, d.vid");
while ($result = db_fetch_object($sql)) {
$tids[]= $result->tid;
$vids[$result->vid]= $result->vid;
}
$clause[$op]['tid'] = isset($tids) ? implode("','",$tids) : '';
$clause[$op]['vid']= isset($vids) ? implode("','",$vids) : '';
$taxonomy_access_sql_clause = $clause;
}
else {
$clause[$op][$field] = $taxonomy_access_sql_clause[$op][$field];
}
$return['where'] = ($clause[$op][$field]) ? "$table.$field IN ('".$clause[$op][$field]."')" : "$table.$field IS NULL";
return $return;
}
else {
return array();
}
}
/**
* 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
}
}
}
/**
* Get the OG roles a user has in a group
*
* @param
* User ID
* @param
* Group ID
* @return array
* An array containing the [roleID => roleName] that the user has in this group
*/
function og_user_roles_get_og_roles($uid, $gid) {
$roles = array();
// 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;
}
return $roles;
}
/**
* Implementation of hook_mail_alter().
*/
function og_user_roles_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers) {
if ($mailkey == 'og_mail') {
// Get unsubscribe url
$unsubscribe_url = $headers['List-Unsubscribe'];
// Take out the "<" and ">" characters
$url = preg_replace('/[\<|\>]/', '', $unsubscribe_url);
// Get the group number (i.e., http://www.less-paper.org/og/manage/16 will yield "16")
$gid = basename($url);
// Get the logo path
$logo = og_user_roles_oglogo_gid($gid);
// Construct the url to the logo
$parsed_url = parse_url($url);
$logo_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . '/' . $logo;
$body = preg_replace('/\!group_logo_url/', '', $body);
}
}
?>'. implode('', $item) .'
';
}
else {
$output = t('No content types available.');
}
}
return $output;
}
/**
* Edit og_vocab term
* Format: http://www.scbbs.com/node/ogtermedit?destination=node%2F29%2Fog%2Fvocab%2Fterms%2F26&gids[]=29&tid=116
*/
function og_user_roles_ogtermedit() {
global $user;
$roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
$user->roles = $roles;
$gids = $_GET['gids'];
if ($gids) {
$group_node = node_load($gids[0]);
og_set_group_context($group_node);
}
$tid = $_GET['tid'];
$access = user_access('administer taxonomy');
if ($access === TRUE) {
$output = taxonomy_admin_term_edit($tid);
} else {
$output = 'Access denied.';
watchdog('access denied', 'node/ogtermedit?tid='. $tid .'&gids[]='. $gids[0], WATCHDOG_WARNING);
}
return $output;
}
/**
* View the modr8 log
* Format: http://www.scbbs.com/node/ogmodr8?gids[]=29&event=2
*/
function og_user_roles_ogmodr8() {
global $user;
// Need this in order to not get "undefined function" on "modr8_log_event" call below
require_once drupal_get_path('module', 'modr8'). '/modr8_admin.inc';
$roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
$user->roles = $roles;
$gids = $_GET['gids'];
if ($gids) {
$group_node = node_load($gids[0]);
og_set_group_context($group_node);
}
$event = $_GET['event'];
$access = user_access('moderate content');
if ($access === TRUE) {
$output = modr8_log_event($event);
} else {
$output = 'Access denied.';
watchdog('access denied', 'node/ogmodr8?event='. $event .'&gids[]='. $gids[0], WATCHDOG_WARNING);
}
return $output;
}
/**
* Edit user
* Format: http://www.scbbs.com/oguseredit/76?gids[]=47
* Format: http://www.scbbs.com/oguseredit/76/edit?gids[]=47
*/
function og_user_roles_oguseredit($uid = 0) {
global $user;
$roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
$user->roles = $roles;
$gids = $_GET['gids'];
if ($gids) {
$group_node = node_load($gids[0]);
$gid = $gids[0];
og_set_group_context($group_node);
}
$account = user_load(array('uid' => $uid));
$in_this_group = 'no';
$oguseredit = 'yes';
if (user_access('no oguseredit', $account)) $oguseredit = 'no';
/**
* This checks all groups the user is a member of to see if any one has given the
* 'no oguseredit' permission. If one group has given him a role with this
* permission, then his profile cannot be edited using oguseredit.
*/
$user_gids = array_keys($account->og_groups);
if ($user_gids) {
foreach ($user_gids as $user_gid) {
if (og_user_roles_user_access('no oguseredit', $user_gid, $uid) === TRUE) $oguseredit = 'no';
}
}
if (array_keys($account->og_groups)) {
if (array_key_exists($gid, $account->og_groups)) $in_this_group = 'yes';
}
$access = user_access('administer users');
if ($access === TRUE && $uid != 1 && $in_this_group == 'yes' && $oguseredit == 'yes') {
if (arg(2) == 'edit') $output = drupal_get_form('user_edit');
if (arg(2) == 'delete') $output = user_edit();
if (is_null(arg(2))) $output = user_view($uid);
} else {
if ($oguseredit == 'no') drupal_set_message('This user account may not be edited using oguseredit.');
if ($in_this_group == 'no') drupal_set_message('This user is not a member of the current group.');
$output = 'Access denied.';
watchdog('access denied', 'oguseredit/'. $uid, WATCHDOG_WARNING);
}
return $output;
}
/**
* Manage users from OG User Roles
* ogusermanage
*/
function og_user_roles_user_manage($gid) {
global $user;
$roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
$user->roles = $roles;
$gids = array_keys($user->og_groups);
$this_gid = $gid;
if ($this_gid) {
$group_node = node_load($this_gid);
og_set_group_context($group_node);
}
// Get the admin status for this user in this group;
$is_admin = 0;
if ($subscriptions = og_get_subscriptions($user->uid)) {
foreach ($subscriptions as $key => $val) {
// Debug
// print "key = " . $key . " is_admin = " . $val['is_admin'];
if ($key == $this_gid) {
$is_admin = $val['is_admin'];
}
}
}
$items = db_query("SELECT uid FROM {users} ORDER BY name");
$output = "";
while ($item = db_fetch_object($items)) {
$uid = $item->uid;
$account = user_load(array('uid' => $uid));
$account_gids = array_keys($account->og_groups);
$common_gids = array_intersect($gids, $account_gids);
if ($common_gids) {
foreach ($common_gids as $common) {
if ($common == $this_gid && $is_admin == 1 && user_access('administer users') && $uid != 1) {
$output .= "