/** * Add an Address for a contact. * * FIXME: Should be using basic_create util * * @param array $params * Array per getfields metadata. * * @return array * API result array */ function civicrm_api3_address_create(&$params) { _civicrm_api3_check_edit_permissions('CRM_Core_BAO_Address', $params); /** * If street_parsing, street_address has to be parsed into * separate parts */ if (array_key_exists('street_parsing', $params)) { if ($params['street_parsing'] == 1) { if (array_key_exists('street_address', $params)) { if (!empty($params['street_address'])) { $parsedItems = CRM_Core_BAO_Address::parseStreetAddress($params['street_address']); if (array_key_exists('street_name', $parsedItems)) { $params['street_name'] = $parsedItems['street_name']; } if (array_key_exists('street_unit', $parsedItems)) { $params['street_unit'] = $parsedItems['street_unit']; } if (array_key_exists('street_number', $parsedItems)) { $params['street_number'] = $parsedItems['street_number']; } if (array_key_exists('street_number_suffix', $parsedItems)) { $params['street_number_suffix'] = $parsedItems['street_number_suffix']; } } } } } if (!isset($params['check_permissions'])) { $params['check_permissions'] = 0; } /** * Create array for BAO (expects address params in as an * element in array 'address' */ $addressBAO = CRM_Core_BAO_Address::add($params, TRUE); if (empty($addressBAO)) { return civicrm_api3_create_error("Address is not created or updated "); } else { $values = _civicrm_api3_dao_to_array($addressBAO, $params); return civicrm_api3_create_success($values, $params, 'Address', $addressBAO); } }
/** * Function to create address associated with contribution record. * @param array $params an associated array * @param int $billingID $billingLocationTypeID * * @return address id * @static */ static function createAddress(&$params, $billingLocationTypeID) { $billingFields = array("street_address", "city", "state_province_id", "postal_code", "country_id"); //build address array $addressParams = array(); $addressParams['location_type_id'] = $billingLocationTypeID; $addressParams['is_billing'] = 1; $addressParams['address_name'] = "{$params['billing_first_name']}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$params['billing_middle_name']}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$params['billing_last_name']}"; foreach ($billingFields as $value) { $addressParams[$value] = $params["billing_{$value}-{$billingLocationTypeID}"]; } require_once "CRM/Core/BAO/Address.php"; $address = CRM_Core_BAO_Address::add($addressParams, false); return $address->id; }
/** * Create address associated with contribution record. * * As long as there is one or more billing field in the parameters we will create the address. * * (historically the decision to create or not was based on the payment 'type' but these lines are greyer than once * thought). * * @param array $params * @param int $billingLocationTypeID * * @return int * address id */ public static function createAddress($params, $billingLocationTypeID) { list($hasBillingField, $addressParams) = self::getBillingAddressParams($params, $billingLocationTypeID); if ($hasBillingField) { $address = CRM_Core_BAO_Address::add($addressParams, FALSE); return $address->id; } return NULL; }
/** * AllAddress() method ( ) with null value */ public function testnullallAddress() { $contactId = Contact::createIndividual(); $fixParams = array('street_address' => 'E 906N Pine Pl W', 'supplemental_address_1' => 'Editorial Dept', 'supplemental_address_2' => '', 'city' => 'El Paso', 'postal_code' => '88575', 'postal_code_suffix' => '', 'state_province_id' => '1001', 'country_id' => '1228', 'geo_code_1' => '31.694842', 'geo_code_2' => '-106.29998', 'location_type_id' => '1', 'is_primary' => '1', 'is_billing' => '0', 'contact_id' => $contactId); CRM_Core_BAO_Address::add($fixParams, $fixAddress = TRUE); $addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id', 'Database check for created contact address.'); $contact_Id = NULL; $allAddress = CRM_Core_BAO_Address::allAddress($contact_Id); $this->assertEquals($allAddress, NULL, 'Checking null for returned addresses.'); Contact::delete($contactId); }
/** * Function to create address associated with contribution record. * @param array $params an associated array * @param int $billingID $billingLocationTypeID * * @return address id * @static */ static function createAddress(&$params, $billingLocationTypeID) { $billingFields = array('street_address', 'city', 'state_province_id', 'postal_code', 'country_id'); //build address array $addressParams = array(); $addressParams['location_type_id'] = $billingLocationTypeID; $addressParams['is_billing'] = 1; $billingFirstName = CRM_Utils_Array::value('billing_first_name', $params); $billingMiddleName = CRM_Utils_Array::value('billing_middle_name', $params); $billingLastName = CRM_Utils_Array::value('billing_last_name', $params); $addressParams['address_name'] = "{$billingFirstName}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$billingMiddleName}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$billingLastName}"; foreach ($billingFields as $value) { $addressParams[$value] = CRM_Utils_Array::value("billing_{$value}-{$billingLocationTypeID}", $params); } $address = CRM_Core_BAO_Address::add($addressParams, FALSE); return $address->id; }
/** * function to retrieve and update geocoding (lat/long) values for a specified 'address object' and 'contact object' using the configured geo-coding method. * * @param object $object valid address/contact object * * @return null * * $access public */ function crm_fix_address($object) { require_once 'CRM/Utils/Geocode/Yahoo.php'; if (is_a($object, 'CRM_Core_BAO_Address')) { $temp = array(); foreach ($object as $name => $value) { $temp[$name] = $value; } $found = CRM_Utils_Geocode_Yahoo::format($temp); $object->copyValues($temp); // code for saving the changes in database $params = array(); $ids = array(); foreach ($object as $name => $value) { $params['location'][1]['address'][$name] = $value; } $ids['location'][1]['id'] = $object->location_id; $ids['location'][1]['address'] = $object->id; CRM_Core_BAO_Address::add($params, $ids, 1); } else { if (is_a($object, 'CRM_Contact_BAO_Contact')) { $params = $ids = $temp = array(); $locations =& crm_get_locations($object); $locNo = 1; foreach ($locations as $loc => $value) { $addObject =& $locations[$locNo]->address; foreach ($addObject as $name => $value) { $temp[$name] = $value; } if (CRM_Utils_Geocode_Yahoo::format($temp)) { $params['location'][$locNo]['address']['geo_code_1'] = $temp['geo_code_1']; $params['location'][$locNo]['address']['geo_code_2'] = $temp['geo_code_2']; $ids['location'][$locNo]['id'] = $object->location[$locNo]->id; $ids['location'][$locNo]['address'] = $object->location[$locNo]->address->id; $locationId = $locNo; CRM_Core_BAO_Address::add($params, $ids, $locationId); } $locNo++; } } else { return _crm_error('Please pass valid contact / address object.'); } } }
/** * 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; }