private function contact_create($mode = 'import') { $this->import_status_update('contact'); // bug fix for $this->contacts keep being appended if (!empty($this->contacts)) { unset($this->contacts); $this->contacts = array(); } for ($i = 0; $i < count($this->csv->data); $i++) { $param = array('version' => '3', 'contact_type' => 'Individual'); if ($mode == 'import') { $param['dupe_check'] = $this->data->dupe_check == 1 ? TRUE : FALSE; // #FIX issue 87: auto-append contact source if it is not mapped // appending the date and the job id to the import if (!in_array('contact_source', $this->contacts)) { $param['contact_source'] = date('Ymd') . '_' . $this->data->jobid; } // #FIX issue 72: Add email only de-dupe feature if ($this->data->dupe_check == 2) { $mapping = array_flip($this->contact_data); $email = $this->csv->data[$i][$mapping['email']]; $contact_id = $this->check_email($email); if (is_numeric($contact_id)) { // for email only dedupe, we delete the param and skip this iteration $this->log->_log('Error on CSV line ' . $i . ':, (Matching contact found:, ' . $contact_id . '),' . implode(',', $this->csv->data[$i]), 'error_csv'); unset($param); continue; } } } // in case we are doing an append with *ONLY* Location data we still need to fill $this->contact if (count($this->contact_data) == 1) { $id = array_values($this->contact_data); $column = array_keys($this->contact_data); if ($id[0] == 'external_identifier') { $contact_id = $this->fetch_contact_id($this->csv->data[$i][$column[0]]); } else { $contact_id = $this->csv->data[$i][$column[0]]; } if (isset($contact_id) && $contact_id != '') { $this->contacts[$contact_id] = $this->csv->data[$i]; } else { // unmatched append row $this->log->_log('Record not found in database:,' . implode(',', $this->csv->data[$i]), 'error_csv'); } } else { foreach ($this->contact_data as $column => $field) { // dealing with some special fields because of CiviCRM's internal workings switch ($field) { case 'birth_date': $param[$field] = civicrm_import_utils::format_date($this->csv->data[$i][$column], $this->data->date_format); break; case 'gender': $param[$field] = civicrm_import_utils::format_gender($this->csv->data[$i][$column]); break; // in appending job you have to get contact_id if they choose to match to external identifier // in appending job you have to get contact_id if they choose to match to external identifier case 'external_identifier': // get the contact id if ($mode == 'import') { $param[$field] = $this->csv->data[$i][$column]; } else { $param['contact_id'] = $this->fetch_contact_id($this->csv->data[$i][$column]); } break; default: $param[$field] = $this->csv->data[$i][$column]; break; } } // print_r($param); // data filtering validation: // if the $param does not fit our validation requirement // i.e. First name, Last name, Email, we do not import them. // #FEATURE: $this->check_filter should return an array of bad fields so we can pin them down in // the log if ($this->check_filter($param, $mode)) { if ($mode == 'import') { // @CiviCRM API (v3) $contact = civicrm_api('contact', 'create', $param); } else { if (isset($param['contact_id']) && $param['contact_id'] != '') { // @CiviCRM API (v3) $param['id'] = $param['contact_id']; unset($param['contact_id']); $contact = civicrm_api('contact', 'create', $param); } else { // log all the ones that did not find a matching record into the error_csv $this->log->_log('Error on CSV line ' . $i . ':, (No matching contact found with the id provided),' . implode(',', $this->csv->data[$i]), 'error_csv'); } } // #FIXED: memory leak from API call CRM_Core_DAO::freeResult(); if ($contact['is_error'] == 1) { $this->log->_log('Error on CSV line ' . $i . ':, (' . $contact['error_message'] . '),' . implode(',', $this->csv->data[$i]), 'error_csv'); } else { $this->contacts[$contact['id']] = $this->csv->data[$i]; $this->contact_imported++; // record the contact import count for tracking if ($this->contact_imported % 100 == 0) { $this->update_count('contact'); } } } else { // log this row as bad row $this->log->_log('Error on CSV line ' . $i . ':, (Bad last name or email on CSV line),' . implode(',', $this->csv->data[$i]), 'error_csv'); } $this->update_count('contact'); } } $this->log->_log($this->contact_imported . ' number of contact records created from ' . count($this->csv->data) . ' number of rows read from CSV file'); // free the original parsed csv from memory unset($this->csv->data); }