APIv4 - Accept `match` param for Export action
authorColeman Watts <coleman@civicrm.org>
Wed, 24 Aug 2022 16:10:27 +0000 (12:10 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 24 Aug 2022 16:10:27 +0000 (12:10 -0400)
This makes the Export action a little friendlier by inserting the `match`
param for you, if specified.

Civi/Api4/Generic/AbstractSaveAction.php
Civi/Api4/Generic/ExportAction.php
Civi/Api4/Generic/Traits/MatchParamTrait.php [new file with mode: 0644]

index d61718d646e0ea9acf229beadfd17ef64a07d723..3ab1838035890d2cb37312cf54ff285852b507f4 100644 (file)
@@ -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']);
-  }
-
 }
index b7e73717960ef36592aed1f02ee26d5f1b4ab2c5..e7cce1bb9fe70140ca5ed7f5482bedcdda26ec3b 100644 (file)
@@ -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 (file)
index 0000000..6d8ed6c
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Generic\Traits;
+
+/**
+ * @method $this setMatch(array $match) Specify fields to match for update.
+ * @method bool getMatch()
+ * @package Civi\Api4\Generic
+ */
+trait MatchParamTrait {
+
+  /**
+   * Specify fields to match for update.
+   *
+   * The API will 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 = [];
+
+  /**
+   * 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']],
+        ['readonly', '!=', TRUE],
+      ],
+    ], ['name']);
+  }
+
+}