From 26c7cecb7c6d4f6811c2ecc96eb25be6fafb28a2 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 24 Aug 2022 12:10:27 -0400 Subject: [PATCH] APIv4 - Accept `match` param for Export action This makes the Export action a little friendlier by inserting the `match` param for you, if specified. --- Civi/Api4/Generic/AbstractSaveAction.php | 30 +----------- Civi/Api4/Generic/ExportAction.php | 7 ++- Civi/Api4/Generic/Traits/MatchParamTrait.php | 50 ++++++++++++++++++++ 3 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 Civi/Api4/Generic/Traits/MatchParamTrait.php diff --git a/Civi/Api4/Generic/AbstractSaveAction.php b/Civi/Api4/Generic/AbstractSaveAction.php index d61718d646..3ab1838035 100644 --- a/Civi/Api4/Generic/AbstractSaveAction.php +++ b/Civi/Api4/Generic/AbstractSaveAction.php @@ -38,6 +38,7 @@ use Civi\Api4\Utils\CoreUtil; * @package Civi\Api4\Generic */ abstract class AbstractSaveAction extends AbstractAction { + use Traits\MatchParamTrait; /** * Array of $ENTITIES to save. @@ -71,20 +72,6 @@ abstract class AbstractSaveAction extends AbstractAction { */ protected $reload = FALSE; - /** - * Specify fields to match for update. - * - * Normally each record is either created or updated based on the presence of an `id`. - * Specifying `$match` fields will also perform an update if an existing $ENTITY matches all specified fields. - * - * Note: the fields named in this param should be without any options suffix (e.g. `my_field` not `my_field:name`). - * Any options suffixes in the $records will be resolved by the api prior to matching. - * - * @var array - * @optionsCallback getMatchFields - */ - protected $match = []; - /** * @throws \API_Exception * @throws \Civi\API\Exception\UnauthorizedException @@ -194,19 +181,4 @@ abstract class AbstractSaveAction extends AbstractAction { return $this; } - /** - * Options callback for $this->match - * @return array - */ - protected function getMatchFields() { - return (array) civicrm_api4($this->getEntityName(), 'getFields', [ - 'checkPermissions' => FALSE, - 'action' => 'get', - 'where' => [ - ['type', 'IN', ['Field', 'Custom']], - ['name', 'NOT IN', (array) CoreUtil::getInfoItem($this->getEntityName(), 'primary_key')], - ], - ], ['name']); - } - } diff --git a/Civi/Api4/Generic/ExportAction.php b/Civi/Api4/Generic/ExportAction.php index b7e7371796..e7cce1bb9f 100644 --- a/Civi/Api4/Generic/ExportAction.php +++ b/Civi/Api4/Generic/ExportAction.php @@ -29,6 +29,7 @@ use Civi\Api4\Utils\CoreUtil; * @method string getUpdate() */ class ExportAction extends AbstractAction { + use Traits\MatchParamTrait; /** * Id of $ENTITY to export @@ -145,7 +146,7 @@ class ExportAction extends AbstractAction { unset($record[$fieldName]); } } - $result[] = [ + $export = [ 'name' => $name, 'entity' => $entityType, 'cleanup' => $this->cleanup, @@ -155,6 +156,10 @@ class ExportAction extends AbstractAction { 'values' => $record, ], ]; + foreach (array_intersect($this->match, array_keys($allFields)) as $match) { + $export['params']['match'][] = $match; + } + $result[] = $export; // Export entities that reference this one $daoName = CoreUtil::getInfoItem($entityType, 'dao'); if ($daoName) { diff --git a/Civi/Api4/Generic/Traits/MatchParamTrait.php b/Civi/Api4/Generic/Traits/MatchParamTrait.php new file mode 100644 index 0000000000..6d8ed6c786 --- /dev/null +++ b/Civi/Api4/Generic/Traits/MatchParamTrait.php @@ -0,0 +1,50 @@ +match + * @return array + */ + protected function getMatchFields() { + return (array) civicrm_api4($this->getEntityName(), 'getFields', [ + 'checkPermissions' => FALSE, + 'action' => 'get', + 'where' => [ + ['type', 'IN', ['Field', 'Custom']], + ['readonly', '!=', TRUE], + ], + ], ['name']); + } + +} -- 2.25.1