Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API - Consistently save custom data for v3 & v4 #24036

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CRM/ACL/BAO/ACLEntityRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static function entityTables(): array {
/**
* @param array $params
*
* @deprecated
* @return CRM_ACL_BAO_ACLEntityRole
*/
public static function create(&$params) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - already calls writeRecord
image

Expand Down
1 change: 1 addition & 0 deletions CRM/Core/BAO/ActionSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public static function getList($filterMapping = NULL, $filterValue = NULL): arra
* @param array $params
* An assoc array of name/value pairs.
*
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK
image

* @return CRM_Core_DAO_ActionSchedule
* @throws \CRM_Core_Exception
*/
Expand Down
2 changes: 2 additions & 0 deletions CRM/Core/BAO/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public function getLocationValues() {
* @param array $params
* @param int $id
*
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

* @return CRM_Core_DAO_Domain
* @throws \CRM_Core_Exception
*/
Expand All @@ -160,6 +161,7 @@ public static function edit($params, $id): CRM_Core_DAO_Domain {
/**
* Create or update domain.
*
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - not called from api anyway

* @param array $params
* @return CRM_Core_DAO_Domain
*/
Expand Down
1 change: 1 addition & 0 deletions CRM/Core/BAO/RecurringEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static function setStatus($status) {
/**
* Create or update a RecurringEntity.
*
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

image

* @param array $params
* @return CRM_Core_DAO_RecurringEntity
*/
Expand Down
5 changes: 2 additions & 3 deletions CRM/Core/BAO/Website.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
*
* @param array $params
*
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok
image

* @return CRM_Core_DAO_Website
* @throws \CRM_Core_Exception
*/
Expand All @@ -36,8 +37,6 @@ public static function create($params) {
/**
* Create website.
*
* If called in a legacy manner this, temporarily, fails back to calling the legacy function.
*
* @param array $params
*
* @return CRM_Core_DAO_Website
Expand All @@ -46,7 +45,7 @@ public static function create($params) {
*/
public static function add($params) {
CRM_Core_Error::deprecatedFunctionWarning('use apiv4');
return self::create($params);
return self::writeRecord($params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK
image

}

/**
Expand Down
40 changes: 10 additions & 30 deletions CRM/Mailing/BAO/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -1442,52 +1442,32 @@ public function &getGroupNames() {
* Add the mailings.
*
* @param array $params
* Reference array contains the values submitted by the form.
* @param array $ids
* Reference array contains the id.
*
*
* @return CRM_Mailing_DAO_Mailing
* @throws \Civi\API\Exception\UnauthorizedException
*/
public static function add(&$params, $ids = []) {
$id = $params['id'] ?? $ids['mailing_id'] ?? NULL;
public static function add($params) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok - ids was deprecated noisily in year zero (2020)

$id = $params['id'] ?? NULL;

if (empty($params['id']) && !empty($ids)) {
CRM_Core_Error::deprecatedWarning('Parameter $ids is no longer used by Mailing::add. Use the api or just pass $params');
}
if (!empty($params['check_permissions']) && CRM_Mailing_Info::workflowEnabled()) {
$params = self::processWorkflowPermissions($params);
}
$action = $id ? 'create' : 'edit';
CRM_Utils_Hook::pre($action, 'Mailing', $id, $params);

$mailing = new static();
if ($id) {
$mailing->id = $id;
$mailing->find(TRUE);
if (!$id) {
$params['domain_id'] = $params['domain_id'] ?? CRM_Core_Config::domainID();
}
$mailing->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());

if (((!$id && empty($params['replyto_email'])) || !isset($params['replyto_email'])) &&
if (
((!$id && empty($params['replyto_email'])) || !isset($params['replyto_email'])) &&
isset($params['from_email'])
) {
$params['replyto_email'] = $params['from_email'];
}
$mailing->copyValues($params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - changes to this function make sense


// CRM-20892 Unset Modifed Date here so that MySQL can correctly set an updated modfied date.
unset($mailing->modified_date);
$result = $mailing->save();
unset($params['modified_date']);

// CRM-20892 Re find record after saing so we can set the updated modified date in the result.
$mailing->find(TRUE);
$result = static::writeRecord($params);

if (isset($mailing->modified_date)) {
$result->modified_date = $mailing->modified_date;
}

CRM_Utils_Hook::post($action, 'Mailing', $mailing->id, $mailing);
// CRM-20892 Re find record after saing so we can set the updated modified date in the result.
$result->find(TRUE);

return $result;
}
Expand Down
1 change: 1 addition & 0 deletions CRM/Member/BAO/MembershipBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CRM_Member_BAO_MembershipBlock extends CRM_Member_DAO_MembershipBlock {
/**
* Create or update a MembershipBlock.
*
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok
image

* @param array $params
* @return CRM_Member_DAO_MembershipBlock
*/
Expand Down
8 changes: 4 additions & 4 deletions CRM/Pledge/BAO/PledgeBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public static function retrieve($params, &$defaults) {
*
* @param array $params
* (reference ) an assoc array of name/value pairs.
*
* @return CRM_Pledge_BAO_PledgeBlock
* @deprecated
* @return CRM_Pledge_DAO_PledgeBlock
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - this is long but completely lacking in goodness
image

public static function &create(&$params) {
$transaction = new CRM_Core_Transaction();
Expand All @@ -61,8 +61,8 @@ public static function &create(&$params) {
* Add or update pledgeBlock.
*
* @param array $params
*
* @return object
* @deprecated
* @return CRM_Pledge_DAO_PledgeBlock
*/
public static function add($params) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - this has some non-api-worthy horror show but nothing the api needs

image

// FIXME: This is assuming checkbox input like ['foo' => 1, 'bar' => 0, 'baz' => 1]. Not API friendly.
Expand Down
25 changes: 22 additions & 3 deletions api/v3/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1334,9 +1334,28 @@ function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) {
else {
// If we have custom fields the BAO may have taken care of it or we may have to.
// DAO::writeRecord always handles custom data.
// Otherwise guess based on the $extendsMap hard-coded list of BAOs that take care of custom data.
if (isset($params['custom']) && $fct !== 'writeRecord' && empty(CRM_Core_BAO_CustomQuery::$extendsMap[$entity])) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($entity)), $bao->id);
if (isset($params['custom']) && $fct !== 'writeRecord') {
// List of BAOs that write custom data in their create or add function.
$alreadyHandled = array_keys(CRM_Core_BAO_CustomQuery::$extendsMap);
$alreadyHandled[] = 'ActivityContact';
$alreadyHandled[] = 'Batch';
$alreadyHandled[] = 'CustomField';
$alreadyHandled[] = 'EntityBatch';
$alreadyHandled[] = 'IM';
$alreadyHandled[] = 'Mailing';
$alreadyHandled[] = 'MailingAB';
$alreadyHandled[] = 'OpenID';
$alreadyHandled[] = 'Phone';
$alreadyHandled[] = 'PledgePayment';
$alreadyHandled[] = 'PriceField';
$alreadyHandled[] = 'PriceFieldValue';
$alreadyHandled[] = 'RelationshipType';
$alreadyHandled[] = 'SavedSearch';
$alreadyHandled[] = 'Tag';
$alreadyHandled[] = 'Website';
if (!in_array($entity, $alreadyHandled)) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($entity)), $bao->id);
}
}
$values = [];
_civicrm_api3_object_to_array($bao, $values[$bao->id]);
Expand Down