Skip to content

Commit

Permalink
Add runImport function
Browse files Browse the repository at this point in the history
This is required both for the queue fix and for QA fixes on the import classes.
  • Loading branch information
eileenmcnaughton committed Jun 4, 2022
1 parent 08a7661 commit 2a9fc51
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,52 @@ protected function getFieldMappings(): array {
return $mappedFields;
}

/**
* Run import.
*
* @param \CRM_Queue_TaskContext $taskContext
*
* @param int $userJobID
* @param int $limit
*
* @return bool
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public static function runImport($taskContext, $userJobID, $limit) {
$userJob = UserJob::get()->addWhere('id', '=', $userJobID)->addSelect('type_id')->execute()->first();
$parserClass = NULL;
foreach (CRM_Core_BAO_UserJob::getTypes() as $userJobType) {
if ($userJob['type_id'] === $userJobType['id']) {
$parserClass = $userJobType['class'];
}
}
$parser = new $parserClass();
$parser->setUserJobID($userJobID);
// Not sure if we still need to init....
$parser->init();
$dataSource = $parser->getDataSourceObject();
$dataSource->setStatuses(['new']);
$dataSource->setLimit($limit);

while ($row = $dataSource->getRow()) {
$values = array_values($row);

try {
$parser->import($parser->getSubmittedValue('onDuplicate'), $values);
}
catch (CiviCRM_API3_Exception $e) {
// When we catch errors here we are not adding to the errors array - mostly
// because that will become obsolete once https://github.com/civicrm/civicrm-core/pull/23292
// is merged and this will replace it as the main way to handle errors (ie. update the table
// and move on).
$parser->setImportStatus((int) $values[count($values) - 1], 'ERROR', $e->getMessage());
}
}
$parser->doPostImportActions();
return TRUE;
}

/**
* Check if an error in custom data.
*
Expand Down

0 comments on commit 2a9fc51

Please sign in to comment.