Index: volunteer.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/volunteer/volunteer.module,v
retrieving revision 1.53
diff -u -r1.53 volunteer.module
--- volunteer.module	12 Feb 2006 00:59:52 -0000	1.53
+++ volunteer.module	21 Feb 2006 06:10:14 -0000
@@ -335,88 +335,63 @@
 
 /********************************/
 
+/**
+ * Check to see if a user is already signed up
+ * for the event in question
+ *
+ * @param int $nid node_id for the event
+ * @param int $contact_id CiviCRM contact_id for the user
+ */
+
+function volunteer_has_user_volunteered($nid, $contact_id){
+  $sql = 'SELECT cid FROM {volunteer_contact_event} WHERE cid = %d AND nid = %d';
+  return db_num_rows(db_query($sql, $contact_id, $nid)) > 0;
+}
+
 function volunteer_volunteer($nid, $wait_list = NULL) {
   global $user;
-
+  $edit = $_POST['edit'];
+  $op = $_POST['op'];
+  
   civicrm_initialize(TRUE);
-  // Not a Drupal form
-  if ($_POST['email'] || $_POST['op'] == t('Volunteer')) {
-    $edit = $_POST;
-    if (isset($edit['email'])) {
-      $edit['mail'] = $edit['email'];
-    }
-    else {
-      $edit['email'] = $user->mail;
-      $edit['mail'] = $edit['email'];
-    }
-  }
-  $mail = $user->mail ? $user->mail : $edit['email'];
-
-  db_set_active('civicrm');
+  // Not a Drupal form, so we need to let CRM have a crack
+  //at it before we do
+  $crm_params = array(); //user data if any
+  $crm_form = volunteer_volunteer_form($nid, $crm_params);
   // check if the user has already signed up
-  $contact = crm_get_contact(array('email' => $mail));
-  db_set_active();
-  if (strtolower(get_class($contact)) == 'crm_contact_bao_contact' && db_num_rows(db_query('SELECT cid FROM {volunteer_contact_event} WHERE cid = %d AND nid = %d', $contact->contact_type_object->contact_id, $nid))) {
-    drupal_set_message(t('You have already signed up for this event.'), 'error');
-    drupal_goto('node/'. $nid);
+  $contact_id = NULL;
+  if ($crm_params['contact_id']) {
+    //CRM knows about this user.  Is he/she signed up?
+    $contact_id = $crm_params['contact_id'];
+    if (volunteer_has_user_volunteered($nid, $contact_id)) {
+      drupal_set_message(t('You have already signed up for this event.'), 'error');
+      drupal_goto('node/'. $nid);
+    }
   }
   // the form has been submitted
-  else if(count($edit)) {
-    // Check data
-    db_set_active('civicrm');
-    $title = crm_uf_get_profile_title(variable_get('volunteer_form', ''));
-    if ($user->uid) {
-      civicrm_validate_data($edit, $user, $title);
-    }
-    db_set_active();
-    if (!form_get_errors()) {
+  if (count($edit) && $contact_id && !form_get_errors()) {
       // Go ahead and save/update and proceed
       //insert into volunteer_contact_event
-      if ($user->uid) {
-        db_set_active('civicrm');
-        $userID = crm_uf_get_match_id( $user->uid );
-        if ($userID) {
-          civicrm_form_data($edit, $user, $title, false);
-          $contact = crm_get_contact(array('contact_id' => $userID));
-        }
-        else {
-          crm_create_contact($edit);
-          $contact = crm_get_contact(array('email' => $user->mail));
-        }
-        db_set_active();
-      }
-      else {
-        db_set_active('civicrm');
-        crm_create_contact($edit);
-        $contact = crm_get_contact(array('email' => $edit['email']));
-        db_set_active();
-      }
+      $contact = crm_get_contact(array('contact_id' => $contact_id));
       $group_id = variable_get('volunteer_contact_source', 1);
-      db_set_active('civicrm');
       $group = crm_get_groups(array('id' => $group_id));
-      db_set_active();
       $group = $group[0];
-      db_set_active('civicrm');
       $contact_array = array($contact);
       crm_add_group_contacts($group, $contact_array);
-      db_set_active();
       $stage = $wait_list ? 1 : 2;
-      db_query('INSERT INTO {volunteer_contact_event} (cid, nid, timestamp, stage) VALUES (%d, %d, %d, %d)', $contact->contact_type_object->contact_id, $nid, time(), $stage);
-      volunteer_mail($contact->id, $nid, 'new_volunteer');
+      db_query('INSERT INTO {volunteer_contact_event} (cid, nid, timestamp, stage) VALUES (%d, %d, %d, %d)', $contact_id, $nid, time(), $stage);
+      volunteer_mail($contact_id, $nid, 'new_volunteer');
       drupal_set_message(variable_get('volunteer_message_thanks', t('Thank you for offering to volunteer! Someone will be in touch with you shortly with details.')));
       drupal_goto('node/'. $nid);
-    }
-    else {
+   }
+   else {
+     // display form
       $event = node_load(array('nid' => $nid));
-      $output = theme('box', t('Volunteer for %event', array('%event' => theme('placeholder', $event->title))), volunteer_volunteer_form($nid, $edit));
-    }
-  }
-  else {
-    // display form
-    $event = node_load(array('nid' => $nid));
-    $output = '<p>'. variable_get('volunteer_message_sign_up', t('Fill out this quick form to volunteer for this event.')) .'</p>';
-    $output .= volunteer_volunteer_form($nid);
-    $output = theme('box', t('Volunteer for %event', array('%event' => theme('placeholder', $event->title))), $output);
+      $output = '<p>'. 
+      variable_get('volunteer_message_sign_up', 
+        t('Fill out this quick form to volunteer for this event.')) .'</p>';
+      $output .= $crm_form;
+      $output = theme('box', t('Volunteer for %event', array('%event' => theme('placeholder', $event->title))), $output);
   }
   drupal_set_title('Volunteering');
   print theme('page', $output);
@@ -424,35 +399,71 @@
 
 /********************************/
 
-function volunteer_volunteer_form($nid, $edit = array()) {
-  global $user;
 
-  db_set_active('civicrm');
+/**
+ * Utility function to extract CRM email data from POST
+ * This should probably move into civicrm.module...
+ */
+function _volunteer_get_email(){
+  if(isset($_POST['op'])){
+  	foreach($_POST as  $key => $val){
+  	  if(strncmp($key, 'email', 5) == 0){
+  	  	//We only need 1 email
+  	  	return $val;
+  	  }
+  	}
+  }
+  return '';
+}
+
+
+function volunteer_volunteer_form($nid, &$edit) {
+  global $user;
+  //why factor this? Because the HTML 1.0 designers did not
+  //care about I18n...
+  $submit_value = t('Volunteer');
+  //Preliminaries for getting CRM parameters
+  $op = $_POST['op'];
   $title = crm_uf_get_profile_title(variable_get('volunteer_form', ''));
-  db_set_active();
-  if ( $user->uid ) {
-    db_set_active('civicrm');
-    $userID = crm_uf_get_match_id( $user->uid );
-    $register = TRUE;
-    if (!$userID && count($edit)) {
-      civicrm_register_data($edit, $user, $title, FALSE);
-      $userID = crm_uf_get_match_id( $user->uid );
-      $register = FALSE;
-    }
-    $form = crm_uf_get_profile_html( $userID, $title, null, FALSE, $register );
-    db_set_active();
-    $form .= form_submit(t('Volunteer'));
-    $url = url("node/$nid");
+  $reset = $op ? FALSE : TRUE;
+  $register = FALSE; //always FALSE in 1.3 and later
+  $userID = $user->uid ? crm_uf_get_match_id( $user->uid ) : NULL;
+  $action = $userID ? CRM_CORE_ACTION_UPDATE : CRM_CORE_ACTION_ADD;
+
+  //The following call not only generates HTML, it also 
+  //interacts with the CRM data store:
+  $crm_form = crm_uf_get_profile_html( $userID, $title, $action, $register, $reset );
+  if ( !$userID && $op) {
+    //if everything went well, we have one now
+     $email = $user->mail ? $user->mail : _volunteer_get_email();
+     $params = array('email' => $email);
+      //$contact = crm_get_contact($params); 
+     $props = crm_contact_search($params); 	
+  }
+  elseif ($userID) {
+    $params = array('contact_id' => $userID);
+    $props = crm_contact_search($params);
+  }
+  
+  if ($props && $op == $submit_value) { 
+    //We've already done what was needed with CRM
+    //(?? what if there was an error), so do our business
+    //with the Drupal side code:
+    $param_list = array_values($props[0]);
+    $edit = $param_list[0]; 
+    //Do we need HTML in this case?
+    //I think not.
+    //Redirects should go here, most likely
+    return '';
   }
   else {
-    db_set_active('civicrm');
-    $_POST['postURL'] = url("volunteer/volunteer/$nid");
-    $form = crm_uf_get_create_html(TRUE);
-    db_set_active();
+    //Either an error occurred, or we haven't
+    //done anything yet.
+    $form = $crm_form;
+    $form .= form_submit(t('Volunteer'));
+    $url = url("volunteer/volunteer/$nid");
+    return form($form, 'post', $url);
   }
-  $url = url("volunteer/volunteer/$nid");
-
-  return form($form, 'post', $url);
 }
 
 
@@ -567,9 +578,7 @@
 
   $event = node_load(array('nid'=>$nid));
 
-  db_set_active('civicrm');
   $volunteer = crm_get_contact(array('contact_id' => $cid));
-  db_set_active();
   $volunteer_data = volunteer_get_volunteers($nid, TRUE, $cid);
   foreach($volunteer_data['data'][0] as $key => $field) {
     $volstats .= $volunteer_data['header'][$key] . ': ' . $field ."\n";
