Skip to content

Commit

Permalink
Fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
Nayjest committed Jun 23, 2016
1 parent b5f39d5 commit 3aa0581
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/Component/PageTotalsRow.php
Expand Up @@ -51,7 +51,7 @@ class PageTotalsRow implements PartInterface, ViewComponentInterface

protected $rowsProcessed = 0;

private $stopDataCollecting = false;
private $isTotalsCalculationFinished = false;

/**
* @var string
Expand All @@ -65,6 +65,9 @@ class PageTotalsRow implements PartInterface, ViewComponentInterface
* of PageTotalsRow::OPERATIN_* constants or Closure or null. Keys must be equal to target column id's
* If $operations has no value for column, default operation will be used for that column.
*
* Closure passed to operations can accept
* accumulated value in first argument and current row value in second argument.
*
* @param array|string[] $operations (optional) keys are column id's and values are operations
* (see PageTotalsRow::OPERATION_* constants) or closures.
* @param string|null $defaultOperation operation that will be used for column
Expand Down Expand Up @@ -92,13 +95,23 @@ public function attachToCompound(Compound $root, $prepend = false)
}

/**
* @return array
* Returns string prefixes for data printed in totals row for different operations.
*
* Keys are operations and values are prefixes.
*
* @return string[]
*/
public function getValuePrefixes()
{
return $this->valuePrefixes;
}

/**
* Sets string prefixes for data printed in totals row for different operations.
*
* @param string[] $valuePrefixes keys are operations and values are prefixes.
* @return $this
*/
public function setValuePrefixes(array $valuePrefixes)
{
$this->valuePrefixes = $valuePrefixes;
Expand All @@ -114,9 +127,8 @@ public function render()
{
/** @var Grid $grid */
$grid = $this->root;
$this->stopDataCollecting = true;
$this->isTotalsCalculationFinished = true;
$tr = $grid->getRecordView();

// set total_row as current grid row
$lastRow = $grid->getCurrentRow();
$grid->setCurrentRow($this->totalData);
Expand Down Expand Up @@ -157,28 +169,30 @@ protected function pushData($field, $value)
if ($this->totalData === null) {
$this->totalData = new \stdClass();
}
if (!is_numeric($value)) {
return;
}
if (!property_exists($this->totalData, $field)) {
$this->totalData->$field = 0;
}
$operation = $this->getOperation($field);
switch ($operation) {
case self::OPERATION_SUM:
if (!is_numeric($value)) {
return;
}
$this->totalData->$field += $value;
break;
case self::OPERATION_COUNT:
$this->totalData->$field = $this->rowsProcessed;
break;
case self::OPERATION_AVG:
$sumFiled = "{$field}_sum_for_totals";
if (!property_exists($this->totalData, $sumFiled)) {
$this->totalData->$sumFiled = 0;
$sumField = "{$field}_sum_for_totals";
if (!property_exists($this->totalData, $sumField)) {
$this->totalData->$sumField = 0;
}
if (is_numeric($value)) {
$this->totalData->$sumField += $value;
}
$this->totalData->$sumFiled += $value;
$this->totalData->$field = round(
$this->totalData->$sumFiled / $this->rowsProcessed,
$this->totalData->$sumField / $this->rowsProcessed,
2
);
break;
Expand All @@ -187,6 +201,9 @@ protected function pushData($field, $value)
break;
default:
if ($operation instanceof Closure) {
if (!property_exists($this->totalData, $field)) {
$this->totalData->$field = 0;
}
$this->totalData->$field = $operation($this->totalData->$field, $value);
break;
}
Expand All @@ -209,7 +226,7 @@ protected function getOperation($columnName)

protected function processCurrentRow()
{
if ($this->stopDataCollecting) {
if ($this->isTotalsCalculationFinished) {
return;
}
$this->rowsProcessed++;
Expand Down

0 comments on commit 3aa0581

Please sign in to comment.