public static function createAddress($params, $billingLocationTypeID) {
[$hasBillingField, $addressParams] = self::getBillingAddressParams($params, $billingLocationTypeID);
if ($hasBillingField) {
- $address = CRM_Core_BAO_Address::add($addressParams, FALSE);
+ $address = CRM_Core_BAO_Address::writeRecord($addressParams);
return $address->id;
}
return NULL;
/**
* This is class to handle address related functions.
*/
-class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
+class CRM_Core_BAO_Address extends CRM_Core_DAO_Address implements Civi\Core\HookInterface {
use CRM_Contact_AccessTrait;
/**
- * Takes an associative array and creates a address.
+ * @deprecated
*
* @param array $params
- * (reference ) an assoc array of name/value pairs.
* @param bool $fixAddress
- * True if you need to fix (format) address values.
- * before inserting in db
- *
- * @return array|NULL|self
- * array of created address
+ * @return CRM_Core_BAO_Address
+ * @throws CRM_Core_Exception
*/
public static function create(array &$params, $fixAddress = TRUE) {
- return self::add($params, $fixAddress);
+ CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
+ if ($fixAddress) {
+ CRM_Core_BAO_Address::fixAddress($params);
+ }
+ return self::writeRecord($params);
}
/**
- * Takes an associative array and adds address.
+ * @deprecated
*
* @param array $params
- * (reference ) an assoc array of name/value pairs.
* @param bool $fixAddress
- * True if you need to fix (format) address values.
- * before inserting in db
- *
- * @return CRM_Core_BAO_Address|null
+ * @return CRM_Core_BAO_Address
+ * @throws CRM_Core_Exception
*/
public static function add(&$params, $fixAddress = FALSE) {
+ return self::create($params, $fixAddress);
+ }
- $address = new CRM_Core_DAO_Address();
- $checkPermissions = $params['check_permissions'] ?? TRUE;
-
- // fixAddress mode to be done
- if ($fixAddress) {
- CRM_Core_BAO_Address::fixAddress($params);
- }
-
- $hook = empty($params['id']) ? 'create' : 'edit';
- CRM_Utils_Hook::pre($hook, 'Address', CRM_Utils_Array::value('id', $params), $params);
-
- CRM_Core_BAO_Block::handlePrimary($params, get_class());
- CRM_Core_BAO_Block::handleBilling($params, get_class());
-
- // (prevent chaining 1 and 3) CRM-21214
- if (isset($params['master_id']) && !CRM_Utils_System::isNull($params['master_id'])) {
- self::fixSharedAddress($params);
- }
-
- $address->copyValues($params);
- $address->save();
-
- if ($address->id) {
- // first get custom field from master address if any
- if (isset($params['master_id']) && !CRM_Utils_System::isNull($params['master_id'])) {
- $address->copyCustomFields($params['master_id'], $address->id, $hook);
+ /**
+ * Event fired before modifying an Address.
+ * @param \Civi\Core\Event\PreEvent $event
+ */
+ public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
+ if (in_array($event->action, ['create', 'edit'])) {
+ CRM_Core_BAO_Block::handlePrimary($event->params, __CLASS__);
+ CRM_Core_BAO_Block::handleBilling($event->params, __CLASS__);
+
+ // (prevent chaining 1 and 3) CRM-21214
+ if (isset($event->params['master_id']) && !CRM_Utils_System::isNull($event->params['master_id'])) {
+ self::fixSharedAddress($event->params);
}
+ }
+ }
- if (isset($params['custom'])) {
- $addressCustom = $params['custom'];
- }
- else {
- $customFields = CRM_Core_BAO_CustomField::getFields('Address', FALSE, TRUE, NULL, NULL,
- FALSE, FALSE, $checkPermissions ? CRM_Core_Permission::EDIT : FALSE);
-
- if (!empty($customFields)) {
- $addressCustom = CRM_Core_BAO_CustomField::postProcess($params,
- $address->id,
- 'Address',
- FALSE,
- $checkPermissions
- );
- }
- }
- if (!empty($addressCustom)) {
- CRM_Core_BAO_CustomValueTable::store($addressCustom, 'civicrm_address', $address->id, $hook);
+ /**
+ * Event fired after modifying an Address.
+ * @param \Civi\Core\Event\PostEvent $event
+ */
+ public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) {
+ // Copy custom data from master address if not supplied
+ if ($event->action === 'create' && !isset($event->params['custom'])) {
+ if (isset($event->params['master_id']) && !CRM_Utils_System::isNull($event->params['master_id'])) {
+ $event->object->copyCustomFields($event->params['master_id'], $event->id, $event->action);
}
-
+ }
+ if (in_array($event->action, ['create', 'edit'])) {
// call the function to sync shared address and create relationships
// if address is already shared, share master_id with all children and update relationships accordingly
// (prevent chaining 2) CRM-21214
- self::processSharedAddress($address->id, $params, $hook);
-
- // lets call the post hook only after we've done all the follow on processing
- CRM_Utils_Hook::post($hook, 'Address', $address->id, $address);
+ self::processSharedAddress($event->id, $event->params, $event->action);
}
-
- return $address;
}
/**
return NULL;
}
CRM_Core_BAO_Block::sortPrimaryFirst($params['address']);
- $contactId = NULL;
$updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE);
$contactId = $params['contact_id'];
$value['manual_geo_code'] = 0;
}
$value['contact_id'] = $contactId;
- $blocks[] = self::add($value, $fixAddress);
+
+ if ($fixAddress) {
+ self::fixAddress($value);
+ }
+
+ // Format custom data
+ if (!isset($value['custom'])) {
+ $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, $value['id'] ?? NULL, 'Address');
+ }
+
+ $blocks[] = self::writeRecord($value);
}
return $blocks;
}
}
$blockFields = array_merge($value, $contactFields);
- $blocks[] = $baoString::create($blockFields);
+ if ($baoString === 'CRM_Core_BAO_Address') {
+ CRM_Core_BAO_Address::fixAddress($blockFields);
+ }
+ $blocks[] = $baoString::writeRecord($blockFields);
}
return $blocks;
use Civi\Api4\LocBlock;
use Civi\Api4\Email;
use Civi\Api4\Phone;
-use Civi\Api4\Address;
/**
*
}
- // Update the Blocks.
- $addresses = empty($params['address']) ? [] : Address::save(FALSE)->setRecords($params['address'])->execute();
+ // Update location Blocks.
+ $addresses = [];
+ // Don't use APIv4 for address because it doesn't handle custom fields in the format used by this form (custom_xx)
+ foreach ($params['address'] ?? [] as $address) {
+ CRM_Core_BAO_Address::fixAddress($address);
+ $address['custom'] = CRM_Core_BAO_CustomField::postProcess($address, $address['id'] ?? NULL, 'Address');
+ $addresses[] = (array) CRM_Core_BAO_Address::writeRecord($address);
+ }
+ // Using APIv4 for email & phone, the form doesn't support custom data for them anyway
$emails = empty($params['email']) ? [] : Email::save(FALSE)->setRecords($params['email'])->execute();
$phones = empty($params['phone']) ? [] : Phone::save(FALSE)->setRecords($params['phone'])->execute();
$item = array_merge($item, \CRM_Core_BAO_Address::parseStreetAddress($item['street_address']));
}
$item['skip_geocode'] = $this->skipGeocode;
- $saved[] = \CRM_Core_BAO_Address::add($item, $this->fixAddress);
+ if ($this->fixAddress) {
+ \CRM_Core_BAO_Address::fixAddress($item);
+ }
+ $saved[] = \CRM_Core_BAO_Address::writeRecord($item);
}
return $saved;
}
if ($v4Entity != 'Setting' && !in_array('id', $v4Params['select'])) {
$v4Params['select'][] = 'id';
}
+ // Convert 'custom' to 'custom.*'
+ $selectCustom = array_search('custom', $v4Params['select']);
+ if ($selectCustom !== FALSE) {
+ $v4Params['select'][$selectCustom] = 'custom.*';
+ }
}
if ($options['limit'] && $v4Entity != 'Setting') {
$v4Params['limit'] = $options['limit'];
$params['check_permissions'] = 0;
}
- if (!isset($params['fix_address'])) {
- $params['fix_address'] = TRUE;
+ if (!isset($params['fix_address']) || $params['fix_address']) {
+ CRM_Core_BAO_Address::fixAddress($params);
}
- /**
- * Create array for BAO (expects address params in as an
- * element in array 'address'
- */
- $addressBAO = CRM_Core_BAO_Address::create($params, $params['fix_address']);
- 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);
- }
+ return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Address');
}
/**
'is_billing' => 1,
'state_province_id' => '3934',
];
- $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
+ $addAddressA = CRM_Core_BAO_Address::writeRecord($addressParamsA);
$addressParamsB[1] = [
'contact_id' => $contactIdB,
];
CRM_Contact_BAO_Contact_Utils::processSharedAddress($addressParamsB);
- $addAddressB = CRM_Core_BAO_Address::add($addressParamsB[1], FALSE);
+ $addAddressB = CRM_Core_BAO_Address::writeRecord($addressParamsB[1]);
foreach ($addAddressA as $key => $value) {
if (!in_array($key, ['id', 'contact_id', 'master_id', 'is_primary', 'is_billing', 'location_type_id', 'manual_geo_code'])) {
'contact_id' => $contactId,
];
- $addAddress = CRM_Core_BAO_Address::add($fixParams, $fixAddress = TRUE);
+ CRM_Core_BAO_Address::fixAddress($fixParams);
+ $addAddress = CRM_Core_BAO_Address::writeRecord($fixParams);
$addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
'Database check for created contact address.'
'contact_id' => $contactId,
];
- CRM_Core_BAO_Address::add($params['address']['1'], $fixAddress = TRUE);
+ CRM_Core_BAO_Address::fixAddress($params['address']['1']);
+ CRM_Core_BAO_Address::writeRecord($params['address']['1']);
// Add address 2
$params['address']['2'] = [
'contact_id' => $contactId,
];
- CRM_Core_BAO_Address::add($params['address']['2'], $fixAddress = TRUE);
+ CRM_Core_BAO_Address::fixAddress($params['address']['2']);
+ CRM_Core_BAO_Address::writeRecord($params['address']['2']);
$addresses = CRM_Core_BAO_Address::getValues($entityBlock);
'contact_id' => $contactId,
];
- CRM_Core_BAO_Address::add($fixParams, $fixAddress = TRUE);
+ CRM_Core_BAO_Address::fixAddress($fixParams);
+ CRM_Core_BAO_Address::writeRecord($fixParams);
$addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
'Database check for created contact address.'
'contact_id' => $contactId,
];
- CRM_Core_BAO_Address::add($fixParams, $fixAddress = TRUE);
+ CRM_Core_BAO_Address::fixAddress($fixParams);
+ CRM_Core_BAO_Address::writeRecord($fixParams);
$addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
'Database check for created contact address.'
'contact_id' => $contactId,
];
- CRM_Core_BAO_Address::add($fixParams, $fixAddress = TRUE);
+ CRM_Core_BAO_Address::fixAddress($fixParams);
+ CRM_Core_BAO_Address::writeRecord($fixParams);
$addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
'Database check for created contact address.'
'is_primary' => '1',
'contact_id' => $contactIdA,
];
- $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
+ $addAddressA = CRM_Core_BAO_Address::writeRecord($addressParamsA);
$addressParamsB = [
'street_address' => '123 Fake St.',
'master_id' => $addAddressA->id,
'contact_id' => $contactIdB,
];
- $addAddressB = CRM_Core_BAO_Address::add($addressParamsB, FALSE);
+ $addAddressB = CRM_Core_BAO_Address::writeRecord($addressParamsB);
$addressParamsC = [
'street_address' => '123 Fake St.',
'master_id' => $addAddressB->id,
'contact_id' => $contactIdC,
];
- $addAddressC = CRM_Core_BAO_Address::add($addressParamsC, FALSE);
+ $addAddressC = CRM_Core_BAO_Address::writeRecord($addressParamsC);
$updatedAddressParamsA = [
'id' => $addAddressA->id,
'is_primary' => '1',
'contact_id' => $contactIdA,
];
- $updatedAddressA = CRM_Core_BAO_Address::add($updatedAddressParamsA, FALSE);
+ $updatedAddressA = CRM_Core_BAO_Address::writeRecord($updatedAddressParamsA);
// CRM-21214 - Has Address C been updated with Address A's new values?
$newAddressC = new CRM_Core_DAO_Address();
'is_primary' => '1',
'contact_id' => $contactIdA,
];
- $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
+ $addAddressA = CRM_Core_BAO_Address::writeRecord($addressParamsA);
$addressParamsB = [
'street_address' => '123 Fake St.',
'is_primary' => '1',
'contact_id' => $contactIdB,
];
- $addAddressB = CRM_Core_BAO_Address::add($addressParamsB, FALSE);
+ $addAddressB = CRM_Core_BAO_Address::writeRecord($addressParamsB);
$addressParamsC = [
'street_address' => '123 Fake St.',
'master_id' => $addAddressA->id,
'contact_id' => $contactIdC,
];
- $addAddressC = CRM_Core_BAO_Address::add($addressParamsC, FALSE);
+ $addAddressC = CRM_Core_BAO_Address::writeRecord($addressParamsC);
$updatedAddressParamsA = [
'id' => $addAddressA->id,
'master_id' => $addAddressB->id,
'contact_id' => $contactIdA,
];
- $updatedAddressA = CRM_Core_BAO_Address::add($updatedAddressParamsA, FALSE);
+ $updatedAddressA = CRM_Core_BAO_Address::writeRecord($updatedAddressParamsA);
$updatedAddressParamsB = [
'id' => $addAddressB->id,
'is_primary' => '1',
'contact_id' => $contactIdB,
];
- $updatedAddressB = CRM_Core_BAO_Address::add($updatedAddressParamsB, FALSE);
+ $updatedAddressB = CRM_Core_BAO_Address::writeRecord($updatedAddressParamsB);
// CRM-21214 - Has Address C been updated with Address B's new values?
$newAddressC = new CRM_Core_DAO_Address();
'is_primary' => '1',
'contact_id' => $contactIdA,
];
- $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
+ $addAddressA = CRM_Core_BAO_Address::writeRecord($addressParamsA);
$updatedAddressParamsA = [
'id' => $addAddressA->id,
'master_id' => $addAddressA->id,
'contact_id' => $contactIdA,
];
- $updatedAddressA = CRM_Core_BAO_Address::add($updatedAddressParamsA, FALSE);
+ $updatedAddressA = CRM_Core_BAO_Address::writeRecord($updatedAddressParamsA);
// CRM-21214 - AdressA shouldn't be master of itself.
$this->assertEmpty($updatedAddressA->master_id);
'contact_id' => $contactIdA,
$customField => 'this is a custom text field',
];
+ $addressParamsA['custom'] = CRM_Core_BAO_CustomField::postProcess($addressParamsA, NULL, 'Address');
- $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
+ $addAddressA = CRM_Core_BAO_Address::writeRecord($addressParamsA);
// without having the custom field, we should still copy the values from master
$addressParamsB = [
'master_id' => $addAddressA->id,
'contact_id' => $contactIdB,
];
- $addAddressB = CRM_Core_BAO_Address::add($addressParamsB, FALSE);
+ $addAddressB = CRM_Core_BAO_Address::writeRecord($addressParamsB);
// 1. check if the custom fields values have been copied from master to shared address
$address = $this->callAPISuccessGetSingle('Address', ['id' => $addAddressB->id, 'return' => $this->getCustomFieldName('text')]);
// 2. now, we update addressA custom field to see if it goes into addressB
$addressParamsA['id'] = $addAddressA->id;
$addressParamsA[$customField] = 'updated custom text field';
- $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
+ $addressParamsA['custom'] = CRM_Core_BAO_CustomField::postProcess($addressParamsA, NULL, 'Address');
+ CRM_Core_BAO_Address::writeRecord($addressParamsA);
$address = $this->callAPISuccessGetSingle('Address', ['id' => $addAddressB->id, 'return' => $this->getCustomFieldName('text')]);
$this->assertEquals($addressParamsA[$customField], $address[$customField]);
'contact_id' => $contactId,
];
- $addAddress = CRM_Core_BAO_Address::add($fixParams, TRUE);
+ CRM_Core_BAO_Address::fixAddress($fixParams);
+ $addAddress = CRM_Core_BAO_Address::writeRecord($fixParams);
$addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
'Database check for created contact address.'
}
/**
- * FIXME: Api4 custom address fields broken?
+ * @param int $version
+ * @dataProvider versionThreeAndFour
*/
- public function testGetWithCustom() {
+ public function testGetWithCustom($version) {
+ $this->_apiversion = $version;
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
$params = $this->_params;
'is_primary' => 1,
];
// api requires contact_id - perhaps incorrectly but use add to get past that.
- $address = CRM_Core_BAO_Address::add($addressParams);
+ $address = CRM_Core_BAO_Address::writeRecord($addressParams);
$location = $this->callAPISuccess('LocBlock', 'create', ['address_id' => $address->id]);
$this->callAPISuccess('Event', 'create', [