Skip to content

Commit

Permalink
made API call post processor looping (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjendres committed Oct 23, 2017
1 parent 9ac03a0 commit d8fe722
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions extension/CRM/Banking/PluginImpl/PostProcessor/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function __construct($config_name) {
if (!isset($config->entity)) $config->entity = NULL;
if (!isset($config->action)) $config->action = NULL;
if (!isset($config->params)) $config->params = array();
if (!isset($config->loop)) $config->loop = array();
if (!isset($config->param_propagation)) $config->param_propagation = array();
}

Expand Down Expand Up @@ -90,14 +91,73 @@ public function processExecutedMatch(CRM_Banking_Matcher_Suggestion $match, CRM_
}
}

// perform the call
// perform the call(s)
try {
$this->logMessage("CALLING {$config->entity}.{$config->action} with " . json_encode($params), 'debug');
civicrm_api3($config->entity, $config->action, $params);
if (!empty($config->loop) && is_array($config->loop)) {
// there is a loop command in here
$this->loopCall($context, $config, $params, 1);

} else {
// no loop -> just execute
$this->logMessage("CALLING {$config->entity}.{$config->action} with " . json_encode($params), 'debug');
civicrm_api3($config->entity, $config->action, $params);
}

} catch (Exception $e) {
$this->logMessage("CALLING {$config->entity}.{$config->action} failed: " . $e->getMessage(), 'error');
}
}
}

/**
* Will recursively call the API based on the entries in the 'loop' params
*/
protected function loopCall($context, $config, $params, $level) {
if ($level <= count($config->loop)) {
if (is_object($config->loop[$level-1]) || is_array($config->loop[$level-1])) {
// ok, all clear -> start looping level $level
foreach ($config->loop[$level-1] as $attribute => $source) {
$values = $this->getLoopValues($context, $source);
// these are the values to loop over in this level
foreach ($values as $value) {
$params[$attribute] = $value;
if ($level < count($config->loop)) {
// this is not the lowest level -> recursive call
$this->loopCall($context, $config, $params, $level+1);
} else {
// this IS the last level, DO the call
$this->logMessage("CALLING {$config->entity}.{$config->action} with " . json_encode($params), 'debug');
civicrm_api3($config->entity, $config->action, $params);
}
}
}
} else {
$this->logMessage("loop paramater is incorrectly structured.", 'error');
}
}
}

/**
* get a list of values from the field
*/
protected function getLoopValues($context, $source) {
$value = $this->getPropagationValue($context->btx, $match, $source);

if ($value === NULL) {
return array();
} elseif (is_array($value)) {
// oh, this is already an array
return $value;
} else {
// check if it is JSON data
$json_list = json_decode($value, TRUE);
if ($json_list && is_array($json_list)) {
return $json_list;
}

// last resort: split by ','
return explode(',', $value);
}
}
}

0 comments on commit d8fe722

Please sign in to comment.