Commit 860d3098 authored by kainuk's avatar kainuk
Browse files

Merge branch 'calc_sort_2' into 'master'

Make the totals (calculations that add up) and percentages sortable.

See merge request !86
parents d7a0d40d 1137040b
# Version 1.37 (not yet released)
* Fixed #94. Renamed the calculation Percentage to Percentage change (to avoid confusion).
* Make the totals (calculations that add up) and percentages sortable.
* Fixed #91. Duplicate column name 'participant_payment_participant_id' when a Data Processor is configured to use two or more Pariticipant Sources by !85
* Fixed issue with Participant and Contribution Searches by !87
......
......@@ -316,8 +316,8 @@ abstract class SqlDataFlow extends AbstractDataFlow {
break;
}
if ($sortSpecification->getField()) {
$fieldName = $sortSpecification->getField()->alias;
$orderBys[] = "`{$fieldName}` {$dir}";
$orderBy = $sortSpecification->getField()->getSqlOrderBy();
$orderBys[] = "{$orderBy} {$dir}";
}
}
if (count($orderBys)) {
......@@ -329,7 +329,7 @@ abstract class SqlDataFlow extends AbstractDataFlow {
/**
* Returns debug information
*
* @return string
* @return array
*/
public function getDebugInformation() {
return array(
......
......@@ -41,6 +41,13 @@ class FieldSpecification implements SqlFieldSpecification {
*/
protected $multiValueField = false;
/**
* the SQL orderby expression
*
* @var string
*/
protected $sqlOrderBy = null;
/**
* @var null|String
*/
......@@ -165,4 +172,18 @@ class FieldSpecification implements SqlFieldSpecification {
$this->multiValueField = $value ? true : false;
}
/**
* @return string
*/
public function getSqlOrderBy() {
return $this->sqlOrderBy ?? "`{$this->alias}`";
}
/**
* @param string $sqlOrderBy
*/
public function setSqlOrderBy($sqlOrderBy) {
$this->sqlOrderBy = $sqlOrderBy;
}
}
......@@ -27,6 +27,15 @@ interface SqlFieldSpecification {
*/
public function getSqlGroupByStatement($table_alias);
/**
* Returns the order by statement for this field
* Often this will be just the field, but for calculations it can
* be something else
*
* @return String
*/
public function getSqlOrderBy();
/**
* Returns the SQL column name for this field.
* This could be used in join statements
......
......@@ -7,13 +7,11 @@
namespace Civi\DataProcessor\FieldOutputHandler\Calculations;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
use Civi\DataProcessor\FieldOutputHandler\AbstractFieldOutputHandler;
use Civi\DataProcessor\FieldOutputHandler\AbstractSimpleSortableFieldOutputHandler;
use Civi\DataProcessor\FieldOutputHandler\FieldOutput;
use Civi\DataProcessor\Exception\DataSourceNotFoundException;
use Civi\DataProcessor\Exception\FieldNotFoundException;
use CRM_Dataprocessor_ExtensionUtil as E;
abstract class CalculationFieldOutputHandler extends AbstractFieldOutputHandler {
abstract class CalculationFieldOutputHandler extends AbstractSimpleSortableFieldOutputHandler {
/**
* @var \Civi\DataProcessor\DataSpecification\FieldSpecification
......
......@@ -7,11 +7,6 @@
namespace Civi\DataProcessor\FieldOutputHandler\Calculations;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
use Civi\DataProcessor\Exception\DataSourceNotFoundException;
use Civi\DataProcessor\Exception\FieldNotFoundException;
use Civi\DataProcessor\FieldOutputHandler\AbstractFieldOutputHandler;
use Civi\DataProcessor\FieldOutputHandler\FieldOutput;
use CRM_Dataprocessor_ExtensionUtil as E;
class PercentageChangeFieldOutputHandler extends CalculationFieldOutputHandler {
......@@ -57,5 +52,20 @@ class PercentageChangeFieldOutputHandler extends CalculationFieldOutputHandler {
}
}
/**
* @return \Civi\DataProcessor\DataSpecification\FieldSpecification
*/
public function getSortableInputFieldSpec() {
$fieldSpec = new FieldSpecification($this->getOutputFieldSpecification()
->getName(),
'String',
$this->getOutputFieldSpecification()->title
);
$expression100 = "`{$this->inputFieldSpecs[0]->alias}`";
$expressionOff = "(`{$this->inputFieldSpecs[1]->alias}`-{$expression100})";
$fieldSpec->setSqlOrderBy("(100 * $expressionOff / $expression100 )");
return $fieldSpec;
}
}
......@@ -6,7 +6,7 @@
namespace Civi\DataProcessor\FieldOutputHandler\Calculations;
use Civi\DataProcessor\FieldOutputHandler\FieldOutput;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
class TotalFieldOutputHandler extends CalculationFieldOutputHandler {
......@@ -27,4 +27,21 @@ class TotalFieldOutputHandler extends CalculationFieldOutputHandler {
return $value;
}
/**
* @return \Civi\DataProcessor\DataSpecification\FieldSpecification
*/
public function getSortableInputFieldSpec() {
$fieldSpec = new FieldSpecification($this->getOutputFieldSpecification()
->getName(),
'String',
$this->getOutputFieldSpecification()->title
);
$terms = [];
foreach ($this->inputFieldSpecs[0] as $inputFieldSpec) {
$terms[] = "`{$inputFieldSpec->alias}`";
}
$fieldSpec->setSqlOrderBy('(' . implode('+', $terms) . ')');
return $fieldSpec;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment