* @return array
*/
public static function getTypes(): array {
- $types = Civi::cache()->get('UserJobTypes');
+ $types = Civi::cache('metadata')->get('UserJobTypes');
if ($types === NULL) {
$types = [];
$classes = ClassScanner::get(['interface' => UserJobInterface::class]);
}
$types = array_merge($types, $declaredTypes);
}
- Civi::cache()->set('UserJobTypes', $types);
+ Civi::cache('metadata')->set('UserJobTypes', $types);
}
// Rekey to numeric to prevent https://lab.civicrm.org/dev/core/-/issues/3719
return array_values($types);
use Civi\BAO\Import;
use Civi\Core\Service\AutoService;
use CRM_Core_BAO_UserJob;
+use CRM_Civiimport_ExtensionUtil as E;
/**
* @service
// CheckPermissions does not reach us here - so we will have to rely on earlier permission filters.
$userJobID = substr($spec->getEntity(), (strpos($spec->getEntity(), '_') + 1));
$userJob = UserJob::get(FALSE)->addWhere('id', '=', $userJobID)->addSelect('metadata', 'job_type', 'created_id')->execute()->first();
-
+ $entity = CRM_Core_BAO_UserJob::getType($userJob['job_type'])['entity'];
foreach ($columns as $column) {
$isInternalField = strpos($column['name'], '_') === 0;
$exists = $isInternalField && $spec->getFieldByName($column['name']);
$field->setTitle(ts('Import field') . ':' . $column['label']);
$field->setLabel($column['label']);
$field->setType('Field');
+ $field->setDataType($column['data_type']);
$field->setReadonly($isInternalField);
$field->setDescription(ts('Data being imported into the field.'));
$field->setColumnName($column['name']);
if ($userJob['job_type'] === $jobType['id'] && $jobType['entity']) {
$field->setFkEntity($jobType['entity']);
$field->setInputType('EntityRef');
+ $field->setInputAttrs(['label' => $entity]);
}
}
}
public function table(): array {
$table = [];
foreach (self::getFieldsForTable($this->tableName()) as $value) {
- $table[$value['name']] = $value['type'] ?? CRM_Utils_Type::T_STRING;
+ $table[$value['name']] = ($value['data_type'] === 'Integer') ? CRM_Utils_Type::T_INT : CRM_Utils_Type::T_STRING;
if (!empty($value['required'])) {
$table[$value['name']] += self::DB_DAO_NOTNULL;
}
* @throws \CRM_Core_Exception
*/
public static function getFieldsForTable(string $tableName): array {
+ $cacheKey = 'civiimport_table_fields' . $tableName;
+ if (\Civi::cache('metadata')->has($cacheKey)) {
+ return \Civi::cache('metadata')->get($cacheKey);
+ }
if (!CRM_Utils_Rule::alphanumeric($tableName)) {
// This is purely precautionary so does not need to be a translated string.
throw new CRM_Core_Exception('Invalid import table');
}
$columns = [];
- $headers = UserJob::get(FALSE)
+ $userJob = UserJob::get(FALSE)
->addWhere('metadata', 'LIKE', '%' . $tableName . '%')
- ->addSelect('metadata')->execute()->first()['metadata']['DataSource']['column_headers'] ?? [];
+ ->addSelect('metadata', 'job_type')->execute()->first();
+ $headers = $userJob['metadata']['DataSource']['column_headers'] ?? [];
+ $entity = \CRM_Core_BAO_UserJob::getType($userJob['job_type'])['entity'];
+
try {
$result = CRM_Core_DAO::executeQuery("SHOW COLUMNS FROM $tableName");
}
}
throw $e;
}
+
$userFieldIndex = 0;
while ($result->fetch()) {
$columns[$result->Field] = ['name' => $result->Field, 'table_name' => $tableName];
- if (substr($result->Field, 1) !== '_') {
- $columns[$result->Field]['label'] = $headers[$userFieldIndex] ?? $result->Field;
+ if (strpos($result->Field, '_') !== 0) {
+ $columns[$result->Field]['label'] = ts('Import field') . ':' . ($headers[$userFieldIndex] ?? $result->Field);
+ $columns[$result->Field]['data_type'] = 'String';
$userFieldIndex++;
}
+ else {
+ $columns[$result->Field]['label'] = ($result->Field === '_entity_id') ? E::ts('Row Imported to %1 ID', [1 => $entity]) : $result->Field;
+ $columns[$result->Field]['fk_entity'] = ($result->Field === '_entity_id') ? $entity : NULL;
+ $columns[$result->Field]['data_type'] = strpos($result->Type, 'int') === 0 ? 'Integer' : 'String';
+ }
}
+ \Civi::cache('metadata')->set($cacheKey, $columns);
return $columns;
}
$importEntities = Import::getImportTables();
foreach ($importEntities as $importEntity) {
try {
- $fields = array_merge(['_id' => TRUE, '_status' => TRUE, '_status_message' => TRUE], Import::getFieldsForUserJobID($importEntity['user_job_id'], FALSE));
+ $fields = array_merge(['_id' => TRUE, '_status' => TRUE, '_status_message' => TRUE, '_entity_id' => TRUE], Import::getFieldsForUserJobID($importEntity['user_job_id'], FALSE));
}
catch (CRM_Core_Exception $e) {
continue;
}
+ $fields['_entity_id']['link'] = [
+ 'entity' => $fields['_entity_id']['fk_entity'],
+ 'action' => 'view',
+ 'target' => '_blank',
+ 'join' => '_entity_id',
+ ];
$createdBy = empty($importEntity['created_by']) ? '' : ' (' . E::ts('Created by %1', [$importEntity['created_by'], 'String']) . ')';
$managedEntities[] = [
'name' => 'SavedSearch_Import' . $importEntity['user_job_id'],
'label' => $field['title'] ?? $field['label'],
'sortable' => TRUE,
'editable' => strpos($field['name'], '_') !== 0,
+ 'link' => $field['link'] ?? NULL,
];
}
$managedEntities[] = [