'.t('Allows group administrators to add members into Drupal system roles limited by group.').'
';
}
}
/**
* Implementation of hook_menu().
*/
function og_user_roles_menu($may_cache) {
global $user;
$items = array();
if (!$may_cache) {
$access = $user->uid; // login is required
$items[] = array('path' => 'node/addform', 'type' => MENU_CALLBACK, 'callback' => 'og_user_roles_addform', 'access' => $access, 'title' => t('Create content'));
if (! user_access('administer site configuration')) {
$user = user_load(array('uid' => $user->uid));
// Original Code
//
// This code causes a "configure member roles" tab to appear for each group the user has
// admin access to. Problem is, it appears on "My Groups" page: http://clients.brixrealtyinc.com/og/my
//
// foreach ($user->og_groups as $key => $sub) {
// if (og_user_roles_is_allowed($key)) {
// $node = node_load(array('nid' => $key));
// Add another tab to the group subscribers page for admins to
// configure member roles
// $items[] = array(
// 'path' => "og/users/$key/roles",
// 'callback' => 'og_user_roles_page',
// 'title' => t('configure member roles'),
// 'callback arguments' => array($key),
// 'type' => MENU_LOCAL_TASK,
// 'access' => node_access('update', $node),
// 'weight' => 5,
// );
// }
// } // end foreach
// Modification
//
// This prevents this tab from appearing on "My Groups" page: http://clients.brixrealtyinc.com/og/my
//
//
foreach ($user->og_groups as $key => $sub) {
if (arg(0) == 'og' && arg(1) == 'users' && arg(2) == $key) {
$key = arg(2);
if (og_user_roles_is_allowed($key)) {
$node = node_load(array('nid' => $key));
// Add another tab to the group subscribers page for admins to
// configure member roles
$items[] = array(
'path' => "og/users/$key/roles",
'callback' => 'og_user_roles_page',
'title' => t('Configure member roles'),
'callback arguments' => array($key),
'type' => MENU_LOCAL_TASK,
'access' => node_access('update', $node),
'weight' => 5,
);
} // end if
} // end if
} // end foreach
//
// end modification
}
else {
// site admin only
$items[] = array(
'path' => 'admin/og/og_user_roles',
'title' => t('Organic groups user roles'),
'description' => t('Allows group administrators to add members into group roles.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'og_user_roles_settings',
'access' => user_access('administer site configuration'),
);
// site admin only, admin will see og_user_roles page on each group page
if ((arg(0) == 'og') && (arg(1) == 'users') && ($gid = arg(2)) && is_numeric($gid) && (og_user_roles_is_allowed($gid))) {
$items[] = array(
'path' => "og/users/$gid/roles",
'callback' => 'og_user_roles_page',
'title' => t('Configure member roles'),
'callback arguments' => array($gid),
'type' => MENU_LOCAL_TASK, 'weight' => 5,
'access' => user_access('administer site configuration')
);
}
}
} // end if
return $items;
}
/**
* Implementation of hook_settings().
*/
function og_user_roles_settings() {
// Get list of all og-enabled node types
$group_types = variable_get('og_node_types', array('og'));
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.
if (!count($roles)) {
$form['og_user_roles'] = array(
'#value' => ''. t('No assignable roles were found. Please create at least one role under administer >> access >> roles.', array('%access' => url('admin/access/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' => theme('placeholder', $name))),
'#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_default"] = array(
'#type' => 'fieldset',
'#title' => t('Default Non-Group Role for new users.'),
);
$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 users to a specific role that is NOT group specific upon signup?'),
);
$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_notify"] = array(
'#type' => 'fieldset',
'#title' => t('Default Group Admin Notification for new subscribers.'),
);
$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?'),
);
$form["og_user_roles_all_groups"] = array(
'#type' => 'fieldset',
'#title' => t('Nodes to automatically place into all available groups.'),
);
$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.'),
);
$form["og_user_roles_tac_og_default"] = array(
'#type' => 'fieldset',
'#title' => t('TAC / OG Access Control Integration.'),
);
$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? Details here: http://groups.drupal.org/node/3700'),
);
return system_settings_form($form);
}
/**
* Menu callback; displays members and role selection
*/
function og_user_roles_page($gid) {
$output = '';
$node = node_load(array('nid' => $gid));
// Get roles associated with this group. We rebuild the associative
// array because the settings form only passes RID and we need the name.
$role_ids = variable_get("og_user_roles_roles_{$node->type}", array());
$all_roles = user_roles();
foreach ($role_ids as $rid => $checked) {
if ($checked != 0) {
$roles[$rid] = $all_roles[$rid];
}
}
if (is_array($roles)) {
// Retrieve list of all group users
$sql = og_list_users_sql(0);
$result = pager_query($sql, 100, 0, NULL, $gid);
$output .= theme('pager',NULL, 100);
$output .= drupal_get_form('og_user_roles_page_form',$gid,$roles,$result);
}
else {
drupal_set_message(t('No roles have been assigned as group roles yet.'));
}
print theme('page', $output);
}
/**
* Form
*/
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
);
//
// Original form
//
// $form['user_roles']['roles'][$account->uid] = array(
// '#type' => 'checkboxes',
// '#options' => $roles,
// '#default_value' => _user_roles($account->uid, $roles),
// );
//
//
// Modification
//
// $form['user_roles']['roles'][$account->uid] = array(
// '#type' => 'select',
// '#multiple' => TRUE,
// '#required' => FALSE,
// '#size' => 2,
// '#options' => $roles,
// '#default_value' => _user_roles($account->uid, $roles),
// );
//
// end modification for pulldown select
//
//
// Multi column checkbox form - as per http://drupal.org/node/41936
//
$form['user_roles']['roles'][$account->uid] = array(
'#type' => 'checkbox_columns',
// '#title' => t('Multi Column Roles'),
// '#title' => t($account->name),
'#title' => l($account->name, "user/$account->uid"),
'#default_value' => _user_roles($account->uid, $roles),
'#columns' => 4,
'#options' => $roles,
'#suffix' => '
',
);
}
// Output form to screen
$form['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
return $form;
}
/**
* Process the form submission
*/
function og_user_roles_page_form_submit($form_id, $form_values) {
//
// Modification
// Added this to get gid
//
if ( is_numeric(arg(2)) ) {
$gid = (int)arg(2);
}
foreach ($form_values['user_roles']['roles'] as $uid => $roles) {
foreach ($roles as $rid => $checked) {
$exists = db_result(db_query("SELECT * FROM {og_users_roles} WHERE uid = %d AND rid = %d AND gid = %d", $uid, $rid, $gid)); // modification
// $exists = db_result(db_query("SELECT * FROM {users_roles} WHERE uid = %d AND rid = %d", $uid, $rid)); // replaced
if ($checked && !$exists) {
db_query("INSERT INTO {og_users_roles} (uid, rid, gid) VALUES (%d, %d, %d)", $uid, $rid, $gid); // modification
// db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $uid, $rid); // replaced
}
elseif (!$checked && $exists) {
db_query("DELETE FROM {og_users_roles} WHERE uid = %d AND rid = %d AND gid = %d", $uid, $rid, $gid); // modification
// db_query("DELETE FROM {users_roles} WHERE uid = %d AND rid = %d", $uid, $rid); // replaced
}
}
}
// Have to rebuild the menu here, in case new menu items were added
menu_rebuild();
drupal_set_message(t('Your changes have been saved'));
}
/**
* This checks to see what roles a current user has against a given set of roles.
*/
function _user_roles($uid, $roles = array()) {
//
// Modification
// Added this to get gid
//
if ( is_numeric(arg(2)) ) {
$gid = (int)arg(2);
}
$roles_output = array();
if (is_array($roles)) {
foreach ($roles as $rid => $role) {
$result = db_result(db_query("SELECT * FROM {og_users_roles} WHERE uid = %d AND rid = %d AND gid = %d", $uid, $rid, $gid)); // modification
// $result = db_result(db_query("SELECT * FROM {users_roles} WHERE uid = %d AND rid = %d", $uid, $rid)); // replaced
if ($result) {
$roles_output[$rid] = $rid;
}
}
}
return $roles_output;
}
/**
* Theme function to render the table for the og_user_roles form.
*/
function theme_og_user_roles_page_form($form) {
$output .= "\n" . '
\n";
$output .= drupal_render($form);
return $output;
}
/**
* Determine if a group is allowed to configure member roles.
*
* @param $nid
* A node ID
* @return boolean
* TRUE if this group type allows roles to be assigned, otherwise FALSE
*/
function og_user_roles_is_allowed($nid) {
$node = node_load(array('nid'=>$nid));
if (in_array($node->type, variable_get('og_node_types', array('og'))) && variable_get("og_user_roles_roles_$node->type", NULL)) {
return TRUE;
}
else {
return FALSE;
}
}
//
// Modification
// Added the following two functions to add and remove roles.
//
/**
* Add role to og_users_roles table.
*/
function og_user_roles_role_join($uid, $rid, $gid) {
db_query("INSERT INTO {og_users_roles} (rid, uid, gid) VALUES ('%d','%d','%d')", $rid, $uid, $gid);
}
/**
* Remove all roles for a user in a group table.
*/
function og_user_roles_role_leave($uid, $gid) {
db_query("DELETE FROM {og_users_roles} WHERE uid = %d AND gid = %d", $uid, $gid);
}
//
// Modification
// Added the following functions from this snippet: http://drupal.org/node/41936
// Creates multiple checkbox columns
//
function expand_checkbox_columns($element) {
$value = is_array($element['#value']) ? $element['#value'] : array();
$element['#type'] = 'checkboxes';
$element['#tree'] = TRUE;
if (count($element['#options']) > 0) {
if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
$element['#default_value'] = array();
}
foreach ($element['#options'] as $key => $choice) {
$class = ($column % $element['#columns']) && $column ? 'checkbox-columns' : 'checkbox-columns-clear';
if (!isset($element[$key])) {
$element[$key] = array('#type' => 'checkbox', '#processed' => TRUE, '#title' => $choice, '#default_value' => in_array($key, $value), '#attributes' => $element['#attributes'], '#prefix' => '', '#suffix' => '
', '#return_value' => $choice);
}
$column++;
}
}
return $element;
}
//
// Modification
// Added the following functions from this snippet: http://drupal.org/node/41936
// To let Drupal know about the new "expand_checkbox_columns".
//
function og_user_roles_elements() {
$type['checkbox_columns'] = array('#input' => TRUE, '#process' => array('expand_checkbox_columns' => array()), '#tree' => TRUE);
return $type;
}
/**
* @ingroup views
*/
/**
* Implementation of hook_views_tables():
* Present fields and filters for user data.
* Requires that usernode be installed.
*/
function og_user_roles_views_tables() {
$tables['og_users_roles'] = array(
'name' => 'og_users_roles',
'provider' => 'internal', // won't show up in external list.
'join' => array(
'left' => array(
'table' => 'node',
'field' => 'uid'
),
'right' => array(
'field' => 'uid'
),
),
'filters' => array(
'rid' => array(
'name' => t('OG User Roles: Role (group)'),
'operator' => 'views_handler_operator_andor',
'list' => 'views_handler_filter_role',
'value-type' => 'array',
'help' => t('Include the node only if the node author is a member of the selected OG group role - uses og_users_roles.'),
),
'gid' => array(
'name' => t('OG User Roles: Group (using og_users_roles)'),
'list' => 'views_handler_filter_group',
'list-type' => 'list',
'operator' => 'views_handler_operator_or',
'value-type' => 'array',
'help' => t('Include or exclude nodes whose node author has roles in the selected groups - uses og_users_roles (less accurate).'),
),
),
);
$tables['og_users_uid'] = array(
'name' => 'og_uid',
'provider' => 'internal', // won't show up in external list.
'join' => array(
'left' => array(
'table' => 'node',
'field' => 'uid'
),
'right' => array(
'field' => 'uid'
),
),
'filters' => array(
'nid' => array(
'name' => t('OG User Roles: Group (using og_uid)'),
'list' => 'views_handler_filter_group',
'list-type' => 'list',
'operator' => 'views_handler_operator_or',
'value-type' => 'array',
'help' => t('Include or exclude nodes whose node author is a member of the selected groups - using og_uid table (more accurate).'),
),
),
);
$tables['og_non_group_roles'] = array(
'name' => 'users_roles',
'provider' => 'internal', // won't show up in external list.
'join' => array(
'left' => array(
'table' => 'node',
'field' => 'uid'
),
'right' => array(
'field' => 'uid'
),
),
'filters' => array(
'rid' => array(
'name' => t('OG User Roles: Role (non-group)'),
'operator' => 'views_handler_operator_andor',
'list' => 'views_handler_filter_role',
'value-type' => 'array',
'help' => t('Include the node only if the node author is a member of the selected role - uses users_roles (non-group roles).'),
),
),
);
return $tables;
}
/*
* Create a list of group names and IDs.
*/
function views_handler_filter_group() {
$list = array();
$list = variable_get('og_node_types', array('og'));
$groupTypes = implode(",", $list);
$vids = array();
$result = db_query("SELECT n.nid, n.title FROM {node} n WHERE n.type IN ('". $groupTypes ."') ORDER BY n.title");
while ($obj = db_fetch_object($result)) {
$vids[$obj->nid] = $obj->title;
}
return $vids;
}
/*
* Implementation of hook_init.
*
* Got the idea of re-directing if this is a add group node request
* from here: http://drupal.org/project/globalredirect
*
*/
function og_user_roles_init ()
{
// Looking for this format: http://www.scbbs.com/node/add/link?gids[]=29
if (arg(0) == 'node' && arg(1) == 'add' && isset($_REQUEST['gids'])) {
$gids = $_GET['gids'];
$gid = intval(current($_REQUEST['gids']));
$type = arg(2);
$path = 'node/addform';
$query = 'type=' . $type . '&gids[]=' . $gid;
drupal_goto($path, $query);
}
}
/*
* Implementation of hook_user.
* Put new users in default role if applicable.
*
* Got this code from here: http://drupal.org/node/28379#comment-132430
*
*/
function og_user_roles_user ($op, &$edit, &$user, $category=null)
{
// If the user opted to create a default role
if (variable_get("og_user_roles_assign_default", 0) == 1) {
$rid = variable_get("og_user_roles_default_value", 0);
// special case for new users
if ($op == "insert")
{
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $user->uid, $rid); //
}
} // end if
// 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
}
/**
* 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;
$user = user_load(array('uid' => $uid));
$username = $user->name;
$node = node_load($nid);
// Send notification of new og subscription to administrators of group;
if (variable_get("og_user_roles_notify_default", 0) == 1) {
$group = $node->title;
$sender = variable_get('site_mail', '');
$subject = 'User ' . $username . ' added to group: ' . $group;
$message = "User " . $username . " added to group: " . $group . "";
// Get the group admin(s)
// FIXED TABLE PREFIX {} - bibo
// SELECT uid FROM {og_uid} {}
$result = db_query("SELECT uid FROM {og_uid} WHERE is_admin = 1 AND nid = " . $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);
} // end while
} // end if
break;
}
}
/**
* Implementation of hook_nodeapi().
*
*/
function og_user_roles_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'access':
//
// This access check is used for TAC and OG permissions integration;
//
if (variable_get("og_user_roles_tac_og_value", 0) == 1) {
$access = FALSE;
$og_access = og_user_roles_og_access_check($teaser, $node);
$tac_access = og_user_roles_taxonomy_access_check($teaser, $node);
if ($og_access === TRUE && $tac_access == FALSE) $access = FALSE;
if ($og_access === FALSE) $access = FALSE;
if ($og_access === TRUE && $tac_access == TRUE) $access = TRUE;
return $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)) {
$nodelistArray = explode(",", $nodelist);
foreach ($nodelistArray as $nodelistItem) {
if ($node->nid == $nodelistItem) {
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;
} // end switch
} // end og_usre_roles_nodeapi function
// 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";
// we use rewrite_sql() in order to avoid disclosing completely private groups (once we support those properly)
// return db_query(db_rewrite_sql($sql, 'oga', 'group_nid'), $nid);
return db_query(db_rewrite_sql($sql, 'oga', 'group_nid'), '');
}
//
// My OG Roles Modifications
//
//
// Modification - Added this function
//
// This function returns an array of role keys and names, like $user->roles
// However, it returns site roles as well as group roles
//
function og_user_roles_all_roles_array($user) {
$roles = og_user_roles_all_roles($user);
return $roles;
}
// 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();
$nodeID = 0;
$location = 0;
$uri_request_id = $_SERVER['REQUEST_URI'];
$arg = explode("/", $uri_request_id);
$ogroles = array();
$x1 = 0;
//
// 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;
//
// 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;
$nodeID = (int)arg(1);
$gid = getgid($nodeID, $uid);
}
// http://doadance.scbbs.com/node/79/edit
// http://doadance.scbbs.com/node/79/outline
// http://doadance.scbbs.com/node/79/track
if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'outline' || arg(2) == 'track' ) ) {
$location = 2;
$nodeID = (int)arg(1);
$gid = getgid($nodeID, $uid);
}
// 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));
$nodeID = $comment->nid;
$gid = getgid($nodeID, $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));
$nodeID = (int)$subsections[0];
$gid = getgid($nodeID, $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;
$nodeID = (int)arg(3);
$gid = getgid($nodeID, $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 = 2;
$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_calendar
//
// http://doadance.scbbs.com/og_calendar/72
if (arg(0) == 'og_calendar' && is_numeric(arg(1)) && is_null(arg(2))) {
$location = 7;
$gid = (int)arg(1);
}
// og_forum
// 0 1 2 3
// http://www.mysite.com/og_forum/39/72?edit[og_groups][]=72
// http://www.mysite.com/og_forum/37/71?sort=asc&order=Replies&edit[og_groups][0]=71
if ($arg[1] == 'og_forum' && strpos($arg[3], 'og_groups') !== false && (!is_null($arg[3])) ) {
$location = 8;
$subsections = explode("=", $arg[3]);
foreach ($subsections as $subsection) {
if (is_numeric($subsection)) {
$gid = (int)$subsection;
}
}
}
// og create nodes (version 5.1)
// 0 1 2 3
// http://sbn.scbbs.com/node/add/forum?gids[]=72
// http://sbn.scbbs.com/node/add/simplenews?gids[]=72
// http://sbn.scbbs.com/node/add/webform?gids[]=72
// http://sbn.scbbs.com/node/add/video?gids[]=72
// http://sbn.scbbs.com/node/add/page?gids[]=72
// http://sbn.scbbs.com/node/add/blog?gids[]=72
// http://sbn.scbbs.com/node/add/event?gids[]=72
// http://sbn.scbbs.com/node/add/poll?gids[]=72
// http://sbn.scbbs.com/node/add/flexinode-3?gids[]=72
if ($arg[1] == 'node' && strpos($arg[3], 'gids') !== false && (!is_null($arg[3])) ) {
$location = 10 . "(" . arg(0) . "|" . arg(1) . "|" . arg(2) . ")";
$subsections = explode("=", $arg[3]);
$gid = (int)$subsections[1];
}
// og create nodes (custom node/20 aliased to node/addform)
// 0 1 2 3
// http://clients.brixrealtyinc.com/node/addform?type=document&gids[]=12
//
if ($arg[1] == 'node') {
if (strpos($arg[2], 'addform') !== false ) {
if (strpos($arg[2], 'gids') !== false ) {
$location = 13;
$subsections = explode("=", $arg[2]);
$gid = (int)$subsections[2];
}
}
}
} // end $gid if
//
// Now, using $uid and $gid we find out what roles this user has in this group;
// FIXED TABLE PREFIX {} - bibo
if (empty($gid)) $gid = 0; // This prevents us from getting error on non-group node/add
$query = 'SELECT {role}.rid, {role}.name FROM {role} INNER JOIN {og_users_roles} ON {role}.rid = {og_users_roles}.rid WHERE {og_users_roles}.uid = ' . $uid . ' AND {og_users_roles}.gid = ' . $gid;
$results = db_query($query);
// $results = db_query('SELECT role.rid, role.name FROM {role} INNER JOIN {og_users_roles} ON role.rid = og_users_roles.rid WHERE {og_users_roles}.uid = ' . $uid . ' AND {og_users_roles}.gid = ' . $gid );
//
// Create an array of these roles;
//
while ( $row = db_fetch_array($results) ) {
$x1 = $row["rid"];
$ogroles[$x1] = $row["name"];
}
//
// First, we need to create an array of $user->roles
//
$c = $user->roles;
//
// Next, we add the $user->roles array to the $ogroles array, if there is an ogroles array;
//
if ($x1 > 0) {
$d = array();
$d = $c + $ogroles;
$d = array_unique($d);
} else {
$d = $c;
}
// Write the test data
og_user_roles_write_test($user, $location, $gid02, $gid, $uri_request_id, $d, $query);
//
// $d is either the merged results, or just $user->roles;
//
return $d;
} // end og_user_roles_all_roles()
function getgid($nodeID, $uid) {
// This will return a $gid based upon the $nodeID and $uid supplied
$gid = 0;
// $result = db_query("select node_access.gid from node_access INNER JOIN og_uid ON node_access.gid = og_uid.nid WHERE node_access.nid = $nodeID AND og_uid.uid = $uid");
// FIXED TABLE PREFIX {} - bibo
$result = db_query("select {node_access}.gid from {node_access} INNER JOIN {og_uid} ON {node_access}.gid = {og_uid}.nid WHERE {node_access}.realm = 'og_subscriber' AND {og_uid}.uid = $uid AND ({node_access}.nid = $nodeID OR {node_access}.gid = $nodeID)"); // modified to check for either the node or group ID in node_access
while($t = db_fetch_object($result)) {
$gid = $t->gid;
}
// If $gid still equals 0 then try searching og_term table (if it exists)
// og forums will typically be listed here
if ($gid == 0) {
// FIXED TABLE PREFIX {} - bibo
if (user_table_exists('{og_term}')) {
// FIXED TABLE PREFIX {} - bibo
$result = db_query("select {og_term}.nid from {og_term} INNER JOIN {og_uid} ON {og_term}.nid = {og_uid}.nid WHERE {og_term}.tid = $nodeID AND {og_uid}.uid = $uid");
while($t = db_fetch_object($result)) {
$gid = $t->nid;
}
}
}
return $gid;
} // end getgid()
//
// Modification - Added this function
//
function user_table_exists($table) {
return db_num_rows(db_query("SHOW TABLES LIKE '{" . db_escape_table($table) . "}'"));
}
function og_user_roles_write_test($user, $location, $gid02, $gid, $uri_request_id, $d, $query) {
//
// See what is found in user_all_roles
//
$testStatus = "";
$testDate = format_date(time(), 'custom', 'Y-m-d h:i:s a');
$testUser = $user->uid;
$testFunction = "og_user_roles_all_roles";
$testSubFunction = $location;
$testString = "group context: " . $gid02;
$testUserName = $user->name;
$testGroup = $gid;
$testURI = $uri_request_id;
$testAArg0 = arg(0);
$testAArg1 = arg(1);
$testAArg2 = arg(2);
$testAArg3 = arg(3);
$testPerm = "Roles Returned: (" . implode(",", $d) . ")
dbQuery = " . $query;
if (user_table_exists('user_test')) {
$testQuery = "insert into user_test (testStatus, testDate, testUser, testUserName, testGroup, testFunction, testSubFunction, testString, testURI, testAArg0, testAArg1, testAArg2, testAArg3, testPerm) values ('" . $testStatus . "','" . $testDate . "','" . $testUser . "','" . $testUserName . "','" . $testGroup . "','" . $testFunction . "','" . $testSubFunction . "','" . $testString . "','" . $testURI . "','" . $testAArg0 . "','" . $testAArg1 . "','" . $testAArg2 . "','" . $testAArg3 . "','" . $testPerm . "')";
db_query($testQuery);
}
}
/**
* Called by og_user_roles_nodeapi('access')
* To check og access permissions
*
*/
function og_user_roles_og_access_check($op, $node = NULL) {
global $user;
$uid = $user->uid;
if ($op != 'create' && $node->nid && $node->status) {
if (isset($user) && is_array($user->og_groups)) {
$gids = array_keys($user->og_groups);
}
else {
$gids[] = 0;
}
if (isset($user) && is_array($user->roles)) {
$rids = array_keys($user->roles);
}
else {
$rids[] = 1;
}
//
// if this node has groups or is a group, then use group sql, else use non-group sql
//
$groups = $node->og_groups;
if ($groups || $node->type == 'group') {
$sql = "
SELECT COUNT(*) FROM {node_access} na
LEFT OUTER JOIN {og_uid} ogu on ogu.nid = na.nid
LEFT OUTER JOIN {og} ogm on ogm.nid = na.nid
WHERE (na.nid = 0 OR na.nid = %d)
AND ((na.realm = 'og_public' and na.gid = 0)
OR (na.realm = 'og_subscriber' AND na.gid in ('".implode("','",$gids)."'))
OR (ogm.nid > 0 and (na.nid in ('".implode("','",$gids)."')) or ogm.directory = 1 )
OR (na.realm = 'term_access' AND na.nid in ('".implode("','",$gids)."')) )
AND (na.grant_$op >= 1 OR (ogu.is_admin = 1 AND ogu.uid = ".$uid.") )
";
} else {
$sql = "
SELECT COUNT(*) FROM {node_access} na
WHERE (na.nid = 0 OR na.nid = %d)
AND (na.realm = 'term_access' AND na.gid in ('".implode("','",$rids)."'))
AND na.grant_$op >= 1
";
}
$result = db_query($sql, $node->nid);
$output = (db_result($result));
if ($output == 0) return FALSE;
if ($output > 0) return TRUE;
}
}
/**
* Called by og_user_roles_nodeapi('access')
* To check taxonomy_access access permissions
*
*/
function og_user_roles_taxonomy_access_check($op, $node = NULL) {
global $user;
$uid = $user->uid;
if ($op != 'create' && $node->nid && $node->status) {
// Discovered that I needed to continue using user->roles to get non-group roles.
// will get group roles using og_users_roles and og_ancestry
//
if (isset($user) && is_array($user->roles)) {
$rids = array_keys($user->roles);
}
else {
$rids[] = 1;
}
$sql = "SELECT COUNT(*) FROM {node_access} na";
$sql .= " LEFT OUTER JOIN {og_users_roles} ogr ON ogr.rid = na.gid";
$sql .= " LEFT OUTER JOIN {og_ancestry} oga ON oga.nid = na.nid";
$sql .= " LEFT OUTER JOIN {og_uid} ogu on ogu.nid = na.nid";
$sql .= " WHERE (na.nid = 0 OR na.nid = %d) AND ((na.realm = 'term_access' AND na.gid in ('".implode("','",$rids)."')) OR (ogr.uid = " . $uid . " AND na.realm = 'term_access' AND oga.group_nid = ogr.gid) )";
$sql .= " AND (na.grant_$op >= 1 OR (ogu.is_admin = 1 AND ogu.uid = ".$uid.") )";
$result = db_query($sql, $node->nid);
$output = (db_result($result));
if ($output == 0) return FALSE;
if ($output > 0) return TRUE;
}
}
/**
* Create content
*
* Format: http://www.scbbs.com/node/addform?type=link&gids[]=29
*
*/
function og_user_roles_addform() {
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'];
$typeFound = 'no';
if ($gids) {
$group_node = node_load($gids[0]);
og_set_group_context($group_node);
}
$type = $_GET['type'];
if ($type) {
//
// Got this from node.module (node_access)
// No matter the type, this should return us the create permission.
//
$module = node_get_types('module', $type);
if ($module == 'node') {
$module = 'node_content'; // Avoid function name collisions.
}
$access = module_invoke($module, 'access', 'create', $type);
if ($access === TRUE) {
$output = node_add($type);
}
}
return $output;
}