/** * Test case for format() with "null" value dates. * * See CRM-19123: Merging contacts: blank date fields write as 1970 */ public function testFormatNullDates() { $params = array('contact_type' => 'Individual', 'birth_date' => 'null', 'deceased_date' => 'null'); $contact = new CRM_Contact_DAO_Contact(); CRM_Contact_BAO_Individual::format($params, $contact); $this->assertEmpty($contact->birth_date); $this->assertEmpty($contact->deceased_date); }
/** * 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 * @param array $ids the array that holds all the db ids * * @return object CRM_Contact_BAO_Individual object * @access public * @static */ function add(&$params, &$ids) { if (!CRM_Contact_BAO_Individual::dataExists($params, $ids)) { return; } $individual =& new CRM_Contact_BAO_Individual(); $individual->copyValues($params); $date = CRM_Utils_Array::value('birth_date', $params); if (is_array($date)) { $individual->birth_date = CRM_Utils_Date::format($date); } else { $individual->birth_date = preg_replace('/[^0-9]/', '', $date); } $individual->middle_name = CRM_Utils_Array::value('middle_name', $params); // hack to make db_do save a null value to a field if (!$individual->birth_date) { $individual->birth_date = 'NULL'; } if (!array_key_exists('is_deceased', $params)) { $individual->is_deceased = 0; } $individual->id = CRM_Utils_Array::value('individual', $ids); return $individual->save(); }
/** * Takes an associative array and creates a contact object. * * The function extracts 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 CRM_Contact_BAO_Contact|CRM_Core_Error|NULL * Created or updated contact object or error object. * (error objects are being phased out in favour of exceptions) */ public static function add(&$params) { $contact = new CRM_Contact_DAO_Contact(); if (empty($params)) { return NULL; } // 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 = 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). // @todo - does this mean we can remove this block? // 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 { if ($employerId) { CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($contact->id, $employerId); } } } // Update cached employer name. if ($contact->contact_type == 'Organization') { CRM_Contact_BAO_Contact_Utils::updateCurrentEmployer($contact->id); } return $contact; }
/** * 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; } //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)) { // create current employer if ($params['current_employer']) { require_once 'CRM/Contact/BAO/Contact/Utils.php'; 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')) { require_once 'CRM/Contact/BAO/Contact/Utils.php'; 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); } // process greetings CRM-4575, cache greetings self::processGreetings($contact); return $contact; }
/** * Function to delete Individual Prefix * * @param int $prefixId ID of the prefix to be deleted. * * @return boolean * * @access public * @static */ function del($prefixId) { require_once 'CRM/Contact/BAO/Individual.php'; $ids = array('individualPrefix' => $prefixId); CRM_Contact_BAO_Individual::updateDisplayNames($ids, CRM_CORE_ACTION_DELETE); $prefix =& new CRM_Core_DAO_IndividualPrefix(); $prefix->id = $prefixId; $prefix->delete(); return true; }
/** * takes an associative array and creates a contact object and all the associated * derived objects (i.e. individual, location, email, phone etc) * * This function is invoked from within the web form layer and also from the api layer * primarily from the profile / contribute forms where we dont have a nice hierarchy * and are too lazy to create one. This function should be obsoleted at some time * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Contact_BAO_Contact object * @access public * @static */ function &createFlat(&$params, &$ids) { require_once 'CRM/Utils/Hook.php'; if (CRM_Utils_Array::value('contact', $ids)) { CRM_Utils_Hook::pre('edit', 'Individual', $ids['contact'], $params); } else { CRM_Utils_Hook::pre('create', 'Individual', null, $params); } CRM_Core_DAO::transaction('BEGIN'); $params['contact_type'] = 'Individual'; $contact = CRM_Contact_BAO_Contact::add($params, $ids); $params['contact_id'] = $contact->id; require_once 'CRM/Contact/BAO/Individual.php'; CRM_Contact_BAO_Individual::add($params, $ids); require_once 'CRM/Core/BAO/LocationType.php'; $locationType =& CRM_Core_BAO_LocationType::getDefault(); $locationTypeId = $locationType->id; $locationIds = CRM_Utils_Array::value('location', $ids); // extract the first location id if ($locationIds) { foreach ($locationIds as $dontCare => $locationId) { $locationIds = $locationId; break; } } $location =& new CRM_Core_DAO_Location(); $location->location_type_id = $locationTypeId; $location->entity_table = 'civicrm_contact'; $location->entity_id = $contact->id; $location->id = CRM_Utils_Array::value('id', $locationIds); if ($location->find(true)) { if (!$location->is_primary) { $location->is_primary = true; } } else { $location->is_primary = true; } $location->save(); $address =& new CRM_Core_BAO_Address(); CRM_Core_BAO_Address::fixAddress($params); if (!$address->copyValues($params)) { $address->id = CRM_Utils_Array::value('address', $locationIds); $address->location_id = $location->id; $address->save(); } $phone =& new CRM_Core_BAO_Phone(); if (!$phone->copyValues($params)) { $blockIds = CRM_Utils_Array::value('phone', $locationIds); $phone->id = CRM_Utils_Array::value(1, $blockIds); $phone->location_id = $location->id; $phone->is_primary = true; $phone->save(); } $email =& new CRM_Core_BAO_Email(); if (!$email->copyValues($params)) { $blockIds = CRM_Utils_Array::value('email', $locationIds); $email->id = CRM_Utils_Array::value(1, $blockIds); $email->location_id = $location->id; $email->is_primary = true; $email->save(); } /* Process custom field values and other values */ foreach ($params as $key => $value) { if ($key == 'group') { CRM_Contact_BAO_GroupContact::create($params['group'], $contact->id); } else { if ($key == 'tag') { require_once 'CRM/Core/BAO/EntityTag.php'; CRM_Core_BAO_EntityTag::create($params['tag'], $contact->id); } else { if ($cfID = CRM_Core_BAO_CustomField::getKeyID($key)) { $custom_field_id = $cfID; $cf =& new CRM_Core_BAO_CustomField(); $cf->id = $custom_field_id; if ($cf->find(true)) { switch ($cf->html_type) { case 'Select Date': $date = CRM_Utils_Date::format($value); if (!$date) { $date = ''; } $customValue = $date; break; case 'CheckBox': $customValue = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value)); break; //added a case for Multi-Select //added a case for Multi-Select case 'Multi-Select': $customValue = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value)); break; default: $customValue = $value; } } CRM_Core_BAO_CustomValue::updateValue($contact->id, $custom_field_id, $customValue); } } } } CRM_Core_DAO::transaction('COMMIT'); if (CRM_Utils_Array::value('contact', $ids)) { CRM_Utils_Hook::post('edit', 'Individual', $contact->id, $contact); } else { CRM_Utils_Hook::post('create', 'Individual', $contact->id, $contact); } return $contact; }