--- user_import.module-old	2007-12-04 15:56:15.000000000 -0500
+++ user_import.module	2007-12-04 15:58:40.000000000 -0500
@@ -6,6 +6,10 @@
  * Import users from a comma separated file (csv).
  */
 
+define(USER_IMPORT_NODEPROFILE_DEFAULT_AREACODE,'614');
+define(USER_IMPORT_NODEPROFILE_DEFAULT_STATE,'OH');
+define(USER_IMPORT_NODEPROFILE_DEFAULT_COUNTRY,'US');
+
 /**
  * - - - - - - - -  HOOKS - - - - - - - - 
  */
@@ -717,7 +721,7 @@
     $output .= '<p>' . t('<strong>CSV File:</strong> %file', array('%file' => $import['oldfilename'])) . '<br />';
     
     // get profile fields
-    $profile_fields = _user_import_profile('fid', 'title');
+    $profile_fields = array_merge(_user_import_profile('fid', 'title'),_user_import_nodeprofile());
     
     // add email address options
     $profile_fields['email'] = t('Email Address');
@@ -1082,7 +1086,7 @@
     $handle = _user_import_file_open($form['filepath']['#value'], $form['filename']['#value']);
     $data_row = _user_import_file_row($form['filename']['#value'], $handle);
 
-    $profile_fields = _user_import_profile('fid', 'title');
+    $profile_fields = array_merge(_user_import_profile('fid', 'title'),_user_import_nodeprofile());
 
     // add default and email address options
     $profile_fields[0] = '-------------';
@@ -1223,6 +1227,110 @@
     return;
 }
 
+function _user_import_save_nodeprofile($account, $fields) {
+
+//    error_log(print_r($fields,true));
+
+    foreach($fields as $fieldpath=>$fieldvalue) {
+        list($np,$contenttype,$fieldname,$fieldparam)=explode('/',$fieldpath);
+        if($np==='nodeprofile') {
+            $nodeprofile[$contenttype] = array('type'=>$contenttype);
+            if(!isset($values[$contenttype])) {
+                $values[$contenttype] = array();
+                $values[$contenttype]['status'] = 1;
+                $values[$contenttype]['name'] = $account->name;
+            }
+            if($fieldinfo=content_fields($fieldname,$contenttype)) {
+                switch($fieldinfo['type']) {
+                    case 'ca_phone':
+                        $v=preg_replace('/[^0-9]/','',$fieldvalue);
+                        if($v!='') {
+                            if(strlen($v)<10) {
+                                // give it a default area code
+                                $fieldvalue=USER_IMPORT_NODEPROFILE_DEFAULT_AREACODE.$fieldvalue;
+                            }
+                            $values[$contenttype][$fieldname][0][$fieldparam] = $fieldvalue;
+                        }
+                        break;
+                    case 'cck_fullname':
+                        if($fieldparam==='(fullname)') {
+                            // break the fullname apart into the components that cck_fullname expects
+                            $v=explode(' ',preg_replace('/\ +/',' ',trim($fieldvalue)),3);
+                            switch(count($v)) {
+                                case 1:
+                                    $values[$contenttype][$fieldname][0]['first']='';
+                                    $values[$contenttype][$fieldname][0]['middle']='';
+                                    $values[$contenttype][$fieldname][0]['last']=$v[0];
+                                    break;
+                                case 2:
+                                    $values[$contenttype][$fieldname][0]['first']=$v[0];
+                                    $values[$contenttype][$fieldname][0]['middle']='';
+                                    $values[$contenttype][$fieldname][0]['last']=$v[1];
+                                    break;
+                                case 3:
+                                    $values[$contenttype][$fieldname][0]['first']=$v[0];
+                                    $values[$contenttype][$fieldname][0]['middle']=$v[1];
+                                    $values[$contenttype][$fieldname][0]['last']=$v[2];
+                                    break;
+                            }
+                        } else {
+                            $values[$contenttype][$fieldname][0][$fieldparam] = $fieldvalue;
+                        }
+                        break;
+                    case 'cck_address':
+                        if(!isset($values[$contenttype][$fieldname][0]['street1'])) $values[$contenttype][$fieldname][0]['street1']='';
+                        if(!isset($values[$contenttype][$fieldname][0]['apt'])) $values[$contenttype][$fieldname][0]['apt']='';
+                        if(!isset($values[$contenttype][$fieldname][0]['street2'])) $values[$contenttype][$fieldname][0]['street2']='';
+                        if(!isset($values[$contenttype][$fieldname][0]['city'])) $values[$contenttype][$fieldname][0]['city']='';
+                        if(!isset($values[$contenttype][$fieldname][0]['state'])) $values[$contenttype][$fieldname][0]['state']=USER_IMPORT_NODEPROFILE_DEFAULT_STATE;
+                        if(!isset($values[$contenttype][$fieldname][0]['zip'])) $values[$contenttype][$fieldname][0]['zip']='';
+                        if(!isset($values[$contenttype][$fieldname][0]['country'])) $values[$contenttype][$fieldname][0]['country']=USER_IMPORT_NODEPROFILE_DEFAULT_COUNTRY;
+                        if(!isset($values[$contenttype][$fieldname][0]['other'])) $values[$contenttype][$fieldname][0]['other']='';
+                        if($fieldparam==='(citystatezip)') {
+                            // break the fullname apart into the components that cck_address expects
+                            $v=explode(' ',preg_replace('/[^A-Za-z0-9\'\-\ ]/','',preg_replace('/\ +/',' ',trim($fieldvalue))));
+                            switch(count($v)) {
+                                case 1:
+                                    if(is_numeric($v[0]))
+                                        $values[$contenttype][$fieldname][0]['zip']=$v[0];
+                                    else
+                                        $values[$contenttype][$fieldname][0]['city']=$v[0];
+                                    break;
+                                case 2:
+                                    $values[$contenttype][$fieldname][0]['city']=$v[0];
+                                    $values[$contenttype][$fieldname][0]['state']=$v[1];
+                                    break;
+                                default:
+                                    $values[$contenttype][$fieldname][0]['city']=implode(' ',array_slice($v,0,count($v)-2));
+                                    $values[$contenttype][$fieldname][0]['state']=$v[count($v)-2];
+                                    $values[$contenttype][$fieldname][0]['zip']=$v[count($v)-1];
+                            }
+                        } else {
+                            $values[$contenttype][$fieldname][0][$fieldparam] = $fieldvalue;
+                        }
+                    default:
+                        if($fieldinfo['multiple']=='1') {
+                            // if this is a multivalue field, assume that the CSV field is itself comma-separated
+                            foreach(explode(', ',$fieldvalue) as $v) {
+                                $values[$contenttype][$fieldname]['keys'][$v] = $v;
+                            }
+                        } else {
+                            $values[$contenttype][$fieldname][0][$fieldparam] = $fieldvalue;
+                        }
+                }
+            }
+        }
+    }
+
+
+    foreach($nodeprofile as $contenttype=>$node) {
+        error_log($node['type'].':'.print_r($values[$contenttype],true));
+        drupal_execute($contenttype.'_node_form', $values[$contenttype], $node);
+    }
+
+	return;
+}
+
 // Send email when account is created    
 function _user_import_send_email($account, $password, $profile, $subject, $body, $format, $css, $subscribed) {
 
@@ -1321,9 +1429,57 @@
     return;
 }
 
+function _user_import_nodeprofile() {
+
+    if (!module_exists('nodeprofile')) return array();
+
+//print "<pre>";
+    // build a list of available cck fields for nodeprofile-approved content types
+    $fields=array();
+    foreach(nodeprofile_get_types() as $contenttype) {
+        foreach(content_fields() as $fieldname=>$fieldinfo) {
+            if($fieldinfo=content_fields($fieldname,$contenttype->type)) {
+                $fieldpath='nodeprofile/'.$contenttype->type.'/'.$fieldname;
+                $fieldlabel='['.$contenttype->name.'] '.$fieldinfo['widget']['label'];
+//print $fieldinfo['type']."<br>";
+                switch($fieldinfo['type']) {
+                    case 'cck_fullname':
+                        $fields[$fieldpath.'/(fullname)'] = $fieldlabel.': (full name)';
+                        $fields[$fieldpath.'/first'] = $fieldlabel.': First';
+                        $fields[$fieldpath.'/middle'] = $fieldlabel.': Middle';
+                        $fields[$fieldpath.'/last'] = $fieldlabel.': Last';
+                        break;
+                    case 'cck_address':
+                        $fields[$fieldpath.'/(citystatezip)'] = $fieldlabel.': (city/state/zip)';
+                        $fields[$fieldpath.'/street1'] = $fieldlabel.': Street 1';
+                        $fields[$fieldpath.'/apt'] = $fieldlabel.': Apartment';
+                        $fields[$fieldpath.'/street2'] = $fieldlabel.': Street 2';
+                        $fields[$fieldpath.'/city'] = $fieldlabel.': City';
+                        $fields[$fieldpath.'/state'] = $fieldlabel.': State';
+                        $fields[$fieldpath.'/zip'] = $fieldlabel.': ZIP';
+                        $fields[$fieldpath.'/country'] = $fieldlabel.': Country';
+                        $fields[$fieldpath.'/other'] = $fieldlabel.': Other';
+                        break;
+                    case 'text':
+                        if($fieldinfo['widget']['type'] === 'options_select') {
+                            $fields[$fieldpath.'/key'] = $fieldlabel;
+                        } else {
+                            $fields[$fieldpath.'/value'] = $fieldlabel;
+                        }
+                        break;
+                    default:
+                        $fields[$fieldpath.'/value'] = $fieldlabel;
+                }
+            }
+        }
+    }
+
+    return $fields;
+}
+
 function _user_import_profile($key = 'fid', $return_value = NULL) {
     
-    if (!module_exists('profile')) return; 
+    if (!module_exists('profile')) return array();
     
     static $fields_static;
     
@@ -1541,7 +1697,7 @@
                     
                     if ($setting['field_match'] == 'password') $password = trim($data[$column_id]);
             
-                    if ($setting['field_match'] != 0 && $setting['field_match'] != 'email' && $setting['field_match'] != 'password') $fields[$setting['field_match']] = $data[$column_id];
+                    if ($setting['field_match'] !== 0 && $setting['field_match'] !== 'email' && $setting['field_match'] !== 'password') $fields[$setting['field_match']] = $data[$column_id];
                     
                     if ($setting['username'] > 0) {
                             
@@ -1583,8 +1739,13 @@
                      
                         // import info to profile
                         if (is_array($fields)) {
-                            while (list ($fid, $content) = each ($fields)) {
-                                _user_import_save_profile($fid, $account->uid, trim($content));
+                            if (module_exists('profile')) {
+                                while (list ($fid, $content) = each ($fields)) {
+                                    _user_import_save_profile($fid, $account->uid, trim($content));
+                                }
+                            }
+                            if (module_exists('nodeprofile')) {
+                                _user_import_save_nodeprofile($account, $fields);
                             }
                         }
     
