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

Add not-yet-used function to allow the import to run in static (queue) context #23691

Merged
merged 2 commits into from
Jun 5, 2022
Merged
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
9 changes: 9 additions & 0 deletions CRM/Activity/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class CRM_Activity_Import_Form_DataSource extends CRM_Import_Form_DataSource {

const IMPORT_ENTITY = 'Activity';

/**
* Get the name of the type to be stored in civicrm_user_job.type_id.
*
* @return string
*/
public function getUserJobType(): string {
return 'activity_import';
}

/**
* @var bool
*/
Expand Down
9 changes: 9 additions & 0 deletions CRM/Contact/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
*/
class CRM_Contact_Import_Form_DataSource extends CRM_Import_Form_DataSource {

/**
* Get the name of the type to be stored in civicrm_user_job.type_id.
*
* @return string
*/
public function getUserJobType(): string {
return 'contact_import';
}

/**
* Get any smarty elements that may not be present in the form.
*
Expand Down
9 changes: 9 additions & 0 deletions CRM/Contribute/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class CRM_Contribute_Import_Form_DataSource extends CRM_Import_Form_DataSource {

const IMPORT_ENTITY = 'Contribution';

/**
* Get the name of the type to be stored in civicrm_user_job.type_id.
*
* @return string
*/
public function getUserJobType(): string {
return 'contribution_import';
}

/**
* Build the form object.
*/
Expand Down
31 changes: 31 additions & 0 deletions CRM/Core/BAO/UserJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ public static function getTypes(): array {
'id' => 1,
'name' => 'contact_import',
'label' => ts('Contact Import'),
'class' => 'CRM_Contact_Import_Parser_Contact',
],
[
'id' => 2,
'name' => 'contribution_import',
'label' => ts('Contribution Import'),
'class' => 'CRM_Contribute_Import_Parser_Contribution',
],
[
'id' => 3,
'name' => 'membership_import',
'label' => ts('Membership Import'),
'class' => 'CRM_Member_Import_Parser_Membership',
],
[
'id' => 4,
'name' => 'activity_import',
'label' => ts('Activity Import'),
'class' => 'CRM_Activity_Import_Parser_Activity',
],
[
'id' => 5,
'name' => 'participant_import',
'label' => ts('Participant Import'),
'class' => 'CRM_Event_Import_Parser_Participant',
],
[
'id' => 6,
'name' => 'custom_field_import',
'label' => ts('Multiple Value Custom Field Import'),
'class' => 'CRM_Custom_Import_Parser_Api',
],
];
}
Expand Down
9 changes: 9 additions & 0 deletions CRM/Custom/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class CRM_Custom_Import_Form_DataSource extends CRM_Import_Form_DataSource {

const IMPORT_ENTITY = 'Multi value custom data';

/**
* Get the name of the type to be stored in civicrm_user_job.type_id.
*
* @return string
*/
public function getUserJobType(): string {
return 'custom_field_import';
}

/**
* Get the import entity (translated).
*
Expand Down
9 changes: 9 additions & 0 deletions CRM/Event/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class CRM_Event_Import_Form_DataSource extends CRM_Import_Form_DataSource {

const IMPORT_ENTITY = 'Participant';

/**
* Get the name of the type to be stored in civicrm_user_job.type_id.
*
* @return string
*/
public function getUserJobType(): string {
return 'participant_import';
}

/**
* Build the form object.
*
Expand Down
2 changes: 1 addition & 1 deletion CRM/Import/Forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ protected function createUserJob(): int {
$id = UserJob::create(FALSE)
->setValues([
'created_id' => CRM_Core_Session::getLoggedInContactID(),
'type_id:name' => 'contact_import',
'type_id:name' => $this->getUserJobType(),
'status_id:name' => 'draft',
// This suggests the data could be cleaned up after this.
'expires_date' => '+ 1 week',
Expand Down
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
9 changes: 9 additions & 0 deletions CRM/Member/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class CRM_Member_Import_Form_DataSource extends CRM_Import_Form_DataSource {

const IMPORT_ENTITY = 'Membership';

/**
* Get the name of the type to be stored in civicrm_user_job.type_id.
*
* @return string
*/
public function getUserJobType(): string {
return 'membership_import';
}

/**
* Build the form object.
*
Expand Down