Skip to content
Snippets Groups Projects
Commit e67232f5 authored by jaapjansma's avatar jaapjansma
Browse files

Added multiplier to aggregate function field.

parent 64f73864
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
* Regression fix for issue #125 after refactoring for filter collections.
* Added In- and Exclude to filter contact has contributions for the campaign field.
* Added multiplier to aggregate function field.
# Version 1.64
......
......@@ -8,6 +8,7 @@ namespace Civi\DataProcessor\DataSpecification;
use Civi\DataProcessor\Exception\InvalidConfigurationException;
use CRM_Dataprocessor_ExtensionUtil as E;
use CRM_Utils_Type;
/**
* Class used to add aggregate functions (such as SUM) to a database query.
......@@ -18,6 +19,8 @@ class AggregateFunctionFieldSpecification extends FieldSpecification implements
protected $function;
protected $multiplier = 1;
/**
* Aggregate the field in all the records and return the aggregated value.
*
......@@ -78,7 +81,7 @@ class AggregateFunctionFieldSpecification extends FieldSpecification implements
break;
}
return $value;
return $value * $this->multiplier;
}
public function setAggregateFunction($function) {
......@@ -89,6 +92,10 @@ class AggregateFunctionFieldSpecification extends FieldSpecification implements
$this->function = $function;
}
public function setMultiplier(string $multiplier) {
$this->multiplier = (float) $multiplier;
}
/**
* Convert a fieldSpecification into the aggregate function field specification.
*
......@@ -121,7 +128,11 @@ class AggregateFunctionFieldSpecification extends FieldSpecification implements
if ($isCountQuery) {
return "";
}
return "{$function}({$function_arg}`{$table_alias}`.`{$this->name}`) AS `{$this->alias}`";
$multiplier = '1';
if ($this->multiplier) {
$multiplier = CRM_Utils_Type::escape($this->multiplier, 'Float');
}
return "({$function}({$function_arg}`{$table_alias}`.`{$this->name}`) * {$multiplier}) AS `{$this->alias}`";
}
/**
......
......@@ -79,6 +79,9 @@ class AggregateFunctionFieldOutputHandler extends AbstractFormattedNumberOutputH
$this->aggregateField = AggregateFunctionFieldSpecification::convertFromFieldSpecification($this->inputFieldSpec, $configuration['function']);
$this->aggregateField->alias = $alias;
if (!empty($configuration['multiplier']) && $configuration['multiplier'] != '1') {
$this->aggregateField->setMultiplier($configuration['multiplier']);
}
$this->dataSource->ensureFieldInSource($this->aggregateField);
$this->outputFieldSpec = clone $this->inputFieldSpec;
......@@ -109,6 +112,9 @@ class AggregateFunctionFieldOutputHandler extends AbstractFormattedNumberOutputH
'class' => 'crm-select2 huge',
'placeholder' => E::ts('- select -'),
));
$form->add('text', 'multiplier', E::ts('Multiplier'), [
'class' => 'huge',
]);
if (isset($field['configuration'])) {
$configuration = $field['configuration'];
......@@ -118,6 +124,9 @@ class AggregateFunctionFieldOutputHandler extends AbstractFormattedNumberOutputH
if (isset($configuration['function'])) {
$this->defaults['function'] = $configuration['function'];
}
if (isset($configuration['multiplier'])) {
$this->defaults['multiplier'] = $configuration['multiplier'];
}
$form->setDefaults($this->defaults);
}
}
......@@ -144,6 +153,7 @@ class AggregateFunctionFieldOutputHandler extends AbstractFormattedNumberOutputH
$configuration['field'] = $field;
$configuration['datasource'] = $datasource;
$configuration['function'] = $submittedValues['function'];
$configuration['multiplier'] = $submittedValues['multiplier'];
return $configuration;
}
......
......@@ -37,5 +37,10 @@
<div class="content">{$form.suffix.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.multiplier.label}</div>
<div class="content">{$form.multiplier.html}</div>
<div class="clear"></div>
</div>
{/crmScope}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment