Skip to content

Commit

Permalink
Merge pull request #27 from vinuvarshith/csvimport-getoptions-cache-fix
Browse files Browse the repository at this point in the history
Fix Field Options Caching in getFieldOptionsMeta()
  • Loading branch information
eileenmcnaughton authored Apr 11, 2019
2 parents 8196f6b + eb5bea0 commit b93025a
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions CRM/Csvimport/Task/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CRM_Csvimport_Task_Import {
* @return mixed
*/
private static function getFieldsMeta($entity) {
if(!isset(self::$entityFieldsMeta[$entity])) {
if (!isset(self::$entityFieldsMeta[$entity])) {
try{
self::$entityFieldsMeta[$entity] = array();
self::$entityFieldsMeta[$entity] = civicrm_api3($entity, 'getfields', array(
Expand All @@ -33,18 +33,18 @@ private static function getFieldsMeta($entity) {
* @return mixed
*/
private static function getFieldOptionsMeta($entity, $field) {
if(!isset(self::$entityFieldOptionsMeta[$entity])) {
if (!isset(self::$entityFieldOptionsMeta[$entity][$field])) {
try{
self::$entityFieldOptionsMeta[$entity] = array();
self::$entityFieldOptionsMeta[$entity] = civicrm_api3($entity, 'getoptions', array(
self::$entityFieldOptionsMeta[$entity][$field] = array();
self::$entityFieldOptionsMeta[$entity][$field] = civicrm_api3($entity, 'getoptions', array(
'field' => $field,
'context' => "match",
))['values'];
} catch (CiviCRM_API3_Exception $e) {
// nothing
}
}
return self::$entityFieldOptionsMeta[$entity];
return self::$entityFieldOptionsMeta[$entity][$field];
}

/**
Expand All @@ -57,7 +57,7 @@ private static function getFieldOptionsMeta($entity, $field) {
* @return bool
*/
public static function ImportEntity(CRM_Queue_TaskContext $ctx, $entity, $batch, $errFileName) {
if( !$entity || !isset($batch)) {
if (!$entity || !isset($batch)) {
CRM_Core_Session::setStatus('Invalid params supplied to import queue!', 'Queue task - Init', 'error');
return false;
}
Expand All @@ -77,7 +77,7 @@ public static function ImportEntity(CRM_Queue_TaskContext $ctx, $entity, $batch,

// add validation for options select fields
$validation = self::validateFields($entity, $params, $ignoreCase);
if(isset($validation['error'])) {
if (isset($validation['error'])) {
array_unshift($origParams, $validation['error']);
$error = $origParams;
$validation = array();
Expand All @@ -95,7 +95,7 @@ public static function ImportEntity(CRM_Queue_TaskContext $ctx, $entity, $batch,
}

// validation errors
if($error) {
if ($error) {
$errors[] = $error;
continue;
}
Expand Down Expand Up @@ -138,13 +138,13 @@ public static function ImportEntity(CRM_Queue_TaskContext $ctx, $entity, $batch,
}

// api chaining errors
if($error) {
if ($error) {
$errors[] = $error;
continue;
}

// Check if entity needs to be updated/created
if($allowUpdate) {
if ($allowUpdate) {
$uniqueFields = CRM_Csvimport_Import_ControllerBaseClass::findAllUniqueFields($entity);
foreach ($uniqueFields as $uniqueField) {
$fieldCount = 0;
Expand Down Expand Up @@ -189,7 +189,7 @@ public static function ImportEntity(CRM_Queue_TaskContext $ctx, $entity, $batch,
}
}

if(count($errors) > 0) {
if (count($errors) > 0) {
$ret = self::addErrorsToReport($errFileName, $errors);
if(isset($ret['error'])) {
CRM_Core_Session::setStatus($ret['error'], 'Queue task', 'error');
Expand Down Expand Up @@ -241,7 +241,7 @@ private static function validateField($entity, $field, $value, $ignoreCase = FAL
$options = self::getFieldOptionsMeta($entity, $field);

$optionKeys = array_keys($options);
if($ignoreCase) {
if ($ignoreCase) {
$optionKeys = array_map('strtolower', $optionKeys);
$value = strtolower($value);
}
Expand All @@ -251,24 +251,24 @@ private static function validateField($entity, $field, $value, $ignoreCase = FAL
$isValid = TRUE;

foreach ($value as $k => $mval) {
if(!empty($mval) && !in_array($mval, $optionKeys)) {
if (!empty($mval) && !in_array($mval, $optionKeys)) {
$isValid = FALSE;
// check 'label' if 'name' not found
foreach ($options as $name => $label) {
if($mval == $label || ($ignoreCase && strcasecmp($mval, $label) == 0)) {
if ($mval == $label || ($ignoreCase && strcasecmp($mval, $label) == 0)) {
$value[$k] = $name;
$valueUpdated = TRUE;
$isValid = TRUE;
}
}
if(!$isValid) {
if (!$isValid) {
return array('error' => ts('Invalid value for field') . ' (' . $field . ') => ' . $mval);
}
}
}

if(count($value) == 1) {
if(!$valueUpdated) {
if (count($value) == 1) {
if (!$valueUpdated) {
return array('error' => 0);
}
$value = array_pop($value);
Expand Down

0 comments on commit b93025a

Please sign in to comment.