From 6663a727f7e82e814b8c8e3faf08a5e0b81eb0c2 Mon Sep 17 00:00:00 2001 From: Luis Monteiro Date: Wed, 1 Apr 2020 10:45:12 -0300 Subject: [PATCH] Adding to DataArray to handle 3 levels hierarchy --- core/DataArray.php | 50 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/core/DataArray.php b/core/DataArray.php index 1fc2a84a06f..4bb08e9f694 100644 --- a/core/DataArray.php +++ b/core/DataArray.php @@ -22,11 +22,13 @@ class DataArray { protected $data = array(); protected $dataTwoLevels = array(); + protected $dataTreeLevels = array(); - public function __construct($data = array(), $dataArrayByLabel = array()) + public function __construct($data = array(), $dataArrayByLabel = array(), $dataArrayTree = array()) { $this->data = $data; $this->dataTwoLevels = $dataArrayByLabel; + $this->dataTreeLevels = $dataArrayTree; } /** @@ -44,6 +46,11 @@ public function getDataArrayWithTwoLevels() return $this->dataTwoLevels; } + public function getDataArrayWithTreeLevels() + { + return $this->dataTreeLevels; + } + public function sumMetricsVisits($label, $row) { if (!isset($this->data[$label])) { @@ -329,6 +336,14 @@ public function sumMetricsVisitsPivot($parentLabel, $label, $row) $this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]); } + public function sumMetricsVisitsPivotTree($parentLabel, $midLabel, $label, $row) + { + if (!isset($this->dataTreeLevels[$parentLabel][$midLabel][$label])) { + $this->dataTreeLevels[$parentLabel][$midLabel][$label] = static::makeEmptyRow(); + } + $this->doSumVisitsMetrics($row, $this->dataTreeLevels[$parentLabel][$midLabel][$label]); + } + public function sumMetricsGoalsPivot($parentLabel, $label, $row) { $idGoal = $row['idgoal']; @@ -354,11 +369,24 @@ public function sumMetricsEventsPivot($parentLabel, $label, $row) $this->doSumEventsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]); } + public function sumMetricsEventsPivotTree($parentLabel, $midLabel, $label, $row) + { + if (!isset($this->dataTreeLevels[$parentLabel][$midLabel][$label])) { + $this->dataTreeLevels[$parentLabel][$midLabel][$label] = static::makeEmptyRow(); + } + $this->doSumEventsMetrics($row, $this->dataTreeLevels[$parentLabel][$midLabel][$label] ); + } + public function setRowColumnPivot($parentLabel, $label, $column, $value) { $this->dataTwoLevels[$parentLabel][$label][$column] = $value; } + public function setRowColumnPivotTree($parentLabel, $midLabel, $label, $column, $value) + { + $this->dataTreeLevels[$parentLabel][$midLabel][$label][$column] = $value; + } + public function enrichMetricsWithConversions() { $this->enrichWithConversions($this->data); @@ -366,6 +394,12 @@ public function enrichMetricsWithConversions() foreach ($this->dataTwoLevels as &$metricsBySubLabel) { $this->enrichWithConversions($metricsBySubLabel); } + + foreach ($this->dataTreeLevels as &$metricsByMidLabel) { + foreach ($metricsByMidLabel as &$metricsBySubLabel) { + $this->enrichWithConversions($metricsBySubLabel); + } + } } /** @@ -426,12 +460,24 @@ public function asDataTable() { $dataArray = $this->getDataArray(); $dataArrayTwoLevels = $this->getDataArrayWithTwoLevels(); + $dataArrayTreeLevels = $this->getDataArrayWithTreeLevels(); + $midTableByLabel = null; $subtableByLabel = null; + + if ( !empty($dataArrayTreeLevels)) { + $midTableByLabel = array(); + foreach ($dataArrayTreeLevels as $label => $midTable) { + foreach($midTable as $lastLabel => $lastTable) { + $midTableByLabel[$label][$lastLabel] = DataTable::makeFromIndexedArray($lastTable); + } + } + } + if (!empty($dataArrayTwoLevels)) { $subtableByLabel = array(); foreach ($dataArrayTwoLevels as $label => $subTable) { - $subtableByLabel[$label] = DataTable::makeFromIndexedArray($subTable); + $subtableByLabel[$label] = DataTable::makeFromIndexedArray($subTable, $midTableByLabel != null && isset($midTableByLabel[$label]) ? $midTableByLabel[$label] : null); } } return DataTable::makeFromIndexedArray($dataArray, $subtableByLabel);