Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // $Id: caubo_registration.module,v 1.9.2.11 2008/11/02 13:04:01 Exp $
- /**
- * @file
- *
- *
- * Custom user registration process for CAUBO
- */
- /**
- * Implementation of hook_menu().
- *
- * @return array
- */
- function caubo_registration_menu() {
- $items['admin/settings/caubo_registration'] = array(
- 'title' => 'CAUBO Registration',
- 'description' => 'Configure Registration for CAUBO.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('_caubo_registration_admin_settings_form'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- );
- return $items;
- }
- function caubo_registration_form_alter(&$form, &$form_state, $form_id) {
- if ($form_id != "user_register") return;
- if ($form_state['redirect'] == "user") drupal_goto("user");
- if (isset($form_state['storage']['page']) && $form_state['storage']['page'] == 2) {
- caubo_register_page_two($form, $form_state);
- return;
- }
- if (isset($form_state['storage']['page']) && $form_state['storage']['page'] == 3) {
- caubo_register_page_three($form, $form_state);
- return;
- }
- //page 1 of registration is displayed
- $form['group_contact_details']['#type'] = 'value';
- $form['submit']['#value'] = "next";
- // lets store whatever submit functions we have and replace with our own
- // we want to store so we can add these back on later so we don't interfere with other modules
- $form_state['storage']['submits'] = $form['#submit'];
- $form['#submit'] = array('caubo_register_form_submit');
- return;
- }
- // Second page of registration
- function caubo_register_page_two(&$form, &$form_state) {
- global $pass_email_address;
- // set Page 1 fields as entered on Page 1 and disable
- $form['name']['#value'] = $form_state['values']['name'];
- $form['mail']['#value'] = $form_state['values']['mail'];
- $form['title']['#value'] = $form_state['content_profile_registration']['profile']['node']->title;
- $form['name']['#disabled'] = true;
- $form['mail']['#disabled'] = true;
- $form['title']['#disabled'] = true;
- // To HIDE the fields rather than show DISABLED
- /* $form['name']['#type'] = 'value';
- $form['mail']['#type'] = 'value';
- $form['title']['#type'] = 'value';*/
- // set Organization but display as disabled
- $pass_email_address = $form['mail']['#value'];
- $form['group_contact_details']['field_organization']['#pre_render'] = array('_caubo_limit_organizations');
- $form['group_contact_details']['field_organization']['#value'][0]['nid'] = $form_state['storage']['orgid'];
- $form['group_contact_details']['#attributes'] = array('style' => 'display:none');
- $settings['caubo_registration']['disorg'] = 1; // disable Org selector as FormAPI seems to have a bug
- // add Role selection
- _caubo_registration_roles($form, $form_state);
- // replace user_reg submit function with our own
- foreach ($form_state['storage']['submits'] as $key => $submit) {
- if ($submit == "user_register_submit") $form_state['storage']['submits'][$key] = 'caubo_user_register_submit';
- }
- $form['#submit'] = $form_state['storage']['submits'];
- // add our JS twekas
- $settings['caubo_registration']['clean'] = true;
- drupal_add_js($settings, 'setting');
- drupal_add_js(drupal_get_path('module', 'caubo_registration') . "/caubo_registration.js");
- // set custom page title
- drupal_set_title(t("User Account - select your preferred Title"));
- return $form;
- }
- // Third page of registration - same as Page 2 except Org selector is enabled and we will return to Page 2
- function caubo_register_page_three(&$form, &$form_state) {
- global $pass_email_address;
- // set Page 1 fields as entered on Page 1 and disable
- $form['name']['#value'] = $form_state['values']['name'];
- $form['mail']['#value'] = $form_state['values']['mail'];
- $form['title']['#value'] = $form_state['content_profile_registration']['profile']['node']->title;
- $pass_email_address = $form['mail']['#value'];
- $form['name']['#disabled'] = true;
- $form['mail']['#disabled'] = true;
- $form['title']['#disabled'] = true;
- $form['group_contact_details']['field_organization']['#pre_render'] = array('_caubo_limit_organizations');
- $form_state['storage']['page_three'] = TRUE;
- // return to Page 2
- $form['submit']['#value'] = "next";
- $form['#submit'] = array('caubo_register_form_submit');
- // add our JS tweaks
- $settings['caubo_registration']['clean'] = true;
- drupal_add_js($settings, 'setting');
- drupal_add_js(drupal_get_path('module', 'caubo_registration') . "/caubo_registration.js");
- // set custom page title
- drupal_set_title(t("User Account - select your Organization"));
- return $form;
- }
- function _caubo_limit_organizations($element) {
- global $pass_email_address;
- $nids = _caubo_get_organizations_from_email($pass_email_address);
- foreach ($element['nid']['nid']['#options'] as $nid => $option) {
- if (!in_array($nid, $nids)) unset($element['nid']['nid']['#options'][$nid]);
- }
- return $element;
- }
- function test_form_validate($form, &$form_state){
- // Validating page 2
- if (isset($form_state['storage']['page_two'])) {
- form_set_error('last-name', 'Please enter a last name.'); // Example only
- }
- return;
- }
- /**
- * handle sorting out which page to go to based on if distinct or non-distinct org case
- *
- * @param mixed $form
- * @param mixed $form_state
- */
- function caubo_register_form_submit($form, &$form_state) {
- if ($orgid = $form_state['content_profile_registration']['profile']['node']->field_organization[0]['nid']) $orgs[] = $orgid;
- else $orgs = _caubo_get_organizations_from_email($form['mail']['#value']);
- $n = count($orgs);
- switch ($n) {
- case 0:
- drupal_access_denied(); // likely do something else here
- break;
- case 1: // distinct case - goto Page 2
- $form_state['rebuild'] = TRUE;
- $form_state['storage']['page'] = 2;
- $form_state['storage']['orgid'] = current($orgs);
- $form_state['storage']['page_one_values'] = $form_state['values']; // Carry forward Page 1 values - WHY??
- break;
- default: // non-distinct
- $form_state['rebuild'] = TRUE;
- $form_state['storage']['page'] = 3;
- $form_state['storage']['page_one_values'] = $form_state['values'];
- break;
- }
- }
- /**
- * return an array of NIDs matching Org nodes with email_domains set that match $mail
- *
- * - more than one Org can use this mail domain
- * - an Org may have more than mail domain set
- *
- * @param mixed $mail
- */
- function _caubo_get_organizations_from_email($mail) {
- $matches = explode("@", $mail);
- $domain = $matches[1];
- $sql = "SELECT n.nid FROM {content_field_email_domains} AS e
- Inner Join {node} AS n ON n.vid = e.vid
- WHERE e.field_email_domains_value = '%s' AND n.status = 1 AND n.type = 'organization'";
- $results = db_query($sql, $domain);
- $nids = array();
- while ($result = db_fetch_object($results)) $nids[] = $result->nid;
- return $nids;
- }
- function _caubo_get_organization_type($orgid) {
- $org = node_load($orgid);
- return $org->field_orgtype[0]['value'];
- }
- function _caubo_registration_roles(&$form, &$form_state){
- $roles = array();
- $display_roles = variable_get('caubo_registration_profile_roles', $roles);
- $approval_roles = variable_get('caubo_registration_profile_roles_approval', $roles);
- if (empty($display_roles)){
- $link = l("here.", 'admin/settings/caubo_registration');
- drupal_set_message("You must configure the CAUBO Registration module " . $link, "error");
- return;
- }
- $orgid = $form_state['storage']['orgid'];
- $orgtype = _caubo_get_organization_type($orgid);
- if ($orgtype === "1") $filter = "C.";
- if ($orgtype === "2") $filter = "U.";
- foreach($display_roles as $role){
- if ($role) {
- $role_name = db_result(db_query("SELECT name FROM {role} WHERE rid = %d", $role));
- if (substr($role_name, 0, 2) != $filter) {
- if(in_array($role, $approval_roles)) {
- $role_name .= " <i>*needs administration approval</i>";
- }
- $roles[$role] = $role_name;
- }
- }
- }
- asort($roles);
- $form['roles'] = array(
- '#type' => 'radios',
- '#title' =>'Choose a role',
- '#options' => $roles,
- '#weight' => 20,
- );
- }
- /**
- * admin settings form.
- *
- * @return
- */
- function _caubo_registration_admin_settings_form() {
- //Obtain all roles
- $result = db_query("SELECT * FROM {role} WHERE rid > 2");
- if ($result == TRUE){
- while ($role = db_fetch_array($result)){
- $roles[$role['rid']] = $role['name'];
- $default_value[$role['rid']]=0;
- }
- $form['caubo_registration_profile_roles'] = array(
- '#type' => 'checkboxes',
- '#title' =>'Roles',
- '#options' => $roles,
- '#description' => "Choose roles that will be displayed on registration form",
- '#default_value' => variable_get('caubo_registration_profile_roles', $default_value),
- );
- $form['caubo_registration_profile_roles_approval'] = array(
- '#type' => 'checkboxes',
- '#title' =>'Approval Roles',
- '#options' => $roles,
- '#description' => "Choose roles that need administration approval. Users that select this role during registration will
- be disabled until administrator approves them.",
- '#default_value' => variable_get('caubo_registration_profile_roles_approval', $default_value),
- );
- return system_settings_form($form);
- }
- }
- /**
- * Submit handler for the user registration form. - OUR VERSION
- *
- * This function is shared by the installation form and the normal registration form,
- * which is why it can't be in the user.pages.inc file.
- */
- function caubo_user_register_submit($form, &$form_state) {
- global $base_url;
- $admin = user_access('administer users');
- $mail = $form_state['values']['mail'];
- $name = $form_state['values']['name'];
- if (!variable_get('user_email_verification', TRUE) || $admin) {
- $pass = $form_state['values']['pass'];
- }
- else {
- $pass = user_password();
- };
- $notify = isset($form_state['values']['notify']) ? $form_state['values']['notify'] : NULL;
- $from = variable_get('site_mail', ini_get('sendmail_from'));
- if (isset($form_state['values']['roles'])) {
- $roles = array($form_state['values']['roles']);
- $rid = $form_state['values']['roles'];
- }
- else {
- $roles = array();
- }
- if (!$admin && array_intersect(array_keys($form_state['values']), array('uid', 'init', 'session', 'status'))) {
- watchdog('security', 'Detected malicious attempt to alter protected user fields.', array(), WATCHDOG_WARNING);
- $form_state['redirect'] = 'user/register';
- return;
- }
- // The unset below is needed to prevent these form values from being saved as
- // user data.
- unset($form_state['values']['form_token'], $form_state['values']['submit'], $form_state['values']['op'], $form_state['values']['notify'],
- $form_state['values']['form_id'], $form_state['values']['affiliates'], $form_state['values']['destination']);
- $merge_data = array('pass' => $pass, 'init' => $mail, 'roles' => $roles);
- // NOTE - this is where we deviate from std user_register and instead of simply checking the site wide (admin//user/settings) setting for the type of
- // approval required (none or admin) - we now check against the selected role and set accordingly
- if (!$admin) {
- $approval_roles = variable_get('caubo_registration_profile_roles_approval', array());
- if(in_array($rid, $approval_roles)) $merge_data['status'] = 0;
- else $merge_data['status'] = 1;
- }
- $account = user_save('', array_merge($form_state['values'], $merge_data));
- // this won't save our Role so let's do it now
- db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $rid);
- // Terminate if an error occured during user_save().
- if (!$account) {
- drupal_set_message(t("Error saving user account."), 'error');
- $form_state['redirect'] = '';
- return;
- }
- $form_state['user'] = $account;
- watchdog('user', 'New user: %name (%email).', array('%name' => $name, '%email' => $mail), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit'));
- // The first user may login immediately, and receives a customized welcome e-mail.
- if ($account->uid == 1) {
- drupal_set_message(t('Welcome to Drupal. You are now logged in as user #1, which gives you full control over your website.'));
- if (variable_get('user_email_verification', TRUE)) {
- drupal_set_message(t('</p><p> Your password is <strong>%pass</strong>. You may change your password below.</p>', array('%pass' => $pass)));
- }
- user_authenticate(array_merge($form_state['values'], $merge_data));
- $form_state['redirect'] = 'user/1/edit';
- return;
- }
- else {
- // Add plain text password into user account to generate mail tokens.
- $account->password = $pass;
- if ($admin && !$notify) {
- drupal_set_message(t('Created a new user account for <a href="@url">%name</a>. No e-mail has been sent.', array('@url' => url("user/$account->uid"), '%name' => $account->name)));
- }
- else if (!variable_get('user_email_verification', TRUE) && $account->status && !$admin) {
- // No e-mail verification is required, create new user account, and login
- // user immediately.
- _user_mail_notify('register_no_approval_required', $account);
- if (user_authenticate(array_merge($form_state['values'], $merge_data))) {
- drupal_set_message(t('Registration successful. You are now logged in.'));
- }
- $form_state['redirect'] = '';
- return;
- }
- else if ($account->status || $notify) {
- // Create new user account, no administrator approval required.
- $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
- _user_mail_notify($op, $account);
- if ($notify) {
- drupal_set_message(t('Password and further instructions have been e-mailed to the new user <a href="@url">%name</a>.', array('@url' => url("user/$account->uid"), '%name' => $account->name)));
- }
- else {
- drupal_set_message(t('Your password and further instructions have been sent to your e-mail address.'));
- $form_state['redirect'] = 'user';
- _caubo_clean_form_state($form_state);
- return;
- }
- }
- else {
- // Create new user account, administrator approval required.
- _user_mail_notify('register_pending_approval', $account);
- drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your e-mail address.'));
- $form_state['redirect'] = 'user';
- _caubo_clean_form_state($form_state);
- return;
- }
- }
- }
- function _caubo_clean_form_state(&$form_state) {
- $form_state['storage'] = array();
- unset($form_state['content_profile_registration'], $form_state['values']);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement