function invite_form_user_profile_form_alter(&$form, &$form_state){
  $account = $form['#user'];

  if (user_access('administer invitations')) {
  	 $invite = db_select('invite', 'i')
      ->fields('i')
      ->condition('invitee', $account->uid)
      ->execute()
      ->fetchObject();
      
    $default_value = '';
    if(!empty($invite)) {
    	$inviter = user_load($invite->uid);
    	$default_value = $inviter->mail;
    }
  	
  	$form['inviter'] = array(
  		'#type' => 'textfield',
  	    '#title' => t('Inviter Email'),
  		'#default_value' => $default_value,
  	    '#element_validate' => array('invite_inviter_validate'),
  	);  
  	if (!empty($default_value)) {
  		$form['inviter']['#disabled'] = TRUE;
  	}	
  }   
  $form['#submit'] = array('invite_user_form_submit');
}

function invite_inviter_validate($element, &$form_state, $form) {
  if (!empty($element['#value'])) {
  	if (!valid_email_address($element['#value'])) {
      form_set_error('inviter', t('Invalid Email Address.'));
  	} else {
  	  $inviter = user_load_by_mail($form_state['values']['inviter']);
  	  if (empty($inviter)) {
  	  	 form_set_error('inviter', t('An account with that email address does not exist.'));
  	  } else {
  	  	$form_state['inviter_account'] = $inviter; 
  	  }
  	}
  }
 
}

function invite_user_form_submit($form, &$form_state) {
  $account = $form['#user'];

  if (empty($form['inviter']['#defaul_value']) && !empty($form_state['inviter_account'])) {
  	$inviter = $form_state['inviter_account'];
  	if(!empty($inviter->uid)) {
  	  $invite = invite_create();
      $invite->email = $account->mail;
      $invite->uid = $inviter->uid;
      $invite->invitee = $account->uid;
      $invite->joined = REQUEST_TIME;
      invite_save($invite);
  	}
  }  
}

If the module maintainers are interested in adding this feature, please reply to this and I'll submit a patch.

Comments

glekli’s picture

Hi solomongifford,

A patch would be most definitely welcome. I have just taken a brief look, but the code looks good, thanks. One thing that comes to mind is: you might want to use an autocomplete user name field instead of a text field for an email address. With validating the email against the user database, you are basically selecting a user here.

SolomonGifford’s picture

Hey, I thought about that. I think, however, that since the invite module is an "email" invitation module, it makes sense to connect by email. However, to be orthogonal, I see the point of the username. So, the attached patch allows a user to search by username or email but only the username is used in the lookup.

Patch attached.

glekli’s picture

Status: Needs review » Fixed

It seems to be working nicely. Well done, thanks. Committed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.