/** * takes an associative array and creates a contact object * * the function extract all the params it needs to initialize the create a * contact object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * * @return object CRM_Contact_BAO_Contact object * @access public * @static */ static function add(&$params) { $contact = new CRM_Contact_DAO_Contact(); if (empty($params)) { return; } //fix for validate contact sub type CRM-5143 if (isset($params['contact_sub_type'])) { if (empty($params['contact_sub_type'])) { $params['contact_sub_type'] = 'null'; } else { if (!CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type'], TRUE)) { // we'll need to fix tests to handle this // CRM-7925 CRM_Core_Error::fatal(ts('The Contact Sub Type does not match the Contact type for this record')); } if (is_array($params['contact_sub_type'])) { $params['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['contact_sub_type']) . CRM_Core_DAO::VALUE_SEPARATOR; } else { $params['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . trim($params['contact_sub_type'], CRM_Core_DAO::VALUE_SEPARATOR) . CRM_Core_DAO::VALUE_SEPARATOR; } } } else { // reset the value // CRM-101XX $params['contact_sub_type'] = 'null'; } //fixed contact source if (isset($params['contact_source'])) { $params['source'] = $params['contact_source']; } //fix for preferred communication method $prefComm = CRM_Utils_Array::value('preferred_communication_method', $params); if ($prefComm && is_array($prefComm)) { unset($params['preferred_communication_method']); $newPref = array(); foreach ($prefComm as $k => $v) { if ($v) { $newPref[$k] = $v; } } $prefComm = $newPref; if (is_array($prefComm) && !empty($prefComm)) { $prefComm = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($prefComm)) . CRM_Core_DAO::VALUE_SEPARATOR; $contact->preferred_communication_method = $prefComm; } else { $contact->preferred_communication_method = ''; } } $allNull = $contact->copyValues($params); $contact->id = CRM_Utils_Array::value('contact_id', $params); if ($contact->contact_type == 'Individual') { $allNull = FALSE; //format individual fields CRM_Contact_BAO_Individual::format($params, $contact); } elseif ($contact->contact_type == 'Household') { if (isset($params['household_name'])) { $allNull = FALSE; $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('household_name', $params, ''); } } elseif ($contact->contact_type == 'Organization') { if (isset($params['organization_name'])) { $allNull = FALSE; $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('organization_name', $params, ''); } } // privacy block $privacy = CRM_Utils_Array::value('privacy', $params); if ($privacy && is_array($privacy) && !empty($privacy)) { $allNull = FALSE; foreach (self::$_commPrefs as $name) { $contact->{$name} = CRM_Utils_Array::value($name, $privacy, FALSE); } } // since hash was required, make sure we have a 0 value for it, CRM-1063 // fixed in 1.5 by making hash optional // only do this in create mode, not update if ((!array_key_exists('hash', $contact) || !$contact->hash) && !$contact->id) { $allNull = FALSE; $contact->hash = md5(uniqid(rand(), TRUE)); } // Even if we don't need $employerId, it's important to call getFieldValue() before // the contact is saved because we want the existing value to be cached. // createCurrentEmployerRelationship() needs the old value not the updated one. CRM-10788 $employerId = empty($contact->id) ? NULL : CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact->id, 'employer_id'); if (!$allNull) { $contact->save(); CRM_Core_BAO_Log::register($contact->id, 'civicrm_contact', $contact->id); } if ($contact->contact_type == 'Individual' && (isset($params['current_employer']) || isset($params['employer_id']))) { // create current employer $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : CRM_Utils_Array::value('current_employer', $params); $newContact = FALSE; if (empty($params['contact_id'])) { $newContact = TRUE; } if ($newEmployer) { CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId, $newContact); } else { //unset if employer id exits if ($employerId) { CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($contact->id, $employerId); } } } //update cached employee name if ($contact->contact_type == 'Organization') { CRM_Contact_BAO_Contact_Utils::updateCurrentEmployer($contact->id); } return $contact; }
/** * Function to set is_delete true or restore deleted contact. * * @param CRM_Contact_DAO_Contact $contact * Contact DAO object. * @param bool $restore * True to set the is_delete = 1 else false to restore deleted contact, * i.e. is_delete = 0 * * @return bool */ public static function contactTrashRestore($contact, $restore = FALSE) { $updateParams = array('id' => $contact->id, 'is_deleted' => $restore ? 0 : 1); CRM_Utils_Hook::pre('update', $contact->contact_type, $contact->id, $updateParams); $params = array(1 => array($contact->id, 'Integer')); if (!$restore) { $query = "DELETE FROM civicrm_uf_match WHERE contact_id = %1"; CRM_Core_DAO::executeQuery($query, $params); } $contact->copyValues($updateParams); $contact->save(); CRM_Utils_Hook::post('update', $contact->contact_type, $contact->id, $contact); return TRUE; }
/** * takes an associative array and creates a contact object * * the function extract all the params it needs to initialize the create a * contact object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * * @return object CRM_Contact_BAO_Contact object * @access public * @static */ static function add(&$params) { $contact = new CRM_Contact_DAO_Contact(); if (empty($params)) { return; } //fix for validate contact sub type CRM-5143 $subType = CRM_Utils_Array::value('contact_sub_type', $params); if ($subType && !CRM_Contact_BAO_ContactType::isExtendsContactType($subType, $params['contact_type'], true)) { return; } //fixed contact source if (isset($params['contact_source'])) { $params['source'] = $params['contact_source']; } //fix for preferred communication method $prefComm = CRM_Utils_Array::value('preferred_communication_method', $params); if ($prefComm && is_array($prefComm)) { unset($params['preferred_communication_method']); $newPref = array(); foreach ($prefComm as $k => $v) { if ($v) { $newPref[$k] = $v; } } $prefComm = $newPref; if (is_array($prefComm) && !empty($prefComm)) { $prefComm = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, array_keys($prefComm)) . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR; $contact->preferred_communication_method = $prefComm; } else { $contact->preferred_communication_method = ''; } } $allNull = $contact->copyValues($params); $contact->id = CRM_Utils_Array::value('contact_id', $params); if ($contact->contact_type == 'Individual') { $allNull = false; //format individual fields require_once 'CRM/Contact/BAO/Individual.php'; CRM_Contact_BAO_Individual::format($params, $contact); } else { if ($contact->contact_type == 'Household') { if (isset($params['household_name'])) { $allNull = false; $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('household_name', $params, ''); } } else { if ($contact->contact_type == 'Organization') { if (isset($params['organization_name'])) { $allNull = false; $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('organization_name', $params, ''); } } } } // privacy block $privacy = CRM_Utils_Array::value('privacy', $params); if ($privacy && is_array($privacy) && !empty($privacy)) { $allNull = false; foreach (self::$_commPrefs as $name) { $contact->{$name} = CRM_Utils_Array::value($name, $privacy, false); } } // since hash was required, make sure we have a 0 value for it, CRM-1063 // fixed in 1.5 by making hash optional // only do this in create mode, not update if ((!array_key_exists('hash', $contact) || !$contact->hash) && !$contact->id) { $allNull = false; $contact->hash = md5(uniqid(rand(), true)); } if (!$allNull) { $contact->save(); require_once 'CRM/Core/BAO/Log.php'; CRM_Core_BAO_Log::register($contact->id, 'civicrm_contact', $contact->id); } if ($contact->contact_type == 'Individual' && (array_key_exists('current_employer', $params) || array_key_exists('employer_id', $params))) { // create current employer require_once 'CRM/Contact/BAO/Contact/Utils.php'; if ($params['employer_id']) { CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $params['employer_id']); } elseif ($params['current_employer']) { CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $params['current_employer']); } else { //unset if employer id exits if ($employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact->id, 'employer_id')) { CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($contact->id, $employerId); } } } //update cached employee name if ($contact->contact_type == 'Organization') { require_once 'CRM/Contact/BAO/Contact/Utils.php'; CRM_Contact_BAO_Contact_Utils::updateCurrentEmployer($contact->id); } return $contact; }