Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#5634 Add ImportTemplateField entity #31580

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/Contribute/Import/Parser/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function getUserJobInfo(): array {
* @throws \CRM_Core_Exception
*/
protected function getFieldMappings(): array {
$mappedFields = $this->getUserJob()['metadata']['import_mappings'] ?? [];
$mappedFields = $this->getUserJob()['template_fields'] ?? [];
if (empty($mappedFields)) {
foreach ($this->getSubmittedValue('mapper') as $i => $mapperRow) {
$mappedField = $this->getMappingFieldFromMapperInput($mapperRow, 0, $i);
Expand Down
66 changes: 66 additions & 0 deletions CRM/Core/BAO/ImportTemplateField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Api4\Utils\CoreUtil;

/**
* Logic for ImportTemplateField entity
*/
class CRM_Core_BAO_ImportTemplateField extends CRM_Core_DAO_ImportTemplateField {

/**
* Pseudoconstant callback for the 'name' field
*/
public static function getImportableFieldOptions(string $fieldName, array $params):? array {
$values = $params['values'];
$entity = $values['entity'] ?? NULL;
if (!$entity && !empty($values['id'])) {
$entity = self::getDbVal('entity', $values['id']);
}
if (!$entity) {
return NULL;
}
return (array) civicrm_api4($entity, 'getFields', [
'action' => 'save',
'select' => ['name', 'label', 'description'],
'where' => [
['usage', 'CONTAINS', 'import'],
],
]);
}

/**
* Pseudoconstant callback for the 'entity' field
*/
public static function getImportableEntityOptions(string $fieldName, array $params):? array {
$values = $params['values'];
$userJobId = $values['user_job_id'] ?? NULL;
if (!$userJobId && !empty($values['id'])) {
$userJobId = CRM_Core_BAO_UserJob::getDbVal('user_job_id', $values['id']);
}

if (!$userJobId) {
return NULL;
}
$entities = [];
$jobTypes = array_column(CRM_Core_BAO_UserJob::getTypes(), NULL, 'id');
$jobType = CRM_Core_BAO_UserJob::getDbVal('job_type', $values['user_job_id']);

$mainEntityName = $jobTypes[$jobType]['entity'] ?? NULL;
// TODO: For now each job type only supports one entity,
// so this select list doesn't (yet) have more than one option.
$entities[] = [
'id' => $mainEntityName,
'name' => $mainEntityName,
'label' => CoreUtil::getInfoItem($mainEntityName, 'title'),
'icon' => CoreUtil::getInfoItem($mainEntityName, 'icon'),
];

return $entities;
}

}
12 changes: 12 additions & 0 deletions CRM/Core/DAO/ImportTemplateField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

/**
* Placeholder class retained for legacy compatibility.
*/
class CRM_Core_DAO_ImportTemplateField extends CRM_Core_DAO_Base {
}
4 changes: 2 additions & 2 deletions CRM/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ protected function getMappedField(array $fieldMapping, int $mappingID, int $colu
* @throws \CRM_Core_Exception
*/
protected function saveMappingField(int $mappingID, int $columnNumber, bool $isUpdate = FALSE): void {
if (!empty($this->userJob['metadata']['import_mappings'])) {
if (!empty($this->userJob['template_fields'])) {
// In this case Civi-Import has already saved the mapping to civicrm_user_job.metadata
// and the code here is just keeping civicrm_mapping_field in sync.
// Eventually we hope to phase out the use of the civicrm_mapping data &
// just use UserJob and Import Templates (UserJob records with 'is_template' = 1
$mappedFieldData = $this->userJob['metadata']['import_mappings'][$columnNumber];
$mappedFieldData = $this->userJob['template_fields'][$columnNumber];
$mappedField = array_intersect_key($mappedFieldData, array_fill_keys(['name', 'column_number', 'entity_data'], TRUE));
$mappedField['mapping_id'] = $mappingID;
}
Expand Down
67 changes: 4 additions & 63 deletions CRM/Import/Forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,13 @@
* This class helps the forms within the import flow access submitted & parsed values.
*/
class CRM_Import_Forms extends CRM_Core_Form {

use \Civi\UserJob\UserJobTrait;

/**
* @var int
*/
protected $templateID;

/**
* User job id.
*
* This is the primary key of the civicrm_user_job table which is used to
* track the import.
*
* @var int
*/
protected $userJobID;

/**
* Name of the import mapping (civicrm_mapping).
*
Expand Down Expand Up @@ -86,36 +76,6 @@ public function getEntity() {
return $this->controller->getEntity();
}

/**
* @return int|null
*/
public function getUserJobID(): ?int {
if (!$this->userJobID && $this->get('user_job_id')) {
$this->userJobID = $this->get('user_job_id');
}
return $this->userJobID;
}

/**
* Set user job ID.
*
* @param int $userJobID
*/
public function setUserJobID(int $userJobID): void {
$this->userJobID = $userJobID;
// This set allows other forms in the flow ot use $this->get('user_job_id').
$this->set('user_job_id', $userJobID);
}

/**
* User job details.
*
* This is the relevant row from civicrm_user_job.
*
* @var array
*/
protected $userJob;

/**
* @var \CRM_Import_Parser
*/
Expand All @@ -131,25 +91,6 @@ public function setUserJobID(int $userJobID): void {
*/
protected $isQuickFormMode = TRUE;

/**
* Get User Job.
*
* API call to retrieve the userJob row.
*
* @return array
*
* @throws \CRM_Core_Exception
*/
protected function getUserJob(): array {
if (!$this->userJob) {
$this->userJob = UserJob::get()
->addWhere('id', '=', $this->getUserJobID())
->execute()
->first();
}
return $this->userJob;
}

/**
* Get submitted values stored in the user job.
*
Expand Down Expand Up @@ -566,7 +507,7 @@ protected function isUpdateTemplateJob(): bool {
*/
protected function getColumnHeaders(): array {
$headers = $this->getDataSourceObject()->getColumnHeaders();
$mappedFields = $this->getUserJob()['metadata']['import_mappings'] ?? [];
$mappedFields = $this->getUserJob()['template_fields'] ?? [];
if (!empty($mappedFields) && count($mappedFields) > count($headers)) {
// The user has mapped one or more non-database fields, add those in.
$userMappedFields = array_diff_key($mappedFields, $headers);
Expand Down Expand Up @@ -608,7 +549,7 @@ protected function getDataRows($statuses = [], int $limit = 0): array {
$statuses = (array) $statuses;
$rows = $this->getDataSourceObject()->setLimit($limit)->setStatuses($statuses)->getRows();
$headers = $this->getColumnHeaders();
$mappings = $this->getUserJob()['metadata']['import_mappings'] ?? [];
$mappings = $this->getUserJob()['template_fields'] ?? [];
foreach ($rows as &$row) {
foreach ($headers as $index => $header) {
if (!$header) {
Expand Down Expand Up @@ -843,7 +784,7 @@ protected function getParser() {
protected function getMappedFieldLabels(): array {
$mapper = [];
$parser = $this->getParser();
$importMappings = $this->getUserJob()['metadata']['import_mappings'] ?? [];
$importMappings = $this->getUserJob()['template_fields'] ?? [];
if (empty($importMappings)) {
foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mapping) {
$importMappings[] = $parser->getMappingFieldFromMapperInput((array) $mapping, 0, $columnNumber);
Expand Down
55 changes: 1 addition & 54 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* supported.
*/
abstract class CRM_Import_Parser implements UserJobInterface {
use \Civi\UserJob\UserJobTrait;

/**
* Return codes
Expand All @@ -41,23 +42,6 @@ abstract class CRM_Import_Parser implements UserJobInterface {
*/
const DUPLICATE_SKIP = 1, DUPLICATE_UPDATE = 4, DUPLICATE_FILL = 8, DUPLICATE_NOCHECK = 16;

/**
* User job id.
*
* This is the primary key of the civicrm_user_job table which is used to
* track the import.
*
* @var int
*/
protected $userJobID;

/**
* The user job in use.
*
* @var array
*/
protected $userJob;

/**
* Potentially ambiguous options.
*
Expand All @@ -79,31 +63,13 @@ abstract class CRM_Import_Parser implements UserJobInterface {
*/
protected $siteDefaultCountry = NULL;

/**
* @return int|null
*/
public function getUserJobID(): ?int {
return $this->userJobID;
}

/**
* Ids of contacts created this iteration.
*
* @var array
*/
protected $createdContacts = [];

/**
* Set user job ID.
*
* @param int $userJobID
*
* @return self
*/
public function setUserJobID(int $userJobID): self {
$this->userJobID = $userJobID;
return $this;
}

/**
* Countries that the site is restricted to
Expand All @@ -120,25 +86,6 @@ public function getTrackingFields(): array {
return [];
}

/**
* Get User Job.
*
* API call to retrieve the userJob row.
*
* @return array
*
* @throws \CRM_Core_Exception
*/
protected function getUserJob(): array {
if (empty($this->userJob)) {
$this->userJob = UserJob::get()
->addWhere('id', '=', $this->getUserJobID())
->execute()
->first();
}
return $this->userJob;
}

/**
* Get the relevant datasource object.
*
Expand Down
25 changes: 25 additions & 0 deletions Civi/Api4/ImportTemplateField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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;

/**
* ImportTemplateField entity - stores field mappings for import templates.
*
* @see \Civi\Api4\UFGroup
* @orderBy column_number
* @groupWeightsBy user_job_id
* @since 5.83
* @package Civi\Api4
*/
class ImportTemplateField extends Generic\DAOEntity {
use Generic\Traits\SortableEntity;

}
Loading