Skip to content

Commit

Permalink
Import API - remove unused actions, use trait for create/save/update …
Browse files Browse the repository at this point in the history
…actions
  • Loading branch information
colemanw committed Aug 18, 2022
1 parent 7b85de9 commit 6aa7ab7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 58 deletions.
30 changes: 3 additions & 27 deletions ext/civiimport/Civi/Api4/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
*/
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;

Expand Down Expand Up @@ -68,8 +66,8 @@ public static function save(int $userJobID, bool $checkPermissions = TRUE): Save
* @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);
}

Expand All @@ -84,28 +82,6 @@ public static function update(int $userJobID, bool $checkPermissions = TRUE): Up
->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
Expand Down
21 changes: 21 additions & 0 deletions ext/civiimport/Civi/Api4/Import/Create.php
Original file line number Diff line number Diff line change
@@ -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;

}
34 changes: 34 additions & 0 deletions ext/civiimport/Civi/Api4/Import/ImportSaveTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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;
if (empty($item['_id'])) {
throw new \CRM_Core_Exception('Import records can only be updated not created; "_id" is required.');
}
}
return parent::write($items);
}

}
15 changes: 1 addition & 14 deletions ext/civiimport/Civi/Api4/Import/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,9 @@
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;

}
16 changes: 1 addition & 15 deletions ext/civiimport/Civi/Api4/Import/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@

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;

}
4 changes: 2 additions & 2 deletions ext/civiimport/Civi/BAO/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ public function table(): array {
* @throws \CRM_Core_Exception
*/
public static function writeRecord(array $record): CRM_Core_DAO {
$op = empty($record['id']) ? 'create' : 'edit';
$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;
Expand Down

0 comments on commit 6aa7ab7

Please sign in to comment.