.debug */ $_SERVER['HTTP_HOST'] = '127.0.0.1'; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; require_once './_import/common.inc'; require_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); civicrm_initialize(TRUE); set_time_limit(0); define('CIVICRM_DSN', 'mysql://username:password@localhost/database'); define('DOMAINID', 1); $import_fp = fopen('./_import/CNUFiltered'. $argv[1] .'.csv', 'r'); $payments_fp = fopen('./_import/CNUPayments.csv', 'r'); //$reject_fp = fopen('./_import/CNUReject.csv', 'w'); $p = array_flip(array( // simple lookup hash for our exploded lines. 'NOTEID', 'PRIMARYKEY', 'AMOUNT', 'DATE', 'TYPE', 'FUND', 'PAYMENTMETHOD', 'MEMBERCATEGORY', 'MEMBEREXPIREQTR', 'MEMBEREXPIREYR', 'MEMBERSTATUS', 'SENTRECEIPT', 'NOTE' )); $payments = array(); fgetcsv($payments_fp, 1000, ','); // skip header row while (($line = fgetcsv($payments_fp, 1000, ',')) !== FALSE) { $line = array_map('trim_data', $line); $payments[$line[$p['PRIMARYKEY']]][] = array( 'AMOUNT' => $line[$p['AMOUNT']], 'DATE' => $line[$p['DATE']], 'TYPE' => $line[$p['TYPE']], 'FUND' => $line[$p['FUND']], 'PAYMENTMETHOD' => $line[$p['PAYMENTMETHOD']], 'MEMBEREXPIREQTR' => $line[$p['MEMBEREXPIREQTR']], 'MEMBERSTATUS' => $line[$p['MEMBERSTATUS']], 'SENTRECEIPT' => $line[$p['SENTRECEIPT']], 'NOTE' => $line[$p['NOTE']] ); } fclose($payments_fp); unset($payments_fp); $n = array_flip(array( // simple lookup hash for our exploded lines. 'PRIMARYKEY', 'PREFIX', 'FNAME', 'LNAME', 'SUFFIX', 'JOBTITLE', 'COMPANY', 'MAILINGADDR1', 'MAILINGADDR2', 'CITY', 'STATE', 'ZIP', 'COUNTY', 'COUNTRY', 'PRIVPRIMPHONE', 'PRIVPRIMPHONEDESC', 'SECONDPHONE', 'SECONDPHONEDESC', 'TERTIARYPHONE', 'TERTIARYPHONEDESC', 'EMAIL', 'OCCUPATION', 'SORTOFPERSON', 'CONTACTTYPE', 'CHAPTER', 'CHAPTERPMT', 'MEMBERSINCEMONTH', 'MEMBERSINCEYEAR', 'MEMBERTHROUGHMONTH', 'MEMBERTHROUGHYEAR', 'MEMBERCATEGORY', 'METHODOFPAYMENT', 'AMOUNTPAID', 'NOJUNKMAIL' )); $custom_accreditation = crm_get_custom_field(array('label' => 'Accreditation')); $custom_accreditation_id = $custom_accreditation->id; $crm_accreditation_values = crm_get_option_values($custom_accreditation); $custom_accreditation_values = array(); while(list(, $value) = each($crm_accreditation_values)) { $custom_accreditation_values[] = $value['value']; } $custom_county = crm_get_custom_field(array('label' => 'Mailing County')); $custom_county_id = $custom_county->id; $custom_fund = crm_get_custom_field(array('label' => 'Fund')); $custom_fund_id = $custom_fund->id; $crm_fund_values = crm_get_option_values($custom_fund); $custom_fund_values = array(); while(list(, $value) = each($crm_fund_values)) { $custom_fund_values[] = $value['value']; } $custom_membership_category = crm_get_custom_field(array('label' => 'Membership Category')); $custom_membership_category_id = $custom_membership_category->id; $crm_membership_category_values = crm_get_option_values($custom_membership_category); $custom_membership_category_values = array(); while(list(, $value) = each($crm_membership_category_values)) { $custom_membership_category_values[] = $value['value']; } $custom_occupation = crm_get_custom_field(array('label' => 'Occupation')); $custom_occupation_id = $custom_occupation->id; $crm_occupation_values = crm_get_option_values($custom_occupation); $custom_occupation_values = array(); while(list(, $value) = each($crm_occupation_values)) { $custom_occupation_values[] = $value['value']; } $custom_since = crm_get_custom_field(array('label' => 'Member since')); $custom_since_id = $custom_since->id; $custom_through = crm_get_custom_field(array('label' => 'Member through')); $custom_through_id = $custom_through->id; unset($value, $custom_accreditation, $crm_accreditation_values, $custom_county, $custom_fund, $crm_fund_values, $custom_membership_category, $crm_membership_category_values, $custom_occupation, $crm_occupation_values, $custom_since, $custom_through); $account->uid = false; // for sending to user_save since user_save doesn't do a proper isset check on ->uid $user = user_load(array('uid' => 1)); // keeps Drupal from whining in the Admin logs even though user_save will work anyway $debug_count = 0; $phone_temp = new CRM_Core_DAO_Phone(); $main_email = new CRM_Core_DAO_Email(); while (($line = fgetcsv($import_fp, 1000, ',')) !== FALSE) { echo $line[$n['PRIMARYKEY']] ."\n"; // comforting visual output to show the script is working; helps with debugging, too $no_spam = false; if (isset($payments[$line[$n['PRIMARYKEY']]])) { // assumes all expire in 2007 $member_through_time = strtotime($payments[$line[$n['PRIMARYKEY']]][0][$p['DATE']]) + 31536000; if ($payments[$line[$n['PRIMARYKEY']]][0][$p['MEMBEREXPIREYR']] == 2008) { $member_through_time += 31536000; } } else { $member_through_time = strtotime($line[$n['MEMBERTHROUGHMONTH']] .' '. $line[$n['MEMBERTHROUGHYEAR']]); } if (strpos($line[$n['EMAIL']], '@') === FALSE) { // #438 has a phone number in the email field $line[$n['EMAIL']] = ''; } if (empty($line[$n['EMAIL']])) { if ($line[$n['PRIVPRIMPHONEDESC']] == 'Primary_email' || $line[$n['PRIVPRIMPHONEDESC']] == 'Alternate_email') { $line[$n['EMAIL']] = $line[$n['PRIVPRIMPHONE']]; } elseif ($line[$n['SECONDPHONEDESC']] == 'Primary_email' || $line[$n['SECONDPHONEDESC']] == 'Alternate_email') { $line[$n['EMAIL']] = $line[$n['SECONDPHONE']]; } elseif ($line[$n['TERTIARYPHONEDESC']] == 'Primary_email' || $line[$n['TERTIARYPHONEDESC']] == 'Alternate_email') { $line[$n['EMAIL']] = $line[$n['TERTIARYPHONE']]; } } if (!empty($line[$n['EMAIL']])) { // can't be an else of the above because the original if might set the email checked here // separate empty check needed to split extra emails for setting civicrm $ind_params spam // since the civicrm user is created right after the drupal user // #121 has two addresses, and drupal crashes the import when fed improper email format $line[$n['EMAIL']] = explode(', ', $line[$n['EMAIL']]); for($i=0, $j=count($line[$n['EMAIL']]); $i<$j; $i++) { if (stristr($line[$n['EMAIL']][$i], 'spam') !== FALSE) { $no_spam = TRUE; } // #2978 and others have _no_spam attached to the email, which Drupal kicks back $line[$n['EMAIL']][$i] = str_replace('_no_spam', '', $line[$n['EMAIL']][$i]); } } $drupal_user = array(); // passed to drupal's user_save $prefix_id = get_name_id($line[$n['PREFIX']], 'PREFIX'); $line[$n['SUFFIX']] .= $line[$n['SUFFIX']] == 'Jr' ? '.' : ''; $suffix_id = get_name_id($line[$n['SUFFIX']], 'SUFFIX'); if ($line[$n['PREFIX']] == 'Mr.') { $gender = 'Male'; } elseif ($line[$n['PREFIX']] == 'Ms.' || $line[$n['PREFIX']] == 'Mrs.') { $gender = 'Female'; } else { $gender = NULL; } /** * leads to follow on getting civicrm to register info: * * @link http://wiki.civicrm.org/confluence/display/CRM/Contact+APIs - crm_create_contact(), crm_create_profile_contact() */ $ind_params = array( 'prefix_id' => $prefix_id ? $prefix_id : NULL, 'first_name' => $line[$n['FNAME']], 'last_name' => $line[$n['LNAME']], 'suffix_id' => $suffix_id ? $suffix_id : NULL, // 'email' => $line[$n['EMAIL']], // does this really belong here? 'job_title' => $line[$n['JOBTITLE']], 'do_not_phone' => 0, 'do_not_trade' => 0, 'gender' => $gender, // "No junk mail" means Do not mail. // Also, any record with "spam" in the email field should have "Do not email" checked. // stripos would be faster, but is php5-only 'do_not_email' => $no_spam ? 1 : 0, 'do_not_mail' => empty($line[$n['NOJUNKMAIL']]) ? 0 : 1 ); if (!empty($line[$n['EMAIL']])) { // things as if the user is already part of the system, not new. // this is so we have control over the inserted uid in {users}. $drupal_user['name'] = $line[$n['FNAME']] .' '. $line[$n['LNAME']]; $drupal_user['pass'] = mt_rand(10000000, 2147483647); $drupal_user['mail'] = $line[$n['EMAIL']][0]; $drupal_user['job_title'] = $line[$n['JOBTITLE']]; $drupal_user['street_address-2'] = $line[$n['MAILINGADDR1']]; $drupal_user['supplemental_address_1-2'] = $line[$n['MAILINGADDR2']]; $drupal_user['status'] = 1; $drupal_user['init'] = $line[$n['EMAIL']][0]; if ($member_through_time > time()) { $drupal_user['roles'] = array(3); // cnu member } //$drupal_user['created'] ? // if user "through" membership is expired, disable the account /** * user_save loads the civicrm_user hook with the insert type * then the automatic civicrm hook creates a contact in the civicrm_contact * table and links the civicrm_contact and drupal userid with a new record in * the civicrm_uf_match table * * civicrm_uf_match mini erd: * id -> auto_increment key for mysql; may not necessarily match increments in other tables * uf_id one->one drupal users table id * contact_id many->1 id in civicrm_contact */ $drupal_user = user_save($account, $drupal_user, 'account'); // user added perfectly. uh-huh. $userid = crm_uf_get_match_id($drupal_user->uid); unset($drupal_user); $contact = crm_get_contact(array('contact_id' => $userid, 'email' => $line[$n['EMAIL']][0])); // the engine for this is CRM_Contact_BAO_Contact::create, which fires CRM_Contact_BAO_Contact::add crm_update_contact($contact, $ind_params); } else { $contact = crm_create_contact($ind_params, 'Individual'); $userid = $contact->id; } // accreditation // using empty() before in_array() might speed things up since there are more empties than there are suffixes $suffix_for_db = array(); if (!empty($line[$n['SUFFIX']])) { $line[$n['SUFFIX']] = preg_replace('/\s\s+/', ' ', $line[$n['SUFFIX']]); if (strpos($line[$n['SUFFIX']], ', ')) { $line[$n['SUFFIX']] = explode(', ', $line[$n['SUFFIX']]); } else { $line[$n['SUFFIX']] = explode(' ', $line[$n['SUFFIX']]); } while(list(, $suffix) = each($line[$n['SUFFIX']])) { $suffix = str_replace(array('Ph.D.', 'PhD', 'P.H.D'), 'Ph. D.', $suffix); if ($suffix == 'P.E.' || $suffix == 'P.E') { $suffix = 'PE'; } elseif ($suffix == 'Ph.D') { // Ph.D == Ph.D. in str_replace $suffix = 'Ph. D.'; } if (in_array($suffix, $custom_accreditation_values)) { $suffix_for_db[] = $suffix; } } $suffix_db_count = count($suffix_for_db); if ($suffix_db_count == 1) { civicrm_db_query("INSERT INTO civicrm_custom_value (custom_field_id, entity_table, entity_id, char_data) VALUES (%d, 'civicrm_contact', %d, '%s')", $custom_accreditation_id, $userid, $suffix_for_db[0]); } elseif ($suffix_db_count > 1) { civicrm_db_query("INSERT INTO civicrm_custom_value (custom_field_id, entity_table, entity_id, char_data) VALUES (%d, 'civicrm_contact', %d, '%s')", $custom_accreditation_id, $userid, implode("", $suffix_for_db)); } unset($suffix, $suffix_for_db, $suffix_db_count); } if ($line[$n['CONTACTTYPE']] == 'List') { assign_tag($userid, 'List'); } // membership category if (in_array($line[$n['MEMBERCATEGORY']], $custom_membership_category_values)) { civicrm_db_query("INSERT INTO civicrm_custom_value (custom_field_id, entity_table, entity_id, char_data) VALUES (%d, 'civicrm_contact', %d, '%s')", $custom_membership_category_id, $userid, $line[$n['MEMBERCATEGORY']]); } // occupation civicrm_db_query("INSERT INTO civicrm_custom_value (custom_field_id, entity_table, entity_id, char_data) VALUES (%d, 'civicrm_contact', %d, '%s')", $custom_occupation_id, $userid, in_array($line[$n['OCCUPATION']], $custom_occupation_values) ? $line[$n['OCCUPATION']] : 'Other'); // mailing county civicrm_db_query("INSERT INTO civicrm_custom_value (custom_field_id, entity_table, entity_id, char_data) VALUES (%d, 'civicrm_contact', %d, '%s')", $custom_county_id, $userid, $line[$n['COUNTY']]); if (!empty($line[$n['MEMBERSINCEMONTH']]) && !empty($line[$n['MEMBERSINCEYEAR']])) { civicrm_db_query("INSERT INTO civicrm_custom_value (custom_field_id, entity_table, entity_id, date_data) VALUES (%d, 'civicrm_contact', %d, '%s')", $custom_since_id, $userid, date('Y-m-d', strtotime($line[$n['MEMBERSINCEMONTH']] .' '. $line[$n['MEMBERSINCEYEAR']]))); } if (!empty($line[$n['MEMBERTHROUGHMONTH']]) && !empty($line[$n['MEMBERTHROUGHYEAR']])) { civicrm_db_query("INSERT INTO civicrm_custom_value (custom_field_id, entity_table, entity_id, date_data) VALUES (%d, 'civicrm_contact', %d, '%s')", $custom_through_id, $userid, date('Y-m-d', $member_through_time)); } // tag prospects from contact type if ($line[$n['CONTACTTYPE']] == 'Prospect') { assign_tag($userid, $line[$n['CONTACTTYPE']]); } assign_tag($userid, $line[$n['SORTOFPERSON']]); // needs to be before the processing of payments.xls in case their payment adds them back to the group if (!empty($line[$n['CHAPTER']])) { $line[$n['CHAPTER']] = explode(', ', $line[$n['CHAPTER']]); // all because of member #12610 while(list(, $value) = each($line[$n['CHAPTER']])) { $group_object = crm_get_groups(array('title' => $value), NULL); if (isset($group_object[0]) && !is_a($group_object, 'CRM_Core_Error')) { $group_contacts = array($contact); crm_add_group_contacts($group_object[0], $group_contacts, $member_through_time > time() ? 'Added' : 'Removed', 'API'); } } } $phone_results = array(); // civicrm makes the last API entry the bolded primary in the GUI, so process the primary last $phone_results['TERTIARYPHONE'] = contact_logic($line[$n['TERTIARYPHONEDESC']]); $phone_results['SECONDPHONE'] = contact_logic($line[$n['SECONDPHONEDESC']]); $phone_results['PRIVPRIMPHONE'] = contact_logic($line[$n['PRIVPRIMPHONEDESC']]); $main_phone = array(); $phone_columns = array_keys($phone_results); $size = count($phone_columns); for($i=0; $i<$size; $i++) { if ($phone_results[$phone_columns[$i]][0] == 'Main' && $phone_results[$phone_columns[$i]][1] != 'Email' && !empty($line[$n[$phone_columns[$i]]])) { $phone_temp->phone = $line[$n[$phone_columns[$i]]]; $phone_temp->phone_type = $phone_results[$phone_columns[$i]][1]; $main_phone[] = $phone_temp; } } $work_phone = array(); for($i=0; $i<$size; $i++) { if ($phone_results[$phone_columns[$i]][0] == 'Work' && $phone_results[$phone_columns[$i]][1] != 'Email' && !empty($line[$n[$phone_columns[$i]]])) { $phone_temp->phone = $line[$n[$phone_columns[$i]]]; $phone_temp->phone_type = $phone_results[$phone_columns[$i]][1]; $work_phone[] = $phone_temp; } } $home_phone = array(); for($i=0; $i<$size; $i++) { if ($phone_results[$phone_columns[$i]][0] == 'Home' && $phone_results[$phone_columns[$i]][1] != 'Email' && !empty($line[$n[$phone_columns[$i]]])) { $phone_temp->phone = $line[$n[$phone_columns[$i]]]; $phone_temp->phone_type = $phone_results[$phone_columns[$i]][1]; $home_phone[] = $phone_temp; } } $email = array(); if (!empty($line[$n['EMAIL']])) { for($m=0, $e=count($line[$n['EMAIL']]); $m<$e; $m++) { $main_email->email = $line[$n['EMAIL']][$m]; $email[] = $main_email; } unset($m, $e); } for($i=0; $i<$size; $i++) { if ($phone_results[$phone_columns[$i]][1] == 'Email' && !empty($line[$n[$phone_columns[$i]]])) { $main_email->email = $line[$n[$phone_columns[$i]]]; $email[] = $main_email; } } unset($phone_results); $postal_code_suffix = NULL; if (preg_match('/^(\d{5})-(\d{4})$/', $line[$n['ZIP']], $postal_code)) { $postal_code_suffix = $postal_code[2]; $postal_code = $postal_code[1]; } elseif (is_numeric($line[$n['ZIP']]) && strlen($line[$n['STATE']]) == 2) { // some contacts have the zero stripped off the front like #13100 // using http://zip4.usps.com/zip4/citytown_zip.jsp and padding 0 on the front, cities match the partial zips $postal_code = str_pad($line[$n['ZIP']], 5, '0', STR_PAD_LEFT); $postal_code_suffix = NULL; } else { $postal_code = $line[$n['ZIP']]; $postal_code_suffix = NULL; } $location_params = array( 'email' => $email, 'phone' => $main_phone, 'street_address' => $line[$n['MAILINGADDR1']], 'supplemental_address_1' => $line[$n['MAILINGADDR2']], 'city' => $line[$n['CITY']], 'postal_code' => $postal_code, // should be split into postal_code_suffix for +4 'postal_code_suffix' => $postal_code_suffix, 'state_province' => $line[$n['STATE']], 'country' => $line[$n['COUNTRY']], 'is_primary' => true, 'location_type' => 'Main' ); $dues_contribution_params = array(); if (!empty($line[$n['AMOUNTPAID']])) { $comped = false; $sponsor = false; if (stristr($line[$n['AMOUNTPAID']], 'comp') !== FALSE) { $comped = true; } if (stristr($line[$n['AMOUNTPAID']], 'spon') !== FALSE) { $sponsor = true; assign_tag($userid, 'Sponsor'); } $line[$n['AMOUNTPAID']] = sprintf("%01.2f", preg_replace('/[^0-9\.]/i', '', $line[$n['AMOUNTPAID']])); $dues_contribution_params = array( 'domain_id' => DOMAINID, // see civicrm_domain 'contact_id' => $userid, 'contribution_type_id' => 2, // "Member Dues" 'contribution_type' => 'Member Dues', // doesn't seem to do anything except keep civicrm from complaining about not having a valid contribution_type 'payment_instrument_id' => $comped ? 6 : payment_logic($line[$n['METHODOFPAYMENT']]), // we don't really know this and it's not required //'receive_date' => date('Y-m-d'), 'total_amount' => $line[$n['AMOUNTPAID']], 'note' => $sponsor ? 'Sponsor' : ''); crm_create_contribution($dues_contribution_params); } $chapter_contribution_params = array(); if (!empty($line[$n['CHAPTERPMT']])) { $line[$n['CHAPTERPMT']] = sprintf("%01.2f", preg_replace('/[^0-9\.]/i', '', $line[$n['CHAPTERPMT']])); $chapter_contribution_params = array( 'domain_id' => DOMAINID, // see civicrm_domain 'contact_id' => $userid, 'contribution_type_id' => 2, // "Member Dues" 'contribution_type' => 'Member Dues', // doesn't seem to do anything except keep civicrm from complaining about not having a valid contribution_type // we don't really know this and it's not required //'receive_date' => date('Y-m-d'), 'total_amount' => $line[$n['CHAPTERPMT']], 'note' => $line[$n['CHAPTER']]); crm_create_contribution($chapter_contribution_params); // fund here } // work with the data from CNUPayments.xls/csv $payment_contribution_params = array(); if (isset($payments[$line[$n['PRIMARYKEY']]])) { while(list(, $payment) = each($payments[$line[$n['PRIMARYKEY']]])) { if (!empty($payment['AMOUNT'])) { $payment['AMOUNT'] = sprintf("%01.2f", $payment['AMOUNT']); switch($payment['PAYMENTMETHOD']) { case 'Check': $payment_method = 4; break; case 'AHI': $payment_method = 7; break; case 'Credit card': $payment_method = 1; break; default: $payment_method = NULL; break; } $payment_contribution_params = array( 'domain_id' => DOMAINID, // see civicrm_domain 'contact_id' => $userid, 'contribution_type_id' => $payment['TYPE'] == 'Donation' ? 1 : 2, 'contribution_type' => $payment['TYPE'] == 'Donation' ? 'Donation' : 'Member Dues', 'payment_instrument_id' => $payment_method, 'receive_date' => date('Y-m-d', strtotime($payment['DATE'])), 'total_amount' => $payment['AMOUNT'], 'note' => $payment['NOTE']); $contrib_obj = crm_create_contribution($payment_contribution_params); // fund civicrm_db_query("INSERT INTO civicrm_custom_value (custom_field_id, entity_table, entity_id, char_data) VALUES (%d, 'civicrm_contribution', %d, '%s')", 19, $contrib_obj->id, in_array($payment['FUND'], $custom_fund_values) ? $payment['FUND'] : NULL); if ($payment['TYPE'] == 'Membership') { $group_object = crm_get_groups(array('title' => $payment['FUND']), NULL); if (isset($group_object[0]) && !is_a($group_object, 'CRM_Core_Error')) { $group_contacts = array($contact); $result = crm_add_group_contacts($group_object[0], $group_contacts, $member_through_time > time() ? 'Added' : 'Removed', 'API'); } } } // this is intentionally outside the amount if statement because the payments.csv // file has some member_expire_qtr and member_expire_yr without amount info civicrm_db_query("REPLACE INTO civicrm_custom_value (custom_field_id, entity_table, entity_id, date_data) VALUES (%d, 'civicrm_contact', %d, '%s')", $custom_through_id, $userid, date('Y-m-d', $member_through_time)); } } if (!empty($work_phone)) { crm_create_location($contact, array('location_type' => 'Work', 'is_primary' => false, 'phone' => $work_phone)); } if (!empty($home_phone)) { crm_create_location($contact, array('location_type' => 'Home', 'is_primary' => false, 'phone' => $home_phone)); } // update, not create // do this after the others to force it as the primary location if (empty($contact->location[1]->id)) { // no drupal acct crm_create_location($contact, $location_params); } else { // drupal acct created it, so just update it // if ($line[$n['PRIMARYKEY']] != 3988) { // causing: Fatal error: Call to a member function on a non-object in /usr/local/apache/htdocs/cnu/sites/default/modules/civicrm/packages/DB/DataObject.php on line 2309 // skip 3988 and it happens again on 3989 anyway... ARGH! crm_update_location($contact, $contact->location[1]->id, $location_params); // } } /* this doesn't work with a chunked import and isn't required by CNU, but it does properly create orgs and relate individuals to them if (!empty($line[$n['COMPANY']]) && $org_object = org_lookup($line[$n['COMPANY']])) { crm_create_relationship($contact, $org_object, 'Employee of', array()); } */ // die('debug halt'); /* $debug_count++; if ($debug_count > 4) { die('debug halt'); } */ } fclose($import_fp); /** * skips the civicrm API to insert tags for entities * * @param integer $userid * @param string $tag */ function assign_tag($userid, $tag) { static $sort_tags = array(); if (empty($sort_tags)) { $result = civicrm_db_query('SELECT id, name FROM civicrm_tag WHERE domain_id = %d ORDER BY id', DOMAINID); while($row = db_fetch_array($result)) { $sort_tags[$row['id']] = $row['name']; } unset($row); } // sometimes people have more than one designation like "Board" and "Chapter Chair" $tags = explode("\n", $tag); while(list(,$tag) = each($tags)) { if ($tag_id = array_search($tag, $sort_tags)) { return civicrm_db_query("INSERT INTO civicrm_entity_tag (entity_table, entity_id, tag_id) VALUES ('civicrm_contact', %d, %d)", $userid, $tag_id); } } return false; } /** * these need logic to parse out the description of each of the contact fields * * @param string $descr * @link http://wiki.civicrm.org/confluence/display/CRM/Contact+APIs#ContactAPIs-crmcreatecontact%28%24params%2C%24contacttype%3D%27Individual%27%29 * @return array location and type */ function contact_logic($descr) { // taking a guess that static is faster // nobody in ##php really knew when I asked static $main_phone_types = array( 'Alternate_phone', 'Car', 'Daytime_phone', 'Direct', 'General', 'Misc', 'Modem', 'Other', 'Pager', 'Phone', 'TTY_TDD', 'University_phone', 'Unknown', '?', 'Voicemail'); static $main_fax_types = array( 'Alternate_fax', 'Base_fax', 'Fax', 'Fax Attn Lt Sprun', 'Shared_fax', 'University_fax'); static $work_phone_types = array( 'Assistant', 'General_office_phone', 'Office_phone', 'Work', 'work', 'Work (@LSE)'); // try to put the more popular types at the top of the if chain to return quicker if (empty($descr)) { return array('', ''); } elseif (in_array($descr, $main_phone_types)) { return array('Main', 'Phone'); } elseif (in_array($descr, $main_fax_types)) { return array('Main', 'Fax'); } // here's to hoping strcasecmp is also faster than in_array elseif (!strcasecmp($descr, 'Cell_phone') || !strcasecmp($descr, 'Mobile')) { return array('Main', 'Mobile'); } elseif (!strcasecmp($descr, 'Office_fax') || !strcasecmp($descr, 'Office_fax02')) { return array('Work', 'Fax'); } elseif (in_array($descr, $work_phone_types)) { return array('Work', 'Phone'); } elseif ($descr == 'Home' || $descr == 'Home_phone') { return array('Home', 'Phone'); } elseif (!strcasecmp($descr, 'Home_fax') || !strcasecmp($descr, 'Home Fax')) { return array('Home', 'Fax'); } elseif (!strcasecmp($descr, 'Alternate_email') || !strcasecmp($descr, 'Primary_email')) { return array('Main', 'Email'); } elseif (!strcasecmp($descr, 'Pager')) { return array('Main', 'Pager'); } elseif (!empty($descr)) { return array('Main', 'Phone'); } else { return array('', ''); } } /** * Get prefix or suffix id from civicrm tables * * @param string $string * @param string $type PREFIX or SUFFIX */ function get_name_id($string, $type = 'PREFIX') { static $prefixs = array(); static $suffixs = array(); if (empty($prefixs)) { $result = civicrm_db_query('SELECT id, name FROM civicrm_individual_prefix ORDER BY id'); while($fix = db_fetch_array($result)) { $prefixs[$fix['id']] = $fix['name']; } $result = civicrm_db_query('SELECT id, name FROM civicrm_individual_suffix ORDER BY id'); while($fix = db_fetch_array($result)) { $suffixs[$fix['id']] = $fix['name']; } } // else add the prefix to the db and insert the new id? return array_search($string, $type == 'PREFIX' ? $prefixs : $suffixs); } /** * Tries to find a group from this import transaction * If it's not found, it creates one and returns the crm object * THIS IMPLEMENTATION COULD MAKE A MESS IF THE GROUPS ARE ALREADY IN THE DATABASE BEFORE THE IMPORT * * @deprecated * @param string $group * @return integer */ function group_lookup($group) { static $finished_groups = array(); $group_keys = array_keys($finished_groups); $size = count($group_keys); for($i=0; $i<$size; $i++) { // can't use array_search because this needs case insensitivity // which means this will probably be slow if (!strcasecmp($group, $group_keys[$i])) { return $finished_groups[$group_keys[$i]]; } } $group_params = array( 'title' => $group, 'is_active' => 1, 'visibility' => 'User and User Admin Only'); $crm_group = crm_create_group($group_params); $finished_groups[$group] = $crm_group; return $crm_group; } /** * The input file does have some payments without methods, so * return NULL for those * * 1: Credit Card * 2: Debit Card * 3: Cash * 4: Check * 5: EFT * 6: Comp * 7: AHI * * @param string $method * @return integer or NULL */ function payment_logic($method) { // some have weird stuff in with the same text as "Check" like a check number if (stristr($method, 'Check')) { return 4; } switch($method) { case 'Website (C.C.)': return 8; break; case 'AH': case 'AHI': return 7; break; case 'Amex': case 'Credit card': case 'credit card': case 'Mastercard': case 'Visa': case 'visa': return 1; break; case 'Check': return 4; break; case 'Comp': return 6; break; case 'Cash': case 'Money order': return 3; break; default: // either no method, comp, or sponsor return NULL; break; } } /** * Tries to find an org from this import transaction * If it's not found, it creates one and returns the contact id * THIS IMPLEMENTATION COULD MAKE A MESS IF THE ORGS ARE ALREADY IN THE DATABASE BEFORE THE IMPORT * * @param string $org * @return integer */ function org_lookup($org) { static $finished_orgs = array(); $org = preg_replace('/\s\s+/', ' ', $org); $org_keys = array_keys($finished_orgs); $size = count($org_keys); for($i=0; $i<$size; $i++) { // can't use array_search because this needs case insensitivity // which means this will probably be slow if (!strcasecmp($org, $org_keys[$i])) { reset($finished_orgs); // remember it's static return $finished_orgs[$org_keys[$i]]; } } $org_params = array('organization_name' => $org); $crm_org = crm_create_contact($org_params, 'Organization'); $finished_orgs[$org] = $crm_org; return $crm_org; } /** * don't think you need to strip with fgetcsv, but maybe trim * * @return string */ function trim_data($value) { return trim($value); }