Skip to content

Commit

Permalink
[REF] Cleanup on import rows error
Browse files Browse the repository at this point in the history
Trying (and not yet succeeding) to replicat https://lab.civicrm.org/dev/core/-/issues/2566
I was able to step through & eliminate one more place where we call dao->query() instead
of CRM_Core_DAO::executeQuery
  • Loading branch information
eileenmcnaughton committed Apr 30, 2021
1 parent 03e4bc8 commit 89b7039
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
48 changes: 28 additions & 20 deletions CRM/Contact/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,30 +691,38 @@ public static function exportCSV($fileName, $header, $data) {
/**
* Update the record with PK $id in the import database table.
*
* @deprecated - call setImportStatus directly as the parameters are simpler,
*
* @param int $id
* @param array $params
*/
public function updateImportRecord($id, &$params) {
$statusFieldName = $this->_statusFieldName;
$primaryKeyName = $this->_primaryKeyName;

if ($statusFieldName && $primaryKeyName) {
$dao = new CRM_Core_DAO();
$db = $dao->getDatabaseConnection();

$query = "UPDATE $this->_tableName
SET $statusFieldName = ?,
${statusFieldName}Msg = ?
WHERE $primaryKeyName = ?";
$args = [
$params[$statusFieldName],
CRM_Utils_Array::value("${statusFieldName}Msg", $params),
$id,
];

//print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
public function updateImportRecord($id, $params): void {
$this->setImportStatus((int) $id, $params[$this->_statusFieldName] ?? '', $params["{$this->_statusFieldName}Msg"] ?? '');
}

$db->query($query, $args);
/**
* Set the import status for the given record.
*
* If this is a sql import then the sql table will be used and the update
* will not happen as the relevant fields don't exist in the table - hence
* the checks that statusField & primary key are set.
*
* @param int $id
* @param string $status
* @param string $message
*/
public function setImportStatus(int $id, string $status, string $message): void {
if ($this->_statusFieldName && $this->_primaryKeyName) {
CRM_Core_DAO::executeQuery("
UPDATE $this->_tableName
SET $this->_statusFieldName = %1,
{$this->_statusFieldName}Msg = %2
WHERE $this->_primaryKeyName = %3
", [
1 => [$status, 'String'],
2 => [$message, 'String'],
3 => [$id, 'Integer'],
]);
}
}

Expand Down
21 changes: 4 additions & 17 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ public function preview(&$values) {
* @return bool
* the result of this processing
*/
public function summary(&$values) {
public function summary(&$values): int {
$erroneousField = NULL;
$response = $this->setActiveFieldValues($values, $erroneousField);
$this->setActiveFieldValues($values, $erroneousField);

$errorMessage = NULL;
$errorRequired = FALSE;
Expand Down Expand Up @@ -381,24 +381,12 @@ public function summary(&$values) {
$this->isErrorInCoreData($params, $errorMessage);
if ($errorMessage) {
$tempMsg = "Invalid value for field(s) : $errorMessage";
// put the error message in the import record in the DB
$importRecordParams = [
$statusFieldName => 'ERROR',
"${statusFieldName}Msg" => $tempMsg,
];
$this->updateImportRecord($values[count($values) - 1], $importRecordParams);
$this->setImportStatus($values[count($values) - 1], 'ERROR', $tempMsg);
array_unshift($values, $tempMsg);
$errorMessage = NULL;
return CRM_Import_Parser::ERROR;
}

//if user correcting errors by walking back
//need to reset status ERROR msg to null
//now currently we are having valid data.
$importRecordParams = [
$statusFieldName => 'NEW',
];
$this->updateImportRecord($values[count($values) - 1], $importRecordParams);
$this->setImportStatus($values[count($values) - 1], 'NEW', '');

return CRM_Import_Parser::VALID;
}
Expand Down Expand Up @@ -431,7 +419,6 @@ public function getAllFields() {
* @throws \API_Exception
*/
public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
$config = CRM_Core_Config::singleton();
$this->_unparsedStreetAddressContacts = [];
if (!$doGeocodeAddress) {
// CRM-5854, reset the geocode method to null to prevent geocoding
Expand Down

0 comments on commit 89b7039

Please sign in to comment.