/** * Create or update contact * * @param $param * * @return array * @throws CiviCRM_API3_Exception */ public function createContact($param) { $h = $param->cons_hash; $contact = array('sequential' => 1, 'contact_type' => 'Individual', 'email' => $h->emails[0]->email, $this->apiAddressGet => array('id' => '$value.address_id', 'contact_id' => '$value.id'), $this->apiGroupContactGet => array('group_id' => $this->groupId, 'contact_id' => '$value.id', 'status' => 'Added'), 'return' => 'id,email,first_name,last_name'); $contacIds = CRM_Speakcivi_Logic_Contact::getContactByEmail($h->emails[0]->email); if (is_array($contacIds) && count($contacIds) > 0) { $contact['id'] = array('IN' => array_keys($contacIds)); $result = civicrm_api3('Contact', 'get', $contact); if ($result['count'] == 1) { $contact = $this->prepareParamsContact($param, $contact, $result, $result['values'][0]['id']); } elseif ($result['count'] > 1) { $lastname = $this->cleanLastname($h->lastname); $newContact = $contact; $newContact['first_name'] = $h->firstname; $newContact['last_name'] = $lastname; $similarity = $this->glueSimilarity($newContact, $result['values']); unset($newContact); $contactIdBest = $this->chooseBestContact($similarity); $contact = $this->prepareParamsContact($param, $contact, $result, $contactIdBest); } } else { $this->newContact = true; $contact = $this->prepareParamsContact($param, $contact); } return civicrm_api3('Contact', 'create', $contact); }