Skip to content

Commit

Permalink
Merge pull request #25944 from colemanw/writeRecordRef
Browse files Browse the repository at this point in the history
[REF] Refactor location-related BAOs to use `writeRecord`
  • Loading branch information
colemanw committed Mar 30, 2023
2 parents dee8a1e + 909e0a6 commit 5054b50
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 177 deletions.
2 changes: 1 addition & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ public static function getContributionDetails($exportMode, $componentIds) {
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;
Expand Down
119 changes: 51 additions & 68 deletions CRM/Core/BAO/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,96 +20,70 @@
/**
* 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;
}

/**
Expand Down Expand Up @@ -1336,7 +1310,6 @@ public static function legacyCreate(array $params, bool $fixAddress) {
return NULL;
}
CRM_Core_BAO_Block::sortPrimaryFirst($params['address']);
$contactId = NULL;

$updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE);
$contactId = $params['contact_id'];
Expand Down Expand Up @@ -1386,7 +1359,17 @@ public static function legacyCreate(array $params, bool $fixAddress) {
$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;
}
Expand Down
5 changes: 4 additions & 1 deletion CRM/Core/BAO/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ public static function create($blockName, $params) {
}

$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;
Expand Down
8 changes: 6 additions & 2 deletions CRM/Core/BAO/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class CRM_Core_BAO_Email extends CRM_Core_DAO_Email implements Civi\Core\HookInt

/**
* @deprecated
*
* @param array $params
* @return CRM_Core_BAO_Email
* @throws CRM_Core_Exception
*/
public static function create($params) {
// FIXME: switch CRM_Core_BAO_Block::create to call writeRecord (once Address, IM, Phone create functions go through it)
// then this can be uncommented:
// CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
return self::writeRecord($params);
}

Expand All @@ -41,7 +43,7 @@ public static function create($params) {
*/
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, get_class());
CRM_Core_BAO_Block::handlePrimary($event->params, __CLASS__);

if (!empty($event->params['email'])) {
// lower case email field to optimize queries
Expand Down Expand Up @@ -96,8 +98,10 @@ public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event)

/**
* @deprecated
*
* @param array $params
* @return CRM_Core_BAO_Email
* @throws CRM_Core_Exception
*/
public static function add($params) {
CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
Expand Down
29 changes: 17 additions & 12 deletions CRM/Core/BAO/IM.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,39 @@
/**
* This class contain function for IM handling
*/
class CRM_Core_BAO_IM extends CRM_Core_DAO_IM {
class CRM_Core_BAO_IM extends CRM_Core_DAO_IM implements Civi\Core\HookInterface {
use CRM_Contact_AccessTrait;

/**
* Create or update IM record.
* @deprecated
*
* @param array $params
*
* @return \CRM_Core_DAO|\CRM_Core_DAO_IM
* @throws \CRM_Core_Exception
* @return CRM_Core_DAO_IM
* @throws CRM_Core_Exception
*/
public static function create($params) {
CRM_Core_BAO_Block::handlePrimary($params, __CLASS__);
CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
return self::writeRecord($params);
}

/**
* Create or update IM record.
*
* Event fired before modifying an IM.
* @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__);
}
}

/**
* @deprecated
*
* @param array $params
*
* @return \CRM_Core_DAO|\CRM_Core_DAO_IM
* @throws \CRM_Core_Exception
* @return CRM_Core_DAO_IM
* @throws CRM_Core_Exception
*/
public static function add($params) {
CRM_Core_Error::deprecatedFunctionWarning('use the v4 api');
return self::create($params);
}

Expand Down
28 changes: 16 additions & 12 deletions CRM/Core/BAO/OpenID.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,39 @@
/**
* This class contains function for Open Id
*/
class CRM_Core_BAO_OpenID extends CRM_Core_DAO_OpenID {
class CRM_Core_BAO_OpenID extends CRM_Core_DAO_OpenID implements Civi\Core\HookInterface {
use CRM_Contact_AccessTrait;

/**
* Create or update OpenID record.
* @deprecated
*
* @param array $params
*
* @return CRM_Core_DAO_OpenID
*
* @throws \CRM_Core_Exception
* @throws CRM_Core_Exception
*/
public static function create($params) {
CRM_Core_BAO_Block::handlePrimary($params, __CLASS__);
CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
return self::writeRecord($params);
}

/**
* Create or update OpenID record.
*
* Event fired before modifying an OpenID.
* @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__);
}
}

/**
* @deprecated
*
* @param array $params
*
* @return \CRM_Core_DAO|\CRM_Core_DAO_IM
* @throws \CRM_Core_Exception
* @return CRM_Core_DAO_OpenID
* @throws CRM_Core_Exception
*/
public static function add($params) {
CRM_Core_Error::deprecatedFunctionWarning('use the v4 api');
return self::create($params);
}

Expand Down
34 changes: 17 additions & 17 deletions CRM/Core/BAO/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,39 @@
/**
* Class contains functions for phone.
*/
class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone {
class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone implements Civi\Core\HookInterface {
use CRM_Contact_AccessTrait;

/**
* Create phone object - note that the create function calls 'add' but
* has more business logic
* @deprecated
*
* @param array $params
*
* @return \CRM_Core_DAO_Phone
*
* @throws \CRM_Core_Exception
* @return CRM_Core_DAO_Phone
* @throws CRM_Core_Exception
*/
public static function create($params) {
CRM_Core_BAO_Block::handlePrimary($params, get_class());
CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
return self::writeRecord($params);
}

/**
* Takes an associative array and adds phone.
*
* @deprecated use create.
* Event fired before modifying a Phone.
* @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__);
}
}

/**
* @deprecated
*
* @param array $params
* (reference ) an assoc array of name/value pairs.
*
* @return object
* CRM_Core_BAO_Phone object on success, null otherwise
*
* @return CRM_Core_DAO_Phone
* @throws \CRM_Core_Exception
*/
public static function add($params) {
CRM_Core_Error::deprecatedFunctionWarning('Use the v4 api');
return self::create($params);
}

Expand Down
Loading

0 comments on commit 5054b50

Please sign in to comment.