Hi,
I used Webform module on my drupal site. I would like to go a little further with the additional processing:
I have a webform for users to send a mail to the administrator with attached file. Anonymous users can access this form. What I want to do is :
when the user sends the fom, PHP checks if his email adress is already in the database (table 'users'). if not, it creates a new user account with a temporary login (his name) and a temporary password (his email adress for exemple). The new user get an email with his account infos. Then he can loggin an modify his settings.
Is it possible with webform and additionla processing or is there a module that would do that?
So far here is the PHP code (I'm very new to PHP5 so it might be a little confused)

<?php
$verif = db_query("SELECT COUNT (mail AS user_mail) FROM {users} WHERE user_mail = ".$node->email_from_name);
  if($verif==0){
  echo 'nouvo';
$prenom = $node ->prenom;
echo $prenom;
  	$nouvo = db_query("INSERT INTO {users} (uid, name, pass, mail, init) VALUES ('','$prenom', '$mail', '$mail', '$mail')");
	 $destinataire= $mail;
	 $sujet = "Votre compte créaprint à été créé";
	 $headers = "From: MySite.com <info@mysite.com>\r\n";
	 $headers .="MIME-version: 1.0\r\n";
	 $headers .='Content-Type: text/html; charset="UTF-8"'."\n"; 
	 $message= "Bienvenue sur le site MySite.com. Votre compte est désormais actif. Pour vous connecter utilisez votre pr&eacute;nom comme login et votre e-mail comme mot de passe. Vous pourrez ensuite modifier ces donn&eacute;e dans votre profil.";
	 mail($destinataire, $sujet, $message, $headers);
	 
  }
?>

when I submit the form, I see the first echo, 'nouvo' but there nothing else happens (the new user is not created and no mail is sent).
Please help.
Thanks. Bee.

CommentFileSizeAuthor
#3 White_paper_workflow-proposed.pdf11.88 KBgeoffs52

Comments

kulfi’s picture

A few months old, but here's how I did it:

Code in Additional Validation:

<?php 

$userEmail = $form_values['submitted_tree']['general']['email'];

global $user;

$results = db_query("SELECT uid FROM {users} WHERE mail = '%s'", $userEmail);

while ($result = db_fetch_object($results)) {
	if ($result->uid && $user->uid != $result->uid) {
		form_set_error('email', 'An account with this email address already exists. Please login to submit this form.');
	}
}

?>

Code in Additional Processing:

<?php

$userEmail = $form_values['submitted_tree']['general']['email'];

$registration_values = array();
//$registration_values = array('mail' => $userEmail, 'name' => $userEmail);
$registration_values['mail'] = $userEmail;
$registration_values['name'] = $userEmail;
$registration_values['pass'] = user_password();
$registration_values['notify'] = TRUE;
//$registration_values['status'] = 1;  /* this doesn't work, it results in a watchdog error if an anonymous user submits */

user_register_submit('user_register', $registration_values);

?>

My 'email' component is in a 'general' fieldset. If yours isn't, it'll be $userEmail = $form_values['submitted_tree']['email'].

quicksketch’s picture

Status: Active » Closed (fixed)

Closing after lack of activity. Thanks ulfk.

geoffs52’s picture

Version: 5.x-1.7 » 6.x-2.7
Assigned: nina_bee » Unassigned
Status: Closed (fixed) » Active
Issue tags: +user registration, +access control, +webform, +additional processing
StatusFileSize
new11.88 KB

Can we open this back up? I'm looking to do exactly this.

I have a Webform for people to download a PDF file. I only want authenticated users to access the PDF file so I set up the Private Upload module to handle that. Works great. I even followed the great HowTo to set the default values (http://drupal.org/node/296453) of the fields in the form that match the Profile fields in the site registration so that an already logged in, authenticated user wouldn't have to fill out the form again - it would just be pre-populated. But the problem is if an anonymous user fills out the Webform they can't get the PDF file because only authenticated users can have access to it. Then I thought, well I'll just create a new role called "Whitepaper" where people with that role assigned can access the protected PDF, but then in order to be given a role you have to be authenticated anyway. So I'm back to my original desire anyway which is to have people register to download a PDF (of research) while at the same time I essentially sign them up for the site as a whole (as an authenticated user).

Use Case:
Anonymous user comes to the site and sees a promotion for a research whitepaper. They go to the landing page and decide it's something they want and fills out the Webform to download.

They fill out the form and it takes them to a page where they can access the PDF (or you could simply just take them to the PDF directly). At the same time, they are registered as an authenticated user of the site (we told them they would be on the form/landing page) and they get a confirmation email with instructions on how to change their auto-generated password (or we could have them assign a username & password on the Webform, which then becomes their site username and password).

While the attached is not exactly how I described above, it is a visual representation of what I would ultimately like to do.

I'm not a programmer, so not sure from the code above what variables I would need to change for my specific site or what is static for all sites. Anyone who could help would be appreciated. Or point me in a different way to accomplish what I need.

Thanks!

miiimooo’s picture

I like this approach though I think it could be improved as it leaves some problems open. Ideally, you would have a component type for this - let's call it user. Now, when someone goes to a webform and is not authenticated they get to fill in the drupal user registration form (would be even more ideal to customise and shorten this - e.g. automatic password and one e-mail address field only). They web form would just store the uid in its tables and create the drupal user. That way it should be possible to keep stuff in sync in case users change their name or their email address. Is there documentation on creating components somewhere?

damienmckenna’s picture

Why not use Content Profile to do this?

quicksketch’s picture

Status: Active » Closed (fixed)

Support with custom coding is not provided in the Webform issue queue.

rlnorthcutt’s picture

In case someone happens to stumble on this again, I had to modify the code above to work. This is pretty generic, and has some logic for already existing usernames:

Additional Validation:

<?php 

$userEmail = $form_values['submitted_tree']['email'];

global $user;

$results = db_query("SELECT uid FROM {users} WHERE mail = '%s'", $userEmail);

while ($result = db_fetch_object($results)) {
    if ($result->uid && $user->uid != $result->uid) {
        form_set_error('submitted][email', 'An account with this email address has already joined Reset San Francisco.');
    }
}

?>

Additional Processing:

<?php

$userEmail = $form_values['submitted_tree']['email'];
$userName = $form_values['submitted_tree']['first_name'].$form_values['submitted_tree']['last_name'];

$results = db_query("SELECT uid FROM {users} WHERE name = '%s'", $userName);

while ($result = db_fetch_object($results)) {
    if ($result->uid) {
        $num = rand(100, 999);
        $userName .= $num;
    }
}

$add_user = array();
$add_user['values']['name'] = $userName;
$add_user['values']['mail'] = $userEmail;
$add_user['values']['conf_mail'] = $userEmail;
$pass = user_password(8);
$add_user['values']['pass']['pass1'] = $pass;
$add_user['values']['pass']['pass2'] = $pass;
$add_user['values']['op'] = t('Create new account');
drupal_execute('user_register', $add_user);
?>

NOTE: I have a First Name and Last Name field on the form, and am using the combination of both as the username (JohnSmith). If that username already exists, then I add a random 3 digit number to the end (JohnSmith311). This should work most of the time.

Ron
Custom Websites