/** * Create CMS user using Profile. * * @param array $params * @param string $mail * Email id for cms user. * * @return int * contact id that has been created */ public static function create(&$params, $mail) { $config = CRM_Core_Config::singleton(); $ufID = $config->userSystem->createUser($params, $mail); //if contact doesn't already exist create UF Match if ($ufID !== FALSE && isset($params['contactID'])) { // create the UF Match record $ufmatch['uf_id'] = $ufID; $ufmatch['contact_id'] = $params['contactID']; $ufmatch['uf_name'] = $params[$mail]; CRM_Core_BAO_UFMatch::create($ufmatch); } return $ufID; }
/** * Test for Contact.get id=@user:username. */ public function testContactGetByUsername() { // Setup - create contact with a uf-match. $cid = $this->individualCreate(array('contact_type' => 'Individual', 'first_name' => 'testGetByUsername', 'last_name' => 'testGetByUsername')); $ufMatchParams = array('domain_id' => CRM_Core_Config::domainID(), 'uf_id' => 99, 'uf_name' => 'the-email-matching-key-is-not-really-the-username', 'contact_id' => $cid); $ufMatch = CRM_Core_BAO_UFMatch::create($ufMatchParams); $this->assertTrue(is_numeric($ufMatch->id)); // setup - mock the calls to CRM_Utils_System_*::getUfId $userSystem = $this->getMock('CRM_Utils_System_UnitTests', array('getUfId')); $userSystem->expects($this->once())->method('getUfId')->with($this->equalTo('exampleUser'))->will($this->returnValue(99)); CRM_Core_Config::singleton()->userSystem = $userSystem; // perform a lookup $result = $this->callAPISuccess('Contact', 'get', array('id' => '@user:exampleUser')); $this->assertEquals('testGetByUsername', $result['values'][$cid]['first_name']); }
/** * Update the email value for the contact and user profile. * * @param int $contactId * Contact ID of the user. * @param string $emailAddress * Email to be modified for the user. */ public static function updateContactEmail($contactId, $emailAddress) { $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; $emailAddress = $strtolower($emailAddress); $ufmatch = new CRM_Core_DAO_UFMatch(); $ufmatch->contact_id = $contactId; $ufmatch->domain_id = CRM_Core_Config::domainID(); if ($ufmatch->find(TRUE)) { // Save the email in UF Match table $ufmatch->uf_name = $emailAddress; CRM_Core_BAO_UFMatch::create((array) $ufmatch); //check if the primary email for the contact exists //$contactDetails[1] - email //$contactDetails[3] - email id $contactDetails = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactId); if (trim($contactDetails[1])) { $emailID = $contactDetails[3]; //update if record is found $query = "UPDATE civicrm_email\n SET email = %1\n WHERE id = %2"; $p = array(1 => array($emailAddress, 'String'), 2 => array($emailID, 'Integer')); $dao = CRM_Core_DAO::executeQuery($query, $p); } else { //else insert a new email record $email = new CRM_Core_DAO_Email(); $email->contact_id = $contactId; $email->is_primary = 1; $email->email = $emailAddress; $email->save(); $emailID = $email->id; } CRM_Core_BAO_Log::register($contactId, 'civicrm_email', $emailID); } }