array( 'title' => 'OG', 'description' => 'Configure OG invites for LDAP provisioning settings.', 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_invite_og_admin_settings'), 'access arguments' => array('administer site configuration'), 'file' => 'ldapprov_invite_og.admin.inc', ), 'node/%node/og-invite' => array( 'title' => 'Invite', 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_invite_og_form', 1), 'access callback' => 'ldapprov_invite_og_can_invite', 'access arguments' => array(1), 'type' => variable_get('ldapprov_invite_og_tab', 1) ? MENU_LOCAL_TASK : MENU_CALLBACK, 'weight' => 8, ), 'node/%node/og-invite/autocomplete' => array( 'title' => 'Invite autocomlete', 'page callback' => 'ldapprov_invite_og_autocomplete', 'page arguments' => array(1), 'access callback' => 'ldapprov_invite_og_can_invite', 'access arguments' => array(1), 'type' => MENU_CALLBACK, ), ); } /** * Check if a user can invite other users to the group. */ function ldapprov_invite_og_can_invite($node) { if (!ldapprov_invite_access() || !isset($node->og_selective)) return FALSE; global $user; if (module_exists('og') && !isset($user->og_groups) && $user->uid) { // We have to perform a load in order to assure that the $user->og_groups bits are present. // $user->og_groups bits are missing when the i18n module is enabled. $user = user_load(array('uid' => $user->uid)); } switch ($node->og_selective) { case OG_OPEN: case OG_MODERATED: return og_is_group_member($node, FALSE); break; case OG_CLOSED: case OG_INVITE_ONLY: return og_is_group_admin($node); break; } } ////////////////////////////////////////////////////////////////////////////// // Mail strings /** * Returns a mail string for a variable name. * * Used by ldapprov_invite_og_mail() and the settings forms to retrieve strings. */ function _ldapprov_invite_og_mail_text($key, $language = NULL, $variables = array()) { $langcode = isset($language) ? $language->language : NULL; if ($admin_setting = variable_get('ldapprov_invite_og_mail_'. $key, FALSE)) { // An admin setting overrides the default string. return t($admin_setting, $variables, $langcode); } else { // No override, return default string. switch ($key) { case 'invite_subject': return t('You have been invited to the !site', $variables, $langcode); case 'invite_body': return t("Your friend, !name has invited you to join a group \"!group\" at !site site. You may now register to the site at\n\n!register_uri\n\n----------\n\n!message\n\n-- !site team", $variables, $langcode); } } } ////////////////////////////////////////////////////////////////////////////// // Invites /** * Invites form. */ function ldapprov_invite_og_form(&$form_state, $node) { $form = ldapprov_invite_form(); $form['invite']['#title'] = t('Invite a contact to the group'); $form['invite']['#description'] = t('If you invite a new user to the site, then upon registration the user will be subscribed to the group %group based on the group membership settings. If you invite an existing user, the user will receive an e-mail message with the invitation to join the group.', array('%group' => $node->title)); $form['invite']['mail']['#description'] = t('Type the comma separated list of e-mails of the people you would like to invite to the group.'); $form['invite']['mail']['#autocomplete_path'] = 'node/'. $node->nid .'/og-invite/autocomplete'; $form['invite']['message']['#description'] = t('This message will be added to the e-mails sent to the people you are inviting to the group.'); $form['invite']['gid'] = array( '#type' => 'hidden', '#value' => $node->nid, ); return $form; } /** * Invite form validation. */ function ldapprov_invite_og_form_validate($form, &$form_state) { global $user; $values = $form_state['values']; $mails = array_filter(array_map(create_function('$a', 'return trim($a);'), explode(',', $values['mail']))); if (in_array($user->mail, $mails)) form_set_error('mail', t('You may not invite yourself - @self.', array('@self' => $user->mail))); $bad_mails = array(); foreach ($mails as $mail) { if (($account = user_load(array('mail' => $mail))) && $account->status == 0) form_set_error('mail', t('A user %name with the e-mail address %mail is blocked.', array('%name' => $account->name, '%mail' => $account->mail))); else if ($account && og_is_group_member($values['gid'], FALSE, $account->uid)) form_set_error('mail', t('A user %name with the e-mail address %mail is already a member of the group.', array('%name' => $account->name, '%mail' => $account->mail))); else if (!$account && db_result(db_query_range("SELECT rid FROM {ldapprov_invite} WHERE uid = %d AND mail = '%s'", $user->uid, $mail, 0, 1))) form_set_error('mail', t('The invitation already was sent to user with e-mail address %mail.', array('%mail' => $mail))); else if (valid_email_address($mail)) $form_state['valid_mails'][] = $mail; else $bad_mails[] = $mail; } if (count($bad_mails)) form_set_error('mail', t('Invalid email address: @value.', array('@value' => implode(', ', $bad_mails)))); } /** * Invite form submission. */ function ldapprov_invite_og_form_submit($form, &$form_state) { global $user; $values = $form_state['values']; $node = node_load($values['gid']); $local_mails = $form_state['mails'] = array(); foreach ($form_state['valid_mails'] as $mail) { if (user_load(array('mail' => $mail))) $local_mails[] = $mail; else $form_state['mails'][] = $mail; } $variables = array( '@group' => $node->title, '@description' => $node->og_description, '@site' => variable_get('site_name', 'drupal'), '!group_url' => url("og/subscribe/$node->nid", array('absolute' => TRUE)), '@body' => $values['message'], ); foreach ($local_mails as $mail) { $message = drupal_mail('og', 'invite_user', $mail, $GLOBALS['language'], $variables, $user->mail); if ($message['result']) { watchdog('ldapprov_invite_og', 'Invite e-mail was mailed from %name to %mail.', array('%name' => $user->name, '%mail' => $mail)); drupal_set_message(t('The invite has been mailed to e-mail address %mail.', array('%mail' => $mail))); } else { watchdog('ldapprov_invite_og', 'Error mailing invite e-mail from %user to %mail.', array('%name' => $user->name, '%mail' => $mail), WATCHDOG_ERROR); drupal_set_message(t('Unable to send mail. Please contact the site admin.'), 'error'); } } $data = array( 'type' => 'og', 'gid' => $node->nid, 'string' => t('Group !group', array('!group' => l($node->title, 'node/'. $node->nid))), 'variables' => array('!group' => $node->title), ); ldapprov_invite_form_submit($form, $form_state, $data); } /** * Invite form autocomplete function. */ function ldapprov_invite_og_autocomplete($node, $string = '') { global $user; $matches = array(); if (!empty($string)) { $mails = array_map(create_function('$a', 'return trim($a);'), explode(',', $string)); if ($string = array_pop($mails)) { $result = db_query_range("SELECT u.name, u.mail FROM {users} u LEFT JOIN {og_uid} ou ON u.uid = ou.uid WHERE u.status = '1' AND (LOWER(u.name) LIKE LOWER('%s%%') OR LOWER(u.mail) LIKE LOWER('%s%%')) AND (ou.nid <> %d OR ou.nid IS NULL) AND u.uid <> %d ORDER BY u.mail", $string, $string, $node->nid, $user->uid, 0, 10); while ($account = db_fetch_object($result)) { $matches[(!empty($mails) ? implode(', ', $mails) .', ' : '') . $account->mail] = check_plain($account->mail) .' ('. check_plain($account->name) .')'; } } } drupal_json($matches); } ////////////////////////////////////////////////////////////////////////////// // ldapprov_invite hooks /** * Implementation of hook_ldapinvite(). */ function ldapprov_invite_og_ldapinvite($uid, &$account, $data) { if ($data['type'] != 'og') return; $node = node_load($data['gid']); if (!og_is_group_type($node->type)) return; $user = user_load($uid); if (og_is_group_admin($node, $user)) { og_save_subscription($node->nid, $account->uid, array('is_active' => 1)); } else if (og_is_group_member($node, FALSE, $uid)) { switch ($node->og_selective) { case OG_OPEN: case OG_MODERATED: og_subscribe_user($node->nid, $account); break; } } }