Import API - remove unused actions, use trait for create/save/update actions
authorColeman Watts <coleman@civicrm.org>
Wed, 17 Aug 2022 18:29:17 +0000 (14:29 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 18 Aug 2022 13:28:29 +0000 (09:28 -0400)
ext/civiimport/Civi/Api4/Import.php
ext/civiimport/Civi/Api4/Import/Create.php [new file with mode: 0644]
ext/civiimport/Civi/Api4/Import/ImportSaveTrait.php [new file with mode: 0644]
ext/civiimport/Civi/Api4/Import/Save.php
ext/civiimport/Civi/Api4/Import/Update.php
ext/civiimport/Civi/Api4/Service/Spec/Provider/ImportSpecProvider.php
ext/civiimport/Civi/BAO/Import.php

index 55c3c3a330eaa182e1bebbf726fe3d2204b49a9e..c44a68a921703f45b440f38a985230a50540b265 100644 (file)
  */
 namespace Civi\Api4;
 
-use Civi\Api4\Generic\BasicReplaceAction;
 use Civi\Api4\Generic\CheckAccessAction;
-use Civi\Api4\Generic\DAOCreateAction;
-use Civi\Api4\Generic\DAODeleteAction;
 use Civi\Api4\Generic\DAOGetAction;
 use Civi\Api4\Generic\DAOGetFieldsAction;
 use Civi\Api4\Action\GetActions;
+use Civi\Api4\Import\Create;
 use Civi\Api4\Import\Save;
 use Civi\Api4\Import\Update;
 
@@ -68,8 +66,8 @@ class Import {
    * @return \Civi\Api4\Generic\DAOCreateAction
    * @throws \API_Exception
    */
-  public static function create(int $userJobID, bool $checkPermissions = TRUE): DAOCreateAction {
-    return (new DAOCreateAction('Import_' . $userJobID, __FUNCTION__))
+  public static function create(int $userJobID, bool $checkPermissions = TRUE): Create {
+    return (new Create('Import_' . $userJobID, __FUNCTION__))
       ->setCheckPermissions($checkPermissions);
   }
 
@@ -84,28 +82,6 @@ class Import {
       ->setCheckPermissions($checkPermissions);
   }
 
-  /**
-   * @param int $userJobID
-   * @param bool $checkPermissions
-   * @return \Civi\Api4\Generic\DAODeleteAction
-   * @throws \API_Exception
-   */
-  public static function delete(int $userJobID, bool $checkPermissions = TRUE): DAODeleteAction {
-    return (new DAODeleteAction('Import_' . $userJobID, __FUNCTION__))
-      ->setCheckPermissions($checkPermissions);
-  }
-
-  /**
-   * @param int $userJobID
-   * @param bool $checkPermissions
-   * @return \Civi\Api4\Generic\BasicReplaceAction
-   * @throws \API_Exception
-   */
-  public static function replace(int $userJobID, bool $checkPermissions = TRUE): BasicReplaceAction {
-    return (new BasicReplaceAction('Import_' . $userJobID, __FUNCTION__))
-      ->setCheckPermissions($checkPermissions);
-  }
-
   /**
    * @param int $userJobID
    * @param bool $checkPermissions
diff --git a/ext/civiimport/Civi/Api4/Import/Create.php b/ext/civiimport/Civi/Api4/Import/Create.php
new file mode 100644 (file)
index 0000000..af9a4f1
--- /dev/null
@@ -0,0 +1,21 @@
+<?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\Import;
+
+use Civi\Api4\Generic\DAOCreateAction;
+
+class Create extends DAOCreateAction {
+
+  use ImportSaveTrait;
+
+}
diff --git a/ext/civiimport/Civi/Api4/Import/ImportSaveTrait.php b/ext/civiimport/Civi/Api4/Import/ImportSaveTrait.php
new file mode 100644 (file)
index 0000000..1a9227e
--- /dev/null
@@ -0,0 +1,31 @@
+<?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\Import;
+
+/**
+ * Code shared by Import Save/Update actions.
+ */
+trait ImportSaveTrait {
+
+  /**
+   * @inheritDoc
+   */
+  protected function write(array $items) {
+    $userJobID = str_replace('Import_', '', $this->_entityName);
+    foreach ($items as &$item) {
+      $item['_user_job_id'] = (int) $userJobID;
+    }
+    return parent::write($items);
+  }
+
+}
index e77519f9b7e76dbd43788bd8acac8305560c2405..292dfeb52500456ea3ee1c7774180520d45e5fc0 100644 (file)
 namespace Civi\Api4\Import;
 
 use Civi\Api4\Generic\DAOSaveAction;
-use Civi\Api4\Generic\Result;
 
 class Save extends DAOSaveAction {
 
-  /**
-   * Import save action.
-   *
-   * This is copied from `DAOSaveAction` to add the user_job_id to the array & to
-   * the reference to '_id' not 'id'.
-   *
-   * @inheritDoc
-   */
-  public function _run(Result $result): void {
-    $userJobID = str_replace('Import_', '', $this->_entityName);
-    $this->defaults['user_job_id'] = (int) $userJobID;
-    parent::_run($result);
-  }
+  use ImportSaveTrait;
 
 }
index 988e871049b9cda138ee5043518e2857d85e707a..1902d09c1edfbeb7b830e25764316566aa0d3356 100644 (file)
@@ -5,20 +5,6 @@ use Civi\Api4\Generic\DAOUpdateAction;
 
 class Update extends DAOUpdateAction {
 
-  /**
-   * Update import table records.
-   *
-   * @param array $items
-   * @return array
-   * @throws \API_Exception
-   * @throws \CRM_Core_Exception
-   */
-  protected function updateRecords(array $items): array {
-    $userJobID = str_replace('Import_', '', $this->_entityName);
-    foreach ($items as &$item) {
-      $item['user_job_id'] = (int) $userJobID;
-    }
-    return parent::updateRecords($items);
-  }
+  use ImportSaveTrait;
 
 }
index 57861482df174ef8fda9d13020ae2f3e3da551a4..61480f8d2505817b4ede29bd0e0f399758a836eb 100644 (file)
@@ -32,16 +32,6 @@ class ImportSpecProvider implements Generic\SpecProviderInterface {
     $userJobID = substr($spec->getEntity(), (strpos($spec->getEntity(), '_') + 1));
     $userJob = UserJob::get(FALSE)->addWhere('id', '=', $userJobID)->addSelect('metadata', 'job_type', 'created_id')->execute()->first();
 
-    if (in_array($action, ['create', 'update'], TRUE)) {
-      $field = new FieldSpec('user_job_id', $spec->getEntity(), 'String');
-      $field->setTitle(ts('User Job ID'));
-      $field->setLabel(ts('User Job ID'));
-      $field->setFkEntity('UserJob');
-      $field->setReadonly(TRUE);
-      $field->setDefaultValue($userJobID);
-      $spec->addFieldSpec($field);
-    }
-
     $headers = $userJob['metadata']['DataSource']['column_headers'] ?? [];
     foreach ($columns as $column) {
       $isInternalField = strpos($column['name'], '_') === 0;
index a2ad5c9f855fbbcce4739edce0340f729605fdfe..a83e69295ca3f7b762598b636b11fd688d38cdbc 100644 (file)
@@ -70,7 +70,6 @@ class Import extends CRM_Core_DAO {
         continue;
       }
       $importEntities[$tables->id] = [
-        'user_job_id' => $tables->id,
         'table_name' => $tableName,
         'created_by' => $tables->display_name,
         'created_id' => $tables->created_id ? (int) $tables->created_id : NULL,
@@ -180,13 +179,13 @@ class Import extends CRM_Core_DAO {
    * @throws \CRM_Core_Exception
    */
   public static function writeRecord(array $record): CRM_Core_DAO {
-    $op = empty($record['id']) ? 'create' : 'edit';
-    $userJobID = $record['user_job_id'];
+    $op = empty($record['_id']) ? 'create' : 'edit';
+    $userJobID = $record['_user_job_id'];
     $entityName = 'Import_' . $userJobID;
     $userJob = UserJob::get($record['check_permissions'])->addWhere('id', '=', $userJobID)->addSelect('metadata', 'job_type', 'created_id')->execute()->first();
 
     $tableName = $userJob['metadata']['DataSource']['table_name'];
-    CRM_Utils_Hook::pre($op, $entityName, $record['id'] ?? NULL, $record);
+    CRM_Utils_Hook::pre($op, $entityName, $record['_id'] ?? NULL, $record);
     $fields = self::getAllFields($tableName);
     $instance = new self();
     $instance->__table = $tableName;