| 5 og.module | og_subscribe_user($gid, $account, $request = NULL) |
| 6 og.module | og_subscribe_user($gid, $account, $request = NULL) |
Create a new membership for a given user to given group. Edits to membership should go through og_save_subscription(). No access control since this is an API function.
string 'approval', 'subscribed' or 'rejected' depending on the group's configuration.
<?php
function og_subscribe_user($gid, $account, $request = NULL) {
// moderated groups must approve all members (selective=1)
$node = node_load($gid);
switch ($node->og_selective) {
case OG_MODERATED:
og_save_subscription($gid, $account->uid, array('is_active' => 0));
$sql = og_list_users_sql(1, 1);
$res = db_query($sql, $node->nid);
$admins = array();
while ($row = db_fetch_object($res)) {
$admins[] = $row->uid;
}
if (!empty($admins)) {
// Prepend user's request text with standard cruft. Should be a
// variable but that's a bit annoying.
if ($request) {
$request = t("\n\nPersonal message from @name:\n------------------\n\n@request", array('@name' => $account->name, '@request' => $request));
}
$variables = array(
'@group' => $node->title,
'@username' => $account->name,
'!approve_url' => url("og/approve/$node->nid/$account->uid", NULL, NULL, TRUE),
'!group_url' => url("og/users/$node->nid", NULL, NULL, TRUE),
);
$message = array(
'subject' => _og_user_mail_text('og_request_user_subject', $variables),
'body' => _og_user_mail_text('og_request_user_body', $variables) . $request,
);
// Send notifications to each admin; Sending an array of recipients
// implies that this is a bulk message.
module_invoke_all('og', 'user request', $gid, $admins, $message);
}
$return_value = array(
'type' => 'approval',
'message' => t('Membership request to the %group group awaits approval by an administrator.', array('%group' => $node->title)),
);
break;
case OG_OPEN:
og_save_subscription($gid, $account->uid, array('is_active' => 1));
$return_value = array(
'type' => 'subscribed',
'message' => t('You are now a member of the %group.', array('%group' => $node->title)),
);
break;
case OG_CLOSED:
case OG_INVITE_ONLY:
// admins can add members to these groups, but others can't.
if (og_is_node_admin($node)) {
og_save_subscription($gid, $account->uid, array('is_active' => 1));
}
else {
$return_value = array(
'type' => 'rejected',
'message' => t('Membership request to the @group group was rejected, only group administrators can add users to this group.', array('@group' => $node->title)),
);
}
}
return $return_value;
}
?>