Skip to content

Commit

Permalink
Merge pull request #23691 from eileenmcnaughton/contact_type
Browse files Browse the repository at this point in the history
Add not-yet-used function to allow the import to run in static (queue) context
  • Loading branch information
seamuslee001 authored Jun 5, 2022
2 parents 2e04078 + 2a9fc51 commit a77d014
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 1 deletion.
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 @@ -1655,6 +1655,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

0 comments on commit a77d014

Please sign in to comment.