From 2a9fc5177180f7a8a9b824c04f5ef880ab845422 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 5 Jun 2022 10:32:43 +1200 Subject: [PATCH] Add runImport function This is required both for the queue fix and for QA fixes on the import classes. --- CRM/Import/Parser.php | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 93c5260ff197..67878b087d7b 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -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. *