array( 'title' => 'Signup', 'description' => 'Configure signup invites for LDAP provisioning settings.', 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_invite_signup_admin_settings'), 'access arguments' => array('administer site configuration'), 'file' => 'ldapprov_invite_signup.admin.inc', ), ); } /** * Implementation of hook_form_alter(). */ function ldapprov_invite_signup_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case 'signup_status_invite_form': ldapprov_invite_signup_alter_invite_form($form, $form_state); break; } } ////////////////////////////////////////////////////////////////////////////// // Mail strings /** * Returns a mail string for a variable name. * * Used by ldapprov_invite_signup_mail() and the settings forms to retrieve strings. */ function _ldapprov_invite_signup_mail_text($key, $language = NULL, $variables = array()) { $langcode = isset($language) ? $language->language : NULL; if ($admin_setting = variable_get('ldapprov_invite_signup_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 signup for a \"!title\" 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 /** * Alter the signup invite form to change validate and submit handlers. */ function ldapprov_invite_signup_alter_invite_form(&$form, $form_state) { $form['#validate'] = array('ldapprov_invite_signup_invite_form_validate'); $form['#submit'] = array('ldapprov_invite_signup_invite_form_submit', 'signup_status_invite_form_submit'); } /** * Invite form validation. */ function ldapprov_invite_signup_invite_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' => $mail))); } else if ($account && db_result(db_query_range("SELECT sid FROM {signup_log} WHERE uid = %d AND nid = %d", $account->uid, $values['nid'], 0, 1))) { form_set_error('mail', t('A user %name with the e-mail address %mail is already signuped to the node.', array('%name' => $account->name, '%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_signup_invite_form_submit($form, &$form_state) { global $user; $values = $form_state['values']; $node = node_load($values['nid']); $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; } $form_state['valid_mails'] = $local_mails; $data = array( 'type' => 'signup', 'nid' => $node->nid, 'string' => t('Node !title', array('!title' => l($node->title, 'node/'. $node->nid))), 'variables' => array('!title' => $node->title, '!message' => $values['message']), ); ldapprov_invite_form_submit($form, $form_state, $data); } ////////////////////////////////////////////////////////////////////////////// // ldapprov_invite hooks /** * Implementation of hook_ldapinvite(). */ function ldapprov_invite_signup_ldapinvite($uid, &$account, $data) { if ($data['type'] != 'signup') return; $node = node_load($data['nid']); if (!isset($node->signup) || $node->signup != 1) return; $user = user_load($uid); if (signup_status_invite_can_invite($node, $user)) { $variables = array( '!title' => $node->title, '!site' => variable_get('site_name', 'drupal'), '!message' => $data['message'], '!name' => $user->name, ); db_query("INSERT INTO {signup_log} (uid, nid, signup_time, form_data, status) VALUES (%d, %d, %d, '%s', %d)", $account->uid, $data['nid'], time(), serialize(array()), SIGNUP_STATUS_INVITE_STATUS); $sid = db_last_insert_id('signup_log', 'sid'); _signup_status_change('invite', (object)array('sid' => $sid, 'uid' => $account->uid, 'nid' => $data['nid'], 'anon_mail' => '', 'status' => SIGNUP_STATUS_INVITE_STATUS)); $signup_url = url('signup/edit/'. $sid, array('absolute' => TRUE)); $message = drupal_mail('signup_status_invite', 'invite', $account->mail, $GLOBALS['language'], array('account' => $account, 'variables' => array_merge($variables, array('!signup_url' => $signup_url))), $user->mail); if ($message['result']) { watchdog('ldapprov_invite_status', 'Invite e-mail was mailed from %name to %mail.', array('%name' => $user->name, '%mail' => $account->mail)); } else { watchdog('ldapprov_invite_status', 'Error mailing invite e-mail from %user to %mail.', array('%name' => $user->name, '%mail' => $account->mail), WATCHDOG_ERROR); } } }