Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- * Code for the EDB User feature.
- */
- include_once 'edb_user.features.inc';
- include_once 'includes/edb_user.views.inc';
- /**
- * Implements hook_form_FORM_ID_alter().
- */
- function edb_user_form_user_profile_form_alter(&$form, &$form_state) {
- if (!user_access('administer users')) {
- // Hide legacy fields and other fields that should not be accessible to lower-level users
- $legacy_fields = array(
- 'field_legacy_project_timeframe',
- 'field_legacy_project_wherestatus',
- 'field_legacy_requires_database',
- 'field_init_value',
- 'field_forum_id_',
- 'field_user_last_campaign',
- 'field_user_last_asset',
- 'field_blog_author_avatar',
- # hiding these fields as well even though it's not a "legacy" field
- 'field_formid',
- 'flag_notify_field_defaults',
- );
- foreach ($legacy_fields as $legacy_field) {
- $form[$legacy_field]['#access'] = FALSE;
- }
- // Hide the blog author bio.
- $blog_author_fields = $form['#fieldgroups']['group_user_blog_author_info']->children;
- if (!empty($blog_author_fields)) {
- foreach ($blog_author_fields as $blog_author_field) {
- $form[$blog_author_field]['#access'] = FALSE;
- }
- }
- }
- }
- /**
- * Implements hook_form_FORM_ID_alter().
- */
- function edb_user_form_user_pass_alter(&$form, &$form_state){
- $form['actions']['submit']['#submit'][] = 'edb_user_pass_form_submit';
- }
- /**
- * custom submit handler for password reset page
- * @param $form
- * @param $form_state
- */
- function edb_user_pass_form_submit($form, &$form_state){
- global $language;
- $account = $form_state['values']['account'];
- // Mail one time login URL and instructions using current language.
- $mail = _user_mail_notify('password_reset', $account, $language);
- if (!empty($mail)) {
- watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
- drupal_set_message(t('If there is an account associated with (email address), you will receive an email with a link to reset your password.'));
- }
- $form_state['redirect'] = 'user';
- return;
- }
- /**
- * Implements hook_form_FORM_ID_alter().
- */
- function edb_user_form_user_register_alter(&$form, &$form_state){
- $excluded_options = array(
- 'Information Technology',
- 'Telecommunications',
- 'Healthcare Facilities/Services',
- 'Media/Marketing/Advertising',
- 'Insurance',
- 'Technology',
- 'Aerospace & Defense',
- 'Communications - Equipment/Services',
- 'Computer Software',
- 'Manufacturing- Hardware/Software',
- 'Public Sector',
- 'Communications - Equipment & Services',
- 'Financial Services',
- 'Education',
- 'Research/Development',
- 'Financial',
- );
- foreach ($excluded_options as $key) {
- $lang = $form['field_type_of_business']['#language'];
- unset($form['field_type_of_business'][$lang]['#options'][$key]);
- }
- //Hide Embargoed countries from Country options without removing already existing users of these countries.
- $country_excluded = array(
- 'BY','CU','ER','IR','KP','SY','VE','SD','SS',
- );
- foreach ($country_excluded as $country) {
- $lang = $form['field_postal_address']['#language'];
- unset($form['field_postal_address'][$lang][0]['country']['#options'][$country]);
- }
- $form['field_postal_address'][$lang][0]['country']['#empty_option'] = t('- Select -');
- $form['field_postal_address'][$lang][0]['country']['#required'] = TRUE;
- }
- /**
- * Implements hook_form_FORM_ID_alter().
- */
- function edb_user_form_user_register_form_alter(&$form, &$form_state) {
- // EDB-1001: Hide specific options from field, without removing them from existing records
- $excluded_options = array(
- 'Information Technology',
- 'Telecommunications',
- 'Healthcare Facilities/Services',
- 'Media/Marketing/Advertising',
- 'Insurance',
- 'Technology',
- 'Aerospace & Defense',
- 'Communications - Equipment/Services',
- 'Computer Software',
- 'Manufacturing- Hardware/Software',
- 'Public Sector',
- 'Communications - Equipment & Services',
- 'Financial Services',
- 'Education',
- 'Research/Development',
- 'Financial',
- );
- foreach ($excluded_options as $key) {
- $lang = $form['field_type_of_business']['#language'];
- unset($form['field_type_of_business'][$lang]['#options'][$key]);
- }
- //Hide Embargoed countries from Country options without removing already existing users of these countries.
- $country_excluded = array(
- 'BY','CU','ER','IR','KP','SY','VE','SD','SS',
- );
- foreach ($country_excluded as $country) {
- $lang = $form['field_postal_address']['#language'];
- unset($form['field_postal_address'][$lang][0]['country']['#options'][$country]);
- }
- $form['field_postal_address'][$lang][0]['country']['#empty_option'] = t('- Select -');
- $form['field_postal_address'][$lang][0]['country']['#required'] = TRUE;
- $form['actions']['submit']['#submit'][] = 'edb_user_register_form_submit';
- }
- /**
- * custom submit handler for user registration form
- * @param $form
- * @param $form_state
- */
- function edb_user_register_form_submit($form, &$form_state){
- $reg_pass_set = !variable_get('user_email_verification', TRUE);
- // Test here for a valid pre-auth -- if the pre-auth is set to the auth user, we
- // handle things a bit differently.
- $pre_auth = logintoboggan_validating_id() != DRUPAL_AUTHENTICATED_RID;
- // If we are allowing user selected passwords then skip the auto-generate function
- // The new user's status will be 1 (visitors can create own accounts) if reg_pass_set == 1
- // Immediate login, we are going to assign a pre-auth role, until email validation completed
- if ($reg_pass_set) {
- $pass = $form_state['values']['pass'];
- $status = 1;
- }
- else {
- $pass = user_password();
- $status = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS;
- }
- // The unset below is needed to prevent these form values from being saved as
- // user data.
- form_state_values_clean($form_state);
- // Set the roles for the new user -- add the pre-auth role if they can pick their own password,
- // and the pre-auth role isn't anon or auth user.
- $validating_id = logintoboggan_validating_id();
- $roles = isset($form_state['values']['roles']) ? array_filter($form_state['values']['roles']) : array();
- if ($reg_pass_set && ($validating_id > DRUPAL_AUTHENTICATED_RID)) {
- $roles[$validating_id] = 1;
- }
- $form_state['values']['pass'] = $pass;
- $form_state['values']['init'] = $form_state['values']['mail'];
- $form_state['values']['roles'] = $roles;
- $form_state['values']['status'] = $status;
- $account = $form['#user'];
- entity_form_submit_build_entity('user', $account, $form, $form_state);
- // Populate $edit with the properties of $account, which have been edited on
- // this form by taking over all values, which appear in the form values too.
- $edit = array_intersect_key((array) $account, $form_state['values']);
- $account = user_save($account, $edit);
- // Terminate if an error occurred during user_save().
- if (!$account) {
- drupal_set_message(t("Error saving user account."), 'error');
- $form_state['redirect'] = '';
- return;
- }
- $form_state['user'] = $account;
- $form_state['values']['uid'] = $account->uid;
- watchdog('user', 'New user: %name (%email).', array('%name' => $form_state['values']['name'], '%email' => $form_state['values']['mail']), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));
- // Add plain text password into user account to generate mail tokens.
- $account->password = $pass;
- // Compose the appropriate user message. Validation emails are only sent if:
- // 1. Users can set their own password.
- // 2. The pre-auth role isn't the auth user.
- // 3. Visitors can create their own accounts.
- $message = t('Thank you for registering with us!');
- if($reg_pass_set && $pre_auth && variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {
- $message = t('A validation e-mail has been sent to your e-mail address. You will need to follow the instructions in that message in order to gain full access to the site.');
- }
- if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {
- // Create new user account, no administrator approval required.
- $mailkey = 'register_no_approval_required';
- } elseif (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) {
- // Create new user account, administrator approval required.
- $mailkey = 'register_pending_approval';
- $message = t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />Once it has been approved, you will receive an e-mail containing further instructions.');
- }
- // Mail the user.
- _user_mail_notify($mailkey, $account);
- drupal_set_message($message);
- // where do we need to redirect after registration?
- $redirect = _logintoboggan_process_redirect(variable_get('logintoboggan_redirect_on_register', ''), $account);
- // Log the user in if they created the account and immediate login is enabled.
- if($reg_pass_set && variable_get('logintoboggan_immediate_login_on_register', TRUE)) {
- $form_state['redirect'] = logintoboggan_process_login($account, $form_state['values'], $redirect);
- }
- else {
- // Redirect to the appropriate page.
- $form_state['redirect'] = $redirect;
- }
- }
- /**
- * Implements hook_menu_alter().
- */
- function edb_user_menu_alter(&$items) {
- $items['admin/config/people/accounts/remove-field-country'] = array(
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('edb_user_remove_field_country'),
- 'type' => MENU_CALLBACK,
- 'access arguments' => array('administer nodes'),
- 'title' => 'Remove the deprecated field_country from the site',
- );
- }
- /**
- * Simple form to trigger removing "number of employees" field altogether
- * @param $form
- * @param $form_state
- */
- function edb_user_remove_field_country($form, &$form_state) {
- if(!isset($form_state['storage']['confirm'])) {
- $form['text'] = array(
- '#markup' => '<p>' . t('Use this form one time to completely remove the deprecated field "country".') . '</p>',
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Begin'),
- '#submit' => array('edb_user_remove_field_country_submit'),
- );
- return $form;
- } else {
- drupal_set_message('Are you sure that you want to completely remove the deprecated field "country"?');
- return confirm_form($form);
- }
- }
- /**
- * Validation function for form edb_user_remove_field_country
- * @param $form
- * @param $form_state
- */
- function edb_user_remove_field_country_validate ($form, &$form_state) {
- // if field_country has any data in it, form_set_error
- $query = "SELECT field_country_value FROM field_data_field_country WHERE field_country_value IS NOT NULL;";
- $number_rows = db_query($query)->rowCount();
- if ($number_rows !== 0) {
- form_set_error('submit', t('Cannot remove the Country field, as there is still data in it.'));
- }
- }
- /**
- * @param $form
- * @param $form_state
- */
- function edb_user_remove_field_country_submit($form, &$form_state) {
- if(!isset($form_state['storage']['confirm'])) {
- $form_state['storage']['confirm'] = TRUE;
- $form_state['rebuild'] = TRUE;
- } else {
- drupal_set_message('Purging "country" field from the database.');
- field_delete_field('field_country');
- // set a range large enough to delete all uses of this field
- $really_big_range = 9999999;
- field_purge_batch($really_big_range);
- drupal_set_message('Purge complete.');
- }
- }
- /**
- * Implements hook_action_info().
- */
- function edb_user_action_info() {
- return array(
- 'edb_user_action_migrate_field_country_values' => array(
- 'type' => 'user',
- 'label' => t('Migrate values from user field_country, which we are deprecating'),
- 'behavior' => array('changes_property'),
- 'configurable' => FALSE,
- 'vbo_configurable' => FALSE,
- 'triggers' => array('any'),
- ),
- );
- }
- /**
- * Action function to migrate some field_country values to field_postal_address
- * @param $entity
- * @param $context
- */
- function edb_user_action_migrate_field_country_values (&$entity, $context) {
- // field_country stored country name; field_postal_address stores
- // country data using ISO 2-character country codes - look up this row's
- // country code
- module_load_include('inc', 'locale', 'locale');
- $country_codes = country_get_list();
- // The official list has 'Russia' and 'South Korea' - db has variants of these
- $supplemental_country_codes = array('RU' => 'Russian Federation', 'KR' => 'Korea, Republic of');
- $lang = field_language('user', $entity, 'field_country');
- $country_code = array_search($entity->field_country[$lang][0]['value'], $country_codes);
- if (!$country_code) {
- $country_code = array_search($entity->field_country[$lang][0]['value'], $supplemental_country_codes);
- }
- $lang = field_language('user', $entity, 'field_postal_address');
- # only update field_postal_address if its country value is empty
- if (!empty($entity->field_postal_address) && $entity->field_postal_address[$lang][0]['country'] !== '' && $country_code) {
- $entity->field_postal_address[$lang][0]['country'] = $country_code;
- }
- # done with this user - unset field_country
- unset($entity->field_country[$lang][0]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement