/** * Test case for retrieve( ). * * Test with all values. */ public function testRetrieve() { //take the common contact params $params = $this->contactParams(); $params['note'] = 'test note'; //create the contact with given params. $contact = CRM_Contact_BAO_Contact::create($params); //Now check $contact is object of contact DAO.. $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); $contactId = $contact->id; //create the organization contact with the given params. $orgParams = array('organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4), 'contact_type' => 'Organization'); $orgContact = CRM_Contact_BAO_Contact::add($orgParams); $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object'); //create employee of relationship. CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contactId, $orgContact->id); //retrieve the contact values from database. $values = array(); $searchParams = array('contact_id' => $contactId); $retrieveContact = CRM_Contact_BAO_Contact::retrieve($searchParams, $values); //Now check $retrieveContact is object of contact DAO.. $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object'); //Now check the ids. $this->assertEquals($contactId, $retrieveContact->id, 'Check for contact id'); //Now check values retrieve from database with params. $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.'); $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.'); $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.'); //Now check values of address $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']), CRM_Utils_Array::value('1', $values['address'])); //Now check values of email $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']), CRM_Utils_Array::value('1', $values['email'])); //Now check values of phone $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']), CRM_Utils_Array::value('1', $values['phone'])); //Now check values of mobile $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']), CRM_Utils_Array::value('2', $values['phone'])); //Now check values of openid $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']), CRM_Utils_Array::value('1', $values['openid'])); //Now check values of im $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']), CRM_Utils_Array::value('1', $values['im'])); //Now check values of Note Count. $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count'); foreach ($values['note'] as $key => $val) { $retrieveNote = CRM_Utils_Array::value('note', $val); //check the note value $this->assertEquals($params['note'], $retrieveNote, 'Check for note'); } //Now check values of Relationship Count. $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count'); foreach ($values['relationship']['data'] as $key => $val) { //Now check values of Relationship organization. $this->assertEquals($orgContact->id, $val['contact_id_b'], 'Check for organization'); //Now check values of Relationship type. $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type'); //delete the organization. $this->contactDelete(CRM_Utils_Array::value('contact_id_b', $val)); } //delete all notes related to contact CRM_Core_BAO_Note::cleanContactNotes($contactId); //cleanup DB by deleting the contact $this->contactDelete($contactId); $this->quickCleanup(array('civicrm_contact')); }
/** * Create relationship between contacts who share an address. * * Note that currently we create relationship only for Individual contacts * Individual + Household and Individual + Orgnization * * @param int $masterAddressId * Master address id. * @param array $params * Associated array of submitted values. */ public static function processSharedAddressRelationship($masterAddressId, $params) { // get the contact type of contact being edited / created $currentContactType = CRM_Contact_BAO_Contact::getContactType($params['contact_id']); $currentContactId = $params['contact_id']; // if current contact is not of type individual return if ($currentContactType != 'Individual') { return; } // get the contact id and contact type of shared contact // check the contact type of shared contact, return if it is of type Individual $query = 'SELECT cc.id, cc.contact_type FROM civicrm_contact cc INNER JOIN civicrm_address ca ON cc.id = ca.contact_id WHERE ca.id = %1'; $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($masterAddressId, 'Integer'))); $dao->fetch(); // if current contact is not of type individual return, since we don't create relationship between // 2 individuals if ($dao->contact_type == 'Individual') { return; } $sharedContactType = $dao->contact_type; $sharedContactId = $dao->id; // create relationship between ontacts who share an address if ($sharedContactType == 'Organization') { return CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($currentContactId, $sharedContactId); } // get the relationship type id of "Household Member of" $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Household Member of', 'id', 'name_a_b'); if (!$relTypeId) { CRM_Core_Error::fatal(ts("You seem to have deleted the relationship type 'Household Member of'")); } $relParam = array('is_active' => TRUE, 'relationship_type_id' => $relTypeId, 'contact_id_a' => $currentContactId, 'contact_id_b' => $sharedContactId); // If already there is a relationship record of $relParam criteria, avoid creating relationship again or else // it will casue CRM-16588 as the Duplicate Relationship Exception will revert other contact field values on update if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($relParam, $currentContactId, $sharedContactId)) { return; } try { // create relationship civicrm_api3('relationship', 'create', $relParam); } catch (CiviCRM_API3_Exception $e) { // We catch and ignore here because this has historically been a best-effort relationship create call. // presumably it could refuse due to duplication or similar and we would ignore that. } }
/** * 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; }
/** * Function to create relationship between contacts who share an address * * Note that currently we create relationship only for Individual contacts * Individual + Household and Individual + Orgnization * * @param int $masterAddressId master address id * @param array $params associated array of submitted values * * @return void * @access public * @static */ static function processSharedAddressRelationship($masterAddressId, $params) { if (!$masterAddressId) { return; } // get the contact type of contact being edited / created $currentContactType = CRM_Contact_BAO_Contact::getContactType($params['contact_id']); $currentContactId = $params['contact_id']; // if current contact is not of type individual return if ($currentContactType != 'Individual') { return; } // get the contact id and contact type of shared contact // check the contact type of shared contact, return if it is of type Individual $query = 'SELECT cc.id, cc.contact_type FROM civicrm_contact cc INNER JOIN civicrm_address ca ON cc.id = ca.contact_id WHERE ca.id = %1'; $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($masterAddressId, 'Integer'))); $dao->fetch(); // if current contact is not of type individual return, since we don't create relationship between // 2 individuals if ($dao->contact_type == 'Individual') { return; } $sharedContactType = $dao->contact_type; $sharedContactId = $dao->id; // create relationship between ontacts who share an address if ($sharedContactType == 'Organization') { return CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($currentContactId, $sharedContactId); } else { // get the relationship type id of "Household Member of" $relationshipType = 'Household Member of'; } $cid = array('contact' => $currentContactId); $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $relationshipType, 'id', 'name_a_b'); if (!$relTypeId) { CRM_Core_Error::fatal(ts("You seem to have deleted the relationship type '%1'", array(1 => $relationshipType))); } // create relationship $relationshipParams = array('is_active' => TRUE, 'relationship_type_id' => $relTypeId . '_a_b', 'contact_check' => array($sharedContactId => TRUE)); list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($relationshipParams, $cid); }
/** * 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; }
/** * test case for retrieve( ) * test with all values. */ function testRetrieve() { //take the common contact params $params = $this->contactParams(); $params['note'] = 'test note'; $params['create_employer'] = 'Yahoo'; require_once 'CRM/Contact/BAO/Contact.php'; //create the contact with given params. $contact = CRM_Contact_BAO_Contact::create($params); //Now check $contact is object of contact DAO.. $this->assertType('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); $contactId = $contact->id; //create employee of relationship. require_once 'CRM/Contact/BAO/Contact/Utils.php'; CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contactId, $params['create_employer']); //retrieve the contact values from database. $values = array(); $searchParams = array('contact_id' => $contactId); $retrieveContact = CRM_Contact_BAO_Contact::retrieve($searchParams, $values); //Now check $retrieveContact is object of contact DAO.. $this->assertType('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object'); //Now check the ids. $this->assertEquals($contactId, $retrieveContact->id, 'Check for contact id'); //Now check values retrieve from database with params. $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.'); $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.'); $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.'); //Now check values of address // $this->assertAttributesEquals( CRM_Utils_Array::value( 'address', $params ), // CRM_Utils_Array::value( 'address', $values ) ); //Now check values of email $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']), CRM_Utils_Array::value('1', $values['email'])); //Now check values of phone $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']), CRM_Utils_Array::value('1', $values['phone'])); //Now check values of mobile $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']), CRM_Utils_Array::value('2', $values['phone'])); //Now check values of openid $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']), CRM_Utils_Array::value('1', $values['openid'])); //Now check values of im $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']), CRM_Utils_Array::value('1', $values['im'])); //Now check values of Note Count. $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count'); foreach ($values['note'] as $key => $val) { $retrieveNote = CRM_Utils_Array::value('note', $val); //check the note value $this->assertEquals($params['note'], $retrieveNote, 'Check for note'); } //Now check values of Relationship Count. $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count'); foreach ($values['relationship']['data'] as $key => $val) { //Now check values of Relationship organization. $this->assertEquals($params['create_employer'], $val['name'], 'Check for organization'); //Now check values of Relationship type. $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type'); //delete the organization. Contact::delete(CRM_Utils_Array::value('cid', $val)); } //cleanup DB by deleting the contact Contact::delete($contactId); }