/** * Add() method (create and update modes) */ public function testAdd() { $contactId = Contact::createIndividual(); $params = array(); $params = array('phone' => '(415) 222-1011 x 221', 'is_primary' => 1, 'location_type_id' => 1, 'phone_type' => 'Mobile', 'contact_id' => $contactId); CRM_Core_BAO_Phone::add($params); $phoneId = $this->assertDBNotNull('CRM_Core_DAO_Phone', $contactId, 'id', 'contact_id', 'Database check for created phone record.'); $this->assertDBCompareValue('CRM_Core_DAO_Phone', $phoneId, 'phone', 'id', '(415) 222-1011 x 221', "Check if phone field has expected value in new record ( civicrm_phone.id={$phoneId} )."); // Now call add() to modify the existing phone number $params = array(); $params = array('id' => $phoneId, 'contact_id' => $contactId, 'phone' => '(415) 222-5432'); CRM_Core_BAO_Phone::add($params); $this->assertDBCompareValue('CRM_Core_DAO_Phone', $phoneId, 'phone', 'id', '(415) 222-5432', "Check if phone field has expected value in updated record ( civicrm_phone.id={$phoneId} )."); Contact::delete($contactId); }
/** * Ensure that civicrm_contact.modified_date is updated when manipulating an email. */ public function testTimestampsPhone() { $test = $this; $this->_testTimestamps(array('INSERT' => function ($contactId) use($test) { $params = array('phone' => '202-555-1000', 'is_primary' => 1, 'location_type_id' => 1, 'contact_id' => $contactId); CRM_Core_BAO_Phone::add($params); $test->assertDBQuery('202-555-1000', 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1', array(1 => array($contactId, 'Integer'))); }, 'UPDATE' => function ($contactId) use($test) { CRM_Core_DAO::executeQuery('UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1', array(1 => array($contactId, 'Integer'))); }, 'DELETE' => function ($contactId) use($test) { CRM_Core_DAO::executeQuery('DELETE FROM civicrm_phone WHERE contact_id = %1', array(1 => array($contactId, 'Integer'))); })); }
/** * 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 * @param array $locationId * * @return object CRM_Core_BAO_Location object on success, null otherwise * @access public * @static */ function add(&$params, &$ids, $locationId) { if (!CRM_Core_BAO_Location::dataExists($params, $locationId, $ids)) { return null; } $location =& new CRM_Core_BAO_Location(); if (!isset($params['contact_id'])) { require_once 'CRM/Core/BAO/Domain.php'; $location->entity_table = CRM_Core_BAO_Domain::getTableName(); $location->entity_id = $params['domain_id']; } else { $location->entity_table = CRM_Contact_BAO_Contact::getTableName(); $location->entity_id = $params['contact_id']; } $location->location_type_id = CRM_Utils_Array::value('location_type_id', $params['location'][$locationId]); $location->name = CRM_Utils_Array::value('name', $params['location'][$locationId]); $location->is_primary = CRM_Utils_Array::value('is_primary', $params['location'][$locationId], false); // check if there exists another location has is_primary set, and if so reset that // if no location has is_primary, make this one is_primart if ($location->is_primary) { // reset all other locations with the same entity table entity id $sql = "UPDATE " . CRM_Core_BAO_Location::getTableName() . "\n SET is_primary = 0 WHERE \n entity_table = '{$location->entity_table}' AND\n entity_id = '{$location->entity_id}' "; CRM_Core_DAO::executeQuery($sql); } else { // make sure there is at once location with is_primary set $sql = "SELECT count( " . CRM_Core_BAO_Location::getTableName() . ".id )\n FROM " . CRM_Core_BAO_Location::getTableName() . " WHERE\n entity_table = '{$location->entity_table}' AND\n entity_id = '{$location->entity_id}' AND\n is_primary = 1"; $count = CRM_Core_DAO::singleValueQuery($sql); if ($count == 0) { $location->is_primary = true; } } $location->id = CRM_Utils_Array::value('id', $ids['location'][$locationId]); $location->save(); $params['location'][$locationId]['id'] = $location->id; $address_object = CRM_Core_BAO_Address::add($params, $ids, $locationId); $location->address = $address_object; // set this to true if this has been made the primary IM. // the rule is the first entered value is the primary object $isPrimaryPhone = $isPrimaryEmail = $isPrimaryIM = true; $location->phone = array(); $location->email = array(); $location->im = array(); for ($i = 1; $i <= CRM_CONTACT_FORM_LOCATION_BLOCKS; $i++) { $location->phone[$i] = CRM_Core_BAO_Phone::add($params, $ids, $locationId, $i, $isPrimaryPhone); $location->email[$i] = CRM_Core_BAO_Email::add($params, $ids, $locationId, $i, $isPrimaryEmail); $location->im[$i] = CRM_Core_BAO_IM::add($params, $ids, $locationId, $i, $isPrimaryIM); } return $location; }
static function processNormalization($fromContactId, $toContactId) { $processInfo = array('name' => 0, 'phone' => 0, 'address' => 0); if (empty($fromContactId) || empty($toContactId)) { return $processInfo; } $contactIds = range($fromContactId, $toContactId); $normalization = CRM_Utils_Normalize::singleton(); $formattedContactIds = $formattedPhoneIds = $formattedAddressIds = array(); foreach ($contactIds as $contactId) { $contact = new CRM_Contact_DAO_Contact(); $contact->id = $contactId; if ($contact->find()) { $params = array('id' => $contactId, 'contact_id' => $contactId); $orgContactValues = array(); CRM_Contact_BAO_Contact::retrieve($params, $orgContactValues); //update contacts name fields. $formatNameValues = array(); foreach ($normalization->getNameFields() as $field) { $nameValue = CRM_Utils_Array::value($field, $orgContactValues); if (empty($nameValue)) { continue; } $formatNameValues[$field] = $nameValue; } if (!empty($formatNameValues)) { $formatNameValues['contact_type'] = $orgContactValues['contact_type']; $formattedNameValues = $formatNameValues; //format name values $normalization->normalize_contact($formattedNameValues); //check formatted diff, only update if there is difference. $formatDiff = array_diff($formatNameValues, $formattedNameValues); if (!empty($formatDiff)) { $formattedNameValues['id'] = $formattedNameValues['contact_id'] = $orgContactValues['id']; $formattedNameValues['contact_type'] = $orgContactValues['contact_type']; $contactUpdated = CRM_Contact_BAO_Contact::add($formattedNameValues); if ($contactUpdated->id) { $formattedContactIds[$contactUpdated->id] = $contactUpdated->id; } $contactUpdated->free(); } } //update phone fields. if (isset($orgContactValues['phone']) && is_array($orgContactValues['phone'])) { foreach ($orgContactValues['phone'] as $cnt => $orgPhoneValues) { if (!isset($orgPhoneValues['id']) || empty($orgPhoneValues['id']) || empty($orgPhoneValues['phone'])) { continue; } $formattedPhoneValues = $orgPhoneValues; //format phone fields $normalization->normalize_phone($formattedPhoneValues); //do check for formatted difference, than only update. $formattedDiff = array_diff_assoc($orgPhoneValues, $formattedPhoneValues); if (!empty($formattedDiff)) { $phoneUpdated = CRM_Core_BAO_Phone::add($formattedPhoneValues); if ($phoneUpdated->id) { $formattedPhoneIds[$phoneUpdated->id] = $phoneUpdated->id; } $phoneUpdated->free(); } } } //update address. if (isset($orgContactValues['address']) && is_array($orgContactValues['address'])) { foreach ($orgContactValues['address'] as $cnt => $orgAddressValues) { if (!isset($orgAddressValues['id']) || empty($orgAddressValues['id'])) { continue; } $formattedAddressValues = $orgAddressValues; //format addrees fields $normalization->normalize_address($formattedAddressValues); //do check for formatted difference, than only update. $formattedDiff = array_diff($orgAddressValues, $formattedAddressValues); if (!empty($formattedDiff)) { $addressUpdated = CRM_Core_BAO_Address::add($formattedAddressValues, FALSE); if ($addressUpdated->id) { $formattedAddressIds[$addressUpdated->id] = $addressUpdated->id; } $addressUpdated->free(); } } } } $contact->free(); } $processInfo = array('name' => $formattedContactIds, 'phone' => $formattedPhoneIds, 'address' => $formattedAddressIds); return $processInfo; }