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

Fixed issues with Aggregated Contribution data source

parent c5e2c658
No related branches found
Tags 1.117
No related merge requests found
# Version 1.118 (not yet released) # Version 1.118 (not yet released)
* Fixed issues with Aggregated Contribution data source
# Version 1.117 # Version 1.117
* Fixed issue with clicking export button * Fixed issue with clicking export button
......
...@@ -23,7 +23,6 @@ use Civi\DataProcessor\DataSpecification\FieldExistsException; ...@@ -23,7 +23,6 @@ use Civi\DataProcessor\DataSpecification\FieldExistsException;
use Civi\DataProcessor\DataSpecification\FieldSpecification; use Civi\DataProcessor\DataSpecification\FieldSpecification;
use Civi\DataProcessor\DataSpecification\Utils as DataSpecificationUtils; use Civi\DataProcessor\DataSpecification\Utils as DataSpecificationUtils;
use Civi\DataProcessor\ProcessorType\AbstractProcessorType; use Civi\DataProcessor\ProcessorType\AbstractProcessorType;
use CRM_Dataprocessor_ExtensionUtil as E; use CRM_Dataprocessor_ExtensionUtil as E;
use CRM_Dataprocessor_Utils_Date; use CRM_Dataprocessor_Utils_Date;
...@@ -178,10 +177,19 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource { ...@@ -178,10 +177,19 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource {
if ($this->isAggregationEnabled()) { if ($this->isAggregationEnabled()) {
$this->getAggregationDataFlow(); $this->getAggregationDataFlow();
$dataFlow = new CombinedSqlDataFlow('', $this->aggregationDateFlow->getPrimaryTable(), $this->aggregationDateFlow->getPrimaryTableAlias()); if ($this->aggregationDateFlow instanceof SubqueryDataFlow) {
$primaryTable = $this->aggregationDateFlow->getPrimaryTable();
$primaryTableAlias = $this->aggregationDateFlow->getPrimaryTableAlias();
} elseif ($this->aggregationDateFlow instanceof SqlTableDataFlow) {
$primaryTable = $this->aggregationDateFlow->getTable();
$primaryTableAlias = $this->aggregationDateFlow->getTableAlias();
}
$dataFlow = new CombinedSqlDataFlow('', $primaryTable, $primaryTableAlias);
$dataFlow->addSourceDataFlow(new DataFlowDescription($this->aggregationDateFlow)); $dataFlow->addSourceDataFlow(new DataFlowDescription($this->aggregationDateFlow));
$dataFlowDescription = new DataFlowDescription($this->entityDataFlow, $this->getAggregationJoin($this->getEntityTableAlias())); if ($this->isAggregationJoinable()) {
$dataFlow->addSourceDataFlow($dataFlowDescription); $dataFlowDescription = new DataFlowDescription($this->entityDataFlow, $this->getAggregationJoin($this->getEntityTableAlias()));
$dataFlow->addSourceDataFlow($dataFlowDescription);
}
return $dataFlow; return $dataFlow;
} else { } else {
return $this->entityDataFlow; return $this->entityDataFlow;
...@@ -200,8 +208,8 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource { ...@@ -200,8 +208,8 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource {
$aggrgeate_field_spec = clone $this->getAvailableFields()->getFieldSpecificationByName($this->getAggregateField()); $aggrgeate_field_spec = clone $this->getAvailableFields()->getFieldSpecificationByName($this->getAggregateField());
$aggrgeate_field_spec->setMySqlFunction($this->getAggregateFunction()); $aggrgeate_field_spec->setMySqlFunction($this->getAggregateFunction());
$aggrgeate_field_spec->alias = $this->getAggregateField(); $aggrgeate_field_spec->alias = $this->getAggregateField();
$tableAlias = '_aggregated_' . $this->getSourceName();
$this->aggretated_table_dataflow = new SqlTableDataFlow($this->getTable(), '_aggregated_'.$this->getSourceName()); $this->aggretated_table_dataflow = new SqlTableDataFlow($this->getTable(), $tableAlias);
$this->aggretated_table_dataflow->getDataSpecification()->addFieldSpecification($aggrgeate_field_spec->name, $aggrgeate_field_spec); $this->aggretated_table_dataflow->getDataSpecification()->addFieldSpecification($aggrgeate_field_spec->name, $aggrgeate_field_spec);
foreach ($groupByFields as $groupByField) { foreach ($groupByFields as $groupByField) {
$this->aggretated_table_dataflow->getDataSpecification() $this->aggretated_table_dataflow->getDataSpecification()
...@@ -209,8 +217,7 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource { ...@@ -209,8 +217,7 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource {
$this->aggretated_table_dataflow->getGroupByDataSpecification() $this->aggretated_table_dataflow->getGroupByDataSpecification()
->addFieldSpecification($groupByField->name, $groupByField); ->addFieldSpecification($groupByField->name, $groupByField);
} }
$this->aggregationDateFlow = new SubqueryDataFlow($tableAlias, $this->getTable(), $tableAlias);
$this->aggregationDateFlow = new SubqueryDataFlow('_aggregated_'.$this->getSourceName(), $this->getTable(), '_aggregated_' . $this->getSourceName());
$this->aggregationDateFlow->addSourceDataFlow(new DataFlowDescription($this->aggretated_table_dataflow)); $this->aggregationDateFlow->addSourceDataFlow(new DataFlowDescription($this->aggretated_table_dataflow));
} }
...@@ -241,6 +248,15 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource { ...@@ -241,6 +248,15 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource {
return $join; return $join;
} }
protected function isAggregationJoinable(): bool {
switch ($this->getAggregateFunction()) {
case 'MAX':
case 'MIN':
return TRUE;
}
return FALSE;
}
/** /**
* Load the fields from this entity. * Load the fields from this entity.
* *
...@@ -477,13 +493,19 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource { ...@@ -477,13 +493,19 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource {
} }
if ($this->isAggregationEnabled()) { if ($this->isAggregationEnabled()) {
if ($this->aggregationDateFlow instanceof SubqueryDataFlow) {
$primaryTableAlias = $this->aggregationDateFlow->getPrimaryTableAlias();
} elseif ($this->aggregationDateFlow instanceof SqlTableDataFlow) {
$primaryTableAlias = $this->aggregationDateFlow->getTableAlias();
}
if ($join instanceof SimpleJoin && $join->getLeftTable() == $this->getSourceName()) { if ($join instanceof SimpleJoin && $join->getLeftTable() == $this->getSourceName()) {
$join->setLeftTable($this->aggregationDateFlow->getPrimaryTableAlias()); $join->setLeftTable($primaryTableAlias);
$join->setLeftPrefix($this->aggregationDateFlow->getPrimaryTableAlias()); $join->setLeftPrefix($primaryTableAlias);
} }
elseif ($join instanceof SimpleJoin && $join->getRightTable() == $this->getSourceName()) { elseif ($join instanceof SimpleJoin && $join->getRightTable() == $this->getSourceName()) {
$join->setRightTable($this->aggregationDateFlow->getPrimaryTableAlias()); $join->setRightTable($primaryTableAlias);
$join->setRightPrefix($this->aggregationDateFlow->getPrimaryTableAlias()); $join->setRightPrefix($primaryTableAlias);
} }
} }
return $this; return $this;
...@@ -496,8 +518,10 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource { ...@@ -496,8 +518,10 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource {
public function getAvailableFields() { public function getAvailableFields() {
if (!$this->availableFields) { if (!$this->availableFields) {
$this->availableFields = new DataSpecification(); $this->availableFields = new DataSpecification();
$this->loadFields($this->availableFields, array()); if (!$this->isAggregationEnabled() || $this->isAggregationJoinable()) {
$this->loadCustomGroupsAndFields($this->availableFields, false); $this->loadFields($this->availableFields, []);
$this->loadCustomGroupsAndFields($this->availableFields, FALSE);
}
} }
return $this->availableFields; return $this->availableFields;
} }
...@@ -535,7 +559,7 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource { ...@@ -535,7 +559,7 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource {
if (!$dataFlow->getDataSpecification()->doesAliasExists($fieldSpecification->alias)) { if (!$dataFlow->getDataSpecification()->doesAliasExists($fieldSpecification->alias)) {
$dataFlow->getDataSpecification()->addFieldSpecification($fieldSpecification->alias, $fieldSpecification); $dataFlow->getDataSpecification()->addFieldSpecification($fieldSpecification->alias, $fieldSpecification);
} }
} elseif ($originalFieldSpecification) { } elseif ($originalFieldSpecification && (!$this->isAggregationEnabled() || $this->getAggregateField() != $originalFieldSpecification->name)) {
$this->ensureEntity(); $this->ensureEntity();
$this->entityDataFlow->getDataSpecification()->addFieldSpecification($fieldSpecification->alias, $fieldSpecification); $this->entityDataFlow->getDataSpecification()->addFieldSpecification($fieldSpecification->alias, $fieldSpecification);
} }
......
...@@ -78,12 +78,20 @@ class AggregatedContributionSource extends AbstractCivicrmEntitySource { ...@@ -78,12 +78,20 @@ class AggregatedContributionSource extends AbstractCivicrmEntitySource {
public function getAvailableFields() { public function getAvailableFields() {
if (!$this->availableFields) { if (!$this->availableFields) {
parent::getAvailableFields(); parent::getAvailableFields();
$id = new FieldSpecification('id', 'Integer', E::ts('Contribution ID'), null, $this->getSourceName().'_id'); switch ($this->getAggregateField()) {
$this->availableFields->addFieldSpecification('id', $id); case 'id':
$totalAmount = new FieldSpecification('total_amount', 'Float', E::ts('Total amount'), null, $this->getSourceName().'_total_amount'); $id = new FieldSpecification('id', 'Integer', E::ts('Contribution ID'), NULL, $this->getSourceName() . '_id');
$this->availableFields->addFieldSpecification('total_amount', $totalAmount); $this->availableFields->addFieldSpecification('id', $id);
$receiveDate = new FieldSpecification('receive_date', 'Date', E::ts('Receive Date'), null, $this->getSourceName().'_receive_date'); break;
$this->availableFields->addFieldSpecification('receive_date', $receiveDate); case 'total_amount':
$totalAmount = new FieldSpecification('total_amount', 'Float', E::ts('Total amount'), NULL, $this->getSourceName() . '_total_amount');
$this->availableFields->addFieldSpecification('total_amount', $totalAmount);
break;
case 'receive_date':
$receiveDate = new FieldSpecification('receive_date', 'Date', E::ts('Receive Date'), NULL, $this->getSourceName() . '_receive_date');
$this->availableFields->addFieldSpecification('receive_date', $receiveDate);
break;
}
$contactId = new FieldSpecification('contact_id', 'Float', E::ts('Contact ID'), null, $this->getSourceName().'_contact_id'); $contactId = new FieldSpecification('contact_id', 'Float', E::ts('Contact ID'), null, $this->getSourceName().'_contact_id');
$this->availableFields->addFieldSpecification('contact_id', $contactId); $this->availableFields->addFieldSpecification('contact_id', $contactId);
$count = new FieldSpecification('count', 'Integer', E::ts('Count'), null, $this->getSourceName().'_count'); $count = new FieldSpecification('count', 'Integer', E::ts('Count'), null, $this->getSourceName().'_count');
...@@ -132,10 +140,10 @@ class AggregatedContributionSource extends AbstractCivicrmEntitySource { ...@@ -132,10 +140,10 @@ class AggregatedContributionSource extends AbstractCivicrmEntitySource {
} elseif ($this->getAvailableFields()->doesFieldExist($fieldSpecification->name)) { } elseif ($this->getAvailableFields()->doesFieldExist($fieldSpecification->name)) {
$originalFieldSpecification = $this->getAvailableFields()->getFieldSpecificationByName($fieldSpecification->name); $originalFieldSpecification = $this->getAvailableFields()->getFieldSpecificationByName($fieldSpecification->name);
} }
if ($originalFieldSpecification) { if ($originalFieldSpecification && $this->getAggregateField() == $originalFieldSpecification->name) {
$originalFieldSpecification = clone $originalFieldSpecification; $originalFieldSpecification = clone $originalFieldSpecification;
$originalFieldSpecification->alias = $originalFieldSpecification->name; $originalFieldSpecification->alias = $fieldSpecification->alias;
$this->aggretated_table_dataflow->getDataSpecification()->addFieldSpecification($originalFieldSpecification->alias, $originalFieldSpecification); $this->aggregationDateFlow->getDataSpecification()->addFieldSpecification($fieldSpecification->alias, $originalFieldSpecification);
} }
} catch (FieldExistsException $e) { } catch (FieldExistsException $e) {
// Do nothing. // Do nothing.
......
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