From 6c1e9587bd2740f03ec9f366fdc32706b4c6cf93 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 10 May 2021 11:18:16 -0400 Subject: [PATCH] Afform - Fix saving joined entities (email, address, phone, etc) --- .../Api4/Action/Afform/AbstractProcessor.php | 4 ++-- .../core/Civi/Api4/Action/Afform/Submit.php | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php b/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php index 4d1799bd78..1682f9bf5b 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php @@ -75,11 +75,11 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction { 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; diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php index 8c81828147..ec505c10c4 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php @@ -54,7 +54,7 @@ class Submit extends AbstractProcessor { $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']); @@ -72,26 +72,33 @@ class Submit extends AbstractProcessor { $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), ]); } -- 2.25.1