*/
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;
* @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);
}
->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
--- /dev/null
+<?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;
+
+}
--- /dev/null
+<?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);
+ }
+
+}
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;
}
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;
}
$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;
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,
* @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;