This CodeLet let you Programmatically manage members of sub groups. also you can add sub group.
you can inviter/remove peoples of sub groups. Invitation will be sent by email.
after installation of this CodeLet you must have to pass the group gid on query string,
Ex. yourdomain/subgroups/$gid.
CodeLet Dependencies:
- Organic groups ( og )
- Subgroups for Organic groups ( og_subgroups )
- OG Invite People ( og_invite_people ).
CodeLet
<?php
/**
* Implementation of hook_menu
*/
function custom_menu() {
$items = array();
$items['subgroups/%'] = array(
'title' => 'Sub Groups',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'page callback' => '_custom_sub_group_list',
'page arguments' => array(1),
);
return $items;
}
function _custom_sub_group_list($gid = NULL) {
global $user;
if($gid == NULL) return '';
$qry = "SELECT node_og_membership.nid AS node_og_membership_nid, node_og_membership.title AS node_og_membership_title
FROM
{og_membership} og_membership
LEFT JOIN {node} node_og_membership
ON og_membership.gid = node_og_membership.nid AND og_membership.group_type = 'node'
LEFT JOIN {og_users_roles} og_users_roles_og_membership
ON og_membership.etid = og_users_roles_og_membership.uid
INNER JOIN {og_role} og_users_roles_og_membership__og_role
ON og_users_roles_og_membership.rid = og_users_roles_og_membership__og_role.rid
WHERE node_og_membership.nid IN(SELECT etid
FROM `og_membership`
WHERE entity_type = 'node'
AND gid = $gid) AND (( (og_membership.gid = og_users_roles_og_membership.gid)
AND (og_membership.group_type = og_users_roles_og_membership.group_type)
AND (og_membership.etid = '" . $user->uid . "' ) )AND(( (og_membership.state IN ('1'))
AND (node_og_membership.status = '1')
AND (og_membership.entity_type LIKE 'user') AND (node_og_membership.type = 'subgroup')
AND (og_users_roles_og_membership__og_role.name IN ('administrator member', 'member')) )))
ORDER BY node_og_membership_title ASC";
$records = db_query($qry);
$output = '';
$row = array();
$header = array(t('Name'), t('Invite'), t('Remove'));
while ($result = $records->fetchAssoc()) {
$row[] = array($result['node_og_membership_title'],
_custom_gcm_invite($result['node_og_membership_nid']),
_custom_gcm_remove_group_member($result['node_og_membership_nid']));
}
$sub_groupform = _custom_sub_group_save_form();
return $sub_groupform . theme('table', array('header' => $header, 'rows' => $row));
}
/**
* _custom_gcm_invite
* render invite peopel form.
*/
function _custom_gcm_invite($sn__nid){
global $user;
if($user->uid != 0)
return drupal_render(drupal_get_form('_custom_invite_form_' . $sn__nid, $sn__nid));
else return '';
}
/**
* Implements hook_forms();
*/
function custom_forms($form_id, $args) {
if (FALSE !== strpos($form_id, '_custom_invite_form')) {
if ($form_id == '_custom_invite_form_' . $args[0]) {
$forms[$form_id] = array('callback' => '_custom_invite_form');
return $forms;
}
}
if (FALSE !== strpos($form_id, '_custom_remove_group_member_form')) {
if ($form_id == '_custom_remove_group_member_form_' . $args[0]) {
$forms[$form_id] = array('callback' => '_custom_remove_group_member_form');
return $forms;
}
}
}
/**
* _custom_invite_form
* invite people form
*/
function _custom_invite_form($form, &$form_state, $gid, $group_type = 'node') {
$form['email'] = array(
'#title' => 'email',
'#description' => 'enter a valid email address',
'#type' => 'textfield',
'#size' => 25,
'#required' => TRUE,
);
$form['state'] = array('#type' => 'value', '#value' => 1);
$form['group_type'] = array('#type' => 'value', '#value' => $group_type);
$form['gid'] = array('#type' => 'value', '#value' => $gid);
$field_names = og_get_group_audience_fields('user', 'user', $group_type);
$field_name = !empty($form_state['values']['field_name']) ? $form_state['values']['field_name'] : key($field_names);
// There is only a single field name, so just pass it as a value.
$form['field_name'] = array(
'#type' => 'value',
'#value' => $field_name,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Invite'),
);
$form_state['field_name'] = $field_name;
return $form;
}
/**
* _custom_invite_form - Validation handler
*/
function _custom_invite_form_validate($form, &$form_state) {
$group_type = $form_state['values']['group_type'];
$gid = $form_state['values']['gid'];
$email = $form_state['values']['email'];
if (!valid_email_address($email)) {
form_set_error('email', t('Invalid email.'));
}
$form_state['email'] = $email;
}
/**
* _custom_invite_form - Submit handler
*/
function _custom_invite_form_submit($form, &$form_state) {
$group_type = $form_state['values']['group_type'];
$gid = $form_state['values']['gid'];
$email = $form_state['email'];
$state = $form_state['values']['state'];
$field_name = $form_state['field_name'];
// Add group membership form.
$values = array();
// Add group membership form. We still don't have the user or state.
$og_membership = og_membership_create($group_type, $gid, 'user', 0, $field_name, $values);
$had_account = FALSE;
if ($account = user_load_by_mail($email)) {
$had_account = TRUE;
$message = '%user is already known and has been added to the group.';
}
else{
// Create a user and return $account object.
$account = _og_invite_people_create_user($email);
$message = '%user has been created and an email has been sent inviting them to the group.';
}
if($had_account && og_is_member($group_type, $gid, 'user', $account)) {
drupal_set_message(t('%user is already member of this group', array('%user' => format_username($account))));
}
else{
// Add user to group.
$og_membership->etid = $account->uid;
$og_membership->state = $state;
$og_membership->save();
// Offer invitee user account object in form_state.
$form_state['account'][] = $account;
// Invoke hook_og_invite_people_invited() - after a user is created and added to group.
module_invoke_all('og_invite_people_invited', $account, $og_membership);
drupal_set_message(t($message, array('%user' => format_username($account))));
}
}
/**
* _custom_gcm_remove_group_member
*/
function _custom_gcm_remove_group_member($sn__nid){
if(_custom_check_user_isadmin_ismanager($sn__nid))
return drupal_render(drupal_get_form('_custom_remove_group_member_form_' . $sn__nid, $sn__nid));
else return '';
}
/**
* _custom_remove_group_member_form
*/
function _custom_remove_group_member_form($form, &$form_state, $gid, $group_type = 'node') {
$form['uid'] = array(
'#title' => 'user',
'#type' => 'select',
'#options' => _custom_get_group_members($gid),
'#required' => TRUE,
);
$form['state'] = array('#type' => 'value', '#value' => 1);
$form['group_type'] = array('#type' => 'value', '#value' => $group_type);
$form['gid'] = array('#type' => 'value', '#value' => $gid);
$field_names = og_get_group_audience_fields('user', 'user', $group_type);
$field_name = !empty($form_state['values']['field_name']) ? $form_state['values']['field_name'] : key($field_names);
// There is only a single field name, so just pass it as a value.
$form['field_name'] = array(
'#type' => 'value',
'#value' => $field_name,
);
$form['entity_type'] = array(
'#type' => 'value',
'#value' => 'user',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Remove'),
);
$form_state['field_name'] = $field_name;
return $form;
}
/**
* _custom_remove_group_member_form - Validation handler
*/
function _custom_remove_group_member_form_validate($form, &$form_state) {
$group_type = $form_state['values']['group_type'];
$gid = $form_state['values']['gid'];
$user = $form_state['values']['uid'];
if ($user == '') {
form_set_error('uid', t('Select user.'));
}
$form_state['uid'] = $user;
}
/**
* _custom_remove_group_member_form - Submit handler
*/
function _custom_remove_group_member_form_submit($form, &$form_state) {
$group_type = $form_state['values']['group_type'];
$gid = $form_state['values']['gid'];
$field_name = $form_state['values']['field_name'];
$og_membership = new OgMembership($form_state['values'], $group_type);
$og_membership->etid = $form_state['values']['uid'];
// get membership id
$sn__mid = db_query("SELECT id FROM {og_membership} WHERE gid = $gid AND entity_type = 'user' AND etid = ". $og_membership->etid ." AND group_type = '$group_type'")->fetchField();
if(!empty($sn__mid))
$og_membership->id = $sn__mid;
// Don't process the group manager, if exists.
$group = entity_load_single($group_type, $gid);
if (!empty($group->uid) && $group->uid == $og_membership->etid && $og_membership->entity_type == 'user') {
return;
}
$og_membership->delete();
}
/**
* _custom_get_group_members
* @param int $sn__gid
* @return multitype:string An
*/
function _custom_get_group_members($sn__gid) {
$record = db_query("SELECT users.name AS user_name, users.uid AS uid
FROM {users} users
LEFT JOIN {og_membership} og_membership_users ON users.uid = og_membership_users.etid AND og_membership_users.entity_type = 'user'
WHERE (( (og_membership_users.gid = '$sn__gid' ) )AND(( (users.status <> '0') AND (users.uid <> 1) AND (og_membership_users.state IN ('1')) )))
");
$arr_options = array('' => ' -Select user-');
while($result = $record->fetchAssoc()) {
$arr_options[$result['uid']] = $result['user_name'];
}
return $arr_options;
}
/**
*
* @param unknown_type $gid
* @return boolean
*/
function _custom_check_user_isadmin_ismanager($gid = NULL) {
global $user;
$record = db_query("SELECT users.name AS user_name, users.uid AS uid
FROM {users} users
LEFT JOIN {og_membership} og_membership_users ON users.uid = og_membership_users.etid AND og_membership_users.entity_type = 'user'
WHERE (( (og_membership_users.gid = '$gid' ) )AND(( (users.status <> '0') AND (og_membership_users.state IN ('1')) )))
");
$record = db_query("SELECT users.name AS user_name, users.uid AS uid
FROM {users} users
LEFT JOIN {og_membership} og_membership_users ON users.uid = og_membership_users.etid AND og_membership_users.entity_type = 'user'
WHERE (( (og_membership_users.gid = '$gid' ) )AND(( (users.status <> '0') AND (og_membership_users.state IN ('1')) )))
");
if($gid == NULL || $user->uid == 0) return FALSE;
return TRUE;
}
/**
* _custom_gcm_invite
*/
function _custom_sub_group_save_form() {
return drupal_render(drupal_get_form('_custom_create_sub_group_form'));
}
/**
*
* @return multitype:string Ambigous <The, string, A, Optional>
*/
function _custom_create_sub_group_form() {
$form['custom'] = array(
'#type' => 'fieldset',
'#title' => t('SUB GROUP'),
'#collapsible' => TRUE,
'#group' => 'additional_settings',
'#description' => t(''),
);
$form['gid'] = array(
'#type' => 'hidden',
'#default_value' => arg(1)
);
$form['custom']['title'] = array(
'#title' => 'New Sub Group',
'#type' => 'textfield',
'#size' => 25,
'#required' => TRUE,
);
$form['custom']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
return $form;
}
/**
* submit handler
*/
function _custom_create_sub_group_form_submit($form, &$form_state) {
global $user;
//Create node
$gid = $form_state['values']['gid'];
$node = new StdClass();
$node->type = 'subgroup';
$node->uid = $user->uid;
$node->title = $form_state['values']['title'];
$node->status = 1;
$node->language = 'und';
$node->is_new = TRUE;
if($node = node_submit($node)) {
$nid = node_save($node);
}
if(isset($node->nid)) {
//Get the full node that needs to be subscribed to the group
$node = node_load($node->nid);
//Set the values
$values = array(
'entity_type' => 'node',
'entity' => $node,
'state' => OG_STATE_ACTIVE,
);
//Add this node to the group
$result = og_group('node', $gid, $values);
drupal_set_message(t('@node has been created', array('@node' => $node->title)));
}else {
drupal_set_message('DB:Error ', 'error');
}
}
?>
Info file details
name = "custom"
description = "Customization of OG Feature"
dependencies[] = og
dependencies[] = og_subgroups
dependencies[] = og_invite_people
core = 7.x
package = Custom