-
-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24603 from eileenmcnaughton/import
Fix civiimport crash on unmapped fields, remove overzealous cleanup, add api to help debug & test
- Loading branch information
Showing
12 changed files
with
255 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Api4\Import; | ||
|
||
use Civi\Api4\Generic\DAOGetAction; | ||
use Civi\Api4\Generic\Result; | ||
|
||
class Import extends DAOGetAction { | ||
|
||
use ImportProcessTrait; | ||
|
||
/** | ||
* @throws \CRM_Core_Exception | ||
*/ | ||
public function _run(Result $result): void { | ||
$userJobID = (int) str_replace('Import_', '', $this->_entityName); | ||
$where = $this->where; | ||
$this->addWhere('_status', 'IN', ['new', 'valid']); | ||
$this->getImportRows($result); | ||
$parser = $this->getParser($userJobID); | ||
foreach ($result as $row) { | ||
$parser->import(array_values($row)); | ||
} | ||
$parser->doPostImportActions(); | ||
|
||
// Re-fetch the validated result with updated messages. | ||
$this->where = $where; | ||
$this->addSelect('*'); | ||
parent::_run($result); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Api4\Import; | ||
|
||
use Civi\Api4\Generic\Result; | ||
use Civi\Api4\Import; | ||
use Civi\Api4\UserJob; | ||
|
||
/** | ||
* Code shared by Import Save/Update actions. | ||
* | ||
* @method getCheckPermissions() | ||
*/ | ||
trait ImportProcessTrait { | ||
|
||
/** | ||
* Get the parser for the import | ||
* | ||
* @return \CRM_Import_Parser|\CRM_Contribute_Import_Parser_Contribution | ||
* | ||
* @throws \CRM_Core_Exception | ||
*/ | ||
protected function getParser(int $userJobID) { | ||
$userJob = UserJob::get($this->getCheckPermissions()) | ||
->addWhere('id', '=', $userJobID) | ||
->addSelect('job_type') | ||
->execute() | ||
->first(); | ||
$parserClass = NULL; | ||
foreach (\CRM_Core_BAO_UserJob::getTypes() as $userJobType) { | ||
if ($userJob['job_type'] === $userJobType['id']) { | ||
$parserClass = $userJobType['class']; | ||
} | ||
} | ||
/** @var \CRM_Import_Parser|\CRM_Contribute_Import_Parser_Contribution $parser */ | ||
$parser = new $parserClass(); | ||
$parser->setUserJobID($userJobID); | ||
$parser->init(); | ||
return $parser; | ||
} | ||
|
||
/** | ||
* Get the selected import rows. | ||
* | ||
* @param \Civi\Api4\Generic\Result $result | ||
* | ||
* @throws \CRM_Core_Exception | ||
*/ | ||
protected function getImportRows(Result $result): void { | ||
$userJobID = (int) str_replace('Import_', '', $this->_entityName); | ||
$this->addSelect('*'); | ||
$importFields = array_keys((array) Import::getFields($userJobID, $this->getCheckPermissions()) | ||
->addSelect('name') | ||
->addWhere('name', 'NOT LIKE', '_%') | ||
->execute() | ||
->indexBy('name')); | ||
$importFields[] = '_id'; | ||
$importFields[] = '_entity_id'; | ||
$this->setSelect($importFields); | ||
parent::_run($result); | ||
foreach ($result as &$row) { | ||
if ($row['_entity_id']) { | ||
// todo - how should we handle this? Skip, exception. At this case | ||
// it is non ui accessible so this is good for now. | ||
throw new \CRM_Core_Exception('Row already imported'); | ||
} | ||
// Push ID to the end as the get has moved it to the front & order matters here. | ||
$rowID = $row['_id']; | ||
unset($row['_id'], $row['_entity_id']); | ||
$row['_id'] = $rowID; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Api4\Import; | ||
|
||
use Civi\Api4\Generic\DAOGetAction; | ||
use Civi\Api4\Generic\Result; | ||
|
||
class Validate extends DAOGetAction { | ||
|
||
use ImportProcessTrait; | ||
|
||
/** | ||
* @throws \CRM_Core_Exception | ||
*/ | ||
public function _run(Result $result): void { | ||
$userJobID = (int) str_replace('Import_', '', $this->_entityName); | ||
|
||
$this->getImportRows($result); | ||
$parser = $this->getParser($userJobID); | ||
foreach ($result as $row) { | ||
$parser->validateRow($row); | ||
} | ||
|
||
// Re-fetch the validated result with updated messages. | ||
$this->addSelect('*'); | ||
parent::_run($result); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.