Skip to content

Commit

Permalink
Merge pull request #20264 from colemanw/afformJoinSaveFix
Browse files Browse the repository at this point in the history
Afform - Fix saving joined entities (email, address, phone, etc)
  • Loading branch information
seamuslee001 authored May 13, 2021
2 parents ca90123 + 6c1e958 commit fbd8d65
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ protected static function getJoinWhereClause($mainEntityName, $joinEntityName, $
if (self::fieldExists($joinEntityName, 'entity_id')) {
$params[] = ['entity_id', '=', $mainEntityId];
if (self::fieldExists($joinEntityName, 'entity_table')) {
$params[] = ['entity_table', '=', 'civicrm_' . _civicrm_api_get_entity_name_from_camel($mainEntityName)];
$params[] = ['entity_table', '=', 'civicrm_' . \CRM_Core_DAO_AllCoreTables::convertEntityNameToLower($mainEntityName)];
}
}
else {
$mainEntityField = _civicrm_api_get_entity_name_from_camel($mainEntityName) . '_id';
$mainEntityField = \CRM_Core_DAO_AllCoreTables::convertEntityNameToLower($mainEntityName) . '_id';
$params[] = [$mainEntityField, '=', $mainEntityId];
}
return $params;
Expand Down
19 changes: 13 additions & 6 deletions ext/afform/core/Civi/Api4/Action/Afform/Submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function processContacts(AfformSubmitEvent $event) {
$api4 = $event->formDataModel->getSecureApi4($entityName);
foreach ($contacts as $contact) {
$saved = $api4('Contact', 'save', ['records' => [$contact['fields']]])->first();
self::saveJoins($api4, 'Contact', $saved['id'], $contact['joins'] ?? []);
self::saveJoins('Contact', $saved['id'], $contact['joins'] ?? []);
}
}
unset($event->entityValues['Contact']);
Expand All @@ -72,26 +72,33 @@ public static function processGenericEntity(AfformSubmitEvent $event) {
$api4 = $event->formDataModel->getSecureApi4($entityName);
foreach ($records as $record) {
$saved = $api4($entityType, 'save', ['records' => [$record['fields']]])->first();
self::saveJoins($api4, $entityType, $saved['id'], $record['joins'] ?? []);
self::saveJoins($entityType, $saved['id'], $record['joins'] ?? []);
}
}
unset($event->entityValues[$entityType]);
}
}

protected static function saveJoins($api4, $mainEntityName, $entityId, $joins) {
protected static function saveJoins($mainEntityName, $entityId, $joins) {
foreach ($joins as $joinEntityName => $join) {
$values = self::filterEmptyJoins($joinEntityName, $join);
// FIXME: Replace/delete should only be done to known contacts
// TODO: REPLACE works for creating or updating contacts, but different logic would be needed if
// the contact was being auto-updated via a dedupe rule; in that case we would not want to
// delete any existing records.
if ($values) {
$api4($joinEntityName, 'replace', [
civicrm_api4($joinEntityName, 'replace', [
// Disable permission checks because the main entity has already been vetted
'checkPermissions' => FALSE,
'where' => self::getJoinWhereClause($mainEntityName, $joinEntityName, $entityId),
'records' => $values,
]);
}
// REPLACE doesn't work if there are no records, have to use DELETE
else {
try {
$api4($joinEntityName, 'delete', [
civicrm_api4($joinEntityName, 'delete', [
// Disable permission checks because the main entity has already been vetted
'checkPermissions' => FALSE,
'where' => self::getJoinWhereClause($mainEntityName, $joinEntityName, $entityId),
]);
}
Expand Down

0 comments on commit fbd8d65

Please sign in to comment.