og_invite_people.module in OG Invite People 7
Same filename and directory in other branches
OG Invite People
OG 7.x-2.x required
File
og_invite_people.moduleView source
<?php
/**
* @file
* OG Invite People
*
* OG 7.x-2.x required
*/
/**
* Implements hook_menu().
*/
function og_invite_people_menu() {
$items = array();
$items['group/%/%/admin/people/invite'] = array(
'page callback' => 'drupal_get_form',
'title' => 'Invite People',
'page arguments' => array(
'_og_invite_people_new_users_form',
1,
2,
),
'type' => MENU_LOCAL_TASK,
'weight' => 4,
'access callback' => 'og_invite_people_user_access_group',
'access arguments' => array(
'invite people',
1,
2,
),
);
return $items;
}
/**
* Implements hook_og_ui_get_group_admin()
*/
function og_invite_people_og_ui_get_group_admin($group_type, $gid) {
$items = array();
if (og_user_access($group_type, $gid, 'invite people')) {
$items['og_invite_people'] = array(
'title' => t('Invite People'),
'description' => t('Invited users will be created and notified by email.'),
'href' => 'admin/people/invite',
);
}
return $items;
}
/**
* Check if entity is a group, and user has permission - Access.
*/
function og_invite_people_user_access_group($perm, $group_type, $gid) {
$group = entity_load_single($group_type, $gid);
if (!$group || !og_is_group($group_type, $group)) {
return FALSE;
}
// Extract the group's bundle.
list(, , $bundle) = entity_extract_ids($group_type, $group);
// Verify the bundle has roles
if (!og_roles($group_type, $bundle, $gid)) {
return FALSE;
}
$entity_info = entity_get_info($group_type);
if (!$group_type || !$entity_info) {
// Not a valid entity type.
return FALSE;
}
return og_is_group($group_type, $gid) && og_user_access($group_type, $gid, $perm);
}
/**
* Implement hook_og_permission().
*/
function og_invite_people_og_permission() {
$items = array();
$items['invite people'] = array(
'title' => t('Invite people'),
'description' => t('Users may invite other users to the group without approval.'),
'default role' => array(
OG_ADMINISTRATOR_ROLE,
),
);
return $items;
}
/**
* Main OG Invite People form.
*/
function _og_invite_people_new_users_form($form, &$form_state, $group_type, $gid) {
og_set_breadcrumb($group_type, $gid, array(
l(t('Group'), "{$group_type}/{$gid}/group"),
));
$group = entity_load_single($group_type, $gid);
$label = entity_label($group_type, $group);
list(, , $bundle) = entity_extract_ids($group_type, $group);
$og_roles = og_roles($group_type, $bundle, $gid, FALSE, FALSE);
$form['group_type'] = array(
'#type' => 'value',
'#value' => $group_type,
);
$form['gid'] = array(
'#type' => 'value',
'#value' => $gid,
);
$form['og_invite_people'] = array(
'#type' => 'fieldset',
'#title' => t('Invite new users to %group', array(
'%group' => $label,
)),
);
$form['og_invite_people']['invitee'] = array(
'#type' => 'textarea',
'#title' => t('User email(s)'),
'#description' => t('Use commas or new lines to split email addresses. The new users will be created and invited as members of this group. A one time login link will be sent to the invitees\' email. '),
'#required' => TRUE,
);
$form['og_invite_people']['roles'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Assign roles'),
'#description' => t('Choose one or more roles you would like to assign to the users. ("member" role is assigned by default)'),
'#options' => $og_roles,
'#size' => 5,
);
$form['og_invite_people']['state'] = array(
'#type' => 'value',
'#value' => OG_STATE_ACTIVE,
);
$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);
if (count($field_names) > 1) {
$form['og_invite_people']['field_name'] = array(
'#type' => 'select',
'#title' => t('Field name'),
'#options' => $field_names,
'#default_value' => $field_name,
'#description' => t('Select the field name, the group membership should be registered in.'),
);
}
else {
// There is only a single field name, so just pass it as a value.
$form['og_invite_people']['field_name'] = array(
'#type' => 'value',
'#value' => $field_name,
);
}
$form_state['field_name'] = $field_name;
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Invite user(s)'),
);
return $form;
}
/**
* Validation handler for _og_invite_people_new_users_form.
*/
function _og_invite_people_new_users_form_validate($form, &$form_state) {
$invitee_emails = preg_split('/\\s*[,\\n]\\s*/', $form_state['values']['invitee']);
$form_state['invitee_emails'] = array();
foreach (array_filter($invitee_emails) as $invitee_email) {
if (!valid_email_address($invitee_email)) {
form_set_error('invitee', t('Invalid email ' . $invitee_email));
}
else {
$form_state['invitee_emails'][] = $invitee_email;
}
}
}
/**
* Submit handler for _og_invite_people_new_users_form.
*/
function _og_invite_people_new_users_form_submit($form, &$form_state) {
$group_type = $form_state['values']['group_type'];
$gid = $form_state['values']['gid'];
$invitee_emails = $form_state['invitee_emails'];
$og_roles = $form_state['values']['roles'];
$state = $form_state['values']['state'];
$field_name = $form_state['field_name'];
$entity_type = 'user';
$all_states = og_group_content_states();
// Add group membership form.
$values = array();
foreach ($invitee_emails as $invitee_email) {
$had_account = FALSE;
// Try to load the user by email and see if exists.
if ($account = user_load_by_mail($invitee_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($invitee_email);
}
// Check if user is already assigned to the group. Any "state" (active, pending,... counts as existing).
if ($had_account && og_is_member($group_type, $gid, $entity_type, $account, array())) {
// Load OG membership.
$og_membership = og_get_membership($group_type, $gid, $entity_type, $account->uid);
$message = t('%user is already a member of this group.', array(
'%user' => format_username($account),
));
}
else {
// Create OG membership and add user to group.
$og_membership = og_membership_create($group_type, $gid, $entity_type, $account->uid, $field_name, $values);
$message = t('%user has been created and an email notification was sent.', array(
'%user' => format_username($account),
));
}
// Update OG memebership if necessary.
if ($og_membership->state != $state) {
$message .= t(' Membership state changed from "%prev_state" to "%post_state"', array(
'%prev_state' => $all_states[$og_membership->state],
'%post_state' => $all_states[$state],
));
$og_membership->state = $state;
}
// Save the OG membership.
$og_membership
->save();
// Add OG roles to users.
foreach ($og_roles as $rid) {
og_role_grant($group_type, $gid, $account->uid, $rid);
}
// 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);
// Print the message for each user.
drupal_set_message($message);
}
}
/**
* Create new user and send email.
*
* @param string $invitee_email
* @return object $account
*/
function _og_invite_people_create_user($invitee_email) {
// This will generate a random password.
$password = user_password(8);
// Username from email.
$name = explode('@', $invitee_email);
// To prepare the email address as a username, trim any potential leading
// and trailing spaces and replace simple illegal characters with hyphens.
// @see user_validate_name().
$name = preg_replace('/[^\\x{80}-\\x{F7} a-z0-9@_.\'-]/i', '-', trim($name[0]));
// Check if user name is available and append _NUM if existing.
if (user_load_by_name($name)) {
$i = 1;
$new_name = $name . '_' . $i;
while (user_load_by_name($new_name)) {
$i++;
$new_name = $name . '_' . $i;
}
$name = $new_name;
}
// Set up the user fields.
$fields = array(
'name' => $name,
'mail' => $invitee_email,
'pass' => $password,
'status' => 1,
'init' => 'email address',
'roles' => array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
),
);
// Create new user function.
$account = user_save('', $fields);
// Send the e-mail through the user module.
drupal_mail('user', 'register_admin_created', $invitee_email, NULL, array(
'account' => $account,
), variable_get('site_mail', NULL));
return $account;
}
Functions
Name![]() |
Description |
---|---|
og_invite_people_menu | Implements hook_menu(). |
og_invite_people_og_permission | Implement hook_og_permission(). |
og_invite_people_og_ui_get_group_admin | Implements hook_og_ui_get_group_admin() |
og_invite_people_user_access_group | Check if entity is a group, and user has permission - Access. |
_og_invite_people_create_user | Create new user and send email. |
_og_invite_people_new_users_form | Main OG Invite People form. |
_og_invite_people_new_users_form_submit | Submit handler for _og_invite_people_new_users_form. |
_og_invite_people_new_users_form_validate | Validation handler for _og_invite_people_new_users_form. |